This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-3695-eeb1e41f959589347ac98a0575a325cdc923f6e9 in repository https://gitbox.apache.org/repos/asf/iceberg-rust.git
commit d15dca02c53db98de233e43aeebb6e4d0c620c10 Author: Christian <[email protected]> AuthorDate: Mon Nov 16 15:20:42 2026 +0000 fix(spec): validate the metadata log in build() with the entry for the previous file (#3695) * fix(spec): validate the metadata log in build() with the entry for the previous file * test(spec): metadata change after a branch snapshot within the clock-skew tolerance builds --- crates/iceberg/src/spec/table_metadata_builder.rs | 65 ++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/iceberg/src/spec/table_metadata_builder.rs b/crates/iceberg/src/spec/table_metadata_builder.rs index ad561980a..872922039 100644 --- a/crates/iceberg/src/spec/table_metadata_builder.rs +++ b/crates/iceberg/src/spec/table_metadata_builder.rs @@ -1127,12 +1127,13 @@ impl TableMetadataBuilder { .build(&schema)?; self.update_snapshot_log()?; - self.metadata.try_normalize()?; + // Validate the metadata log with the entry for the previous file, as every reader does. if let Some(hist_entry) = self.previous_history_entry.take() { self.metadata.metadata_log.push(hist_entry); } let expired_metadata_logs = self.expire_metadata_log(); + self.metadata.try_normalize()?; Ok(TableMetadataBuildResult { metadata: self.metadata, @@ -2878,6 +2879,68 @@ mod tests { assert_eq!(metadata.default_partition_spec().as_ref(), &expected); } + /// Adds a branch snapshot from a writer whose clock is `skew_ms` ahead, then changes a + /// property, which takes the current time. Returns the snapshot time and the second build. + fn build_after_fast_branch_snapshot(skew_ms: i64) -> (i64, Result<TableMetadataBuildResult>) { + let builder = builder_without_changes(FormatVersion::V2); + let fast = builder.metadata.last_updated_ms + skew_ms; + let snapshot = Snapshot::builder() + .with_snapshot_id(2) + .with_timestamp_ms(fast) + .with_sequence_number(1) + .with_schema_id(0) + .with_manifest_list("/snap-2.avro") + .with_summary(Summary { + operation: Operation::Append, + additional_properties: HashMap::new(), + }) + .build(); + let on_branch = builder + .set_branch_snapshot(snapshot, "b") + .unwrap() + .build() + .unwrap() + .metadata; + assert_eq!(on_branch.last_updated_ms, fast); + + let result = on_branch + .into_builder(Some( + "s3://bucket/test/location/metadata/metadata2.json".to_string(), + )) + .set_properties(HashMap::from([("k".to_string(), "v".to_string())])) + .unwrap() + .build(); + (fast, result) + } + + #[test] + fn test_metadata_change_after_a_fast_branch_snapshot_is_refused_in_build() { + // The change is two hours before the entry for the previous file. No reader could load + // the result, so the build fails. + let (fast, result) = build_after_fast_branch_snapshot(2 * 60 * 60_000); + let err = result.unwrap_err(); + assert_eq!(err.kind(), ErrorKind::DataInvalid); + assert!( + err.message() + .ends_with(&format!(": before last metadata log entry at {fast}")), + "{}", + err.message() + ); + } + + #[test] + fn test_metadata_change_after_a_slightly_fast_branch_snapshot_builds() { + // 30 seconds of clock skew is within the one-minute tolerance. + let (fast, result) = build_after_fast_branch_snapshot(30_000); + assert_eq!( + result.unwrap().metadata.metadata_log.last(), + Some(&MetadataLog { + metadata_file: "s3://bucket/test/location/metadata/metadata2.json".to_string(), + timestamp_ms: fast, + }) + ); + } + #[test] fn test_expire_metadata_log() { let builder = builder_without_changes(FormatVersion::V2);
