Skip to content

Apache DataFusion SQL Query Engine

refactor: use bitwise and boolean compute functions (#6568)

Igor Izvekov9cb6161dde76
diff --git a/datafusion/physical-expr/src/aggregate/bit_and_or_xor.rs b/datafusion/physical-expr/src/aggregate/bit_and_or_xor.rs
index eeb48035be..f43fe2464b 100644
--- a/datafusion/physical-expr/src/aggregate/bit_and_or_xor.rs
+++ b/datafusion/physical-expr/src/aggregate/bit_and_or_xor.rs
@@ -39,104 +39,8 @@ use crate::aggregate::row_accumulator::{
use crate::aggregate::utils::down_cast_any_ref;
use crate::expressions::format_state_name;
use arrow::array::Array;
-use arrow::array::PrimitiveArray;
-use arrow::datatypes::ArrowNativeTypeOp;
-use arrow::datatypes::ArrowNumericType;
+use arrow::compute::{bit_and, bit_or, bit_xor};
use datafusion_row::accessor::RowAccessor;
-use std::ops::BitAnd as BitAndImplementation;
-use std::ops::BitOr as BitOrImplementation;
-use std::ops::BitXor as BitXorImplementation;
-
-// TODO: remove this macro rules after implementation in arrow-rs
-// https://github.com/apache/arrow-rs/pull/4210
-macro_rules! bit_operation {
- ($NAME:ident, $OP:ident, $NATIVE:ident, $DEFAULT:expr, $DOC:expr) => {
- #[doc = $DOC]
- ///
- /// Returns `None` if the array is empty or only contains null values.
- fn $NAME<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
- where
- T: ArrowNumericType,
- T::Native: $NATIVE<Output = T::Native> + ArrowNativeTypeOp,
- {
- let default;
- if $DEFAULT == -1 {
- default = T::Native::ONE.neg_wrapping();
- } else {
- default = T::default_value();
- }
-
- let null_count = array.null_count();
-
- if null_count == array.len() {
- return None;
- }
-
- let data: &[T::Native] = array.values();
-
- match array.nulls() {
- None => {
- let result = data
- .iter()
- .fold(default, |accumulator, value| accumulator.$OP(*value));
-
- Some(result)
- }
- Some(nulls) => {
- let mut result = default;
- let data_chunks = data.chunks_exact(64);
- let remainder = data_chunks.remainder();
-
- let bit_chunks = nulls.inner().bit_chunks();
- data_chunks
- .zip(bit_chunks.iter())
- .for_each(|(chunk, mask)| {
- // index_mask has value 1 << i in the loop
- let mut index_mask = 1;
- chunk.iter().for_each(|value| {
- if (mask & index_mask) != 0 {
- result = result.$OP(*value);
- }
- index_mask <<= 1;
- });
- });
-
- let remainder_bits = bit_chunks.remainder_bits();
-
- remainder.iter().enumerate().for_each(|(i, value)| {
- if remainder_bits & (1 << i) != 0 {
- result = result.$OP(*value);
- }
- });
-
- Some(result)
- }
- }
- }
- };
-}
-
-bit_operation!(
- bit_and,
- bitand,
- BitAndImplementation,
- -1,
- "Returns the bitwise and of all non-null input values."
-);
-bit_operation!(
- bit_or,
- bitor,
- BitOrImplementation,
- 0,
- "Returns the bitwise or of all non-null input values."
-);
-bit_operation!(
- bit_xor,
- bitxor,
- BitXorImplementation,
- 0,
- "Returns the bitwise xor of all non-null input values."
-);
// returns the new value after bit_and/bit_or/bit_xor with the new values, taking nullability into account
macro_rules! typed_bit_and_or_xor_batch {
diff --git a/datafusion/physical-expr/src/aggregate/bool_and_or.rs b/datafusion/physical-expr/src/aggregate/bool_and_or.rs
index 15b6964f7a..bbab4dfce6 100644
--- a/datafusion/physical-expr/src/aggregate/bool_and_or.rs
+++ b/datafusion/physical-expr/src/aggregate/bool_and_or.rs
@@ -36,22 +36,9 @@ use crate::aggregate::row_accumulator::{
use crate::aggregate::utils::down_cast_any_ref;
use crate::expressions::format_state_name;
use arrow::array::Array;
+use arrow::compute::{bool_and, bool_or};
use datafusion_row::accessor::RowAccessor;
-fn bool_and(array: &BooleanArray) -> Option<bool> {
- if array.null_count() == array.len() {
- return None;
- }
- Some(array.false_count() == 0)
-}
-
-fn bool_or(array: &BooleanArray) -> Option<bool> {
- if array.null_count() == array.len() {
- return None;
- }
- Some(array.true_count() != 0)
-}
-
// returns the new value after bool_and/bool_or with the new values, taking nullability into account
macro_rules! typed_bool_and_or_batch {
($VALUES:expr, $ARRAYTYPE:ident, $SCALAR:ident, $OP:ident) => {{