Skip to content

Commit

Permalink
Replace i64 with DateTime (#94)
Browse files Browse the repository at this point in the history
* Replace i64 with DateTime

* Expose TimestampMillis only via public APIs

* Avoid changing fields on structs and remove TimestampMillis

* Pin UUID version
  • Loading branch information
fqaiser94 authored Nov 22, 2023
1 parent 9d1a6ab commit 497a1b5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ tokio = { version = "1", features = ["macros"] }
typed-builder = "^0.18"
url = "2"
urlencoding = "2"
uuid = "1.5.0"
# We pin uuid's version to 1.5.0 because this bug: https://github.com/uuid-rs/uuid/issues/720
uuid = "~1.5.0"
6 changes: 5 additions & 1 deletion crates/catalog/rest/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ mod _serde {

#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
use iceberg::spec::ManifestListLocation::ManifestListFile;
use iceberg::spec::{
FormatVersion, NestedField, Operation, PrimitiveType, Schema, Snapshot, SnapshotLog,
Expand Down Expand Up @@ -984,7 +985,10 @@ mod tests {
uuid!("b55d9dda-6561-423a-8bfc-787980ce421f"),
table.metadata().uuid()
);
assert_eq!(1646787054459, table.metadata().last_updated_ms());
assert_eq!(
Utc.timestamp_millis_opt(1646787054459).unwrap(),
table.metadata().last_updated_ms()
);
assert_eq!(
vec![&Arc::new(
Schema::builder()
Expand Down
11 changes: 8 additions & 3 deletions crates/iceberg/src/spec/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/*!
* Snapshots
*/
use chrono::{DateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -116,8 +117,8 @@ impl Snapshot {
}
/// Get the timestamp of when the snapshot was created
#[inline]
pub fn timestamp(&self) -> i64 {
self.timestamp_ms
pub fn timestamp(&self) -> DateTime<Utc> {
Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
}
/// Create snapshot builder
pub fn builder() -> SnapshotBuilder {
Expand Down Expand Up @@ -309,6 +310,7 @@ pub enum SnapshotRetention {

#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
use std::collections::HashMap;

use crate::spec::snapshot::{
Expand All @@ -334,7 +336,10 @@ mod tests {
.try_into()
.unwrap();
assert_eq!(3051729675574597004, result.snapshot_id());
assert_eq!(1515100955770, result.timestamp());
assert_eq!(
Utc.timestamp_millis_opt(1515100955770).unwrap(),
result.timestamp()
);
assert_eq!(
Summary {
operation: Operation::Append,
Expand Down
15 changes: 12 additions & 3 deletions crates/iceberg/src/spec/table_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use super::{

use _serde::TableMetadataEnum;

use chrono::{DateTime, TimeZone, Utc};

static MAIN_BRANCH: &str = "main";
static DEFAULT_SPEC_ID: i32 = 0;
static DEFAULT_SORT_ORDER_ID: i64 = 0;
Expand Down Expand Up @@ -133,8 +135,8 @@ impl TableMetadata {

/// Returns last updated time.
#[inline]
pub fn last_updated_ms(&self) -> i64 {
self.last_updated_ms
pub fn last_updated_ms(&self) -> DateTime<Utc> {
Utc.timestamp_millis_opt(self.last_updated_ms).unwrap()
}

/// Returns schemas
Expand Down Expand Up @@ -242,7 +244,7 @@ impl TableMetadata {

/// Append snapshot to table
pub fn append_snapshot(&mut self, snapshot: Snapshot) {
self.last_updated_ms = snapshot.timestamp();
self.last_updated_ms = snapshot.timestamp().timestamp_millis();
self.last_sequence_number = snapshot.sequence_number();

self.refs
Expand Down Expand Up @@ -771,6 +773,13 @@ pub struct SnapshotLog {
pub timestamp_ms: i64,
}

impl SnapshotLog {
/// Returns the last updated timestamp as a DateTime<Utc> with millisecond precision
pub fn timestamp(self) -> DateTime<Utc> {
Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
}
}

#[cfg(test)]
mod tests {

Expand Down

0 comments on commit 497a1b5

Please sign in to comment.