Skip to content

Commit 6cc9122

Browse files
committed
find ancestor before timestamp
we cannot use snapshot_as_of_timestamp() as it finds previously current snapshots but not necessarily an ancestor. An example is here: https://iceberg.apache.org/docs/nightly/spark-queries/?h=ancestor#history
1 parent 254413f commit 6cc9122

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pyiceberg/table/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
SnapshotLogEntry,
114114
SnapshotSummaryCollector,
115115
Summary,
116+
ancestor_right_before_timestamp,
116117
ancestors_of,
117118
update_snapshot_summaries,
118119
)
@@ -2050,7 +2051,11 @@ def rollback_to_timestamp(self, timestamp: int) -> ManageSnapshots:
20502051
This for method chaining
20512052
"""
20522053
self._commit_if_ref_updates_exist()
2053-
if (snapshot := self._transaction._table.snapshot_as_of_timestamp(timestamp, inclusive=False)) is None:
2054+
if (
2055+
snapshot := ancestor_right_before_timestamp(
2056+
self._transaction._table.current_snapshot(), self._transaction.table_metadata, timestamp
2057+
)
2058+
) is None:
20542059
raise ValidationError(f"Cannot roll back, no valid snapshot older than: {timestamp}")
20552060

20562061
update, requirement = self._transaction._set_ref_snapshot(

pyiceberg/table/snapshots.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ def set_when_positive(properties: Dict[str, str], num: int, property_name: str)
419419
properties[property_name] = str(num)
420420

421421

422+
def ancestor_right_before_timestamp(
423+
current_snapshot: Optional[Snapshot], table_metadata: TableMetadata, timestamp_ms: int
424+
) -> Optional[Snapshot]:
425+
"""Get the ancestor right before the given timestamp."""
426+
if current_snapshot is not None:
427+
for ancestor in ancestors_of(current_snapshot, table_metadata):
428+
if ancestor.timestamp_ms < timestamp_ms:
429+
return ancestor
430+
return None
431+
432+
422433
def ancestors_of(current_snapshot: Optional[Snapshot], table_metadata: TableMetadata) -> Iterable[Snapshot]:
423434
"""Get the ancestors of and including the given snapshot."""
424435
if current_snapshot:

0 commit comments

Comments
 (0)