fix: reject transform renames that collide column names - #879
Closed
harsh-thakkar7 wants to merge 1 commit into
Closed
harsh-thakkar7 wants to merge 1 commit into
harsh-thakkar7 wants to merge 1 commit into
Conversation
Renaming a column onto the name of another existing column silently collapsed the two into one and discarded the source column's entire dataset, because the new CREATE TABLE column list was built with dict(new_column_pairs) and the earlier entry was overwritten. Detect duplicate target names and raise TransformError before any SQL is executed, leaving the table untouched.
Author
|
Closing this — I don't think this change is mature enough to land right now. Thanks for the project! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Table.transform(rename={"b": "a"})on a table that already has bothaandbsilently destroyed columnband all of its data:The rebuilt table was constructed from
dict(new_column_pairs)intransform_sql(), so two columns mapped to the same target name collapsed to one — the earlier entry (with its data copy) was silently dropped from the CREATE TABLE and from theINSERT INTO ... SELECTcopy.Fix
After computing the transformed column list, detect target names that would appear more than once and raise
TransformErrorbefore any SQL executes, leaving the table and its data untouched:A legitimate name swap (
rename={"a": "b", "b": "a"}) and reusing a dropped column's name (e.g.drop=["a"], rename={"b": "a"}) are both still allowed.Tests
Added to
tests/test_transform.py:test_transform_rename_cannot_lose_data_by_colliding_columns— renamingb→araisesTransformErrorand the original rows and schema are untouched (this failed before the fix by silently dropping the column).test_transform_rename_two_columns_to_same_name_errors— two columns renamed to one target name also raise.test_transform_can_swap_two_column_names— a straighta↔bswap still works.📚 Documentation preview 📚: https://sqlite-utils--879.org.readthedocs.build/en/879/