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/06/22 14:38:30 UTC

[GitHub] [arrow-rs] alamb commented on a diff in pull request #4430: feat: support `NullBuilder`

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


##########
arrow-array/src/builder/null_builder.rs:
##########
@@ -0,0 +1,184 @@
+// 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::builder::ArrayBuilder;
+use crate::{ArrayRef, NullArray};
+use arrow_data::ArrayData;
+use arrow_schema::DataType;
+use std::any::Any;
+use std::sync::Arc;
+
+/// Builder for [`NullArray`]
+///
+/// # Example
+///
+/// Create a `NullArray` from a `NullBuilder`
+///
+/// ```
+///
+/// # use arrow_array::{Array, NullArray, builder::NullBuilder};
+///
+/// let mut b = NullBuilder::new();
+/// b.append_empty_value();
+/// b.append_null();
+/// b.append_nulls(3);
+/// b.append_empty_values(3);
+/// let arr = b.finish();
+///
+/// assert_eq!(8, arr.len());
+/// assert_eq!(8, arr.null_count());
+/// ```
+#[derive(Debug)]
+pub struct NullBuilder {
+    len: usize,
+}
+
+impl Default for NullBuilder {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl NullBuilder {
+    /// Creates a new null builder
+    pub fn new() -> Self {
+        Self { len: 0 }
+    }
+
+    /// Creates a new null builder with space for `capacity` elements without re-allocating
+    pub fn with_capacity(capacity: usize) -> Self {
+        Self { len: capacity }
+    }
+
+    /// Returns the capacity of this builder measured in slots of type `T`
+    pub fn capacity(&self) -> usize {
+        self.len
+    }
+
+    /// Appends a null slot into the builder
+    #[inline]
+    pub fn append_null(&mut self) {
+        self.len += 1;
+    }
+
+    /// Appends `n` `null`s into the builder.
+    #[inline]
+    pub fn append_nulls(&mut self, n: usize) {
+        self.len += n;
+    }
+
+    /// Appends a null slot into the builder
+    #[inline]
+    pub fn append_empty_value(&mut self) {
+        self.append_null();
+    }
+
+    /// Appends `n` `null`s into the builder.
+    #[inline]
+    pub fn append_empty_values(&mut self, n: usize) {
+        self.append_nulls(n);
+    }
+
+    /// Builds the [NullArray] and reset this builder.
+    pub fn finish(&mut self) -> NullArray {
+        let len = self.len();
+        let builder = ArrayData::builder(DataType::Null).len(len);

Review Comment:
   Perhaps we could use `ArrayDataBuilder::new_null(DataType::Null, len)` here?
   
   https://docs.rs/arrow-data/42.0.0/src/arrow_data/data/mod.rs.html#577-584



##########
arrow-array/src/builder/null_builder.rs:
##########
@@ -0,0 +1,184 @@
+// 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::builder::ArrayBuilder;
+use crate::{ArrayRef, NullArray};
+use arrow_data::ArrayData;
+use arrow_schema::DataType;
+use std::any::Any;
+use std::sync::Arc;
+
+/// Builder for [`NullArray`]
+///
+/// # Example
+///
+/// Create a `NullArray` from a `NullBuilder`
+///
+/// ```
+///
+/// # use arrow_array::{Array, NullArray, builder::NullBuilder};
+///
+/// let mut b = NullBuilder::new();
+/// b.append_empty_value();
+/// b.append_null();
+/// b.append_nulls(3);
+/// b.append_empty_values(3);
+/// let arr = b.finish();
+///
+/// assert_eq!(8, arr.len());
+/// assert_eq!(8, arr.null_count());
+/// ```
+#[derive(Debug)]
+pub struct NullBuilder {
+    len: usize,
+}
+
+impl Default for NullBuilder {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl NullBuilder {

Review Comment:
   👍  these look consistent with https://arrow.apache.org/docs/cpp/api/builder.html#classarrow_1_1_null_builder to me 



##########
arrow-array/src/builder/null_builder.rs:
##########
@@ -0,0 +1,184 @@
+// 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::builder::ArrayBuilder;
+use crate::{ArrayRef, NullArray};
+use arrow_data::ArrayData;
+use arrow_schema::DataType;
+use std::any::Any;
+use std::sync::Arc;
+
+/// Builder for [`NullArray`]
+///
+/// # Example
+///
+/// Create a `NullArray` from a `NullBuilder`
+///
+/// ```
+///
+/// # use arrow_array::{Array, NullArray, builder::NullBuilder};
+///
+/// let mut b = NullBuilder::new();
+/// b.append_empty_value();
+/// b.append_null();
+/// b.append_nulls(3);
+/// b.append_empty_values(3);
+/// let arr = b.finish();
+///
+/// assert_eq!(8, arr.len());
+/// assert_eq!(8, arr.null_count());
+/// ```
+#[derive(Debug)]
+pub struct NullBuilder {
+    len: usize,
+}
+
+impl Default for NullBuilder {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl NullBuilder {
+    /// Creates a new null builder
+    pub fn new() -> Self {
+        Self { len: 0 }
+    }
+
+    /// Creates a new null builder with space for `capacity` elements without re-allocating
+    pub fn with_capacity(capacity: usize) -> Self {
+        Self { len: capacity }
+    }
+
+    /// Returns the capacity of this builder measured in slots of type `T`
+    pub fn capacity(&self) -> usize {
+        self.len
+    }
+
+    /// Appends a null slot into the builder
+    #[inline]
+    pub fn append_null(&mut self) {
+        self.len += 1;
+    }
+
+    /// Appends `n` `null`s into the builder.
+    #[inline]
+    pub fn append_nulls(&mut self, n: usize) {
+        self.len += n;
+    }
+
+    /// Appends a null slot into the builder
+    #[inline]
+    pub fn append_empty_value(&mut self) {
+        self.append_null();
+    }
+
+    /// Appends `n` `null`s into the builder.
+    #[inline]
+    pub fn append_empty_values(&mut self, n: usize) {
+        self.append_nulls(n);
+    }
+
+    /// Builds the [NullArray] and reset this builder.
+    pub fn finish(&mut self) -> NullArray {
+        let len = self.len();
+        let builder = ArrayData::builder(DataType::Null).len(len);
+
+        let array_data = unsafe { builder.build_unchecked() };
+        NullArray::from(array_data)
+    }
+
+    /// Builds the [NullArray] without resetting the builder.
+    pub fn finish_cloned(&self) -> NullArray {

Review Comment:
   Same comments as self.finish



##########
arrow-array/src/builder/null_builder.rs:
##########
@@ -0,0 +1,184 @@
+// 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::builder::ArrayBuilder;
+use crate::{ArrayRef, NullArray};
+use arrow_data::ArrayData;
+use arrow_schema::DataType;
+use std::any::Any;
+use std::sync::Arc;
+
+/// Builder for [`NullArray`]
+///
+/// # Example
+///
+/// Create a `NullArray` from a `NullBuilder`
+///
+/// ```
+///
+/// # use arrow_array::{Array, NullArray, builder::NullBuilder};
+///
+/// let mut b = NullBuilder::new();
+/// b.append_empty_value();
+/// b.append_null();
+/// b.append_nulls(3);
+/// b.append_empty_values(3);
+/// let arr = b.finish();
+///
+/// assert_eq!(8, arr.len());
+/// assert_eq!(8, arr.null_count());
+/// ```
+#[derive(Debug)]
+pub struct NullBuilder {
+    len: usize,
+}
+
+impl Default for NullBuilder {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl NullBuilder {
+    /// Creates a new null builder
+    pub fn new() -> Self {
+        Self { len: 0 }
+    }
+
+    /// Creates a new null builder with space for `capacity` elements without re-allocating
+    pub fn with_capacity(capacity: usize) -> Self {
+        Self { len: capacity }
+    }
+
+    /// Returns the capacity of this builder measured in slots of type `T`
+    pub fn capacity(&self) -> usize {
+        self.len
+    }
+
+    /// Appends a null slot into the builder
+    #[inline]
+    pub fn append_null(&mut self) {
+        self.len += 1;
+    }
+
+    /// Appends `n` `null`s into the builder.
+    #[inline]
+    pub fn append_nulls(&mut self, n: usize) {
+        self.len += n;
+    }
+
+    /// Appends a null slot into the builder
+    #[inline]
+    pub fn append_empty_value(&mut self) {
+        self.append_null();
+    }
+
+    /// Appends `n` `null`s into the builder.
+    #[inline]
+    pub fn append_empty_values(&mut self, n: usize) {
+        self.append_nulls(n);
+    }
+
+    /// Builds the [NullArray] and reset this builder.
+    pub fn finish(&mut self) -> NullArray {
+        let len = self.len();
+        let builder = ArrayData::builder(DataType::Null).len(len);
+
+        let array_data = unsafe { builder.build_unchecked() };

Review Comment:
   Give there is so little to validate, I wonder if calling `build()` would be acceptable (and would avoid an `unsafe`)? I would feel better about `unsafe` if `ArrayDataBuilder::new_null(DataType::Null, len)` was used



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