Skip to content

Commit

Permalink
fix: Correctly calculate highest_field_id in schema (apache#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
liurenjie1024 authored Sep 9, 2024
1 parent f78c59b commit e08c0e5
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions crates/iceberg/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ impl SchemaBuilder {

/// Builds the schema.
pub fn build(self) -> Result<Schema> {
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);
Expand All @@ -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,

Expand Down Expand Up @@ -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());
}
}

0 comments on commit e08c0e5

Please sign in to comment.