Skip to content
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
17 changes: 17 additions & 0 deletions crates/iceberg/src/spec/table_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,22 @@ impl TableMetadata {
.insert(snapshot.snapshot_id(), Arc::new(snapshot));
}

fn construct_refs(&mut self) {
if let Some(current_snapshot_id) = self.current_snapshot_id {
if !self.refs.contains_key(MAIN_BRANCH) {
self.refs
.insert(MAIN_BRANCH.to_string(), SnapshotReference {
snapshot_id: current_snapshot_id,
retention: SnapshotRetention::Branch {
min_snapshots_to_keep: None,
max_snapshot_age_ms: None,
max_ref_age_ms: None,
},
});
}
}
}

/// Normalize this partition spec.
///
/// This is an internal method
Expand All @@ -435,6 +451,7 @@ impl TableMetadata {
pub(super) fn try_normalize(&mut self) -> Result<&mut Self> {
self.validate_current_schema()?;
self.normalize_current_snapshot()?;
self.construct_refs();
self.validate_refs()?;
self.validate_chronological_snapshot_logs()?;
self.validate_chronological_metadata_logs()?;
Expand Down
31 changes: 31 additions & 0 deletions crates/iceberg/src/spec/table_metadata_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,14 +1220,19 @@ impl From<TableMetadataBuildResult> for TableMetadata {

#[cfg(test)]
mod tests {
use std::fs::File;
use std::io::BufReader;
use std::thread::sleep;

use super::*;
use crate::io::FileIOBuilder;
use crate::spec::{
BlobMetadata, NestedField, NullOrder, Operation, PartitionSpec, PrimitiveType, Schema,
SnapshotRetention, SortDirection, SortField, StructType, Summary, Transform, Type,
UnboundPartitionField,
};
use crate::table::Table;
use crate::TableIdent;

const TEST_LOCATION: &str = "s3://bucket/test/location";
const LAST_ASSIGNED_COLUMN_ID: i32 = 3;
Expand Down Expand Up @@ -2381,4 +2386,30 @@ mod tests {
last_updated_ms
);
}

#[test]
fn test_construct_default_main_branch() {
// Load the table without ref
let file = File::open(format!(
"{}/testdata/table_metadata/{}",
env!("CARGO_MANIFEST_DIR"),
"TableMetadataV2Valid.json"
))
.unwrap();
let reader = BufReader::new(file);
let resp = serde_json::from_reader::<_, TableMetadata>(reader).unwrap();

let table = Table::builder()
.metadata(resp)
.metadata_location("s3://bucket/test/location/metadata/v1.json".to_string())
.identifier(TableIdent::from_strs(["ns1", "test1"]).unwrap())
.file_io(FileIOBuilder::new("memory").build().unwrap())
.build()
.unwrap();

assert_eq!(
table.metadata().refs.get(MAIN_BRANCH).unwrap().snapshot_id,
table.metadata().current_snapshot_id().unwrap()
);
}
}
Loading