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
32 changes: 15 additions & 17 deletions pyiceberg/table/update/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,26 @@ def _new_manifest_list_file_name(snapshot_id: int, attempt: int, commit_uuid: uu
class CommitWindow:
"""Tracks the commit range to validate against during retry.

base: The snapshot when the operation began (exclusive lower bound, fixed across retries).
base_id: The starting snapshot id (exclusive lower bound, fixed across retries). It is kept
even when the refreshed metadata no longer exposes the snapshot.
head: The branch HEAD snapshot after metadata refresh (inclusive upper bound).
"""

base: Snapshot | None
head: Snapshot | None
base_id: int | None = None

@classmethod
def resolve(cls, metadata: TableMetadata, base_id: int | None, branch: str | None) -> CommitWindow:
"""Resolve a CommitWindow from metadata, starting snapshot ID, and target branch."""
head = metadata.snapshot_by_name(branch)
base = metadata.snapshot_by_id(base_id) if base_id is not None else None
if base_id is not None and base is None:
raise ValidationException(f"Cannot find starting snapshot {base_id}")
return cls(base=base, head=head)
return cls(head=metadata.snapshot_by_name(branch), base_id=base_id)

def is_empty(self) -> bool:
"""Return True if no concurrent commits occurred (validation can be skipped).

A None base means the operation started on a table with no snapshots. If a head
exists, another writer landed the first snapshot concurrently, so the window is not
empty and validation must run against the whole history.
A None base_id means the operation started on a table with no snapshots. If a head
exists, another writer landed a snapshot concurrently, so the window is not empty.
"""
return self.head is None or (self.base is not None and self.base.snapshot_id == self.head.snapshot_id)
return self.head is None or (self.base_id is not None and self.base_id == self.head.snapshot_id)


class _SnapshotProducer(UpdateTableMetadata[U], Generic[U]):
Expand Down Expand Up @@ -502,7 +498,7 @@ def _validate_concurrent_changes(self) -> None:
return

catalog_head = self._commit_window.head
starting_snapshot = self._commit_window.base
starting_snapshot_id = self._commit_window.base_id

if catalog_head is None:
return
Expand All @@ -512,20 +508,22 @@ def _validate_concurrent_changes(self) -> None:
conflict_detection_filter = self._predicate if self._predicate != AlwaysFalse() else None

if isolation_level == IsolationLevel.SERIALIZABLE:
_validate_added_data_files(table, catalog_head, conflict_detection_filter, starting_snapshot, self._case_sensitive)
_validate_added_data_files(table, catalog_head, conflict_detection_filter, starting_snapshot_id, self._case_sensitive)

if self._predicate != AlwaysFalse():
# partition_set is None, so any concurrently committed delete file counts as
# a conflict even in unrelated partitions. Scope this via partition projection
# (as Java does) when delete-file write support lands.
_validate_no_new_delete_files(
table, catalog_head, conflict_detection_filter, None, starting_snapshot, self._case_sensitive
table, catalog_head, conflict_detection_filter, None, starting_snapshot_id, self._case_sensitive
)
_validate_deleted_data_files(
table, catalog_head, conflict_detection_filter, starting_snapshot_id, self._case_sensitive
)
_validate_deleted_data_files(table, catalog_head, conflict_detection_filter, starting_snapshot, self._case_sensitive)

if self._deleted_data_files:
_validate_data_files_exist(table, catalog_head, self._deleted_data_files, starting_snapshot)
_validate_no_new_deletes_for_data_files(table, catalog_head, self._deleted_data_files, starting_snapshot)
_validate_data_files_exist(table, catalog_head, self._deleted_data_files, starting_snapshot_id)
_validate_no_new_deletes_for_data_files(table, catalog_head, self._deleted_data_files, starting_snapshot_id)

def _build_partition_projection(self, spec_id: int) -> BooleanExpression:
project = inclusive_projection(self.schema(), self.spec(spec_id), self._case_sensitive)
Expand Down
Loading