Skip to content

Commit 82da826

Browse files
authored
feat: add construct_ref for table_metadata (#1043)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> ## What changes are included in this PR? <!-- Provide a summary of the modifications in this PR. List the main changes such as new features, bug fixes, refactoring, or any other updates. --> This PR add a construct_ref to insert the main branch if it's not found in refs. ref from: https://github.com/apache/iceberg-python/blob/f45966208dac7c0a2fbe5b16d643a816db2bacb3/pyiceberg/table/metadata.py#L117 ## Are these changes tested? <!-- Specify what test covers (unit test, integration test, etc.). If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> --------- Co-authored-by: ZENOTME <[email protected]>
1 parent f59f590 commit 82da826

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

crates/iceberg/src/spec/table_metadata.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,22 @@ impl TableMetadata {
425425
.insert(snapshot.snapshot_id(), Arc::new(snapshot));
426426
}
427427

428+
fn construct_refs(&mut self) {
429+
if let Some(current_snapshot_id) = self.current_snapshot_id {
430+
if !self.refs.contains_key(MAIN_BRANCH) {
431+
self.refs
432+
.insert(MAIN_BRANCH.to_string(), SnapshotReference {
433+
snapshot_id: current_snapshot_id,
434+
retention: SnapshotRetention::Branch {
435+
min_snapshots_to_keep: None,
436+
max_snapshot_age_ms: None,
437+
max_ref_age_ms: None,
438+
},
439+
});
440+
}
441+
}
442+
}
443+
428444
/// Normalize this partition spec.
429445
///
430446
/// This is an internal method
@@ -435,6 +451,7 @@ impl TableMetadata {
435451
pub(super) fn try_normalize(&mut self) -> Result<&mut Self> {
436452
self.validate_current_schema()?;
437453
self.normalize_current_snapshot()?;
454+
self.construct_refs();
438455
self.validate_refs()?;
439456
self.validate_chronological_snapshot_logs()?;
440457
self.validate_chronological_metadata_logs()?;

crates/iceberg/src/spec/table_metadata_builder.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,14 +1220,19 @@ impl From<TableMetadataBuildResult> for TableMetadata {
12201220

12211221
#[cfg(test)]
12221222
mod tests {
1223+
use std::fs::File;
1224+
use std::io::BufReader;
12231225
use std::thread::sleep;
12241226

12251227
use super::*;
1228+
use crate::io::FileIOBuilder;
12261229
use crate::spec::{
12271230
BlobMetadata, NestedField, NullOrder, Operation, PartitionSpec, PrimitiveType, Schema,
12281231
SnapshotRetention, SortDirection, SortField, StructType, Summary, Transform, Type,
12291232
UnboundPartitionField,
12301233
};
1234+
use crate::table::Table;
1235+
use crate::TableIdent;
12311236

12321237
const TEST_LOCATION: &str = "s3://bucket/test/location";
12331238
const LAST_ASSIGNED_COLUMN_ID: i32 = 3;
@@ -2381,4 +2386,30 @@ mod tests {
23812386
last_updated_ms
23822387
);
23832388
}
2389+
2390+
#[test]
2391+
fn test_construct_default_main_branch() {
2392+
// Load the table without ref
2393+
let file = File::open(format!(
2394+
"{}/testdata/table_metadata/{}",
2395+
env!("CARGO_MANIFEST_DIR"),
2396+
"TableMetadataV2Valid.json"
2397+
))
2398+
.unwrap();
2399+
let reader = BufReader::new(file);
2400+
let resp = serde_json::from_reader::<_, TableMetadata>(reader).unwrap();
2401+
2402+
let table = Table::builder()
2403+
.metadata(resp)
2404+
.metadata_location("s3://bucket/test/location/metadata/v1.json".to_string())
2405+
.identifier(TableIdent::from_strs(["ns1", "test1"]).unwrap())
2406+
.file_io(FileIOBuilder::new("memory").build().unwrap())
2407+
.build()
2408+
.unwrap();
2409+
2410+
assert_eq!(
2411+
table.metadata().refs.get(MAIN_BRANCH).unwrap().snapshot_id,
2412+
table.metadata().current_snapshot_id().unwrap()
2413+
);
2414+
}
23842415
}

0 commit comments

Comments
 (0)