Skip to content
Merged
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
10 changes: 6 additions & 4 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,11 @@ def upsert(

update_row_cnt = len(rows_to_update)

# build the match predicate filter
overwrite_mask_predicate = upsert_util.create_match_filter(rows_to_update, join_cols)
if len(rows_to_update) > 0:
# build the match predicate filter
overwrite_mask_predicate = upsert_util.create_match_filter(rows_to_update, join_cols)

tx.overwrite(rows_to_update, overwrite_filter=overwrite_mask_predicate)
tx.overwrite(rows_to_update, overwrite_filter=overwrite_mask_predicate)
Comment on lines -1204 to +1205
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we enforce not producing empty snapshots lower in the stack, in the write functions themselves (overwrite/append)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a great suggestion. I was also looking into that, but there are two caveats:

  • Before computing all the manifests, we don't know if they are empty. The delete manifests are lazily computed, so making sure that we short-circuit earlier in the process, makes sure that we don't do all the work.
  • There is also a behavioral aspect around it, for example, when you do tbl.append(df) where df is empty, or tbl.delete('x = 10'), you may still want to produce a snapshot to indicate that something happened. My opinion is that it should result in a no-op, but Java creates a snapshot:

image

I believe @sungwy and I had a similar discussion.


if when_not_matched_insert_all:
expr_match = upsert_util.create_match_filter(matched_iceberg_table, join_cols)
Expand All @@ -1211,7 +1212,8 @@ def upsert(

insert_row_cnt = len(rows_to_insert)

tx.append(rows_to_insert)
if insert_row_cnt > 0:
tx.append(rows_to_insert)

return UpsertResult(rows_updated=update_row_cnt, rows_inserted=insert_row_cnt)

Expand Down
13 changes: 13 additions & 0 deletions tests/table/test_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from pyiceberg.io.pyarrow import schema_to_pyarrow
from pyiceberg.schema import Schema
from pyiceberg.table import UpsertResult
from pyiceberg.table.snapshots import Operation
from pyiceberg.table.upsert_util import create_match_filter
from pyiceberg.types import IntegerType, NestedField, StringType
from tests.catalog.test_base import InMemoryCatalog, Table
Expand Down Expand Up @@ -368,9 +369,21 @@ def test_upsert_with_identifier_fields(catalog: Catalog) -> None:
)
upd = tbl.upsert(df)

expected_operations = [Operation.APPEND, Operation.OVERWRITE, Operation.APPEND, Operation.APPEND]

assert upd.rows_updated == 1
assert upd.rows_inserted == 1

assert [snap.summary.operation for snap in tbl.snapshots() if snap.summary is not None] == expected_operations

# This should be a no-op
upd = tbl.upsert(df)

assert upd.rows_updated == 0
assert upd.rows_inserted == 0

assert [snap.summary.operation for snap in tbl.snapshots() if snap.summary is not None] == expected_operations


def test_upsert_into_empty_table(catalog: Catalog) -> None:
identifier = "default.test_upsert_into_empty_table"
Expand Down