Skip to content

Commit ede4720

Browse files
authored
fix: Less Panics for Snapshot timestamps (apache#614)
1 parent ced661f commit ede4720

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

crates/iceberg/src/spec/snapshot.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ use std::collections::HashMap;
2222
use std::sync::Arc;
2323

2424
use _serde::SnapshotV2;
25-
use chrono::{DateTime, TimeZone, Utc};
25+
use chrono::{DateTime, Utc};
2626
use serde::{Deserialize, Serialize};
2727
use typed_builder::TypedBuilder;
2828

2929
use super::table_metadata::SnapshotLog;
30-
use crate::error::Result;
30+
use crate::error::{timestamp_ms_to_utc, Result};
3131
use crate::io::FileIO;
3232
use crate::spec::{ManifestList, SchemaId, SchemaRef, StructType, TableMetadata};
3333
use crate::{Error, ErrorKind};
3434

35+
/// The ref name of the main branch of the table.
36+
pub const MAIN_BRANCH: &str = "main";
37+
3538
/// Reference to [`Snapshot`].
3639
pub type SnapshotRef = Arc<Snapshot>;
3740
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
@@ -125,8 +128,14 @@ impl Snapshot {
125128
}
126129
/// Get the timestamp of when the snapshot was created
127130
#[inline]
128-
pub fn timestamp(&self) -> DateTime<Utc> {
129-
Utc.timestamp_millis_opt(self.timestamp_ms).unwrap()
131+
pub fn timestamp(&self) -> Result<DateTime<Utc>> {
132+
timestamp_ms_to_utc(self.timestamp_ms)
133+
}
134+
135+
/// Get the timestamp of when the snapshot was created in milliseconds
136+
#[inline]
137+
pub fn timestamp_ms(&self) -> i64 {
138+
self.timestamp_ms
130139
}
131140

132141
/// Get the schema id of this snapshot.
@@ -386,8 +395,9 @@ mod tests {
386395
assert_eq!(3051729675574597004, result.snapshot_id());
387396
assert_eq!(
388397
Utc.timestamp_millis_opt(1515100955770).unwrap(),
389-
result.timestamp()
398+
result.timestamp().unwrap()
390399
);
400+
assert_eq!(1515100955770, result.timestamp_ms());
391401
assert_eq!(
392402
Summary {
393403
operation: Operation::Append,

crates/iceberg/src/spec/table_metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl TableMetadata {
258258

259259
/// Append snapshot to table
260260
pub fn append_snapshot(&mut self, snapshot: Snapshot) {
261-
self.last_updated_ms = snapshot.timestamp().timestamp_millis();
261+
self.last_updated_ms = snapshot.timestamp_ms();
262262
self.last_sequence_number = snapshot.sequence_number();
263263

264264
self.refs

0 commit comments

Comments
 (0)