This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-4361-4e604fe30d06ced1dc21853d7bbd7176aabf41d4
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git

commit bdebaf35ae5215b6679c418b926ca174bda8953b
Author: Sohail Shaikh <[email protected]>
AuthorDate: Wed Nov 25 22:13:05 2026 +0000

    Pick the latest timestamp in snapshot_as_of_timestamp instead of the last 
log entry (#4361)
    
    The snapshot log is not guaranteed to be ordered by timestamp, so iterating 
it in reverse could return an older snapshot when entries were appended out of 
order.
---
 pyiceberg/table/__init__.py | 13 +++++++++----
 tests/table/test_init.py    | 28 ++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py
index a8fcdb16e..65b1599fa 100644
--- a/pyiceberg/table/__init__.py
+++ b/pyiceberg/table/__init__.py
@@ -1686,10 +1686,15 @@ class Table:
             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 547ca94fc..4cdace07f 100644
--- a/tests/table/test_init.py
+++ b/tests/table/test_init.py
@@ -240,6 +240,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
 

Reply via email to