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


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

Review Comment:
   The return value should be `Result`, user passed value maybe wrong.



##########
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:
   This should be a `MappedFields`.



##########
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:
   We need to check duplication of `id` and `name` here.



##########
crates/iceberg/src/spec/name_mapping.rs:
##########
@@ -20,11 +20,26 @@
 use serde::{Deserialize, Serialize};
 use serde_with::{serde_as, DefaultOnNull};
 
+/// 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 {
-    pub root: Vec<MappedField>,
+    root: Vec<MappedField>,

Review Comment:
   This should contains a `MappedFields`



##########
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:
   Given we are going to add a lot of visitors for `NameMapping`, how about we 
create a `NameMapping` module, and puts everything related there.



##########
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());
+                for name in field.names() {
+                    name_to_id.insert(name.to_string(), id);
+                }
+            }
+        }
+
+        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: String) -> Option<i32> {
   ```



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