Jefffrey commented on code in PR #22707:
URL: https://github.com/apache/datafusion/pull/22707#discussion_r3339196343
##########
datafusion/functions-aggregate-common/src/aggregate/count_distinct/native.rs:
##########
@@ -518,3 +519,85 @@ impl Accumulator for
Bitmap65536DistinctCountAccumulatorI16 {
size_of_val(self) + 8192
}
}
+
+/// Optimized COUNT DISTINCT accumulator for `Boolean` using two flags.
+///
+/// Tracks whether `false` and `true` have been observed; nulls are skipped.
+/// Result is always 0, 1, or 2.
+#[derive(Debug)]
+pub struct BooleanDistinctCountAccumulator {
+ seen: [bool; 2],
+}
+
+impl BooleanDistinctCountAccumulator {
+ pub fn new() -> Self {
+ Self { seen: [false; 2] }
+ }
+
+ #[inline]
+ fn count(&self) -> i64 {
+ self.seen.iter().filter(|&&b| b).count() as i64
+ }
+}
+
+impl Default for BooleanDistinctCountAccumulator {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Accumulator for BooleanDistinctCountAccumulator {
+ fn update_batch(&mut self, values: &[ArrayRef]) ->
datafusion_common::Result<()> {
+ if values.is_empty() {
+ return Ok(());
+ }
+
+ let arr = as_boolean_array(&values[0])?;
+ for value in arr.iter().flatten() {
+ self.seen[value as usize] = true;
+ if self.seen[0] && self.seen[1] {
+ break;
+ }
+ }
Review Comment:
We can probably be smarter with these:
- can bail earlier before checking input array if state already has seen both
- can use
[`has_true`](https://docs.rs/arrow/latest/arrow/array/struct.BooleanArray.html#method.has_true)
and
[`has_false`](https://docs.rs/arrow/latest/arrow/array/struct.BooleanArray.html#method.has_false)
instead of checking whole array
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]