diff --git a/crates/iceberg/src/spec/schema.rs b/crates/iceberg/src/spec/schema.rs index 2dc9c4dd1..9364b3a27 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().to_vec(), + 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) @@ -1304,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(