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


##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -17,31 +17,72 @@
 
 //! Iceberg name mapping.
 
-use std::sync::Arc;
+use std::collections::HashMap;
 
 use serde::{Deserialize, Serialize};
 use serde_with::{serde_as, DefaultOnNull};
 
+use crate::spec::{
+    visit_schema, ListType, MapType, NestedFieldRef, PrimitiveType, Schema, 
SchemaVisitor,
+    StructType,
+};
+use crate::Result;
+
 /// Property name for name mapping.
 pub const DEFAULT_SCHEMA_NAME_MAPPING: &str = "schema.name-mapping.default";
 
 /// Iceberg fallback field name to ID mapping.
 #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
-#[serde(transparent)]
 pub struct NameMapping {
     root: Vec<MappedField>,
+    id_to_field: HashMap<i32, MappedField>,
+    name_to_field: HashMap<String, 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(
+        root: Vec<MappedField>,
+        id_to_field: HashMap<i32, MappedField>,
+        name_to_field: HashMap<String, MappedField>,

Review Comment:
   These should be constructed in constructor, not provided by user.



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -55,7 +96,7 @@ pub struct MappedField {
     #[serde(default)]
     #[serde(skip_serializing_if = "Vec::is_empty")]
     #[serde_as(deserialize_as = "DefaultOnNull")]
-    fields: Vec<Arc<MappedField>>,
+    fields: Vec<MappedField>,

Review Comment:
   Yes, clone would be quite expensive for `MappedField`.



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -179,44 +393,48 @@ mod tests {
     #[test]
     fn test_json_name_mapping_deserialization() {
         let name_mapping = r#"
-        [
             {
-                "field-id": 1,
-                "names": [
-                    "id",
-                    "record_id"
-                ]
-            },
-            {
-                "field-id": 2,
-                "names": [
-                    "data"
-                ]
-            },
-            {
-                "field-id": 3,
-                "names": [
-                    "location"
-                ],
-                "fields": [
+                "root": [

Review Comment:
   Yes, we can't change json here, it's required by iceberg spec.



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -79,15 +120,188 @@ impl MappedField {
     }
 
     /// Get a reference to the field mapping for any child fields.
-    pub fn fields(&self) -> &[Arc<MappedField>] {
+    pub fn fields(&self) -> &[MappedField] {
         &self.fields
     }
 }
 
+/// A trait for visiting and transforming a name mapping
+trait NameMappingVisitor {

Review Comment:
   Moving visitors to `visitor` module?



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -17,31 +17,72 @@
 
 //! Iceberg name mapping.
 
-use std::sync::Arc;
+use std::collections::HashMap;
 
 use serde::{Deserialize, Serialize};
 use serde_with::{serde_as, DefaultOnNull};
 
+use crate::spec::{
+    visit_schema, ListType, MapType, NestedFieldRef, PrimitiveType, Schema, 
SchemaVisitor,
+    StructType,
+};
+use crate::Result;
+
 /// Property name for name mapping.
 pub const DEFAULT_SCHEMA_NAME_MAPPING: &str = "schema.name-mapping.default";
 
 /// Iceberg fallback field name to ID mapping.
 #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
-#[serde(transparent)]
 pub struct NameMapping {
     root: Vec<MappedField>,
+    id_to_field: HashMap<i32, MappedField>,
+    name_to_field: HashMap<String, 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(
+        root: Vec<MappedField>,
+        id_to_field: HashMap<i32, MappedField>,
+        name_to_field: HashMap<String, MappedField>,
+    ) -> Self {
+        Self {
+            root,
+            id_to_field,
+            name_to_field,
+        }
     }
 
     /// Get a reference to fields which are to be mapped from name to field ID.
     pub fn fields(&self) -> &[MappedField] {
         &self.root
     }
+
+    /// Parses name_mapping from JSON.
+    pub fn parse_name_mapping(name_mapping: &str) -> Result<Self> {

Review Comment:
   nit: Implement `FromStr` here?



##########
crates/iceberg/src/spec/name_mapping/mod.rs:
##########
@@ -79,15 +120,188 @@ impl MappedField {
     }
 
     /// Get a reference to the field mapping for any child fields.
-    pub fn fields(&self) -> &[Arc<MappedField>] {
+    pub fn fields(&self) -> &[MappedField] {
         &self.fields
     }
 }
 
+/// A trait for visiting and transforming a name mapping
+trait NameMappingVisitor {
+    /// Aggregated result of `MappedField`s
+    type S;
+    /// Result type for processing one `MappedField`
+    type T;
+
+    /// Handles entire `NameMapping` field
+    fn mapping(&self, field_result: Self::S) -> Self::S;
+
+    /// Handles accessing multiple `MappedField`
+    fn fields(&self, field_results: Vec<Self::T>) -> Self::S;

Review Comment:
   This is incorrect, we need to pass `&[MappedField]` here.



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