From 9e4f8bfffbb80be3b9cfdf7b5c8b8c1b3c1c17be Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Wed, 2 Dec 2026 01:20:35 +0530 Subject: [PATCH] Align upsert targets to the source schema 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. --- pyiceberg/table/upsert_util.py | 21 ++++++++++++++------- tests/table/test_upsert.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/pyiceberg/table/upsert_util.py b/pyiceberg/table/upsert_util.py index 1a5692fa69..6a64f61943 100644 --- a/pyiceberg/table/upsert_util.py +++ b/pyiceberg/table/upsert_util.py @@ -72,6 +72,15 @@ def _values_differ( return pc.invert(pc.or_(equal, both_null)) +def _align_to_schema(table: pa.Table, schema: pa.Schema) -> pa.Table: + """Align a table to a schema, adding missing columns as nulls.""" + columns = [ + table.column(field.name).cast(field.type) if field.name in table.column_names else pa.nulls(len(table), type=field.type) + for field in schema + ] + return pa.Table.from_arrays(columns, schema=schema) + + def get_rows_to_update(source_table: pa.Table, target_table: pa.Table, join_cols: list[str]) -> pa.Table: """ Return a table with rows that need to be updated in the target table based on the join columns. @@ -79,6 +88,10 @@ def get_rows_to_update(source_table: pa.Table, target_table: pa.Table, join_cols The table is joined on the identifier columns, and then checked if there are any updated rows. Those are selected and everything is renamed correctly. """ + # Scans of files written before a column was added omit that column. Align the target to the + # source so every source column is compared and the join sees matching types. + target_table = _align_to_schema(target_table, source_table.schema) + all_columns = set(source_table.column_names) join_cols_set = set(join_cols) @@ -105,13 +118,7 @@ def get_rows_to_update(source_table: pa.Table, target_table: pa.Table, join_cols ) from None # Step 1: Prepare source index with join keys and a marker index - # Cast to target table schema, so we can do the join - # See: https://github.com/apache/arrow/issues/37542 - source_index = ( - source_table.cast(target_table.schema) - .select(join_cols_set) - .append_column(SOURCE_INDEX_COLUMN_NAME, pa.array(range(len(source_table)))) - ) + source_index = source_table.select(join_cols_set).append_column(SOURCE_INDEX_COLUMN_NAME, pa.array(range(len(source_table)))) # Step 2: Prepare target index with join keys and a marker target_index = target_table.select(join_cols_set).append_column(TARGET_INDEX_COLUMN_NAME, pa.array(range(len(target_table)))) diff --git a/tests/table/test_upsert.py b/tests/table/test_upsert.py index 4291c18a55..c2280b8d29 100644 --- a/tests/table/test_upsert.py +++ b/tests/table/test_upsert.py @@ -198,6 +198,25 @@ def test_merge_scenario_skip_upd_row(catalog: Catalog) -> None: assert_upsert_result(res, expected_updated, expected_inserted) +def test_merge_after_adding_a_column(catalog: Catalog) -> None: + identifier = "default.test_merge_after_adding_a_column" + _drop_table(catalog, identifier) + + df = pa.table({"order_id": [1, 2], "order_type": ["A", "B"]}) + table = catalog.create_table(identifier, df.schema) + table.append(df) + + with table.update_schema() as update: + update.add_column("ping", StringType()) + + table = catalog.load_table(identifier) + + source_df = pa.table({"order_id": [1], "order_type": ["A"], "ping": ["pong"]}) + res = table.upsert(df=source_df, join_cols=["order_id"], when_matched_update_all=True) + + assert_upsert_result(res, 1, 0) + + def test_merge_scenario_date_as_key(catalog: Catalog) -> None: """ tests a single insert and update; primary key is a date column @@ -955,3 +974,17 @@ def test_get_rows_to_update_compares_nested_values() -> None: target = pa.table({"pk": [1, 2], "value": pa.array([{"a": 1}, {"a": 3}])}) assert get_rows_to_update(source, target, ["pk"]).to_pydict() == {"pk": [2], "value": [{"a": 2}]} + + +def test_get_rows_to_update_aligns_columns_by_name() -> None: + source = pa.table({"value": [20], "pk": [2]}) + target = pa.table({"pk": [2], "value": [10]}) + + assert get_rows_to_update(source, target, ["pk"]).to_pydict() == {"value": [20], "pk": [2]} + + +def test_get_rows_to_update_adds_source_columns_missing_from_target() -> None: + source = pa.table({"pk": [1], "value": [10], "ping": ["pong"]}) + target = pa.table({"pk": [1], "value": [10]}) + + assert get_rows_to_update(source, target, ["pk"]).to_pydict() == {"pk": [1], "value": [10], "ping": ["pong"]}