viirya commented on code in PR #3665:
URL: https://github.com/apache/iceberg-rust/pull/3665#discussion_r4546897296


##########
crates/iceberg/src/transaction/update_schema.rs:
##########
@@ -458,11 +530,35 @@ impl StatelessAction for UpdateSchemaAction {
             .with_identifier_field_ids(base_schema.identifier_field_ids())
             .build()?;
 
-        let updates = vec![
+        let mut updates = vec![
             TableUpdate::AddSchema { schema },
             TableUpdate::SetCurrentSchema { schema_id: -1 },
         ];

Review Comment:
   Good catch. Fixed in c66ea5e: additions whose parent is not in the new 
schema are dropped before the mapping update, so they get no entry. 
`test_add_column_under_deleted_parent_adds_no_name_mapping_entry` covers it.
   
   The root cause is older than this PR: `UpdateSchemaAction` accepts an 
addition under a column deleted in the same update and silently drops it, and 
the dropped ID is not counted in `last-column-id`. Java rejects this case 
(`Cannot add to a column that will be deleted`). I'll track that in a separate 
issue.



##########
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:
   Done in c66ea5e: each level now builds a `HashMap<&str, i32>` of the added 
names once and looks up each alias in it.



-- 
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