Skip to content

Commit 9affc45

Browse files
committed
update inclusive behaviour and test
1 parent ebe0b1d commit 9affc45

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

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."""
1306+
def snapshot_as_of_timestamp(self, timestamp_ms: int, inclusive: bool = True) -> Optional[Snapshot]:
1307+
"""Get the snapshot that was current as of or right before the given timestamp, or None if there is no matching snapshot.
1308+
1309+
Args:
1310+
timestamp_ms: Find snapshot that was current at/before this timestamp
1311+
inclusive: Includes timestamp_ms in search when True. Excludes timestamp_ms when False
1312+
"""
13081313
for log_entry in reversed(self.history()):
1309-
if log_entry.timestamp_ms <= timestamp_ms:
1314+
if (inclusive and log_entry.timestamp_ms <= timestamp_ms) or log_entry.timestamp_ms < timestamp_ms:
13101315
return self.snapshot_by_id(log_entry.snapshot_id)
13111316
return None
13121317

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)