Fix upsert after schema evolution - #3816
Conversation
| def _scan( | ||
| self, | ||
| row_filter: str | BooleanExpression = ALWAYS_TRUE, | ||
| case_sensitive: bool = True, | ||
| branch: str | None = None, | ||
| ) -> DataScan: |
There was a problem hiding this comment.
reviewer note:
the change here is only adding
branch: str | None = None,
| 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) |
There was a problem hiding this comment.
simplifying the logic now that we can pass branch into _scan
| if branch in self.table_metadata.refs: | ||
| matched_iceberg_record_batches_scan = matched_iceberg_record_batches_scan.use_ref(branch) |
There was a problem hiding this comment.
this is now part of the _scan logic, by passing branch in directly
| # 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(), |
There was a problem hiding this comment.
this is the main fix to ensure that we're using the same schema!
|
Nice approach separating file planning from schema projection! I was following #2467 and pulled this down to test it out locally, and noticed a small detail with In Also, as a minor follow-up thought for the future: do you think it might eventually be worth exposing a projected schema override directly on |
Closes #2467
Rationale for this change
Schema updates change the current table schema but do not create snapshots. The target branch can therefore still point to a snapshot with an older
schema_id.Before this change, upsert built its source and target rows with different schemas:
self.table_metadata.schema(), the current transaction schema and the schema recorded on the new snapshot.use_ref()setssnapshot_id, which makesDataScan.projection()use the snapshot schema.After adding
created_at, this produced:When
get_rows_to_updatecasts the source to the target Arrow schema, PyArrow reports:The branch snapshot should determine which files to read, while the current transaction schema should define the rows being compared and written. This change plans files from the target branch and materializes them with the current transaction schema. Older files are projected into the current schema, so the source and matched target rows have the same columns. Snapshot-based projection remains unchanged for normal and time-travel scans.
Are these changes tested?
Yes. Added regression tests for main and a diverged branch.
Are there any user-facing changes?
Yes. Upsert works after schema evolution.