Skip to content

Commit 57e0f90

Browse files
committed
update parent snapshot when there are conflicts and change exception
1 parent 0923dc4 commit 57e0f90

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pyiceberg/table/update/snapshot.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from sortedcontainers import SortedList
2929

30+
from pyiceberg.exceptions import CommitFailedException
3031
from pyiceberg.expressions import (
3132
AlwaysFalse,
3233
BooleanExpression,
@@ -259,9 +260,14 @@ def _commit(self) -> UpdatesAndRequirements:
259260
if not isinstance(self._transaction._table, StagedTable):
260261
starting_snapshot = self._transaction.table_metadata.current_snapshot()
261262
current_snapshot = self._transaction._table.refresh().metadata.current_snapshot()
263+
262264
if starting_snapshot is not None and current_snapshot is not None:
263265
self._validate(starting_snapshot, current_snapshot)
264266

267+
# If the current snapshot is not the same as the parent snapshot, update the parent snapshot id
268+
if current_snapshot is not None and current_snapshot.snapshot_id != self._parent_snapshot_id:
269+
self._parent_snapshot_id = current_snapshot.snapshot_id
270+
265271
with write_manifest_list(
266272
format_version=self._transaction.table_metadata.format_version,
267273
output_file=self._io.new_output(manifest_list_file_path),
@@ -309,7 +315,7 @@ def _validate(self, starting_snapshot: Snapshot, current_snapshot: Snapshot) ->
309315
snapshot_operation = snapshot.summary.operation if snapshot.summary is not None else None
310316

311317
if snapshot_operation not in allowed_operations[self._operation]:
312-
raise ValueError(
318+
raise CommitFailedException(
313319
f"Operation {snapshot_operation} is not allowed when performing {self._operation}. "
314320
"Check for overlaps or conflicts."
315321
)

tests/integration/test_add_files.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,15 @@ def test_conflict_delete_delete(
858858
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
859859
) -> None:
860860
identifier = "default.test_conflict"
861-
tbl1 = _create_table(session_catalog, identifier, format_version, [arrow_table_with_null])
861+
tbl1 = _create_table(session_catalog, identifier, format_version, schema=arrow_table_with_null.schema)
862+
tbl1.append(arrow_table_with_null)
862863
tbl2 = session_catalog.load_table(identifier)
863864

864865
tbl1.delete("string == 'z'")
865866

866-
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
867+
with pytest.raises(
868+
CommitFailedException, match="Operation .* is not allowed when performing .*. Check for overlaps or conflicts."
869+
):
867870
# tbl2 isn't aware of the commit by tbl1
868871
tbl2.delete("string == 'z'")
869872

@@ -874,7 +877,8 @@ def test_conflict_delete_append(
874877
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
875878
) -> None:
876879
identifier = "default.test_conflict"
877-
tbl1 = _create_table(session_catalog, identifier, format_version, [arrow_table_with_null])
880+
tbl1 = _create_table(session_catalog, identifier, format_version, schema=arrow_table_with_null.schema)
881+
tbl1.append(arrow_table_with_null)
878882
tbl2 = session_catalog.load_table(identifier)
879883

880884
# This is allowed
@@ -888,12 +892,15 @@ def test_conflict_append_delete(
888892
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
889893
) -> None:
890894
identifier = "default.test_conflict"
891-
tbl1 = _create_table(session_catalog, identifier, format_version, [arrow_table_with_null])
895+
tbl1 = _create_table(session_catalog, identifier, format_version, schema=arrow_table_with_null.schema)
896+
tbl1.append(arrow_table_with_null)
892897
tbl2 = session_catalog.load_table(identifier)
893898

894899
tbl1.append(arrow_table_with_null)
895900

896-
with pytest.raises(CommitFailedException, match="(branch main has changed: expected id ).*"):
901+
with pytest.raises(
902+
CommitFailedException, match="Operation .* is not allowed when performing .*. Check for overlaps or conflicts."
903+
):
897904
# tbl2 isn't aware of the commit by tbl1
898905
tbl2.delete("string == 'z'")
899906

@@ -904,7 +911,8 @@ def test_conflict_append_append(
904911
spark: SparkSession, session_catalog: Catalog, arrow_table_with_null: pa.Table, format_version: int
905912
) -> None:
906913
identifier = "default.test_conflict"
907-
tbl1 = _create_table(session_catalog, identifier, format_version, [arrow_table_with_null])
914+
tbl1 = _create_table(session_catalog, identifier, format_version, schema=arrow_table_with_null.schema)
915+
tbl1.append(arrow_table_with_null)
908916
tbl2 = session_catalog.load_table(identifier)
909917

910918
tbl1.append(arrow_table_with_null)

0 commit comments

Comments
 (0)