You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2022/08/17 09:41:14 UTC

[arrow-rs] branch master updated: remove len field from StructBuilder (#2468)

This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 42e953107 remove len field from StructBuilder (#2468)
42e953107 is described below

commit 42e9531072366f9f88d524cb95e75ae9ef4bd1e4
Author: Palladium <ps...@gmail.com>
AuthorDate: Wed Aug 17 15:11:09 2022 +0530

    remove len field from StructBuilder (#2468)
---
 arrow/src/array/builder/struct_builder.rs | 29 +++++++----------------------
 1 file changed, 7 insertions(+), 22 deletions(-)

diff --git a/arrow/src/array/builder/struct_builder.rs b/arrow/src/array/builder/struct_builder.rs
index 59bd78398..01a792b5d 100644
--- a/arrow/src/array/builder/struct_builder.rs
+++ b/arrow/src/array/builder/struct_builder.rs
@@ -34,7 +34,6 @@ pub struct StructBuilder {
     fields: Vec<Field>,
     field_builders: Vec<Box<dyn ArrayBuilder>>,
     null_buffer_builder: NullBufferBuilder,
-    len: usize,
 }
 
 impl fmt::Debug for StructBuilder {
@@ -42,7 +41,7 @@ impl fmt::Debug for StructBuilder {
         f.debug_struct("StructBuilder")
             .field("fields", &self.fields)
             .field("bitmap_builder", &self.null_buffer_builder)
-            .field("len", &self.len)
+            .field("len", &self.len())
             .finish()
     }
 }
@@ -54,12 +53,12 @@ impl ArrayBuilder for StructBuilder {
     /// the caller's responsibility to maintain the consistency that all the child field
     /// builder should have the equal number of elements.
     fn len(&self) -> usize {
-        self.len
+        self.null_buffer_builder.len()
     }
 
     /// Returns whether the number of array slots is zero
     fn is_empty(&self) -> bool {
-        self.len == 0
+        self.len() == 0
     }
 
     /// Builds the array.
@@ -176,7 +175,6 @@ impl StructBuilder {
             fields,
             field_builders,
             null_buffer_builder: NullBufferBuilder::new(0),
-            len: 0,
         }
     }
 
@@ -205,7 +203,6 @@ impl StructBuilder {
     #[inline]
     pub fn append(&mut self, is_valid: bool) {
         self.null_buffer_builder.append(is_valid);
-        self.len += 1;
     }
 
     /// Appends a null element to the struct.
@@ -223,40 +220,28 @@ impl StructBuilder {
             let arr = f.finish();
             child_data.push(arr.into_data());
         }
-
+        let length = self.len();
         let null_bit_buffer = self.null_buffer_builder.finish();
 
         let builder = ArrayData::builder(DataType::Struct(self.fields.clone()))
-            .len(self.len)
+            .len(length)
             .child_data(child_data)
             .null_bit_buffer(null_bit_buffer);
 
-        self.len = 0;
-
         let array_data = unsafe { builder.build_unchecked() };
         StructArray::from(array_data)
     }
 
     /// Constructs and validates contents in the builder to ensure that
     /// - fields and field_builders are of equal length
-    /// - the number of items in individual field_builders are equal to self.len
-    /// - the number of items in individual field_builders are equal to self.null_buffer_builder.len()
+    /// - the number of items in individual field_builders are equal to self.len()
     fn validate_content(&self) {
         if self.fields.len() != self.field_builders.len() {
             panic!("Number of fields is not equal to the number of field_builders.");
         }
-        if !self.field_builders.iter().all(|x| x.len() == self.len) {
+        if !self.field_builders.iter().all(|x| x.len() == self.len()) {
             panic!("StructBuilder and field_builders are of unequal lengths.");
         }
-        if !self
-            .field_builders
-            .iter()
-            .all(|x| x.len() == self.null_buffer_builder.len())
-        {
-            panic!(
-                "StructBuilder null buffer length and field_builders length are unequal."
-            );
-        }
     }
 }