viirya commented on code in PR #258:
URL: https://github.com/apache/iceberg-rust/pull/258#discussion_r1523724615


##########
crates/iceberg/src/spec/arrow.rs:
##########
@@ -0,0 +1,553 @@
+// 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::error::Result;
+use crate::spec::{
+    ListType, MapType, NestedField, NestedFieldRef, PrimitiveType, Schema, 
StructType, Type,
+};
+use crate::{Error, ErrorKind};
+use arrow_schema::{DataType, Field, Fields, Schema as ArrowSchema, TimeUnit};
+use std::sync::Arc;
+
+/// A post order arrow schema visitor.
+///
+/// For order of methods called, please refer to [`visit_schema`].
+pub trait ArrowSchemaVisitor {
+    /// Return type of this visitor.
+    type T;
+    type U;
+
+    /// Called before struct/list/map field.
+    fn before_field(&mut self, _field: &Field) -> Result<()> {
+        Ok(())
+    }
+
+    /// Called after struct/list/map field.
+    fn after_field(&mut self, _field: &Field) -> Result<()> {
+        Ok(())
+    }
+
+    /// Called after schema's type visited.
+    fn schema(&mut self, schema: &ArrowSchema, values: Vec<Self::T>) -> 
Result<Self::U>;
+
+    /// Called after struct's fields visited.
+    fn r#struct(&mut self, fields: &Fields, results: Vec<Self::T>) -> 
Result<Self::T>;
+
+    /// Called after list fields visited.
+    fn list(&mut self, list: &DataType, value: Self::T) -> Result<Self::T>;
+
+    /// Called after map's key and value fields visited.
+    fn map(&mut self, map: &DataType, key_value: Self::T, value: Self::T) -> 
Result<Self::T>;
+
+    /// Called when see a primitive type.
+    fn primitive(&mut self, p: &DataType) -> Result<Self::T>;
+}
+
+/// Visiting a type in post order.
+fn visit_type<V: ArrowSchemaVisitor>(r#type: &DataType, visitor: &mut V) -> 
Result<V::T> {
+    match r#type {
+        p if p.is_primitive()
+            || matches!(
+                p,
+                DataType::Boolean
+                    | DataType::Utf8
+                    | DataType::Binary
+                    | DataType::FixedSizeBinary(_)
+            ) =>
+        {
+            visitor.primitive(p)
+        }
+        DataType::List(element_field) => {
+            visitor.before_field(element_field)?;
+            let value = visit_type(element_field.data_type(), visitor)?;
+            visitor.after_field(element_field)?;
+            visitor.list(r#type, value)
+        }
+        DataType::Map(field, _) => match field.data_type() {
+            DataType::Struct(fields) => {
+                if fields.len() != 2 {
+                    return Err(Error::new(
+                        ErrorKind::DataInvalid,
+                        "Map field must have exactly 2 fields",
+                    ));
+                }
+
+                let key_field = &fields[0];
+                let value_field = &fields[1];
+
+                let key_result = {
+                    visitor.before_field(key_field)?;
+                    let ret = visit_type(key_field.data_type(), visitor)?;
+                    visitor.after_field(key_field)?;
+                    ret
+                };
+
+                let value_result = {
+                    visitor.before_field(value_field)?;
+                    let ret = visit_type(value_field.data_type(), visitor)?;
+                    visitor.after_field(value_field)?;
+                    ret
+                };
+
+                visitor.map(r#type, key_result, value_result)
+            }
+            _ => Err(Error::new(
+                ErrorKind::DataInvalid,
+                "Map field must have struct type",
+            )),
+        },
+        DataType::Struct(fields) => visit_struct(fields, visitor),
+        other => Err(Error::new(
+            ErrorKind::DataInvalid,
+            format!("Cannot visit Arrow data type: {other}"),
+        )),
+    }
+}
+
+/// Visit struct type in post order.
+#[allow(dead_code)]
+fn visit_struct<V: ArrowSchemaVisitor>(fields: &Fields, visitor: &mut V) -> 
Result<V::T> {
+    let mut results = Vec::with_capacity(fields.len());
+    for field in fields {
+        visitor.before_field(field)?;
+        let result = visit_type(field.data_type(), visitor)?;
+        visitor.after_field(field)?;
+        results.push(result);
+    }
+
+    visitor.r#struct(fields, results)
+}
+
+/// Visit schema in post order.
+#[allow(dead_code)]
+pub fn visit_schema<V: ArrowSchemaVisitor>(schema: &ArrowSchema, visitor: &mut 
V) -> Result<V::U> {

Review Comment:
   Okay



##########
crates/iceberg/src/spec/arrow.rs:
##########


Review Comment:
   Okay



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