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

struct UnionBuilder will create child buffers with capacity #2560

Merged
merged 5 commits into from Aug 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 21 additions & 6 deletions arrow/src/array/builder/union_builder.rs
Expand Up @@ -73,13 +73,17 @@ impl<T: ArrowNativeType> FieldDataValues for BufferBuilder<T> {

impl FieldData {
/// Creates a new `FieldData`.
fn new<T: ArrowPrimitiveType>(type_id: i8, data_type: DataType) -> Self {
fn new<T: ArrowPrimitiveType>(
type_id: i8,
data_type: DataType,
capacity: usize,
) -> Self {
Self {
type_id,
data_type,
slots: 0,
values_buffer: Box::new(BufferBuilder::<T::Native>::new(1)),
null_buffer_builder: NullBufferBuilder::new(1),
values_buffer: Box::new(BufferBuilder::<T::Native>::new(capacity)),
null_buffer_builder: NullBufferBuilder::new(capacity),
}
}

Expand Down Expand Up @@ -155,6 +159,7 @@ pub struct UnionBuilder {
type_id_builder: Int8BufferBuilder,
/// Builder to keep track of offsets (`None` for sparse unions)
value_offset_builder: Option<Int32BufferBuilder>,
initial_capacity: usize,
}

impl UnionBuilder {
Expand All @@ -175,6 +180,7 @@ impl UnionBuilder {
fields: HashMap::default(),
type_id_builder: Int8BufferBuilder::new(capacity),
value_offset_builder: Some(Int32BufferBuilder::new(capacity)),
initial_capacity: capacity,
}
}

Expand All @@ -185,6 +191,7 @@ impl UnionBuilder {
fields: HashMap::default(),
type_id_builder: Int8BufferBuilder::new(capacity),
value_offset_builder: None,
initial_capacity: capacity,
}
}

Expand Down Expand Up @@ -225,10 +232,18 @@ impl UnionBuilder {
data
}
None => match self.value_offset_builder {
Some(_) => FieldData::new::<T>(self.fields.len() as i8, T::DATA_TYPE),
Some(_) => FieldData::new::<T>(
self.fields.len() as i8,
T::DATA_TYPE,
self.initial_capacity,
),
// In the case of a sparse union, we should pass the maximum of the currently length and the capacity.
None => {
let mut fd =
FieldData::new::<T>(self.fields.len() as i8, T::DATA_TYPE);
let mut fd = FieldData::new::<T>(
self.fields.len() as i8,
T::DATA_TYPE,
self.len.max(self.initial_capacity),
);
for _ in 0..self.len {
fd.append_null();
}
Expand Down