-
Notifications
You must be signed in to change notification settings - Fork 560
Fix upsert after schema evolution #3816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -314,11 +314,19 @@ def _apply( | |
|
|
||
| return self | ||
|
|
||
| def _scan(self, row_filter: str | BooleanExpression = ALWAYS_TRUE, case_sensitive: bool = True) -> DataScan: | ||
| """Minimal data scan of the table with the current state of the transaction.""" | ||
| return DataScan( | ||
| def _scan( | ||
| self, | ||
| row_filter: str | BooleanExpression = ALWAYS_TRUE, | ||
| case_sensitive: bool = True, | ||
| branch: str | None = None, | ||
| ) -> DataScan: | ||
| """Minimal data scan of the current transaction state, optionally scoped to a branch.""" | ||
| scan = DataScan( | ||
| table_metadata=self.table_metadata, io=self._table.io, row_filter=row_filter, case_sensitive=case_sensitive | ||
| ) | ||
| if branch in self.table_metadata.refs: | ||
| return scan.use_ref(branch) | ||
| return scan | ||
|
|
||
| def upgrade_table_version(self, format_version: TableVersion) -> Transaction: | ||
| """Set the table to a certain version. | ||
|
|
@@ -778,9 +786,7 @@ def delete( | |
| bound_delete_filter = bind(self.table_metadata.schema(), delete_filter, case_sensitive) | ||
| preserve_row_filter = _expression_to_complementary_pyarrow(bound_delete_filter, self.table_metadata.schema()) | ||
|
|
||
| file_scan = self._scan(row_filter=delete_filter, case_sensitive=case_sensitive) | ||
| if branch is not None: | ||
| file_scan = file_scan.use_ref(branch) | ||
| file_scan = self._scan(row_filter=delete_filter, case_sensitive=case_sensitive, branch=branch) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simplifying the logic now that we can pass |
||
| files = file_scan.plan_files() | ||
|
|
||
| commit_uuid = uuid.uuid4() | ||
|
|
@@ -926,20 +932,16 @@ def upsert( | |
| # get list of rows that exist so we don't have to load the entire target table | ||
| matched_predicate = upsert_util.create_match_filter(df, join_cols) | ||
|
|
||
| # We must use Transaction.table_metadata for the scan. This includes all uncommitted - but relevant - changes. | ||
| matched_iceberg_file_scan = self._scan(row_filter=matched_predicate, case_sensitive=case_sensitive, branch=branch) | ||
|
|
||
| matched_iceberg_record_batches_scan = DataScan( | ||
| table_metadata=self.table_metadata, | ||
| io=self._table.io, | ||
| row_filter=matched_predicate, | ||
| case_sensitive=case_sensitive, | ||
| # Plan files from the target branch, but read them using the transaction's current schema. | ||
| # A schema update does not create a snapshot, so the branch snapshot may use an older schema. | ||
| matched_iceberg_record_batches = _to_arrow_batch_reader_via_file_scan_tasks( | ||
| matched_iceberg_file_scan, | ||
| self.table_metadata.schema(), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the main fix to ensure that we're using the same schema! |
||
| matched_iceberg_file_scan.plan_files(), | ||
| ) | ||
|
|
||
| if branch in self.table_metadata.refs: | ||
| matched_iceberg_record_batches_scan = matched_iceberg_record_batches_scan.use_ref(branch) | ||
|
Comment on lines
-938
to
-939
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is now part of the |
||
|
|
||
| matched_iceberg_record_batches = matched_iceberg_record_batches_scan.to_arrow_batch_reader() | ||
|
|
||
| batches_to_overwrite = [] | ||
| overwrite_predicates = [] | ||
| rows_to_insert = df | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewer note:
the change here is only adding