Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Less Panics for Snapshot timestamps #614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions crates/iceberg/src/spec/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ use std::collections::HashMap;
use std::sync::Arc;

use _serde::SnapshotV2;
use chrono::{DateTime, TimeZone, Utc};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use super::table_metadata::SnapshotLog;
use crate::error::Result;
use crate::error::{timestamp_ms_to_utc, Result};
use crate::io::FileIO;
use crate::spec::{ManifestList, SchemaId, SchemaRef, StructType, TableMetadata};
use crate::{Error, ErrorKind};

/// The ref name of the main branch of the table.
pub const MAIN_BRANCH: &str = "main";

/// Reference to [`Snapshot`].
pub type SnapshotRef = Arc<Snapshot>;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -125,8 +128,14 @@ impl Snapshot {
}
/// Get the timestamp of when the snapshot was created
#[inline]
pub fn timestamp(&self) -> DateTime<Utc> {
Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
pub fn timestamp(&self) -> Result<DateTime<Utc>> {
timestamp_ms_to_utc(self.timestamp_ms)
}

/// Get the timestamp of when the snapshot was created in milliseconds
#[inline]
pub fn timestamp_ms(&self) -> i64 {
self.timestamp_ms
}

/// Get the schema id of this snapshot.
Expand Down Expand Up @@ -386,8 +395,9 @@ mod tests {
assert_eq!(3051729675574597004, result.snapshot_id());
assert_eq!(
Utc.timestamp_millis_opt(1515100955770).unwrap(),
result.timestamp()
result.timestamp().unwrap()
);
assert_eq!(1515100955770, result.timestamp_ms());
assert_eq!(
Summary {
operation: Operation::Append,
Expand Down
2 changes: 1 addition & 1 deletion crates/iceberg/src/spec/table_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl TableMetadata {

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

self.refs
Expand Down
Loading