szehon-ho commented on code in PR #3665:
URL: https://github.com/apache/iceberg-rust/pull/3665#discussion_r4545944740


##########
crates/iceberg/src/transaction/update_schema.rs:
##########
@@ -328,6 +329,77 @@ fn rebuild_field(
     }
 }
 
+// ---------------------------------------------------------------------------
+// Name mapping update
+// ---------------------------------------------------------------------------
+
+/// Add entries for added columns to a name mapping, as Java's 
`MappingUtil.update` does.
+///
+/// Each added field gets an entry, with entries for its nested fields, 
appended under its
+/// parent's entry. An existing entry in the same struct loses the added name, 
so a dropped
+/// column no longer claims it. Entries of deleted columns are kept.
+fn add_columns_to_name_mapping(
+    mapping: &NameMapping,
+    adds: &HashMap<Option<i32>, Vec<NestedFieldRef>>,
+) -> NameMapping {
+    let root_adds = adds.get(&None).map_or(&[][..], Vec::as_slice);
+    NameMapping::new(add_columns_to_mapped_fields(
+        mapping.fields().iter(),
+        root_adds,
+        adds,
+    ))
+}
+
+fn add_columns_to_mapped_fields<'a>(
+    fields: impl Iterator<Item = &'a MappedField>,
+    added: &[NestedFieldRef],
+    adds: &HashMap<Option<i32>, Vec<NestedFieldRef>>,
+) -> Vec<MappedField> {
+    fields
+        .map(|field| {
+            let nested_added = field
+                .field_id()
+                .and_then(|id| adds.get(&Some(id)))
+                .map_or(&[][..], Vec::as_slice);
+            let nested = add_columns_to_mapped_fields(
+                field.fields().iter().map(Arc::as_ref),
+                nested_added,
+                adds,
+            );
+            let names = field
+                .names()
+                .iter()
+                .filter(|name| {
+                    !added
+                        .iter()
+                        .any(|add| &add.name == *name && Some(add.id) != 
field.field_id())

Review Comment:
   Consider building a `HashMap<&str, i32>` of added names once per parent and 
looking up each alias here. This avoids scanning every addition for every 
existing alias during batch schema changes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to