alamb commented on code in PR #11627:
URL: https://github.com/apache/datafusion/pull/11627#discussion_r1698999874


##########
datafusion/functions-aggregate/src/count.rs:
##########
@@ -433,6 +433,49 @@ impl GroupsAccumulator for CountGroupsAccumulator {
         Ok(vec![Arc::new(counts) as ArrayRef])
     }
 
+    fn convert_to_state(
+        &self,
+        values: &[ArrayRef],
+        opt_filter: Option<&BooleanArray>,
+    ) -> Result<Vec<ArrayRef>> {
+        let values = &values[0];
+
+        let state_array = match (values.logical_nulls(), opt_filter) {
+            (Some(nulls), None) => {
+                let mut builder = Int64Builder::with_capacity(values.len());
+                nulls
+                    .into_iter()
+                    .for_each(|is_valid| builder.append_value(is_valid as 
i64));
+                builder.finish()
+            }
+            (Some(nulls), Some(filter)) => {
+                let mut builder = Int64Builder::with_capacity(values.len());
+                nulls.into_iter().zip(filter.iter()).for_each(
+                    |(is_valid, filter_value)| {
+                        builder.append_value(

Review Comment:
   > I've rewritten state conversion for count on bitand on buffers + cast to 
Int64 in the end, and according to benchmarks from the commit it got 20-25% 
faster.
   
   🎉 
   
   > Just a suggestion -- won't it be better to use BooleanBuffer + & (bitand 
operator) instead of NullBuffer + union? NullBuffer is a bit confusing, so I've 
"pulled" the logic from union right into state conversion function.
   
   I think they are equivalent:  
[`NullBuffer`](https://docs.rs/arrow/latest/arrow/buffer/struct.NullBuffer.html)
 just wraps `BooleanBuffer` and `NullBuffer::union` just calls `&` underneath: 
https://docs.rs/arrow-buffer/52.2.0/src/arrow_buffer/buffer/null.rs.html#76 
(after replicating the `match(nulls, filter)` logic)
   
   I don't have a strong opinion about which is more/less confusing
   
   What I suggest we do is pull the logic to compute the output null mask basd 
on the optional input nullmask and the optional filter into a function (like 
`fn filtered_null_mask`) as it will be used in basically all of the 
`convert_to_state` implementations. As long as it is well documented, I think 
either implementation will work well
   
   > Additionally, I plan to prepare benches and minimize ArrayBuilder usage 
for min / max / sum during tomorrow.
   
   Sounds good -- would you like to keep updating this PR or shall we merge 
this PR and continue improvements with additional PRs on main?



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to