Xuanwo commented on code in PR #40:
URL: https://github.com/apache/iceberg-rust/pull/40#discussion_r1307566766


##########
crates/iceberg/src/lib.rs:
##########
@@ -27,4 +28,5 @@ pub use error::Error;
 pub use error::ErrorKind;
 pub use error::Result;
 
+mod avro;

Review Comment:
   Do we need to expose some API from this mod?



##########
crates/iceberg/src/lib.rs:
##########
@@ -18,6 +18,7 @@
 //! Native Rust implementation of Apache Iceberg
 
 #![deny(missing_docs)]
+#![allow(dead_code)]

Review Comment:
   I prefer to remove this.



##########
crates/iceberg/src/avro/schema.rs:
##########
@@ -0,0 +1,917 @@
+// 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.
+
+//! Conversion between iceberg and avro schema.
+use crate::spec::{
+    visit_schema, ListType, MapType, NestedField, NestedFieldRef, 
PrimitiveType, Schema,
+    SchemaVisitor, StructType, Type, DECIMAL_LENGTH,
+};
+use crate::{ensure_data_valid, Error, ErrorKind, Result};
+use apache_avro::schema::{
+    DecimalSchema, FixedSchema, Name, RecordField as AvroRecordField, 
RecordFieldOrder,
+    RecordSchema, UnionSchema,
+};
+use apache_avro::Schema as AvroSchema;
+use itertools::{Either, Itertools};
+use serde_json::{Number, Value};
+
+const FILED_ID_PROP: &str = "field-id";
+
+struct SchemaToAvroSchema {
+    schema: String,
+}
+
+impl SchemaVisitor for SchemaToAvroSchema {
+    type T = Either<AvroSchema, AvroRecordField>;
+
+    fn schema(
+        &mut self,
+        _schema: &Schema,
+        value: Either<AvroSchema, AvroRecordField>,
+    ) -> Result<Either<AvroSchema, AvroRecordField>> {
+        let mut avro_schema = value.unwrap_left();
+
+        if let AvroSchema::Record(record) = &mut avro_schema {
+            record.name = Name::from(self.schema.as_str());
+        } else {
+            return Err(Error::new(
+                ErrorKind::Unexpected,
+                "Schema result must be avro record!",
+            ));
+        }
+
+        Ok(Either::Left(avro_schema))
+    }
+
+    fn field(
+        &mut self,
+        field: &NestedFieldRef,
+        avro_schema: Either<AvroSchema, AvroRecordField>,
+    ) -> Result<Either<AvroSchema, AvroRecordField>> {
+        let mut field_schema = avro_schema.unwrap_left();
+        if let AvroSchema::Record(record) = &mut field_schema {
+            record.name = Name::from(format!("r{}", field.id).as_str());
+        }
+
+        if !field.required {
+            field_schema = avro_optional(field_schema)?;
+        }
+
+        let mut avro_record_field = AvroRecordField {
+            name: field.name.clone(),
+            schema: field_schema,
+            order: RecordFieldOrder::Ignore,
+            position: 0,
+            doc: field.doc.clone(),
+            aliases: None,
+            default: None,
+            custom_attributes: Default::default(),
+        };
+
+        if !field.required {
+            avro_record_field.default = Some(Value::Null);
+        }
+        avro_record_field.custom_attributes.insert(
+            FILED_ID_PROP.to_string(),
+            Value::Number(Number::from(field.id)),
+        );
+
+        Ok(Either::Right(avro_record_field))
+    }
+
+    fn r#struct(
+        &mut self,
+        _struct: &StructType,
+        results: Vec<Either<AvroSchema, AvroRecordField>>,
+    ) -> Result<Either<AvroSchema, AvroRecordField>> {
+        let avro_fields = results.into_iter().map(|r| 
r.unwrap_right()).collect();
+
+        Ok(Either::Left(AvroSchema::Record(RecordSchema {
+            // The name of this record schema should be determined later, by 
schema name or field
+            // name, here we use a temporary placeholder to do it.
+            name: Name::new("null")?,
+            aliases: None,
+            doc: None,
+            fields: avro_fields,
+            lookup: Default::default(),
+            attributes: Default::default(),
+        })))
+    }
+
+    fn list(
+        &mut self,
+        list: &ListType,
+        value: Either<AvroSchema, AvroRecordField>,
+    ) -> Result<Either<AvroSchema, AvroRecordField>> {
+        let mut field_schema = value.unwrap_left();
+
+        if let AvroSchema::Record(record) = &mut field_schema {
+            record.name = Name::from(format!("r{}", 
list.element_field.id).as_str());
+        }
+
+        if !list.element_field.required {
+            field_schema = avro_optional(field_schema)?;
+        }
+
+        // TODO: We need to add element id prop here, but rust's avro schema 
doesn't support property except record schema.
+        Ok(Either::Left(AvroSchema::Array(Box::new(field_schema))))
+    }
+
+    fn map(
+        &mut self,
+        map: &MapType,
+        key_value: Either<AvroSchema, AvroRecordField>,
+        value: Either<AvroSchema, AvroRecordField>,
+    ) -> Result<Either<AvroSchema, AvroRecordField>> {

Review Comment:
   Code here seems quite verbose.



##########
crates/iceberg/src/avro/schema.rs:
##########
@@ -0,0 +1,917 @@
+// 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.
+
+//! Conversion between iceberg and avro schema.
+use crate::spec::{
+    visit_schema, ListType, MapType, NestedField, NestedFieldRef, 
PrimitiveType, Schema,
+    SchemaVisitor, StructType, Type, DECIMAL_LENGTH,
+};
+use crate::{ensure_data_valid, Error, ErrorKind, Result};
+use apache_avro::schema::{
+    DecimalSchema, FixedSchema, Name, RecordField as AvroRecordField, 
RecordFieldOrder,
+    RecordSchema, UnionSchema,
+};
+use apache_avro::Schema as AvroSchema;
+use itertools::{Either, Itertools};
+use serde_json::{Number, Value};
+
+const FILED_ID_PROP: &str = "field-id";
+
+struct SchemaToAvroSchema {
+    schema: String,
+}
+
+impl SchemaVisitor for SchemaToAvroSchema {
+    type T = Either<AvroSchema, AvroRecordField>;

Review Comment:
   Can we use something like `AvroSchemaType` instead of `Either`?



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