liurenjie1024 commented on code in PR #1299:
URL: https://github.com/apache/iceberg-rust/pull/1299#discussion_r2094956624


##########
crates/iceberg/src/spec/name_mapping/visitor.rs:
##########
@@ -0,0 +1,30 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::spec::MappedField;
+
+/// A trait for visiting and transforming a name mapping
+pub trait NameMappingVisitor {
+    /// Aggregated result of `MappedField`s
+    type S;
+
+    /// Handles entire `NameMapping` field
+    fn mapping(&self, field_result: Self::S) -> Self::S;
+
+    /// Handles a single `MappedField`
+    fn field(&self, field: &MappedField, field_result: Self::S) -> Self::S;
+}

Review Comment:
   ```suggestion
   pub trait NameMappingVisitor {
       type T;
       /// Aggregated result of `MappedField`s
       type S;
   
       /// Handles entire `NameMapping` field
       fn mapping(&mut self, mapping: &NameMapping, field_result: Vec<Self::T>) 
-> Result<Self::S>;
   
       /// Handles a single `MappedField`
       fn field(&mut self, field: &MappedField, field_result: Vec<Self::T>) -> 
Result<Self::T>;
   }
   ```



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -29,19 +40,59 @@ pub const DEFAULT_SCHEMA_NAME_MAPPING: &str = 
"schema.name-mapping.default";
 #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
 #[serde(transparent)]
 pub struct NameMapping {
-    root: Vec<MappedField>,
+    // this is the one field that actually gets round‑tripped
+    root: Vec<Arc<MappedField>>,
+    // both of these get skipped during (de)serialization and default to empty
+    #[serde(skip)]
+    id_to_field: HashMap<i32, Arc<MappedField>>,
+    #[serde(skip)]
+    name_to_field: HashMap<String, Arc<MappedField>>,
 }
 
 impl NameMapping {
-    /// Create a new [`NameMapping`] given a collection of mapped fields.
-    pub fn new(fields: Vec<MappedField>) -> Self {
-        Self { root: fields }
+    /// Creates new `NameMapping` instance
+    pub fn new(schema: &Schema) -> Self {
+        // Create `MappedField` instances by visiting schema.
+        let root = visit_schema(schema, &mut CreateMapping).unwrap();

Review Comment:
   We can construct a `NameMapping` without index here, and then assign the 
indcies to it before returning.



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -77,16 +128,170 @@ impl MappedField {
     pub fn names(&self) -> &[String] {
         &self.names
     }
+}
 
-    /// Get a reference to the field mapping for any child fields.
-    pub fn fields(&self) -> &[Arc<MappedField>] {
-        &self.fields
+/// Recursively visits the entire name mapping using visitor
+fn visit_name_mapping<V>(name_mapping: &Vec<Arc<MappedField>>, visitor: &V) -> 
Result<V::S>
+where
+    V: NameMappingVisitor,
+    V::S: IntoIterator + FromIterator<<V::S as IntoIterator>::Item>,
+{
+    let root_result = visit_fields(name_mapping, visitor);
+    Ok(visitor.mapping(root_result))
+}
+
+/// Recursively visits a slice of mapped fields using visitor
+fn visit_fields<V>(fields: &Vec<Arc<MappedField>>, visitor: &V) -> V::S
+where
+    V: NameMappingVisitor,
+    V::S: IntoIterator + FromIterator<<V::S as IntoIterator>::Item>,

Review Comment:
   Please refine the visiting logic with suggested visitor.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to