Skip to content

Commit

Permalink
Refactor BooleanBuilder Constructors (#2515)
Browse files Browse the repository at this point in the history
* Refactor boolean builder

* Fix errors

* Fix typo in documentation

* impl default trait for BooleanBuilder
  • Loading branch information
psvri committed Aug 19, 2022
1 parent e80f647 commit a90fc64
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion arrow/benches/builder.rs
Expand Up @@ -80,7 +80,7 @@ fn bench_bool(c: &mut Criterion) {
));
group.bench_function("bench_bool", |b| {
b.iter(|| {
let mut builder = BooleanBuilder::new(64);
let mut builder = BooleanBuilder::with_capacity(64);
for _ in 0..NUM_BATCHES {
builder.append_slice(&data[..]);
}
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/array/array_boolean.rs
Expand Up @@ -95,7 +95,7 @@ impl BooleanArray {

// Returns a new boolean array builder
pub fn builder(capacity: usize) -> BooleanBuilder {
BooleanBuilder::new(capacity)
BooleanBuilder::with_capacity(capacity)
}

/// Returns a `Buffer` holding all the values of this array.
Expand Down
17 changes: 14 additions & 3 deletions arrow/src/array/builder/boolean_builder.rs
Expand Up @@ -39,7 +39,7 @@ use super::NullBufferBuilder;
/// ```
/// use arrow::array::{Array, BooleanArray, BooleanBuilder};
///
/// let mut b = BooleanBuilder::new(4);
/// let mut b = BooleanBuilder::new();
/// b.append_value(true);
/// b.append_null();
/// b.append_value(false);
Expand All @@ -66,9 +66,20 @@ pub struct BooleanBuilder {
null_buffer_builder: NullBufferBuilder,
}

impl Default for BooleanBuilder {
fn default() -> Self {
Self::new()
}
}

impl BooleanBuilder {
/// Creates a new primitive array builder
pub fn new(capacity: usize) -> Self {
/// Creates a new boolean builder
pub fn new() -> Self {
Self::with_capacity(1024)
}

/// Creates a new boolean builder with space for `capacity` elements without re-allocating
pub fn with_capacity(capacity: usize) -> Self {
Self {
values_builder: BooleanBufferBuilder::new(capacity),
null_buffer_builder: NullBufferBuilder::new(capacity),
Expand Down
6 changes: 3 additions & 3 deletions arrow/src/array/builder/struct_builder.rs
Expand Up @@ -96,7 +96,7 @@ impl ArrayBuilder for StructBuilder {
pub fn make_builder(datatype: &DataType, capacity: usize) -> Box<dyn ArrayBuilder> {
match datatype {
DataType::Null => unimplemented!(),
DataType::Boolean => Box::new(BooleanBuilder::new(capacity)),
DataType::Boolean => Box::new(BooleanBuilder::with_capacity(capacity)),
DataType::Int8 => Box::new(Int8Builder::new(capacity)),
DataType::Int16 => Box::new(Int16Builder::new(capacity)),
DataType::Int32 => Box::new(Int32Builder::new(capacity)),
Expand Down Expand Up @@ -321,7 +321,7 @@ mod tests {
#[test]
fn test_struct_array_builder_finish() {
let int_builder = Int32Builder::new(10);
let bool_builder = BooleanBuilder::new(10);
let bool_builder = BooleanBuilder::new();

let mut fields = Vec::new();
let mut field_builders = Vec::new();
Expand Down Expand Up @@ -426,7 +426,7 @@ mod tests {
#[should_panic(expected = "StructBuilder and field_builders are of unequal lengths.")]
fn test_struct_array_builder_unequal_field_builders_lengths() {
let mut int_builder = Int32Builder::new(10);
let mut bool_builder = BooleanBuilder::new(10);
let mut bool_builder = BooleanBuilder::new();

int_builder.append_value(1);
int_builder.append_value(2);
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/array/data.rs
Expand Up @@ -2677,7 +2677,7 @@ mod tests {
],
vec![
Box::new(Int32Builder::new(5)),
Box::new(BooleanBuilder::new(5)),
Box::new(BooleanBuilder::with_capacity(5)),
],
);

Expand Down
2 changes: 1 addition & 1 deletion arrow/src/compute/kernels/cast.rs
Expand Up @@ -2072,7 +2072,7 @@ fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray>
where
T: ArrowPrimitiveType + ArrowNumericType,
{
let mut b = BooleanBuilder::new(from.len());
let mut b = BooleanBuilder::with_capacity(from.len());

for i in 0..from.len() {
if from.is_null(i) {
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/compute/kernels/filter.rs
Expand Up @@ -1039,7 +1039,7 @@ mod tests {
#[test]
fn test_filter_string_array_with_negated_boolean_array() {
let a = StringArray::from(vec!["hello", " ", "world", "!"]);
let mut bb = BooleanBuilder::new(2);
let mut bb = BooleanBuilder::with_capacity(2);
bb.append_value(false);
bb.append_value(true);
bb.append_value(false);
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/compute/kernels/take.rs
Expand Up @@ -1083,7 +1083,7 @@ mod tests {
Field::new("b", DataType::Int32, true),
],
vec![
Box::new(BooleanBuilder::new(values.len())),
Box::new(BooleanBuilder::with_capacity(values.len())),
Box::new(Int32Builder::new(values.len())),
],
);
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/json/reader.rs
Expand Up @@ -950,7 +950,7 @@ impl Decoder {
}

fn build_boolean_array(&self, rows: &[Value], col_name: &str) -> Result<ArrayRef> {
let mut builder = BooleanBuilder::new(rows.len());
let mut builder = BooleanBuilder::with_capacity(rows.len());
for row in rows {
if let Some(value) = row.get(&col_name) {
if let Some(boolean) = value.as_bool() {
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/util/integration_util.rs
Expand Up @@ -284,7 +284,7 @@ pub fn array_from_json(
match field.data_type() {
DataType::Null => Ok(Arc::new(NullArray::new(json_col.count))),
DataType::Boolean => {
let mut b = BooleanBuilder::new(json_col.count);
let mut b = BooleanBuilder::with_capacity(json_col.count);
for (is_valid, value) in json_col
.validity
.as_ref()
Expand Down

0 comments on commit a90fc64

Please sign in to comment.