-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add has_true() and has_false() to BooleanArray #9511
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
Changes from all commits
eb18beb
7f748ac
e035516
2f00fc1
4e8b072
c88627a
aae3ac5
b6d5325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow_array::BooleanArray; | ||
| use criterion::*; | ||
| use std::hint; | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| for len in [64, 1024, 65536] { | ||
| // All true (no nulls) | ||
| let all_true = BooleanArray::from(vec![true; len]); | ||
| c.bench_function(&format!("true_count(all_true, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&all_true).true_count()); | ||
| }); | ||
| c.bench_function(&format!("has_true(all_true, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&all_true).has_true()); | ||
| }); | ||
| c.bench_function(&format!("has_false(all_true, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&all_true).has_false()); | ||
| }); | ||
|
|
||
| // All false (no nulls) | ||
| let all_false = BooleanArray::from(vec![false; len]); | ||
| c.bench_function(&format!("true_count(all_false, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&all_false).true_count()); | ||
| }); | ||
| c.bench_function(&format!("has_true(all_false, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&all_false).has_true()); | ||
| }); | ||
| c.bench_function(&format!("has_false(all_false, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&all_false).has_false()); | ||
| }); | ||
|
|
||
| // Mixed: first element differs (best-case short-circuit) | ||
| let mut mixed_early: Vec<bool> = vec![true; len]; | ||
| mixed_early[0] = false; | ||
| let mixed_early = BooleanArray::from(mixed_early); | ||
| c.bench_function(&format!("true_count(mixed_early, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&mixed_early).true_count()); | ||
| }); | ||
| c.bench_function(&format!("has_false(mixed_early, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&mixed_early).has_false()); | ||
| }); | ||
|
|
||
| // With nulls: all valid values true | ||
| let with_nulls: Vec<Option<bool>> = (0..len) | ||
| .map(|i| if i % 10 == 0 { None } else { Some(true) }) | ||
| .collect(); | ||
| let with_nulls = BooleanArray::from(with_nulls); | ||
| c.bench_function(&format!("true_count(nulls_all_true, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&with_nulls).true_count()); | ||
| }); | ||
| c.bench_function(&format!("has_true(nulls_all_true, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&with_nulls).has_true()); | ||
| }); | ||
| c.bench_function(&format!("has_false(nulls_all_true, {len})"), |b| { | ||
| b.iter(|| hint::black_box(&with_nulls).has_false()); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| criterion_group!(benches, criterion_benchmark); | ||
| criterion_main!(benches); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ use crate::array::print_long_array; | |
| use crate::builder::BooleanBuilder; | ||
| use crate::iterator::BooleanIter; | ||
| use crate::{Array, ArrayAccessor, ArrayRef, Scalar}; | ||
| use arrow_buffer::bit_chunk_iterator::UnalignedBitChunk; | ||
| use arrow_buffer::{BooleanBuffer, Buffer, MutableBuffer, NullBuffer, bit_util}; | ||
| use arrow_data::{ArrayData, ArrayDataBuilder}; | ||
| use arrow_schema::DataType; | ||
|
|
@@ -156,7 +157,18 @@ impl BooleanArray { | |
| &self.values | ||
| } | ||
|
|
||
| /// Returns the number of non null, true values within this array | ||
| /// Block size for chunked fold operations in [`Self::has_true`] and [`Self::has_false`]. | ||
| /// Folding this many u64 chunks at a time allows the compiler to autovectorize | ||
| /// the inner loop while still enabling short-circuit exits. | ||
| const CHUNK_FOLD_BLOCK_SIZE: usize = 64; | ||
|
|
||
| /// Returns an [`UnalignedBitChunk`] over this array's values. | ||
| fn unaligned_bit_chunks(&self) -> UnalignedBitChunk<'_> { | ||
| UnalignedBitChunk::new(self.values().values(), self.values().offset(), self.len()) | ||
| } | ||
|
|
||
| /// Returns the number of non null, true values within this array. | ||
| /// If you only need to check if there is at least one true value, consider using `has_true()` which can short-circuit and be more efficient. | ||
| pub fn true_count(&self) -> usize { | ||
| match self.nulls() { | ||
| Some(nulls) => { | ||
|
|
@@ -171,11 +183,80 @@ impl BooleanArray { | |
| } | ||
| } | ||
|
|
||
| /// Returns the number of non null, false values within this array | ||
| /// Returns the number of non null, false values within this array. | ||
| /// If you only need to check if there is at least one false value, consider using `has_false()` which can short-circuit and be more efficient. | ||
| pub fn false_count(&self) -> usize { | ||
| self.len() - self.null_count() - self.true_count() | ||
| } | ||
|
|
||
| /// Returns whether there is at least one non-null `true` value in this array. | ||
| /// | ||
| /// This is more efficient than `true_count() > 0` because it can short-circuit | ||
| /// as soon as a `true` value is found, without counting all set bits. | ||
| /// | ||
| /// Null values are not counted as `true`. Returns `false` for empty arrays. | ||
| pub fn has_true(&self) -> bool { | ||
| match self.nulls() { | ||
| Some(nulls) => { | ||
| let null_chunks = nulls.inner().bit_chunks().iter_padded(); | ||
| let value_chunks = self.values().bit_chunks().iter_padded(); | ||
| null_chunks.zip(value_chunks).any(|(n, v)| (n & v) != 0) | ||
| } | ||
| None => { | ||
| let bit_chunks = self.unaligned_bit_chunks(); | ||
| bit_chunks.prefix().unwrap_or(0) != 0 | ||
| || bit_chunks | ||
| .chunks() | ||
| .chunks(Self::CHUNK_FOLD_BLOCK_SIZE) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (So I think it could terminate even earlier with a smaller constant - as it only has a "termination" branch after the e.g. 8 elements instead of also having a loop end)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call! #9570 |
||
| .any(|block| block.iter().fold(0u64, |acc, &c| acc | c) != 0) | ||
| || bit_chunks.suffix().unwrap_or(0) != 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Returns whether there is at least one non-null `false` value in this array. | ||
| /// | ||
| /// This is more efficient than `false_count() > 0` because it can short-circuit | ||
| /// as soon as a `false` value is found, without counting all set bits. | ||
| /// | ||
| /// Null values are not counted as `false`. Returns `false` for empty arrays. | ||
| pub fn has_false(&self) -> bool { | ||
| match self.nulls() { | ||
| Some(nulls) => { | ||
| let null_chunks = nulls.inner().bit_chunks().iter_padded(); | ||
| let value_chunks = self.values().bit_chunks().iter_padded(); | ||
| null_chunks.zip(value_chunks).any(|(n, v)| (n & !v) != 0) | ||
| } | ||
| None => { | ||
| let bit_chunks = self.unaligned_bit_chunks(); | ||
| // UnalignedBitChunk zeros padding bits; fill them with 1s so | ||
| // they don't appear as false values. | ||
| let lead_mask = !((1u64 << bit_chunks.lead_padding()) - 1); | ||
| let trail_mask = if bit_chunks.trailing_padding() == 0 { | ||
| u64::MAX | ||
| } else { | ||
| (1u64 << (64 - bit_chunks.trailing_padding())) - 1 | ||
| }; | ||
| let (prefix_fill, suffix_fill) = match (bit_chunks.prefix(), bit_chunks.suffix()) { | ||
| (Some(_), Some(_)) => (!lead_mask, !trail_mask), | ||
| (Some(_), None) => (!lead_mask | !trail_mask, 0), | ||
| (None, Some(_)) => (0, !trail_mask), | ||
| (None, None) => (0, 0), | ||
| }; | ||
| bit_chunks | ||
| .prefix() | ||
| .is_some_and(|v| (v | prefix_fill) != u64::MAX) | ||
| || bit_chunks | ||
| .chunks() | ||
| .chunks(Self::CHUNK_FOLD_BLOCK_SIZE) | ||
| .any(|block| block.iter().fold(u64::MAX, |acc, &c| acc & c) != u64::MAX) | ||
| || bit_chunks | ||
| .suffix() | ||
| .is_some_and(|v| (v | suffix_fill) != u64::MAX) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Returns the boolean value at index `i`. | ||
| /// | ||
| /// Note: This method does not check for nulls and the value is arbitrary | ||
|
|
@@ -854,4 +935,128 @@ mod tests { | |
| assert!(sliced.is_valid(1)); | ||
| assert!(!sliced.value(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_all_true() { | ||
| let arr = BooleanArray::from(vec![true, true, true]); | ||
| assert!(arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_all_false() { | ||
| let arr = BooleanArray::from(vec![false, false, false]); | ||
| assert!(!arr.has_true()); | ||
| assert!(arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_mixed() { | ||
| let arr = BooleanArray::from(vec![true, false, true]); | ||
| assert!(arr.has_true()); | ||
| assert!(arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_empty() { | ||
| let arr = BooleanArray::from(Vec::<bool>::new()); | ||
| assert!(!arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_nulls_all_valid_true() { | ||
| let arr = BooleanArray::from(vec![Some(true), None, Some(true)]); | ||
| assert!(arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_nulls_all_valid_false() { | ||
| let arr = BooleanArray::from(vec![Some(false), None, Some(false)]); | ||
| assert!(!arr.has_true()); | ||
| assert!(arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_all_null() { | ||
| let arr = BooleanArray::new_null(5); | ||
| assert!(!arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_false_aligned_suffix_all_true() { | ||
| let arr = BooleanArray::from(vec![true; 129]); | ||
| assert!(arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_false_non_aligned_all_true() { | ||
| // 65 elements: exercises the remainder path in has_false | ||
| let arr = BooleanArray::from(vec![true; 65]); | ||
| assert!(arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_false_non_aligned_last_false() { | ||
| // 64 trues + 1 false: remainder path should find the false | ||
| let mut values = vec![true; 64]; | ||
| values.push(false); | ||
| let arr = BooleanArray::from(values); | ||
| assert!(arr.has_true()); | ||
| assert!(arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_false_exact_64_all_true() { | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Exactly 64 elements, no remainder | ||
| let arr = BooleanArray::from(vec![true; 64]); | ||
| assert!(arr.has_true()); | ||
| assert!(!arr.has_false()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_unaligned_slices() { | ||
| let cases = [ | ||
| (1, 129, true, false), | ||
| (3, 130, true, false), | ||
| (5, 65, true, false), | ||
| (7, 64, true, false), | ||
| ]; | ||
|
|
||
| let base = BooleanArray::from(vec![true; 300]); | ||
|
|
||
| for (offset, len, expected_has_true, expected_has_false) in cases { | ||
| let arr = base.slice(offset, len); | ||
| assert_eq!( | ||
| arr.has_true(), | ||
| expected_has_true, | ||
| "offset={offset} len={len}" | ||
| ); | ||
| assert_eq!( | ||
| arr.has_false(), | ||
| expected_has_false, | ||
| "offset={offset} len={len}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_has_true_has_false_exact_multiples_of_64() { | ||
| let cases = [ | ||
| (64, true, false), | ||
| (128, true, false), | ||
| (192, true, false), | ||
| (256, true, false), | ||
| ]; | ||
|
|
||
| for (len, expected_has_true, expected_has_false) in cases { | ||
| let arr = BooleanArray::from(vec![true; len]); | ||
| assert_eq!(arr.has_true(), expected_has_true, "len={len}"); | ||
| assert_eq!(arr.has_false(), expected_has_false, "len={len}"); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to add this API to
BooleanBufferas well (as a follow on PR)