diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index 899a7a9004..f433b917a9 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -1628,10 +1628,15 @@ def snapshot_as_of_timestamp(self, timestamp_ms: int, inclusive: bool = True) -> timestamp_ms: Find snapshot that was current at/before this timestamp inclusive: Includes timestamp_ms in search when True. Excludes timestamp_ms when False """ - for log_entry in reversed(self.history()): - if (inclusive and log_entry.timestamp_ms <= timestamp_ms) or log_entry.timestamp_ms < timestamp_ms: - return self.snapshot_by_id(log_entry.snapshot_id) - return None + # The snapshot log is not guaranteed to be ordered by timestamp, so pick the latest timestamp rather than the last entry + candidate: SnapshotLogEntry | None = None + for log_entry in self.history(): + if log_entry.timestamp_ms > timestamp_ms or (not inclusive and log_entry.timestamp_ms == timestamp_ms): + continue + # On equal timestamps the later entry wins, it is the state that was current last + if candidate is None or log_entry.timestamp_ms >= candidate.timestamp_ms: + candidate = log_entry + return self.snapshot_by_id(candidate.snapshot_id) if candidate is not None else None def history(self) -> list[SnapshotLogEntry]: """Get the snapshot history of this table.""" diff --git a/tests/table/test_init.py b/tests/table/test_init.py index 13361e9cd1..bc6d97e9c1 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -236,6 +236,34 @@ def test_snapshot_by_timestamp(table_v2: Table) -> None: assert table_v2.snapshot_as_of_timestamp(1515100955770, inclusive=False) is None +def test_snapshot_by_timestamp_with_unordered_snapshot_log(table_v2: Table) -> None: + # Writers with slightly skewed clocks can append snapshot log entries out of timestamp order + first, second = table_v2.metadata.snapshot_log + now = second.timestamp_ms + log = [ + SnapshotLogEntry(snapshot_id=second.snapshot_id, timestamp_ms=now), + SnapshotLogEntry(snapshot_id=first.snapshot_id, timestamp_ms=now - 10_000), + ] + table_v2.metadata = table_v2.metadata.model_copy(update={"snapshot_log": log}) + + latest = table_v2.snapshot_as_of_timestamp(now) + assert latest is not None + assert latest.snapshot_id == second.snapshot_id + + earlier = table_v2.snapshot_as_of_timestamp(now - 5_000) + assert earlier is not None + assert earlier.snapshot_id == first.snapshot_id + + assert table_v2.snapshot_as_of_timestamp(now - 10_000, inclusive=False) is None + + # On equal timestamps the later entry wins + tied = [SnapshotLogEntry(snapshot_id=first.snapshot_id, timestamp_ms=now), log[0]] + table_v2.metadata = table_v2.metadata.model_copy(update={"snapshot_log": tied}) + result = table_v2.snapshot_as_of_timestamp(now) + assert result is not None + assert result.snapshot_id == second.snapshot_id + + def test_snapshot_by_id_does_not_exist(table_v2: Table) -> None: assert table_v2.snapshot_by_id(-1) is None