Skip to content

Fix time-travel lookup to not assume snapshot-log ordering #17358

Closed

Description

@xndai

Apache Iceberg version

1.11.0 (latest release)

Query engine

None

Please describe the bug 🐞

SnapshotUtil.nullableSnapshotIdAsOfTime() finds the time-travel snapshot by iterating snapshot-log and returning the last entry with timestampMillis <= requested. This implicitly assumes entries are in strictly ascending time order.
However:

  • The spec does not mandate ordering of snapshot-log entries
  • TableMetadata only validates ordering with a 1-minute clock-skew tolerance, meaning entries within that window can be in reverse order
  • In the reverse-order case, the loop may return the wrong snapshot — an earlier entry that happens to appear later in the list

Fix: Instead of relying on iteration order, find the entry with the maximum timestamp that is still <= requestedTimestamp.
Something like:

  for (HistoryEntry logEntry : table.history()) {
      if (logEntry.timestampMillis() <= timestampMillis) {
          if (snapshotId == null || logEntry.timestampMillis() > bestTimestamp) {
              snapshotId = logEntry.snapshotId();
              bestTimestamp = logEntry.timestampMillis();
          }
      }
  }

This makes the behavior correct regardless of entry ordering.

Willingness to contribute

  • I can contribute a fix for this bug independently
  • I would be willing to contribute a fix for this bug with guidance from the Iceberg community
  • I cannot contribute a fix for this bug at this time

Activity

  1. added
    bug
    on Jul 24, 2026
  2. xndai commented on Jul 24, 2026

    @xndai
    ContributorAuthor

    PR created #17360

  3. rahulsmahadev commented on Aug 6, 2026

    @rahulsmahadev
    Contributor

    This looks fixed by #17360, which merged on 2026-07-30.

    SnapshotUtil.nullableSnapshotIdAsOfTime now tracks the maximum qualifying timestamp instead of relying on the iteration order of the snapshot log:

    Long snapshotId = null;
    long bestTimestamp = Long.MIN_VALUE;
    for (HistoryEntry logEntry : table.history()) {
      if (logEntry.timestampMillis() <= timestampMillis
          && logEntry.timestampMillis() > bestTimestamp) {
        snapshotId = logEntry.snapshotId();
        bestTimestamp = logEntry.timestampMillis();
      }
    }

    which is the approach suggested here, and TestSnapshotUtil#snapshotIdAsOfTimeWithOutOfOrderHistory covers the reverse-order case. Can this be closed?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions