Repository navigation
Align upsert targets to the source schema - #4551
Anurag-S1ngh wants to merge 2 commits into
Conversation
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.
|
A valid float32 update can return 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 AI assistance: Codex helped trace the change, run the test on both revisions, and draft this comment. |
Aligning the target to the source schema narrowed wider stored columns, so a float64 value could round to an incoming float32 value and the update was skipped. Promote shared columns and join keys to a type that holds both values without losing precision.
|
Confirmed on the current head. The alignment now promotes shared columns to a common type instead of narrowing to the source type, so the stored double keeps full precision and the float32 update is applied. Added your case as an integration test plus unit coverage for mixed column and join key types. |
Closes #3105
Rationale for this change
get_rows_to_updatecast 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 afterupdate_schema().union_by_name()the cast failed with: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)ruff checkandruff format --checkon the changed filesAre there any user-facing changes?
No API change. Upsert now succeeds after a schema evolution that adds a column.