Skip to content

Commit

Permalink
remove len field from StructBuilder (#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
psvri committed Aug 17, 2022
1 parent 63ab69e commit 42e9531
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions arrow/src/array/builder/struct_builder.rs
Expand Up @@ -34,15 +34,14 @@ pub struct StructBuilder {
fields: Vec<Field>,
field_builders: Vec<Box<dyn ArrayBuilder>>,
null_buffer_builder: NullBufferBuilder,
len: usize,
}

impl fmt::Debug for StructBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StructBuilder")
.field("fields", &self.fields)
.field("bitmap_builder", &self.null_buffer_builder)
.field("len", &self.len)
.field("len", &self.len())
.finish()
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -176,7 +175,6 @@ impl StructBuilder {
fields,
field_builders,
null_buffer_builder: NullBufferBuilder::new(0),
len: 0,
}
}

Expand Down Expand Up @@ -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.
Expand All @@ -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."
);
}
}
}

Expand Down

0 comments on commit 42e9531

Please sign in to comment.