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

Fix null_count computation in binary #3062

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions arrow/src/compute/kernels/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3044,4 +3044,61 @@ mod tests {
let c = add(&a, &b).unwrap();
assert_eq!(c, expected);
}

#[test]
fn test_resize_builder() {
let mut null_buffer_builder = BooleanBufferBuilder::new(16);
null_buffer_builder.append_slice(&[
false, false, false, false, false, false, false, false, false, false, false,
false, false, true, true, true,
]);
// `resize` resizes the buffer length to the ceil of byte numbers.
// So the underlying buffer is not changed.
null_buffer_builder.resize(13);
assert_eq!(null_buffer_builder.len(), 13);

let null_buffer = null_buffer_builder.finish();

// `count_set_bits` counts 1-bits in entire buffer. Because above `resize` doesn't
// actually truncate the buffer, `count_set_bits` still return 3.
assert_eq!(null_buffer.count_set_bits(), 3);
// `count_set_bits_offset` takes len in bits as parameter.
assert_eq!(null_buffer.count_set_bits_offset(0, 13), 0);

let mut data_buffer_builder = BufferBuilder::<i32>::new(13);
data_buffer_builder.append_slice(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
let data_buffer = data_buffer_builder.finish();

let arg1: Int32Array = ArrayDataBuilder::new(DataType::Int32)
.len(13)
.null_count(13)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the test skip setting the null count explicitly and instead let the ArrayDataBuilder calculate it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems the parameter is actually ignored and build always recalculates it. Only build_unchecked would use it.

.buffers(vec![data_buffer])
.null_bit_buffer(Some(null_buffer))
.build()
.unwrap()
.into();

assert_eq!(arg1.null_count(), 13);

let mut data_buffer_builder = BufferBuilder::<i32>::new(13);
data_buffer_builder.append_slice(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
let data_buffer = data_buffer_builder.finish();

let arg2: Int32Array = ArrayDataBuilder::new(DataType::Int32)
.len(13)
.null_count(0)
.buffers(vec![data_buffer])
.null_bit_buffer(None)
.build()
.unwrap()
.into();

assert_eq!(arg2.null_count(), 0);

let result_dyn = add_dyn(&arg1, &arg2).unwrap();
let result = result_dyn.as_any().downcast_ref::<Int32Array>().unwrap();

assert_eq!(result.len(), 13);
assert_eq!(result.null_count(), 13);
}
}
4 changes: 2 additions & 2 deletions arrow/src/compute/kernels/arity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ where
let null_buffer = combine_option_bitmap(&[a.data(), b.data()], len).unwrap();
let null_count = null_buffer
.as_ref()
.map(|x| len - x.count_set_bits())
.map(|x| len - x.count_set_bits_offset(0, len))
.unwrap_or_default();

let values = a.values().iter().zip(b.values()).map(|(l, r)| op(*l, *r));
Expand Down Expand Up @@ -241,7 +241,7 @@ where

let null_count = null_buffer
.as_ref()
.map(|x| len - x.count_set_bits())
.map(|x| len - x.count_set_bits_offset(0, len))
.unwrap_or_default();

let mut buffer = BufferBuilder::<O::Native>::new(len);
Expand Down