marvinlanhenke commented on code in PR #285: URL: https://github.com/apache/iceberg-rust/pull/285#discussion_r1531952526
########## 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 I'm still unsure if I can do it like this, the way the `HiveSchemaBuilder` is setup. Currently, I need to keep track if I'm inside a struct in order to avoid pushing each struct field and the struct itself to `self.schema`. However, I dont need to keep track of the complete context (field) but only the current depth. I changed this right now - in order to avoid cloning the `field` which is not neccessary. Perhaps there is a better solution, to avoid the context tracking at all - since I am not super familiar with the SchemaVisitor I might have missed something? -- 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