Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions pyiceberg/table/upsert_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,26 @@ 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.

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)

Expand All @@ -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))))
Expand Down
33 changes: 33 additions & 0 deletions tests/table/test_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]}