From e08c0e51f80b7fc2103962da29e4ede2a4a5aad7 Mon Sep 17 00:00:00 2001 From: Renjie Liu Date: Mon, 9 Sep 2024 11:57:22 +0800 Subject: [PATCH] fix: Correctly calculate highest_field_id in schema (#590) --- crates/iceberg/src/spec/schema.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/iceberg/src/spec/schema.rs b/crates/iceberg/src/spec/schema.rs index 106bfb1d8..63a9e3cb4 100644 --- a/crates/iceberg/src/spec/schema.rs +++ b/crates/iceberg/src/spec/schema.rs @@ -106,8 +106,6 @@ impl SchemaBuilder { /// Builds the schema. pub fn build(self) -> Result { - let highest_field_id = self.fields.iter().map(|f| f.id).max().unwrap_or(0); - let field_id_to_accessor = self.build_accessors(); let r#struct = StructType::new(self.fields); @@ -130,12 +128,13 @@ impl SchemaBuilder { .map(|(k, v)| (k.to_lowercase(), *v)) .collect(); + let highest_field_id = id_to_field.keys().max().cloned().unwrap_or(0); + Ok(Schema { r#struct, schema_id: self.schema_id, highest_field_id, identifier_field_ids: self.identifier_field_ids, - alias_to_id: self.alias_to_id, id_to_field, @@ -2229,4 +2228,13 @@ table { assert!(result.is_ok()); assert_eq!(result.unwrap(), Type::Struct(schema.as_struct().clone())); } + + #[test] + fn test_highest_field_id() { + let schema = table_schema_nested(); + assert_eq!(17, schema.highest_field_id()); + + let schema = table_schema_simple().0; + assert_eq!(3, schema.highest_field_id()); + } }