liurenjie1024 commented on code in PR #317: URL: https://github.com/apache/iceberg-rust/pull/317#discussion_r1553151792
########## crates/iceberg/src/expr/accessor.rs: ########## @@ -0,0 +1,114 @@ +// 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::spec::{Literal, Struct, Type}; +use serde_derive::{Deserialize, Serialize}; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +enum InnerOrType { + Inner(Arc<StructAccessor>), + Type(Type), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +pub struct StructAccessor { + position: i32, + inner_or_type: InnerOrType, Review Comment: This seems inefficient to me, each time we call `r#type`, it needs to recursively call inners for nested field. How about just following: ```rust r#type: Type, inner: Option<StructAccessorRef> ``` ########## crates/iceberg/src/spec/schema.rs: ########## @@ -137,9 +142,55 @@ impl SchemaBuilder { name_to_id, lowercase_name_to_id, id_to_name, + + field_id_to_accessor, }) } + fn build_accessors(&self) -> HashMap<i32, Arc<StructAccessor>> { Review Comment: How about using SchemaVisitor to build this? ########## crates/iceberg/src/expr/accessor.rs: ########## @@ -0,0 +1,114 @@ +// 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::spec::{Literal, Struct, Type}; +use serde_derive::{Deserialize, Serialize}; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +enum InnerOrType { + Inner(Arc<StructAccessor>), + Type(Type), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +pub struct StructAccessor { Review Comment: How about just name it `Accessor`? This is in fact field accessor. ########## crates/iceberg/src/expr/accessor.rs: ########## @@ -0,0 +1,114 @@ +// 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::spec::{Literal, Struct, Type}; +use serde_derive::{Deserialize, Serialize}; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +enum InnerOrType { + Inner(Arc<StructAccessor>), Review Comment: ```suggestion Inner(Box<StructAccessor>), ``` I think the inner accessor is not shared, so it would be more efficient to use `Box` ########## crates/iceberg/src/spec/schema.rs: ########## @@ -137,9 +142,55 @@ impl SchemaBuilder { name_to_id, lowercase_name_to_id, id_to_name, + + field_id_to_accessor, }) } + fn build_accessors(&self) -> HashMap<i32, Arc<StructAccessor>> { Review Comment: Also it would be better to add some uts to show the result. ########## crates/iceberg/src/spec/values.rs: ########## @@ -1141,6 +1141,11 @@ impl Struct { }, ) } + + /// Gets a ref to the field at `position` within the `Struct` + pub fn get(&self, position: i32) -> &Literal { Review Comment: How about implementing `Index` trait? ########## crates/iceberg/src/expr/accessor.rs: ########## @@ -0,0 +1,114 @@ +// 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::spec::{Literal, Struct, Type}; +use serde_derive::{Deserialize, Serialize}; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +enum InnerOrType { + Inner(Arc<StructAccessor>), + Type(Type), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +pub struct StructAccessor { + position: i32, + inner_or_type: InnerOrType, +} + +pub(crate) type StructAccessorRef = Arc<StructAccessor>; + +impl StructAccessor { + pub(crate) fn new(position: i32, r#type: Type) -> Self { + StructAccessor { + position, + inner_or_type: InnerOrType::Type(r#type), + } + } + + pub(crate) fn wrap(position: i32, inner: StructAccessorRef) -> Self { + StructAccessor { + position, + inner_or_type: InnerOrType::Inner(inner), + } + } + + pub fn position(&self) -> i32 { + self.position + } + + fn r#type(&self) -> &Type { + match &self.inner_or_type { + InnerOrType::Inner(inner) => inner.r#type(), + InnerOrType::Type(r#type) => r#type, + } + } + + fn get<'a>(&'a self, container: &'a Struct) -> &Literal { Review Comment: Should we return `Datum` here? This better follows python/java implementation. ########## crates/iceberg/src/expr/accessor.rs: ########## @@ -0,0 +1,114 @@ +// 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::spec::{Literal, Struct, Type}; +use serde_derive::{Deserialize, Serialize}; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +enum InnerOrType { + Inner(Arc<StructAccessor>), + Type(Type), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +pub struct StructAccessor { + position: i32, + inner_or_type: InnerOrType, +} + +pub(crate) type StructAccessorRef = Arc<StructAccessor>; + +impl StructAccessor { + pub(crate) fn new(position: i32, r#type: Type) -> Self { + StructAccessor { + position, + inner_or_type: InnerOrType::Type(r#type), + } + } + + pub(crate) fn wrap(position: i32, inner: StructAccessorRef) -> Self { + StructAccessor { + position, + inner_or_type: InnerOrType::Inner(inner), + } + } + + pub fn position(&self) -> i32 { + self.position + } + + fn r#type(&self) -> &Type { + match &self.inner_or_type { + InnerOrType::Inner(inner) => inner.r#type(), + InnerOrType::Type(r#type) => r#type, + } + } + + fn get<'a>(&'a self, container: &'a Struct) -> &Literal { Review Comment: I think we should return error here, the input may come user. ########## crates/iceberg/src/expr/accessor.rs: ########## @@ -0,0 +1,114 @@ +// 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::spec::{Literal, Struct, Type}; +use serde_derive::{Deserialize, Serialize}; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +enum InnerOrType { + Inner(Arc<StructAccessor>), + Type(Type), +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +pub struct StructAccessor { + position: i32, Review Comment: How about changing this to `usize`? It's somehow like the position index of an array. -- 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]
