Skip to content

Commit ad9c08c

Browse files
committed
updates
1 parent b7ffe25 commit ad9c08c

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

pyiceberg/table/__init__.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,18 +1303,16 @@ def snapshot_by_name(self, name: str) -> Optional[Snapshot]:
13031303
return self.snapshot_by_id(ref.snapshot_id)
13041304
return None
13051305

1306-
def latest_snapshot_before_timestamp(self, timestamp_ms: int) -> Optional[Snapshot]:
1307-
"""Get the snapshot right before the given timestamp, or None if there is no matching snapshot."""
1308-
result, prev_timestamp = None, 0
1309-
if self.metadata.current_snapshot_id is not None:
1310-
for snapshot in self.current_ancestors():
1311-
if snapshot and prev_timestamp < snapshot.timestamp_ms < timestamp_ms:
1312-
result, prev_timestamp = snapshot, snapshot.timestamp_ms
1313-
return result
1306+
def snapshot_at_or_before_timestamp(self, timestamp_ms: int) -> Optional[Snapshot]:
1307+
"""Get the snapshot that was current at or right before the given timestamp, or None if there is no matching snapshot."""
1308+
for log_entry in reversed(self.history()):
1309+
if log_entry.timestamp_ms <= timestamp_ms:
1310+
return self.snapshot_by_id(log_entry.snapshot_id)
1311+
return None
13141312

1315-
def current_ancestors(self) -> List[Optional[Snapshot]]:
1313+
def current_ancestors(self) -> Iterable[Snapshot]:
13161314
"""Get a list of ancestors of and including the current snapshot."""
1317-
return list(ancestors_of(self.current_snapshot(), self.metadata)) # type: ignore
1315+
return ancestors_of(self.current_snapshot(), self.metadata) # type: ignore
13181316

13191317
def history(self) -> List[SnapshotLogEntry]:
13201318
"""Get the snapshot history of this table."""

pyiceberg/table/snapshots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,6 @@ def ancestors_of(current_snapshot: Snapshot, table_metadata: TableMetadata) -> I
423423
"""Get the ancestors of and including the given snapshot."""
424424
if current_snapshot:
425425
yield current_snapshot
426-
if current_snapshot.parent_snapshot_id is not None:
427-
if parent := table_metadata.snapshot_by_id(current_snapshot.parent_snapshot_id):
428-
yield from ancestors_of(parent, table_metadata)
426+
if current_snapshot.parent_snapshot_id is not None:
427+
if parent := table_metadata.snapshot_by_id(current_snapshot.parent_snapshot_id):
428+
yield from ancestors_of(parent, table_metadata)

0 commit comments

Comments
 (0)