kosiew commented on code in PR #27299:
URL: https://github.com/apache/datafusion/pull/27299#discussion_r4783027926


##########
datafusion/functions-aggregate-common/src/aggregate/count_distinct/groups.rs:
##########
@@ -63,23 +105,41 @@ where
         debug_assert_eq!(values.len(), 1);
         self.counts.resize(total_num_groups, 0);
         let arr = values[0].as_primitive::<T>();
-        accumulate(group_indices, arr, opt_filter, |group_idx, value| {
-            if self.seen.insert((group_idx, value)) {
-                self.counts[group_idx] += 1;
-            }
-        });
+        if let Some(first_values) = &mut self.first_values {
+            first_values.resize(total_num_groups, T::Native::default());

Review Comment:
   Could we consider density-based promotion in update_batch and merge_batch? 
With twelve sparse DISTINCT expressions, peak reservations increased from 5.24 
MB to 8.38 MB, introducing spills at 6M. Any guard should preserve Q10/Q11 
gains; otherwise, documenting the trade-off is sufficient.



##########
datafusion/functions-aggregate-common/src/aggregate/count_distinct/groups.rs:
##########
@@ -63,23 +105,41 @@ where
         debug_assert_eq!(values.len(), 1);
         self.counts.resize(total_num_groups, 0);
         let arr = values[0].as_primitive::<T>();
-        accumulate(group_indices, arr, opt_filter, |group_idx, value| {
-            if self.seen.insert((group_idx, value)) {
-                self.counts[group_idx] += 1;
-            }
-        });
+        if let Some(first_values) = &mut self.first_values {
+            first_values.resize(total_num_groups, T::Native::default());
+            accumulate(group_indices, arr, opt_filter, |group_idx, value| {
+                Self::insert_inline(
+                    &mut first_values[group_idx],
+                    &mut self.counts[group_idx],
+                    &mut self.seen,
+                    group_idx,
+                    value,
+                );
+            });
+            self.promote_inline_values();
+        } else {
+            accumulate(group_indices, arr, opt_filter, |group_idx, value| {
+                if self.seen.insert((group_idx, value)) {
+                    self.counts[group_idx] += 1;
+                }
+            });
+        }
         Ok(())
     }
 
     fn evaluate(&mut self, emit_to: EmitTo) -> 
datafusion_common::Result<ArrayRef> {
         let counts = emit_to.take_needed(&mut self.counts);
+        if let Some(first_values) = &mut self.first_values {
+            let _ = emit_to.take_needed(first_values);

Review Comment:
   Optional: consider dropping drain(..n) directly for small discarded inline 
prefixes to avoid allocation. Retain split-and-drop for large prefixes so the 
remaining buffer can shrink.



##########
benchmarks/sql_benchmarks/grouped_count_distinct/README.md:
##########
@@ -20,10 +20,18 @@ distributed rewrite, which remains selected with either 
flag value. The same
 environment variable works with `./bench.sh run grouped_count_distinct`.
 
 Q01-Q09 vary argument and grouping-key types, group count, distinct 
cardinality,
-and skew. Q10 covers the unfavorable native shape: at the default four million
-rows it has one million groups, each with four copies of the same value. Each
-native input row probes both the group table and the per-group distinct set;
-the rewrite's outer aggregate only sees the deduplicated pairs.
+and skew. Q10 has one million groups, each with four copies of the same value,
+at the default four million rows. It exercises the native accumulator's inline
+first value, which lets repeated singleton values skip the distinct hash set.
+The rewrite's outer aggregate only sees the deduplicated pairs.
+
+When the average number of additional distinct values per group exceeds eight,

Review Comment:
   Could we clarify that promotion is checked after each update or merge batch, 
includes NULL-only groups in the average, and remains active through partial 
emissions until EmitTo::All resets the accumulator?



-- 
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]

Reply via email to