Skip to content

Commit 497a1b5

Browse files
authored
Replace i64 with DateTime (#94)
* Replace i64 with DateTime * Expose TimestampMillis only via public APIs * Avoid changing fields on structs and remove TimestampMillis * Pin UUID version
1 parent 9d1a6ab commit 497a1b5

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ tokio = { version = "1", features = ["macros"] }
5555
typed-builder = "^0.18"
5656
url = "2"
5757
urlencoding = "2"
58-
uuid = "1.5.0"
58+
# We pin uuid's version to 1.5.0 because this bug: https://github.com/uuid-rs/uuid/issues/720
59+
uuid = "~1.5.0"

crates/catalog/rest/src/catalog.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ mod _serde {
590590

591591
#[cfg(test)]
592592
mod tests {
593+
use chrono::{TimeZone, Utc};
593594
use iceberg::spec::ManifestListLocation::ManifestListFile;
594595
use iceberg::spec::{
595596
FormatVersion, NestedField, Operation, PrimitiveType, Schema, Snapshot, SnapshotLog,
@@ -984,7 +985,10 @@ mod tests {
984985
uuid!("b55d9dda-6561-423a-8bfc-787980ce421f"),
985986
table.metadata().uuid()
986987
);
987-
assert_eq!(1646787054459, table.metadata().last_updated_ms());
988+
assert_eq!(
989+
Utc.timestamp_millis_opt(1646787054459).unwrap(),
990+
table.metadata().last_updated_ms()
991+
);
988992
assert_eq!(
989993
vec![&Arc::new(
990994
Schema::builder()

crates/iceberg/src/spec/snapshot.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/*!
1919
* Snapshots
2020
*/
21+
use chrono::{DateTime, TimeZone, Utc};
2122
use serde::{Deserialize, Serialize};
2223
use std::collections::HashMap;
2324
use std::sync::Arc;
@@ -116,8 +117,8 @@ impl Snapshot {
116117
}
117118
/// Get the timestamp of when the snapshot was created
118119
#[inline]
119-
pub fn timestamp(&self) -> i64 {
120-
self.timestamp_ms
120+
pub fn timestamp(&self) -> DateTime<Utc> {
121+
Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
121122
}
122123
/// Create snapshot builder
123124
pub fn builder() -> SnapshotBuilder {
@@ -309,6 +310,7 @@ pub enum SnapshotRetention {
309310

310311
#[cfg(test)]
311312
mod tests {
313+
use chrono::{TimeZone, Utc};
312314
use std::collections::HashMap;
313315

314316
use crate::spec::snapshot::{
@@ -334,7 +336,10 @@ mod tests {
334336
.try_into()
335337
.unwrap();
336338
assert_eq!(3051729675574597004, result.snapshot_id());
337-
assert_eq!(1515100955770, result.timestamp());
339+
assert_eq!(
340+
Utc.timestamp_millis_opt(1515100955770).unwrap(),
341+
result.timestamp()
342+
);
338343
assert_eq!(
339344
Summary {
340345
operation: Operation::Append,

crates/iceberg/src/spec/table_metadata.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use super::{
3232

3333
use _serde::TableMetadataEnum;
3434

35+
use chrono::{DateTime, TimeZone, Utc};
36+
3537
static MAIN_BRANCH: &str = "main";
3638
static DEFAULT_SPEC_ID: i32 = 0;
3739
static DEFAULT_SORT_ORDER_ID: i64 = 0;
@@ -133,8 +135,8 @@ impl TableMetadata {
133135

134136
/// Returns last updated time.
135137
#[inline]
136-
pub fn last_updated_ms(&self) -> i64 {
137-
self.last_updated_ms
138+
pub fn last_updated_ms(&self) -> DateTime<Utc> {
139+
Utc.timestamp_millis_opt(self.last_updated_ms).unwrap()
138140
}
139141

140142
/// Returns schemas
@@ -242,7 +244,7 @@ impl TableMetadata {
242244

243245
/// Append snapshot to table
244246
pub fn append_snapshot(&mut self, snapshot: Snapshot) {
245-
self.last_updated_ms = snapshot.timestamp();
247+
self.last_updated_ms = snapshot.timestamp().timestamp_millis();
246248
self.last_sequence_number = snapshot.sequence_number();
247249

248250
self.refs
@@ -771,6 +773,13 @@ pub struct SnapshotLog {
771773
pub timestamp_ms: i64,
772774
}
773775

776+
impl SnapshotLog {
777+
/// Returns the last updated timestamp as a DateTime<Utc> with millisecond precision
778+
pub fn timestamp(self) -> DateTime<Utc> {
779+
Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
780+
}
781+
}
782+
774783
#[cfg(test)]
775784
mod tests {
776785

0 commit comments

Comments
 (0)