Skip to content

Pick the latest timestamp in snapshot_as_of_timestamp instead of the last log entry - #4361

Open
sohailshk wants to merge 1 commit into
apache:mainfrom
sohailshk:fix/snapshot-lookup-log-order
Open

sohailshk wants to merge 1 commit into
apache:mainfrom
sohailshk:fix/snapshot-lookup-log-order

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

Rationale for this change

Table.snapshot_as_of_timestamp walks the snapshot log backwards and returns the first entry at or before the timestamp, which assumes the log is ordered by timestamp. The spec does not guarantee that, and Java's TableMetadata accepts a log entry up to one minute older than the previous one, so inside that skew window the lookup can return an older snapshot. Java fixed the same assumption in SnapshotUtil.nullableSnapshotIdAsOfTime (apache/iceberg#17360, "finding the entry with the maximum timestamp <= requested timestamp").

This picks the entry with the largest timestamp that is at or before (or strictly before, with inclusive=False) the requested one. On equal timestamps the later entry wins, unlike Java which keeps the first: PyIceberg logs every snapshot added in one transaction with its own timestamp_ms, so two can share a millisecond and the later one is the state that was actually current. Later-wins is also what PyIceberg and Java did before. inspect.metadata_log_entries uses the same lookup for latest_snapshot_id.

Are these changes tested?

Yes, test_snapshot_by_timestamp_with_unordered_snapshot_log in tests/table/test_init.py (two entries 10 seconds out of order, plus a tie; fails on main, passes here). tests/table/test_init.py and tests/table/test_inspect.py pass.

Are there any user-facing changes?

Time travel by timestamp returns the right snapshot when the snapshot log has entries slightly out of timestamp order.

…last log entry

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.
@Fokko
Fokko self-requested a review November 19, 2026 14:48

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sohailshk, nice catch and the Java parity reasoning in the description makes it easy to follow.

Verified: the new test fails on main and passes with this change. One comment:

  • pyiceberg/table/__init__.py (snapshot_as_of_timestamp): the >= on equal timestamps changes behavior from first-wins to last-wins on ties. The description makes a compelling case (later entry is the state that was actually current), just flagging it here since Java keeps the first entry on ties, so someone diffing behavior across implementations will notice.

CI is green too, LGTM!

@sohailshk

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I'm keeping >=, so the later entry wins on equal timestamps.

I looked at the Java side: SnapshotUtil.nullableSnapshotIdAsOfTime uses a strict > against the best timestamp, so it keeps the first entry. In the review of that change (apache/iceberg#17360) the tie case was raised and the answer was that the spec leaves it undefined, so first-wins there is a by-product of the loop rather than a decision. Last-wins is what snapshot_as_of_timestamp returned before this PR, so keeping it leaves tie behavior unchanged and only fixes the out-of-order case; and the later entry is the state that was current last. If you would rather match Java exactly, it is a one-character change (> instead of >=) and I can switch it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants