marvinlanhenke commented on code in PR #285:
URL: https://github.com/apache/iceberg-rust/pull/285#discussion_r1532076339


##########
crates/catalog/hms/src/schema.rs:
##########
@@ -0,0 +1,300 @@
+// 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 hive_metastore::FieldSchema;
+use iceberg::spec::{visit_schema, NestedFieldRef, PrimitiveType, Schema, 
SchemaVisitor};
+use iceberg::Result;
+
+type HiveSchema = Vec<FieldSchema>;
+
+#[derive(Debug, Default)]
+pub(crate) struct HiveSchemaBuilder {
+    schema: HiveSchema,
+    context: Vec<NestedFieldRef>,
+}
+
+impl HiveSchemaBuilder {
+    /// Creates a new `HiveSchemaBuilder` from iceberg `Schema`
+    pub fn from_iceberg(schema: &Schema) -> Result<HiveSchemaBuilder> {
+        let mut builder = Self::default();
+        visit_schema(schema, &mut builder)?;
+        Ok(builder)
+    }
+
+    /// Returns the newly converted `HiveSchema`
+    pub fn build(self) -> HiveSchema {
+        self.schema
+    }
+
+    /// Check if is in `StructType` while traversing schema
+    fn is_inside_struct(&self) -> bool {
+        !self.context.is_empty()
+    }
+}
+
+impl SchemaVisitor for HiveSchemaBuilder {
+    type T = String;
+
+    fn schema(
+        &mut self,
+        _schema: &iceberg::spec::Schema,
+        value: Self::T,
+    ) -> iceberg::Result<Self::T> {
+        Ok(value)
+    }
+
+    fn before_struct_field(
+        &mut self,
+        field: &iceberg::spec::NestedFieldRef,
+    ) -> iceberg::Result<()> {
+        self.context.push(field.clone());
+        Ok(())
+    }
+
+    fn r#struct(
+        &mut self,
+        r#_struct: &iceberg::spec::StructType,
+        results: Vec<Self::T>,
+    ) -> iceberg::Result<Self::T> {
+        Ok(format!("struct<{}>", results.join(", ")))
+    }
+
+    fn after_struct_field(
+        &mut self,
+        _field: &iceberg::spec::NestedFieldRef,
+    ) -> iceberg::Result<()> {
+        self.context.pop();
+        Ok(())
+    }
+
+    fn field(
+        &mut self,
+        field: &iceberg::spec::NestedFieldRef,
+        value: Self::T,
+    ) -> iceberg::Result<Self::T> {
+        if self.is_inside_struct() {
+            return Ok(format!("{}:{}", field.name, value));
+        }
+
+        self.schema.push(FieldSchema {
+            name: Some(field.name.clone().into()),
+            r#type: Some(value.clone().into()),
+            comment: field.doc.clone().map(|doc| doc.into()),
+        });
+
+        Ok(value)

Review Comment:
   yes, but doesn't this require me to keep track wether it's a child or a 
parent? 
   I should only push to `self.schema` when its not a children.
   
   I printed out the order for visiting a schema with a `StructType` and two 
`NestedField`s:
   ```text
   running 1 test
   inside primitive: String
   inside field: name string // shouldn't push here since it's a child
   inside primitive: Int
   inside field: age int // shouldn't push here since it's a child
   inside rstruct: ["name:string", "age:int"]
   inside field: person struct<name:string, age:int> // now, I can push since 
it's a parent
   inside rstruct: ["person:struct<name:string, age:int>"]
   inside schema: struct<person:struct<name:string, age:int>>
   ```
   
   I don't see a way right now, how to get rid of tracking the depth of the 
post-order traversal?



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