Fokko commented on code in PR #19:
URL: https://github.com/apache/iceberg-rust/pull/19#discussion_r1282696514
##########
crates/iceberg/src/spec/datatypes.rs:
##########
@@ -252,15 +253,34 @@ impl<'de> Deserialize<'de> for StructType {
impl StructType {
/// Creates a struct type with the given fields.
pub fn new(fields: Vec<StructField>) -> Self {
- let id_lookup = HashMap::from_iter(fields.iter().enumerate().map(|(i,
x)| (x.id, i)));
- Self { fields, id_lookup }
+ Self {
+ fields,
+ id_lookup: OnceCell::default(),
+ }
}
- /// Get structfield with certain id
+ /// Get struct field with certain id
pub fn field_by_id(&self, id: i32) -> Option<&StructField> {
- self.fields.get(*self.id_lookup.get(&id)?)
+ self.field_id_to_index(id).map(|idx| &self.fields[idx])
+ }
+
+ fn field_id_to_index(&self, field_id: i32) -> Option<usize> {
+ self.id_lookup
+ .get_or_init(|| {
Review Comment:
Nice, this is very elegant
##########
crates/iceberg/src/spec/schema.rs:
##########
@@ -0,0 +1,111 @@
+// 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.
+
+//! This module defines schema in iceberg.
+
+use crate::spec::datatypes::{StructField, StructType};
+
+const DEFAULT_SCHEMA_ID: i32 = 0;
+
+/// Defines schema in iceberg.
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub struct Schema {
+ r#struct: StructType,
+ schema_id: i32,
+ highest_field_id: i32,
+}
Review Comment:
I think the `identifier-field-ids` are missing:
https://iceberg.apache.org/spec/#identifier-field-ids
##########
crates/iceberg/src/spec/schema.rs:
##########
@@ -0,0 +1,111 @@
+// 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.
+
+//! This module defines schema in iceberg.
+
+use crate::spec::datatypes::{StructField, StructType};
+
+const DEFAULT_SCHEMA_ID: i32 = 0;
+
+/// Defines schema in iceberg.
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub struct Schema {
+ r#struct: StructType,
+ schema_id: i32,
+ highest_field_id: i32,
+}
+
+/// Schema builder.
+pub struct SchemaBuilder {
+ schema_id: i32,
+ fields: Vec<StructField>,
+}
+
+impl SchemaBuilder {
+ /// Add fields to schem builder
+ pub fn with_fields(mut self, fields: impl IntoIterator<Item =
StructField>) -> Self {
+ self.fields.extend(fields.into_iter());
+ self
+ }
+
+ /// Set schema id.
+ pub fn with_schema_id(mut self, schema_id: i32) -> Self {
Review Comment:
Schema ID can also be optional, then we would just set it to `0`.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]