From b7f3c094ff99dd69be33ffbfc5a09abc00645ee1 Mon Sep 17 00:00:00 2001 From: Christian Thiel Date: Mon, 27 May 2024 15:48:40 +0200 Subject: [PATCH 1/3] Add schema into_builder --- crates/iceberg/src/spec/schema.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/iceberg/src/spec/schema.rs b/crates/iceberg/src/spec/schema.rs index 2dc9c4dd14..e59712e8e4 100644 --- a/crates/iceberg/src/spec/schema.rs +++ b/crates/iceberg/src/spec/schema.rs @@ -269,6 +269,16 @@ impl Schema { } } + /// Create a new schema builder from a schema. + pub fn into_builder(self) -> SchemaBuilder { + SchemaBuilder { + schema_id: self.schema_id, + fields: self.r#struct.fields().iter().cloned().collect(), + alias_to_id: self.alias_to_id, + identifier_field_ids: self.identifier_field_ids, + } + } + /// Get field by field id. pub fn field_by_id(&self, field_id: i32) -> Option<&NestedFieldRef> { self.id_to_field.get(&field_id) From 49ddfba674cbec42b6b37efc7d82fdade4ac722e Mon Sep 17 00:00:00 2001 From: Christian Thiel Date: Mon, 27 May 2024 16:04:52 +0200 Subject: [PATCH 2/3] Add test --- crates/iceberg/src/spec/schema.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/iceberg/src/spec/schema.rs b/crates/iceberg/src/spec/schema.rs index e59712e8e4..b59a2f3b7a 100644 --- a/crates/iceberg/src/spec/schema.rs +++ b/crates/iceberg/src/spec/schema.rs @@ -1314,6 +1314,15 @@ table { .contains("Invalid schema: multiple fields for name baz")); } + #[test] + fn test_schema_into_builder() { + let original_schema = table_schema_nested(); + let builder = original_schema.clone().into_builder(); + let schema = builder.build().unwrap(); + + assert_eq!(original_schema, schema); + } + #[test] fn test_schema_index_by_name() { let expected_name_to_id = HashMap::from( From f1c63c166905cdf27a305d3dc120ae3302c66b2c Mon Sep 17 00:00:00 2001 From: Christian Thiel Date: Mon, 27 May 2024 16:10:47 +0200 Subject: [PATCH 3/3] clippy --- crates/iceberg/src/spec/schema.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/iceberg/src/spec/schema.rs b/crates/iceberg/src/spec/schema.rs index b59a2f3b7a..9364b3a279 100644 --- a/crates/iceberg/src/spec/schema.rs +++ b/crates/iceberg/src/spec/schema.rs @@ -273,7 +273,7 @@ impl Schema { pub fn into_builder(self) -> SchemaBuilder { SchemaBuilder { schema_id: self.schema_id, - fields: self.r#struct.fields().iter().cloned().collect(), + fields: self.r#struct.fields().to_vec(), alias_to_id: self.alias_to_id, identifier_field_ids: self.identifier_field_ids, }