dhruvarya-db commented on code in PR #3260:
URL: https://github.com/apache/iceberg-rust/pull/3260#discussion_r4251840140
##########
crates/iceberg/src/util/snapshot.rs:
##########
@@ -76,10 +78,57 @@ pub fn ancestors_between(
})
}
+/// Resolve the snapshot ID from the main-history entry with the greatest
timestamp
+/// at or before `timestamp_ms` (milliseconds since the Unix epoch), taking
the first
+/// entry on ties.
+///
+/// This matches Java's ordering. PyIceberg takes the last qualifying log
entry,
+/// so ties or out-of-order timestamps can produce different results.
+///
+/// Returns [`ErrorKind::DataInvalid`] if no matching history exists.
+/// The returned snapshot may have expired, so
+/// [`snapshot_by_id`](crate::spec::TableMetadata::snapshot_by_id) can still
return `None`.
+///
+/// ```
+/// # use iceberg::TableIdent;
+/// # use iceberg::io::FileIO;
+/// # use iceberg::table::StaticTable;
+/// use iceberg::util::snapshot::snapshot_id_as_of_time;
+/// # #[tokio::main]
+/// # async fn main() -> iceberg::Result<()> {
+/// # let location = concat!(env!("CARGO_MANIFEST_DIR"),
"/testdata/example_table_metadata_v2.json");
+/// # let ident = TableIdent::from_strs(["ns", "t"])?;
+/// # let table = StaticTable::from_metadata_file(location, ident,
FileIO::new_with_fs()).await?;
+/// let metadata = table.metadata();
+/// // The snapshot log has entries at 1515100955770 and 1555100955770.
+/// let snapshot_id = snapshot_id_as_of_time(&metadata, 1_555_100_955_770)?;
+/// assert_eq!(snapshot_id, 3055729675574597004);
+/// assert!(snapshot_id_as_of_time(&metadata, 1_515_100_955_769).is_err());
+/// # Ok(())
+/// # }
+/// ```
+pub fn snapshot_id_as_of_time(table_metadata: &TableMetadataRef, timestamp_ms:
i64) -> Result<i64> {
+ table_metadata
+ .history()
+ .iter()
+ .enumerate()
+ .filter(|(_, entry)| entry.timestamp_ms() <= timestamp_ms)
+ // Keep the first entry on ties, matching Java's timestamp selection.
+ .max_by_key(|(idx, entry)| (entry.timestamp_ms(), Reverse(*idx)))
Review Comment:
Updated as per your suggestion
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]