Skip to content

Commit

Permalink
Introduce Schema update APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordworms committed Jan 11, 2025
1 parent 4603b64 commit 99890b9
Show file tree
Hide file tree
Showing 4 changed files with 831 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/iceberg/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod statistic_file;
mod table_metadata;
mod table_metadata_builder;
mod transform;
pub(crate) mod update;
mod values;
mod view_metadata;
mod view_version;
Expand Down
22 changes: 22 additions & 0 deletions crates/iceberg/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,28 @@ impl Schema {
pub(crate) fn field_id_to_name_map(&self) -> &HashMap<i32, String> {
&self.id_to_name
}

/// id to parent
pub fn id_to_parent(&self) -> HashMap<i32, String> {
let mut id_to_parent = HashMap::new();
let root_name = String::from("<root>");
Self::build_id_to_parent(self.as_struct(), &root_name, &mut id_to_parent);
id_to_parent
}

/// Helper function to recursively build the ID-to-parent mapping.
fn build_id_to_parent(
struct_type: &StructType,
parent_name: &str,
id_to_parent: &mut HashMap<i32, String>,
) {
for field in struct_type.fields() {
id_to_parent.insert(field.id, parent_name.to_string());
if let Type::Struct(ref nested_struct) = *field.field_type {
Self::build_id_to_parent(nested_struct, &field.name, id_to_parent);
}
}
}
}

impl Display for Schema {
Expand Down
Loading

0 comments on commit 99890b9

Please sign in to comment.