Skip to content

Apache DataFusion SQL Query Engine

fix: reject groups accumulator for NULL-typed bitwise aggregates (#25687)

jackylee884465c20147
diff --git a/datafusion/functions-aggregate/src/bit_and_or_xor.rs b/datafusion/functions-aggregate/src/bit_and_or_xor.rs
index 477ad6f1ef..1928f5c51c 100644
--- a/datafusion/functions-aggregate/src/bit_and_or_xor.rs
+++ b/datafusion/functions-aggregate/src/bit_and_or_xor.rs
@@ -292,6 +292,11 @@ impl AggregateUDFImpl for BitwiseOperation {
}
fn groups_accumulator_supported(&self, args: AccumulatorArgs) -> bool {
+ if args.return_field.data_type().is_null() {
+ // A Null input has no integer groups accumulator (see
+ // create_groups_accumulator), so fall back to the scalar accumulator.
+ return false;
+ }
// DISTINCT only changes the result of XOR; AND and OR are idempotent
!(args.is_distinct && self.operation == BitwiseOperationType::Xor)
}
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index b3f8a5515d..b9dacfa925 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -5161,6 +5161,25 @@ statement ok
drop table bit_distinct;
+# bitwise aggregates over NULL inputs, with and without GROUP BY
+statement ok
+create table bit_null (tag varchar) as values ('A'), ('A'), ('B');
+
+query ???
+SELECT bit_and(NULL), bit_or(NULL), bit_xor(NULL) FROM bit_null;
+----
+NULL NULL NULL
+
+query ???T
+SELECT bit_and(NULL), bit_or(NULL), bit_xor(NULL), tag FROM bit_null GROUP BY tag ORDER BY tag;
+----
+NULL NULL NULL A
+NULL NULL NULL B
+
+statement ok
+drop table bit_null;
+
+
# bit_and_i32
statement ok
create table t (c int) as values (4), (7), (15);