Skip to content

Commit 2831e5a

Browse files
committed
update inclusive behaviour and test
1 parent 12c4a84 commit 2831e5a

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

pyiceberg/table/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,10 +1291,15 @@ def snapshot_by_name(self, name: str) -> Optional[Snapshot]:
12911291
return self.snapshot_by_id(ref.snapshot_id)
12921292
return None
12931293

1294-
def snapshot_at_or_before_timestamp(self, timestamp_ms: int) -> Optional[Snapshot]:
1295-
"""Get the snapshot that was current at or right before the given timestamp, or None if there is no matching snapshot."""
1294+
def snapshot_as_of_timestamp(self, timestamp_ms: int, inclusive: bool = True) -> Optional[Snapshot]:
1295+
"""Get the snapshot that was current as of or right before the given timestamp, or None if there is no matching snapshot.
1296+
1297+
Args:
1298+
timestamp_ms: Find snapshot that was current at/before this timestamp
1299+
inclusive: Includes timestamp_ms in search when True. Excludes timestamp_ms when False
1300+
"""
12961301
for log_entry in reversed(self.history()):
1297-
if log_entry.timestamp_ms <= timestamp_ms:
1302+
if (inclusive and log_entry.timestamp_ms <= timestamp_ms) or log_entry.timestamp_ms < timestamp_ms:
12981303
return self.snapshot_by_id(log_entry.snapshot_id)
12991304
return None
13001305

tests/table/test_init.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,16 @@ def test_snapshot_by_id(table_v2: Table) -> None:
205205

206206

207207
def test_snapshot_by_timestamp(table_v2: Table) -> None:
208-
assert table_v2.snapshot_at_or_before_timestamp(1555100955771) == Snapshot(
209-
snapshot_id=3055729675574597004,
210-
parent_snapshot_id=3051729675574597004,
211-
sequence_number=1,
212-
timestamp_ms=1555100955770,
213-
manifest_list="s3://a/b/2.avro",
214-
summary=Summary(operation=Operation.APPEND),
215-
schema_id=1,
208+
assert table_v2.snapshot_as_of_timestamp(1515100955770) == Snapshot(
209+
snapshot_id=3051729675574597004,
210+
parent_snapshot_id=None,
211+
sequence_number=0,
212+
timestamp_ms=1515100955770,
213+
manifest_list='s3://a/b/1.avro',
214+
summary=Summary(Operation.APPEND),
215+
schema_id=None,
216216
)
217+
assert table_v2.snapshot_as_of_timestamp(1515100955770, inclusive=False) is None
217218

218219

219220
def test_current_ancestors(table_v2: Table) -> None:

0 commit comments

Comments
 (0)