You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/03/28 10:17:09 UTC

[GitHub] [arrow-rs] alamb commented on a diff in pull request #3965: Add Fields abstraction (#3955)

alamb commented on code in PR #3965:
URL: https://github.com/apache/arrow-rs/pull/3965#discussion_r1150359034


##########
arrow-schema/src/fields.rs:
##########
@@ -0,0 +1,139 @@
+// 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::{Field, FieldRef};
+use std::ops::Deref;
+use std::sync::Arc;
+
+/// A cheaply cloneable, owned slice of [`FieldRef`]
+///
+/// Similar to `Arc<Vec<FieldPtr>>` or `Arc<[FieldPtr]>`
+///
+/// Can be constructed in a number of ways
+///
+/// ```
+/// # use std::sync::Arc;
+/// # use arrow_schema::{DataType, Field, Fields};
+/// // Can be constructed from Vec<Field>
+/// Fields::from(vec![Field::new("a", DataType::Boolean, false)]);
+/// // Can be constructed from Vec<FieldRef>
+/// Fields::from(vec![Arc::new(Field::new("a", DataType::Boolean, false))]);
+/// // Can be constructed from an iterator of Field
+/// std::iter::once(Field::new("a", DataType::Boolean, false)).collect::<Fields>();
+/// // Can be constructed from an iterator of FieldRef
+/// std::iter::once(Arc::new(Field::new("a", DataType::Boolean, false))).collect::<Fields>();
+/// ```
+///
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Fields(Arc<[FieldRef]>);
+
+impl std::fmt::Debug for Fields {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        self.0.as_ref().fmt(f)
+    }
+}
+
+impl Fields {
+    /// Returns a new empty [`Fields`]
+    pub fn new() -> Self {
+        Self(Arc::new([]))
+    }
+
+    /// Return size of this instance in bytes.
+    pub fn size(&self) -> usize {
+        self.iter().map(|field| field.size()).sum()
+    }
+}

Review Comment:
   While constructing / Modifying lists of fields, I think it would be great if we could also add functions like
   
   
   ```rust
   /// Maybe something more generic to allow adding a Field and FieldREf
   pub fn push(mut &self, field: Field...) {
   ...
   }
   
   ```



##########
arrow-schema/src/schema.rs:
##########
@@ -18,12 +18,53 @@
 use std::collections::HashMap;
 use std::fmt;
 use std::hash::Hash;
+use std::sync::Arc;
 
 use crate::error::ArrowError;
 use crate::field::Field;
+use crate::{FieldRef, Fields};
+
+/// A builder to facilitate building a [`Schema`] from iteratively from [`FieldRef`]

Review Comment:
   This I think is one of the most expensive operations in DataFusion planning now: https://github.com/apache/arrow-datafusion/issues/5157#issuecomment-1414339134
   
   So 👍 



##########
arrow-schema/src/fields.rs:
##########
@@ -0,0 +1,139 @@
+// 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::{Field, FieldRef};
+use std::ops::Deref;
+use std::sync::Arc;
+
+/// A cheaply cloneable, owned slice of [`FieldRef`]
+///
+/// Similar to `Arc<Vec<FieldPtr>>` or `Arc<[FieldPtr]>`
+///
+/// Can be constructed in a number of ways
+///
+/// ```
+/// # use std::sync::Arc;
+/// # use arrow_schema::{DataType, Field, Fields};
+/// // Can be constructed from Vec<Field>
+/// Fields::from(vec![Field::new("a", DataType::Boolean, false)]);
+/// // Can be constructed from Vec<FieldRef>
+/// Fields::from(vec![Arc::new(Field::new("a", DataType::Boolean, false))]);
+/// // Can be constructed from an iterator of Field
+/// std::iter::once(Field::new("a", DataType::Boolean, false)).collect::<Fields>();
+/// // Can be constructed from an iterator of FieldRef
+/// std::iter::once(Arc::new(Field::new("a", DataType::Boolean, false))).collect::<Fields>();
+/// ```
+///
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Fields(Arc<[FieldRef]>);

Review Comment:
   I agree this is a better formulation than a typedef and will allow for more flexibility



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org