Skip to content

Commit ea0e645

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 6859fa4 commit ea0e645

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
)
@@ -2068,7 +2069,11 @@ def rollback_to_timestamp(self, timestamp: int) -> ManageSnapshots:
20682069
This for method chaining
20692070
"""
20702071
self._commit_if_ref_updates_exist()
2071-
if (snapshot := self._transaction._table.snapshot_as_of_timestamp(timestamp, inclusive=False)) is None:
2072+
if (
2073+
snapshot := ancestor_right_before_timestamp(
2074+
self._transaction._table.current_snapshot(), self._transaction.table_metadata, timestamp
2075+
)
2076+
) is None:
20722077
raise ValidationError(f"Cannot roll back, no valid snapshot older than: {timestamp}")
20732078

20742079
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
@@ -421,6 +421,17 @@ def set_when_positive(properties: Dict[str, str], num: int, property_name: str)
421421
properties[property_name] = str(num)
422422

423423

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

0 commit comments

Comments
 (0)