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.

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.

1 participant