liurenjie1024 commented on code in PR #1299: URL: https://github.com/apache/iceberg-rust/pull/1299#discussion_r2086378297
########## crates/iceberg/src/expr/visitors/name_mapping_visitor.rs: ########## Review Comment: Sorry for misclarification, I mean we should move this to `spec/name_maping/visitor.rs`. It's odd to put `NameMappingVisitor` in expr module. ########## crates/iceberg/src/spec/name_mapping/mod.rs: ########## @@ -84,9 +133,187 @@ impl MappedField { } } +/// Recursively visits the entire name mapping using visitor +fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S +where V: NameMappingVisitor { + let root_result = visit_fields(namespace, visitor); + 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 { + let mut results: Vec<V::T> = Vec::new(); + + for field in fields { + let child_result = visit_fields(&field.fields, visitor); + let field_result = visitor.field(field, child_result); + results.push(field_result); + } + + visitor.fields(results) +} + +struct IndexByName {} + +impl NameMappingVisitor for IndexByName { + type S = HashMap<String, Arc<MappedField>>; + type T = HashMap<String, Arc<MappedField>>; + + fn mapping( + &self, + field_result: HashMap<String, Arc<MappedField>>, + ) -> HashMap<String, Arc<MappedField>> { + field_result + } + + fn fields( + &self, + field_results: Vec<HashMap<String, Arc<MappedField>>>, + ) -> HashMap<String, Arc<MappedField>> { + field_results + .into_iter() + .fold(HashMap::new(), |mut acc, map| { + acc.extend(map); + acc + }) + } + + fn field( + &self, + field: &MappedField, + child_result: HashMap<String, Arc<MappedField>>, + ) -> HashMap<String, Arc<MappedField>> { + let mut result = child_result; + + for name in &field.names { + for (child_key, child_field) in result.clone().iter() { + let composite_key = format!("{}.{}", name, child_key); + result.insert(composite_key, child_field.clone()); + } + } + + for name in &field.names { + result.insert(name.clone(), Arc::new(field.clone())); + } + result + } +} + +struct IndexById {} + +impl NameMappingVisitor for IndexById { + type S = HashMap<i32, Arc<MappedField>>; + type T = HashMap<i32, Arc<MappedField>>; + + fn mapping( + &self, + field_result: HashMap<i32, Arc<MappedField>>, + ) -> HashMap<i32, Arc<MappedField>> { + field_result + } + + fn fields( + &self, + field_results: Vec<HashMap<i32, Arc<MappedField>>>, + ) -> HashMap<i32, Arc<MappedField>> { + field_results + .into_iter() + .fold(HashMap::new(), |mut acc, map| { + acc.extend(map); + acc + }) + } + + fn field( + &self, + field: &MappedField, + field_results: HashMap<i32, Arc<MappedField>>, + ) -> HashMap<i32, Arc<MappedField>> { + let mut result = field_results; + + if let Some(id) = field.field_id { + result.insert(id, Arc::new(field.clone())); + } + result + } +} + +struct CreateMapping; + +impl SchemaVisitor for CreateMapping { + type T = Vec<Arc<MappedField>>; + + fn schema(&mut self, _schema: &Schema, value: Self::T) -> Result<Self::T> { + Ok(value) + } + + fn field(&mut self, _field: &NestedFieldRef, value: Self::T) -> Result<Self::T> { + Ok(value) + } + + fn r#struct(&mut self, struct_type: &StructType, results: Vec<Self::T>) -> Result<Self::T> { + let mapped_fields = struct_type + .fields() + .iter() + .zip(results) + .map(|(field, result)| { + Arc::new(MappedField::new( + Some(field.id), + vec![field.name.clone()], + result, + )) + }) + .collect::<Vec<Arc<MappedField>>>(); + + Ok(mapped_fields) + } + + fn list(&mut self, list: &ListType, value: Self::T) -> Result<Self::T> { + Ok(vec![Arc::new(MappedField::new( + Some(list.element_field.id), + vec!["element".to_string()], + value, + ))]) + } + + fn map(&mut self, map: &MapType, key_value: Self::T, value: Self::T) -> Result<Self::T> { + Ok(vec![ + Arc::new(MappedField::new( + Some(map.key_field.id), + vec!["key".to_string()], + key_value, + )), + Arc::new(MappedField::new( + Some(map.value_field.id), + vec!["value".to_string()], Review Comment: See https://github.com/apache/iceberg-rust/blob/7eb00d5e1571b77c35b5433ac6959ee7340b46ec/crates/iceberg/src/spec/datatypes.rs#L43 ########## crates/iceberg/src/spec/name_mapping/mod.rs: ########## @@ -84,9 +133,187 @@ impl MappedField { } } +/// Recursively visits the entire name mapping using visitor +fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S Review Comment: ```suggestion fn visit_name_mapping<V>(namespace: &NameMapping, visitor: &V) -> V::S ``` It's odd that the name is `visit_name_mapping` while accepts a list of fields. ########## crates/iceberg/src/spec/name_mapping/mod.rs: ########## @@ -84,9 +133,187 @@ impl MappedField { } } +/// Recursively visits the entire name mapping using visitor +fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S +where V: NameMappingVisitor { + let root_result = visit_fields(namespace, visitor); + 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 { + let mut results: Vec<V::T> = Vec::new(); + + for field in fields { + let child_result = visit_fields(&field.fields, visitor); + let field_result = visitor.field(field, child_result); + results.push(field_result); + } + + visitor.fields(results) +} + +struct IndexByName {} + +impl NameMappingVisitor for IndexByName { + type S = HashMap<String, Arc<MappedField>>; + type T = HashMap<String, Arc<MappedField>>; + + fn mapping( + &self, + field_result: HashMap<String, Arc<MappedField>>, + ) -> HashMap<String, Arc<MappedField>> { + field_result + } + + fn fields( + &self, + field_results: Vec<HashMap<String, Arc<MappedField>>>, + ) -> HashMap<String, Arc<MappedField>> { + field_results + .into_iter() + .fold(HashMap::new(), |mut acc, map| { + acc.extend(map); + acc + }) + } + + fn field( + &self, + field: &MappedField, + child_result: HashMap<String, Arc<MappedField>>, + ) -> HashMap<String, Arc<MappedField>> { + let mut result = child_result; + + for name in &field.names { + for (child_key, child_field) in result.clone().iter() { + let composite_key = format!("{}.{}", name, child_key); + result.insert(composite_key, child_field.clone()); + } + } + + for name in &field.names { + result.insert(name.clone(), Arc::new(field.clone())); + } + result + } +} + +struct IndexById {} + +impl NameMappingVisitor for IndexById { + type S = HashMap<i32, Arc<MappedField>>; + type T = HashMap<i32, Arc<MappedField>>; + + fn mapping( + &self, + field_result: HashMap<i32, Arc<MappedField>>, + ) -> HashMap<i32, Arc<MappedField>> { + field_result + } + + fn fields( + &self, + field_results: Vec<HashMap<i32, Arc<MappedField>>>, + ) -> HashMap<i32, Arc<MappedField>> { + field_results + .into_iter() + .fold(HashMap::new(), |mut acc, map| { + acc.extend(map); + acc + }) + } + + fn field( + &self, + field: &MappedField, + field_results: HashMap<i32, Arc<MappedField>>, + ) -> HashMap<i32, Arc<MappedField>> { + let mut result = field_results; + + if let Some(id) = field.field_id { + result.insert(id, Arc::new(field.clone())); + } + result + } +} + +struct CreateMapping; + +impl SchemaVisitor for CreateMapping { + type T = Vec<Arc<MappedField>>; + + fn schema(&mut self, _schema: &Schema, value: Self::T) -> Result<Self::T> { + Ok(value) + } + + fn field(&mut self, _field: &NestedFieldRef, value: Self::T) -> Result<Self::T> { + Ok(value) + } + + fn r#struct(&mut self, struct_type: &StructType, results: Vec<Self::T>) -> Result<Self::T> { + let mapped_fields = struct_type + .fields() + .iter() + .zip(results) + .map(|(field, result)| { + Arc::new(MappedField::new( + Some(field.id), + vec![field.name.clone()], + result, + )) + }) + .collect::<Vec<Arc<MappedField>>>(); + + Ok(mapped_fields) + } + + fn list(&mut self, list: &ListType, value: Self::T) -> Result<Self::T> { + Ok(vec![Arc::new(MappedField::new( + Some(list.element_field.id), + vec!["element".to_string()], Review Comment: Please use constant here: https://github.com/apache/iceberg-rust/blob/7eb00d5e1571b77c35b5433ac6959ee7340b46ec/crates/iceberg/src/spec/datatypes.rs#L39 ########## crates/iceberg/src/spec/name_mapping/mod.rs: ########## @@ -84,9 +133,187 @@ impl MappedField { } } +/// Recursively visits the entire name mapping using visitor +fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S +where V: NameMappingVisitor { + let root_result = visit_fields(namespace, visitor); + 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 { + let mut results: Vec<V::T> = Vec::new(); + + for field in fields { + let child_result = visit_fields(&field.fields, visitor); + let field_result = visitor.field(field, child_result); + results.push(field_result); + } + + visitor.fields(results) +} + +struct IndexByName {} + +impl NameMappingVisitor for IndexByName { + type S = HashMap<String, Arc<MappedField>>; + type T = HashMap<String, Arc<MappedField>>; + + fn mapping( + &self, + field_result: HashMap<String, Arc<MappedField>>, + ) -> HashMap<String, Arc<MappedField>> { + field_result + } + + fn fields( + &self, + field_results: Vec<HashMap<String, Arc<MappedField>>>, + ) -> HashMap<String, Arc<MappedField>> { + field_results + .into_iter() + .fold(HashMap::new(), |mut acc, map| { + acc.extend(map); + acc + }) + } + + fn field( + &self, + field: &MappedField, + child_result: HashMap<String, Arc<MappedField>>, + ) -> HashMap<String, Arc<MappedField>> { + let mut result = child_result; + + for name in &field.names { + for (child_key, child_field) in result.clone().iter() { + let composite_key = format!("{}.{}", name, child_key); + result.insert(composite_key, child_field.clone()); + } + } + + for name in &field.names { + result.insert(name.clone(), Arc::new(field.clone())); + } + result + } +} + +struct IndexById {} + +impl NameMappingVisitor for IndexById { + type S = HashMap<i32, Arc<MappedField>>; + type T = HashMap<i32, Arc<MappedField>>; + + fn mapping( + &self, + field_result: HashMap<i32, Arc<MappedField>>, + ) -> HashMap<i32, Arc<MappedField>> { + field_result + } + + fn fields( + &self, + field_results: Vec<HashMap<i32, Arc<MappedField>>>, + ) -> HashMap<i32, Arc<MappedField>> { + field_results + .into_iter() + .fold(HashMap::new(), |mut acc, map| { + acc.extend(map); + acc + }) + } + + fn field( + &self, + field: &MappedField, + field_results: HashMap<i32, Arc<MappedField>>, + ) -> HashMap<i32, Arc<MappedField>> { + let mut result = field_results; + + if let Some(id) = field.field_id { + result.insert(id, Arc::new(field.clone())); + } + result + } +} + +struct CreateMapping; + +impl SchemaVisitor for CreateMapping { + type T = Vec<Arc<MappedField>>; + + fn schema(&mut self, _schema: &Schema, value: Self::T) -> Result<Self::T> { + Ok(value) + } + + fn field(&mut self, _field: &NestedFieldRef, value: Self::T) -> Result<Self::T> { + Ok(value) + } + + fn r#struct(&mut self, struct_type: &StructType, results: Vec<Self::T>) -> Result<Self::T> { + let mapped_fields = struct_type + .fields() + .iter() + .zip(results) + .map(|(field, result)| { + Arc::new(MappedField::new( + Some(field.id), + vec![field.name.clone()], + result, + )) + }) + .collect::<Vec<Arc<MappedField>>>(); + + Ok(mapped_fields) + } + + fn list(&mut self, list: &ListType, value: Self::T) -> Result<Self::T> { + Ok(vec![Arc::new(MappedField::new( + Some(list.element_field.id), + vec!["element".to_string()], + value, + ))]) + } + + fn map(&mut self, map: &MapType, key_value: Self::T, value: Self::T) -> Result<Self::T> { + Ok(vec![ + Arc::new(MappedField::new( + Some(map.key_field.id), + vec!["key".to_string()], Review Comment: See: https://github.com/apache/iceberg-rust/blob/7eb00d5e1571b77c35b5433ac6959ee7340b46ec/crates/iceberg/src/spec/datatypes.rs#L41 ########## crates/iceberg/src/spec/name_mapping/mod.rs: ########## @@ -84,9 +133,187 @@ impl MappedField { } } +/// Recursively visits the entire name mapping using visitor +fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S Review Comment: Also the return value should be a `Result` ########## crates/iceberg/src/expr/visitors/name_mapping_visitor.rs: ########## @@ -0,0 +1,35 @@ +// 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; + /// Result type for processing one `MappedField` + type T; + + /// Handles entire `NameMapping` field + fn mapping(&self, field_result: Self::S) -> Self::S; + + /// Takes all visited child maps and merges into one map + fn fields(&self, field_results: Vec<Self::T>) -> Self::S; Review Comment: I don't think we need this method. It's required in java since they have a middle layer like `MappedFields`, but this is not the case for rust. I think the whole visitor trait could be simplified as following: ```rust trait Visitor { type S; type T; fn mapping(&self, name_mapping: &NameMaping, children: Vec<Self::T>) -> Result<Self::S>; fn field(&self, field: &MappedField, children: Vec<Self::T>) -> Result<Self::T>; } } ``` -- 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