You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/08/08 21:44:29 UTC

[GitHub] [iceberg] namrathamyske commented on a diff in pull request #5364: Core, API: Support scanning from a branch along with time travel

namrathamyske commented on code in PR #5364:
URL: https://github.com/apache/iceberg/pull/5364#discussion_r940689479


##########
core/src/main/java/org/apache/iceberg/util/SnapshotUtil.java:
##########
@@ -322,6 +323,39 @@ public static long snapshotIdAsOfTime(Table table, long timestampMillis) {
     return snapshotId;
   }
 
+  /**
+   * Returns the ID of the most recent snapshot on the given branch
+   * as of the given time in milliseconds
+   *
+   * @param table a {@link Table}
+   * @param branch a {@link String}
+   * @param timestampMillis the timestamp in millis since the Unix epoch
+   * @return the snapshot ID
+   * @throws IllegalArgumentException when no snapshot is found in the table, on the given branch
+   * older than the timestamp
+   */
+  public static long snapshotIdAsOfTime(Table table, String branch, long timestampMillis) {
+    SnapshotRef ref = table.refs().get(branch);
+    Preconditions.checkArgument(ref != null, "Branch %s does not exist", branch);
+    Preconditions.checkArgument(ref.isBranch(), "Ref %s is a tag, not a branch", branch);
+    Long snapshotId = null;
+    long minimumTimeDifference = Long.MAX_VALUE;
+    for (Snapshot snapshot : ancestorsOf(ref.snapshotId(), table::snapshot)) {
+      if (snapshot.timestampMillis() <= timestampMillis) {
+        if (timestampMillis - snapshot.timestampMillis() <= minimumTimeDifference) {
+          minimumTimeDifference = timestampMillis - snapshot.timestampMillis();
+          snapshotId = snapshot.snapshotId();
+        }
+      }
+    }
+
+    Preconditions.checkArgument(snapshotId != null,
+        "Cannot find a snapshot older than %s on branch %s",
+        DateTimeUtil.formatTimestampMillis(timestampMillis), branch);
+
+    return snapshotId;
+  }
+

Review Comment:
   Can we make reuse `snapshotIdAsOfTime` here? We will also need to make necessary changes for this function to accept a branch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org