Skip to content

Commit

Permalink
Allow no previous location
Browse files Browse the repository at this point in the history
  • Loading branch information
c-thiel committed Oct 2, 2024
1 parent a0e0021 commit edba06c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/iceberg/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ mod tests {
.metadata;
let table_metadata_builder = TableMetadataBuilder::new_from_metadata(
table_metadata,
"s3://db/table/metadata/metadata1.gz.json",
Some("s3://db/table/metadata/metadata1.gz.json".to_string()),
);

let uuid = uuid::Uuid::new_v4();
Expand Down
4 changes: 3 additions & 1 deletion crates/iceberg/src/spec/table_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ impl TableMetadata {
///
/// `current_file_location` is the location where the current version
/// of the metadata file is stored. This is used to update the metadata log.
/// If `current_file_location` is `None`, the metadata log will not be updated.
/// This should only be used to stage-create tables.
#[must_use]
pub fn into_builder(self, current_file_location: impl Into<String>) -> TableMetadataBuilder {
pub fn into_builder(self, current_file_location: Option<String>) -> TableMetadataBuilder {
TableMetadataBuilder::new_from_metadata(self, current_file_location)
}

Expand Down
48 changes: 40 additions & 8 deletions crates/iceberg/src/spec/table_metadata_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,19 @@ impl TableMetadataBuilder {
}

/// Creates a new table metadata builder from the given metadata to modify it.
/// Previous file location is used to populate the Metadata Log.

/// `current_file_location` is the location where the current version
/// of the metadata file is stored. This is used to update the metadata log.
/// If `current_file_location` is `None`, the metadata log will not be updated.
/// This should only be used to stage-create tables.
#[must_use]
pub fn new_from_metadata(
previous: TableMetadata,
previous_file_location: impl Into<String>,
previous_file_location: Option<String>,
) -> Self {
Self {
previous_history_entry: Some(MetadataLog {
metadata_file: previous_file_location.into(),
previous_history_entry: previous_file_location.map(|l| MetadataLog {
metadata_file: l,
timestamp_ms: previous.last_updated_ms,
}),
metadata: previous,
Expand Down Expand Up @@ -1191,7 +1195,9 @@ mod tests {
.build()
.unwrap()
.metadata
.into_builder("s3://bucket/test/location/metadata/metadata1.json")
.into_builder(Some(
"s3://bucket/test/location/metadata/metadata1.json".to_string(),
))
}

#[test]
Expand Down Expand Up @@ -1228,7 +1234,9 @@ mod tests {

// Test can serialize v2
let metadata = metadata
.into_builder("s3://bucket/test/location/metadata/metadata1.json")
.into_builder(Some(
"s3://bucket/test/location/metadata/metadata1.json".to_string(),
))
.upgrade_format_version(FormatVersion::V2)
.unwrap()
.build()
Expand Down Expand Up @@ -1534,7 +1542,9 @@ mod tests {
// Set old spec again
let build_result = build_result
.metadata
.into_builder("s3://bucket/test/location/metadata/metadata1.json")
.into_builder(Some(
"s3://bucket/test/location/metadata/metadata1.json".to_string(),
))
.set_default_partition_spec(0)
.unwrap()
.build()
Expand Down Expand Up @@ -1669,6 +1679,28 @@ mod tests {
assert_eq!(build_result.metadata.metadata_log.len(), 0);
}

#[test]
fn test_no_metadata_log_entry_for_no_previous_location() {
// Used for first commit after stage-creation of tables
let metadata = builder_without_changes(FormatVersion::V2)
.build()
.unwrap()
.metadata;
assert_eq!(metadata.metadata_log.len(), 1);

let build_result = metadata
.into_builder(None)
.set_properties(HashMap::from_iter(vec![(
"foo".to_string(),
"bar".to_string(),
)]))
.unwrap()
.build()
.unwrap();

assert_eq!(build_result.metadata.metadata_log.len(), 1);
}

#[test]
fn test_from_metadata_generates_metadata_log() {
let metadata_path = "s3://bucket/test/location/metadata/metadata1.json";
Expand All @@ -1684,7 +1716,7 @@ mod tests {
.build()
.unwrap()
.metadata
.into_builder(metadata_path);
.into_builder(Some(metadata_path.to_string()));

let builder = builder
.add_default_sort_order(SortOrder::unsorted_order())
Expand Down

0 comments on commit edba06c

Please sign in to comment.