diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 3ea41e3f22..b66c99c74d 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -79,6 +79,7 @@ from pyiceberg.table.update import ( AddPartitionSpecUpdate, AddSchemaUpdate, + AddSnapshotUpdate, AddSortOrderUpdate, AssertCreate, AssertRefSnapshotId, @@ -1238,7 +1239,7 @@ def commit_transaction(self) -> Table: return self._table def _attempt_landed(self) -> bool: - """Refresh the table and return whether a snapshot written by this transaction is present. + """Refresh the table and return whether the snapshots added by this transaction are present. Raises: CommitStateUnknownException: If the table cannot be refreshed, the outcome is unknown and @@ -1251,8 +1252,9 @@ def _attempt_landed(self) -> bool: f"Commit failed and the table could not be refreshed to check whether it landed: {e}" ) from e - return all( - self._table.metadata.snapshot_by_id(producer._snapshot_id) is not None for producer in self._snapshot_producers + added_snapshot_ids = {update.snapshot.snapshot_id for update in self._updates if isinstance(update, AddSnapshotUpdate)} + return bool(added_snapshot_ids) and all( + self._table.metadata.snapshot_by_id(snapshot_id) is not None for snapshot_id in added_snapshot_ids ) def _cleanup_uncommitted_manifests(self) -> None: diff --git a/tests/table/test_commit_retry.py b/tests/table/test_commit_retry.py index 67653be957..99fcc826fd 100644 --- a/tests/table/test_commit_retry.py +++ b/tests/table/test_commit_retry.py @@ -1615,6 +1615,36 @@ def test_lost_response_on_the_final_retry_keeps_the_landed_snapshot(catalog: Cat _assert_single_readable_snapshot(catalog, "default.lost_response_final_retry", [{"x": 1}, {"x": 2}, {"x": 3}]) +def test_lost_response_on_overwrite_of_an_empty_table_keeps_the_landed_snapshot(catalog: Catalog) -> None: + """A producer that stages no snapshot must not make the landed-attempt check fail.""" + catalog.create_namespace("default") + table = catalog.create_table("default.lost_response_empty_overwrite", schema=_test_schema()) + + import pyarrow as pa + + with _commit_outcomes(catalog, "lost"), pytest.warns(UserWarning, match="Delete operation did not match any records"): + table.overwrite(pa.table({"x": [1, 2, 3]})) + + _assert_single_readable_snapshot(catalog, "default.lost_response_empty_overwrite", [{"x": 1}, {"x": 2}, {"x": 3}]) + + +def test_lost_response_on_empty_table_overwrite_before_any_retry_keeps_the_landed_snapshot(catalog: Catalog) -> None: + """The landed attempt is detected before the retry budget is consulted.""" + catalog.create_namespace("default") + table = catalog.create_table( + "default.lost_response_empty_overwrite_no_retries", + schema=_test_schema(), + properties={TableProperties.COMMIT_NUM_RETRIES: "0"}, + ) + + import pyarrow as pa + + with _commit_outcomes(catalog, "lost"), pytest.warns(UserWarning, match="Delete operation did not match any records"): + table.overwrite(pa.table({"x": [1, 2, 3]})) + + _assert_single_readable_snapshot(catalog, "default.lost_response_empty_overwrite_no_retries", [{"x": 1}, {"x": 2}, {"x": 3}]) + + def test_unreadable_commit_outcome_raises_commit_state_unknown(catalog: Catalog) -> None: """When the table cannot be refreshed, the outcome is unknown and no file is deleted.""" catalog.create_namespace("default")