Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove len field from StructBuilder #2468

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions arrow/src/array/builder/struct_builder.rs
Original file line number Diff line number Diff line change
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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could help LLVM out and lift the len from the closure, it should be smart enough to do this automatically, but it can't hurt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information, I wasn't aware of this.

I shall make this change tomorrow.

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