Skip to content

Align upsert targets to the source schema - #4551

Open
Anurag-S1ngh wants to merge 1 commit into
apache:mainfrom
Anurag-S1ngh:fix/3105-upsert-missing-columns
Open

Anurag-S1ngh wants to merge 1 commit into
apache:mainfrom
Anurag-S1ngh:fix/3105-upsert-missing-columns

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Closes #3105

Rationale for this change

get_rows_to_update cast the whole source table to the schema of the scanned target rows. A scan of a data file written before a column was added omits that column, so after update_schema().union_by_name() the cast failed with:

ValueError: Target schema's field names are not matching the table's field names

Align the target rows to the source schema by column name and add any missing source columns as nulls before the join and comparison. The join now selects only the join columns instead of casting the entire table.

This also handles source columns in a different order, which previously failed for the same reason.

Are these changes tested?

  • pytest tests/table/test_upsert.py (30 passed)
  • Added a regression test that appends data, adds a column, then upserts a row containing the new column. It fails with the reported error before this change.
  • Added unit tests for renamed column order and a target missing a source column.
  • ruff check and ruff format --check on the changed files

Are there any user-facing changes?

No API change. Upsert now succeeds after a schema evolution that adds a column.

Scans of files written before a column was added omit that column, so casting the source table to the target schema raised a schema mismatch. Align the target columns by name and add missing ones as nulls.
@shihchengwei-lab

Copy link
Copy Markdown

A valid float32 update can return rows_updated=0 and leave the old double in the table. I reproduced this on 9e4f8bf with PyArrow 25.0.1.

import pyarrow as pa
from pyiceberg.catalog.sql import SqlCatalog

def test_upsert_with_float32_input(tmp_path):
    catalog = SqlCatalog("local", uri="sqlite:///:memory:", warehouse=tmp_path.as_uri())
    catalog.create_namespace("db")
    old = pa.table({"id": [1], "v": pa.array([16777217], type=pa.float64())})
    table = catalog.create_table("db.t", schema=old.schema)
    table.append(old)

    new = pa.table({"id": [1], "v": pa.array([16777216], type=pa.float32())})
    result = table.upsert(new, join_cols=["id"])
    stored = catalog.load_table("db.t").scan().to_arrow()["v"].to_pylist()
    assert (result.rows_updated, stored) == (1, [16777216.0])

The test passes on base 0bef230. On the PR it gets (0, [16777217.0]). In _align_to_schema, casting the stored double to float32 rounds it to the incoming value before the comparison. Could we keep the alignment by name and compare shared columns at a precision that preserves both values?

AI assistance: Codex helped trace the change, run the test on both revisions, and draft this comment.

This branch has not been deployed

No deployments
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 update_schema().union_by_name() due to schema mismatch

2 participants