Fix upsert after schema evolution (#3105) - #3865
Open
hedger9487 wants to merge 1 commit into
Open
Conversation
* Cast only join_cols schema instead of full table schema in get_rows_to_update * Safely handle missing non-key columns in target_table when comparing rows * Add regression tests for upsert after add_column and union_by_name schema evolution * Verify underlying Parquet file replacement and snapshot operations
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.
Closes #3105
Rationale for this change
When an Iceberg table undergoes schema evolution (e.g.
update_schema().add_column(...)orupdate_schema().union_by_name(...)), callingtable.upsert()previously failed with:Root Cause
In
pyiceberg/table/upsert_util.py(get_rows_to_update), Step 1 previously cast the entiresource_table(which has the new schema including evolved columns) totarget_table.schema(which only contains columns present in older data files). PyArrow requires column names to match exactly when casting an entire table, raisingValueError.Fix
join_colsto the target table's join schema (join_schema = pa.schema([target_table.schema.field(col) for col in join_cols])).target_rowasNoneif the column was added during schema evolution and absent intarget_table.add_column,union_by_name, multiple schema evolutions with composite keys, and no-op null comparisons, while explicitly verifying that underlying Parquet data files are properly replaced (Copy-on-Write) and snapshot operations includeOVERWRITEandAPPEND.Are these changes tested?
Yes:
test_upsert_after_schema_evolution(verifies file replacement and snapshot operations)test_upsert_after_schema_evolution_union_by_nametest_upsert_after_multiple_schema_evolutions_with_composite_keystest_upsert_after_schema_evolution_noop_and_nullsAre there any user-facing changes?
No.