Skip to content

Commit d8e6e6f

Browse files
committed
add snapshot_by_timestamp API
1 parent 65a03d2 commit d8e6e6f

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
@@ -1302,6 +1302,10 @@ def snapshot_by_name(self, name: str) -> Optional[Snapshot]:
13021302
return self.snapshot_by_id(ref.snapshot_id)
13031303
return None
13041304

1305+
def snapshot_by_timestamp(self, timestamp_ms: int) -> Optional[Snapshot]:
1306+
"""Get the snapshot right before the given timestamp, or None if there is no matching snapshot."""
1307+
return self.metadata.snapshot_by_timestamp(timestamp_ms)
1308+
13051309
def history(self) -> List[SnapshotLogEntry]:
13061310
"""Get the snapshot history of this table."""
13071311
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)