Skip to content

Fix upsert after schema evolution - #3816

Open
kevinjqliu wants to merge 4 commits into
apache:mainfrom
kevinjqliu:kevinjqliu/codex-fix-upsert-schema-evolution
Open

Fix upsert after schema evolution#3816
kevinjqliu wants to merge 4 commits into
apache:mainfrom
kevinjqliu:kevinjqliu/codex-fix-upsert-schema-evolution

Conversation

@kevinjqliu

@kevinjqliu kevinjqliu commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

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:

After adding created_at, this produced:

Source/current schema: cik_str, ticker, title, created_at
Target snapshot schema: cik_str, ticker, title

When get_rows_to_update casts the source to the target Arrow schema, PyArrow reports:

An error occurred during upsert: Target schema's field names are not matching the table's field names: ['cik_str', 'ticker', 'title', 'created_at'], ['cik_str', 'ticker', 'title']

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.

@kevinjqliu
kevinjqliu marked this pull request as ready for review August 20, 2026 23:33
Copilot AI lite review requested due to automatic review settings August 20, 2026 23:33

This comment was marked as outdated.

Comment on lines +317 to +322
def _scan(
self,
row_filter: str | BooleanExpression = ALWAYS_TRUE,
case_sensitive: bool = True,
branch: str | None = None,
) -> DataScan:

Copy link
Copy Markdown
Contributor Author

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplifying the logic now that we can pass branch into _scan

Comment on lines -938 to -939
if branch in self.table_metadata.refs:
matched_iceberg_record_batches_scan = matched_iceberg_record_batches_scan.use_ref(branch)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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!

@kevinjqliu
kevinjqliu requested review from geruh and a balanced review from Copilot August 21, 2026 00:10
@kevinjqliu kevinjqliu added this to the PyIceberg 0.12.0 milestone Aug 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upsert fails after adding in a new column, target field not found

2 participants