Skip to content

Commit e08c0e5

Browse files
fix: Correctly calculate highest_field_id in schema (apache#590)
1 parent f78c59b commit e08c0e5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

crates/iceberg/src/spec/schema.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ impl SchemaBuilder {
106106

107107
/// Builds the schema.
108108
pub fn build(self) -> Result<Schema> {
109-
let highest_field_id = self.fields.iter().map(|f| f.id).max().unwrap_or(0);
110-
111109
let field_id_to_accessor = self.build_accessors();
112110

113111
let r#struct = StructType::new(self.fields);
@@ -130,12 +128,13 @@ impl SchemaBuilder {
130128
.map(|(k, v)| (k.to_lowercase(), *v))
131129
.collect();
132130

131+
let highest_field_id = id_to_field.keys().max().cloned().unwrap_or(0);
132+
133133
Ok(Schema {
134134
r#struct,
135135
schema_id: self.schema_id,
136136
highest_field_id,
137137
identifier_field_ids: self.identifier_field_ids,
138-
139138
alias_to_id: self.alias_to_id,
140139
id_to_field,
141140

@@ -2229,4 +2228,13 @@ table {
22292228
assert!(result.is_ok());
22302229
assert_eq!(result.unwrap(), Type::Struct(schema.as_struct().clone()));
22312230
}
2231+
2232+
#[test]
2233+
fn test_highest_field_id() {
2234+
let schema = table_schema_nested();
2235+
assert_eq!(17, schema.highest_field_id());
2236+
2237+
let schema = table_schema_simple().0;
2238+
assert_eq!(3, schema.highest_field_id());
2239+
}
22322240
}

0 commit comments

Comments
 (0)