wypoon commented on a change in pull request #1508: URL: https://github.com/apache/iceberg/pull/1508#discussion_r733903603
########## File path: core/src/main/java/org/apache/iceberg/util/SnapshotUtil.java ########## @@ -144,4 +152,80 @@ public static Snapshot snapshotAfter(Table table, long snapshotId) { throw new IllegalStateException( String.format("Cannot find snapshot after %s: not an ancestor of table's current snapshot", snapshotId)); } + + /** + * Returns the ID of the most recent snapshot for the table as of the timestamp. + * + * @param table a {@link Table} + * @param timestampMillis the timestamp in millis since the Unix epoch + * @return the snapshot ID + * @throws IllegalArgumentException when no snapshot is found in the table + * older than the timestamp + */ + public static long snapshotIdAsOfTime(Table table, long timestampMillis) { + Long snapshotId = null; + for (HistoryEntry logEntry : table.history()) { + if (logEntry.timestampMillis() <= timestampMillis) { + snapshotId = logEntry.snapshotId(); + } + } + + Preconditions.checkArgument(snapshotId != null, + "Cannot find a snapshot older than %s", formatTimestampMillis(timestampMillis)); + return snapshotId; + } + + /** + * Returns the schema of the table for the specified snapshot. + * + * @param table a {@link Table} + * @param snapshotId the ID of the snapshot + * @return the schema + */ + public static Schema schemaFor(Table table, long snapshotId) { + Snapshot snapshot = table.snapshot(snapshotId); + Preconditions.checkArgument(snapshot != null, "Cannot find snapshot with ID %s", snapshotId); + Integer schemaId = snapshot.schemaId(); + + // schemaId could be null, if snapshot was created before Iceberg added schema id to snapshot + if (schemaId != null) { + Schema schema = table.schemas().get(schemaId); + Preconditions.checkState(schema != null, + "Cannot find schema with schema id %s", schemaId); + return schema; + } + + // TODO: recover the schema by reading previous metadata files + return table.schema(); + } + + /** + * Convenience method for returning the schema of the table for a snapshot, + * when we have a snapshot id or a timestamp. Only one of them should be specified + * (non-null), or an IllegalArgumentException is thrown. + * + * @param table a {@link Table} + * @param snapshotId the ID of the snapshot + * @param timestampMillis the timestamp in millis since the Unix epoch + * @return the schema + * @throws IllegalArgumentException if both snapshotId and timestampMillis are non-null + */ + public static Schema schemaFor(Table table, Long snapshotId, Long timestampMillis) { Review comment: Yes, this was used in the spark3 part, which has been removed. I'll remove it from this change. -- 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-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org