JeelRajodiya commented on code in PR #22707:
URL: https://github.com/apache/datafusion/pull/22707#discussion_r3339342328
##########
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:
Good call — applied in c08fb269b: `update_batch`/`merge_batch` now bail
early when both flags are set, and use `has_true`/`has_false` (and
short-circuit per-side) instead of iterating.
--
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]