Skip to content

Commit 984057e

Browse files
committed
fix(upsert): add checks for missing table columns before type validations.
1 parent 9928f50 commit 984057e

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎pyiceberg/table/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,11 @@ def upsert(
788788
df_column_names = set(df.schema.names)
789789

790790
for col in join_cols:
791+
if col not in table_arrow_schema.names:
792+
raise ValueError(
793+
f"Join column '{col}' does not exist in the table schema. "
794+
f"Available columns: {', '.join(table_arrow_schema.names)}."
795+
)
791796
table_field = table_arrow_schema.field(col)
792797
# Table-level rejections: These types are fundamentally unreliable or
793798
# unsupported as join keys regardless of the input data format.

‎tests/table/test_upsert.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,25 @@ def test_key_cols_misaligned(catalog: Catalog) -> None:
327327
table.upsert(df=df_src, join_cols=["order_id"])
328328

329329

330+
def test_key_cols_source_only_join_col(catalog: Catalog) -> None:
331+
"""
332+
tests join column present in the source dataframe but missing from the table
333+
"""
334+
335+
identifier = "default.test_key_cols_source_only_join_col"
336+
_drop_table(catalog, identifier)
337+
338+
ctx = SessionContext()
339+
340+
df = ctx.sql("select 1 as order_id, date '2021-01-01' as order_date, 'A' as order_type").to_arrow_table()
341+
table = catalog.create_table(identifier, df.schema)
342+
343+
df_src = ctx.sql("select 1 as order_id, 10 as item_id, date '2021-05-01' as order_date, 'B' as order_type").to_arrow_table()
344+
345+
with pytest.raises(ValueError, match="Join column 'item_id' does not exist in the table schema"):
346+
table.upsert(df=df_src, join_cols=["item_id"])
347+
348+
330349
def test_upsert_with_identifier_fields(catalog: Catalog) -> None:
331350
identifier = "default.test_upsert_with_identifier_fields"
332351
_drop_table(catalog, identifier)

0 commit comments

Comments
 (0)