diff --git a/arrow/src/array/builder/struct_builder.rs b/arrow/src/array/builder/struct_builder.rs index 59bd78398bf..01a792b5d19 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_builders: Vec>, 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." - ); - } } }