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


##########
crates/iceberg/src/spec/mapped_fields.rs:
##########
@@ -0,0 +1,123 @@
+// 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.
+
+//! Iceberg mapped fields.
+
+use std::collections::HashMap;
+
+use serde::{Deserialize, Serialize};
+
+use crate::spec::MappedField;
+
+/// Utility mapping which contains field names to IDs and
+/// field IDs to the underlying [`MappedField`].
+#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
+pub struct MappedFields {

Review Comment:
   > I'm not quite sure what you mean, there is already a name_mapping.rs as a 
separate module. Or do you mean include everything in this file instead and use 
👇 ?
   
   Oh sorry for misclarification. I mean we could create following file 
structures:
   ```
   name_mapping/
      mod.rs
      visitors.rs
   ```
   In rust, `name_maping.rs` is a module called `name_mapping`, and a dir 
`name_maping` + `mod.rs` is also a module called `name_maping`.



##########
crates/iceberg/src/spec/mapped_fields.rs:
##########
@@ -0,0 +1,123 @@
+// 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.
+
+//! Iceberg mapped fields.
+
+use std::collections::HashMap;
+
+use serde::{Deserialize, Serialize};
+
+use crate::spec::MappedField;
+
+/// Utility mapping which contains field names to IDs and
+/// field IDs to the underlying [`MappedField`].
+#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
+pub struct MappedFields {
+    fields: Vec<MappedField>,
+    name_to_id: HashMap<String, i32>,
+    id_to_field: HashMap<i32, MappedField>,
+}
+
+impl MappedFields {
+    /// Create a new [`MappedFields`].
+    pub fn new(fields: Vec<MappedField>) -> Self {
+        let mut name_to_id = HashMap::new();
+        let mut id_to_field = HashMap::new();
+
+        for field in &fields {
+            if let Some(id) = field.field_id() {
+                id_to_field.insert(id, field.clone());

Review Comment:
   Seems java impl doesn't check in `MappedFields`, but checked in index of 
`NameMapping`: 
https://github.com/apache/iceberg/blob/c07f2aabc0a1d02f068ecf1514d2479c0fbdd3b0/core/src/main/java/org/apache/iceberg/mapping/MappingUtil.java#L231
   
   I think it's safe to check here.



##########
crates/iceberg/src/spec/mapped_fields.rs:
##########
@@ -0,0 +1,165 @@
+// 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.
+
+//! Iceberg mapped fields.
+
+use std::collections::HashMap;
+
+use serde::{Deserialize, Serialize};
+
+use crate::spec::MappedField;
+use crate::Error;
+
+/// Utility mapping which contains field names to IDs and
+/// field IDs to the underlying [`MappedField`].
+#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
+pub struct MappedFields {
+    fields: Vec<MappedField>,
+    name_to_id: HashMap<String, i32>,
+    id_to_field: HashMap<i32, MappedField>,
+}
+
+impl MappedFields {
+    /// Create a new [`MappedFields`].
+    ///
+    /// # Errors
+    ///
+    /// This will produce an error if the input fields contain either duplicate
+    /// names or field IDs.
+    pub fn try_new(fields: Vec<MappedField>) -> Result<Self, Error> {
+        let mut name_to_id = HashMap::new();
+        let mut id_to_field = HashMap::new();
+
+        for field in &fields {
+            if let Some(id) = field.field_id() {
+                if id_to_field.contains_key(&id) {
+                    return Err(Error::new(
+                        crate::ErrorKind::DataInvalid,
+                        format!("duplicate id '{id}' is not allowed"),
+                    ));
+                }
+
+                id_to_field.insert(id, field.clone());
+                for name in field.names() {
+                    if name_to_id.contains_key(name) {
+                        return Err(Error::new(
+                            crate::ErrorKind::DataInvalid,
+                            format!("duplicate name '{name}' is not allowed"),
+                        ));
+                    }
+                    name_to_id.insert(name.to_string(), id);
+                }
+            }
+        }
+
+        Ok(Self {
+            fields,
+            name_to_id,
+            id_to_field,
+        })
+    }
+
+    /// Get a reference to the underlying fields.
+    pub fn fields(&self) -> &[MappedField] {
+        &self.fields
+    }
+
+    /// Get a field, by name, returning its ID if it exists, otherwise `None`.
+    pub fn id(&self, field_name: String) -> Option<i32> {

Review Comment:
   ```suggestion
       pub fn id(&self, field_name: &str) -> Option<i32> {
   ```



##########
crates/iceberg/src/spec/name_mapping.rs:
##########
@@ -33,12 +48,38 @@ pub struct NameMapping {
 #[serde(rename_all = "kebab-case")]
 pub struct MappedField {
     #[serde(skip_serializing_if = "Option::is_none")]
-    pub field_id: Option<i32>,
-    pub names: Vec<String>,
+    field_id: Option<i32>,
+    names: Vec<String>,
     #[serde(default)]
     #[serde(skip_serializing_if = "Vec::is_empty")]
     #[serde_as(deserialize_as = "DefaultOnNull")]
-    pub fields: Vec<MappedField>,
+    fields: Vec<MappedField>,

Review Comment:
   We can't change json output since it's iceberg spec. I think we could use 
`#serde[transparent]` to do that?



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