Skip to content

Commit 0063ecb

Browse files
committed
add snapshot_by_timestamp API
1 parent aa361d1 commit 0063ecb

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

pyiceberg/table/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,10 @@ def snapshot_by_name(self, name: str) -> Optional[Snapshot]:
12901290
return self.snapshot_by_id(ref.snapshot_id)
12911291
return None
12921292

1293+
def snapshot_by_timestamp(self, timestamp_ms: int) -> Optional[Snapshot]:
1294+
"""Get the snapshot right before the given timestamp, or None if there is no matching snapshot."""
1295+
return self.metadata.snapshot_by_timestamp(timestamp_ms)
1296+
12931297
def history(self) -> List[SnapshotLogEntry]:
12941298
"""Get the snapshot history of this table."""
12951299
return self.metadata.snapshot_log

pyiceberg/table/metadata.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ def snapshot_by_id(self, snapshot_id: int) -> Optional[Snapshot]:
230230
"""Get the snapshot by snapshot_id."""
231231
return next((snapshot for snapshot in self.snapshots if snapshot.snapshot_id == snapshot_id), None)
232232

233+
def snapshot_by_timestamp(self, timestamp_ms: int) -> Optional[Snapshot]:
234+
"""Get the snapshot right before the given timestamp."""
235+
result, prev_timestamp = None, 0
236+
for snapshot in self.snapshots:
237+
if prev_timestamp < snapshot.timestamp_ms < timestamp_ms:
238+
result, prev_timestamp = snapshot, snapshot.timestamp_ms
239+
return result if result else None
240+
233241
def schema_by_id(self, schema_id: int) -> Optional[Schema]:
234242
"""Get the schema by schema_id."""
235243
return next((schema for schema in self.schemas if schema.schema_id == schema_id), None)

0 commit comments

Comments
 (0)