Dandandan commented on code in PR #22712:
URL: https://github.com/apache/datafusion/pull/22712#discussion_r3340351961
##########
datafusion/functions-aggregate/src/average.rs:
##########
@@ -970,7 +1130,262 @@ where
true
}
+ fn create_blocked_accumulator(
+ &self,
+ block_size: usize,
+ ) -> Result<Option<Box<dyn GroupsAccumulator>>> {
+ if self.sum_data_type == DataType::Float64
+ && self.return_data_type == DataType::Float64
+ {
+ Ok(Some(Box::new(BlockedAvgGroupsAccumulator::new(block_size))))
+ } else {
+ Ok(None)
+ }
+ }
+
fn size(&self) -> usize {
self.counts.capacity() * size_of::<u64>() + self.sums.capacity() *
size_of::<T>()
}
}
+
+impl GroupsAccumulator for BlockedAvgGroupsAccumulator {
+ fn update_batch(
+ &mut self,
+ values: &[ArrayRef],
+ group_indices: &[usize],
+ opt_filter: Option<&BooleanArray>,
+ total_num_groups: usize,
+ ) -> Result<()> {
+ assert_eq!(values.len(), 1, "single argument to update_batch");
+ let values = values[0].as_primitive::<Float64Type>();
+ self.ensure_capacity(total_num_groups);
+
+ let counts = &mut self.counts;
+ let sums = &mut self.sums;
+ let block_size = self.block_size;
+ let len = self.len;
+
+ self.null_state.accumulate(
+ group_indices,
+ values,
+ opt_filter,
+ total_num_groups,
+ |group_index, value| {
+ debug_assert!(group_index < len);
+ let block_idx = group_index / block_size;
+ let value_idx = group_index % block_size;
+ counts[block_idx][value_idx] += 1;
+ sums[block_idx][value_idx] += value;
+ },
+ );
+
+ Ok(())
+ }
+
+ fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+ let (counts, sums) = self.take_values(emit_to);
+ let mut values = Vec::with_capacity(counts.len());
+ let mut nulls = NullBufferBuilder::new(counts.len());
+
+ for (count, sum) in counts.into_iter().zip(sums) {
+ if count == 0 {
+ values.push(0.0);
+ nulls.append_null();
+ } else {
+ values.push(sum / count as f64);
+ nulls.append_non_null();
+ }
+ }
+
+ Ok(Arc::new(Float64Array::new(values.into(), nulls.finish())))
+ }
+
+ fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
+ let (counts, sums) = self.take_values(emit_to);
+ let nulls = Self::nulls_for_counts(&counts);
+
+ Ok(vec![
+ Arc::new(UInt64Array::new(counts.into(), nulls.clone())) as
ArrayRef,
+ Arc::new(Float64Array::new(sums.into(), nulls)) as ArrayRef,
+ ])
+ }
+
+ fn merge_batch(
+ &mut self,
+ values: &[ArrayRef],
+ group_indices: &[usize],
+ opt_filter: Option<&BooleanArray>,
+ total_num_groups: usize,
+ ) -> Result<()> {
+ assert_eq!(values.len(), 2, "two arguments to merge_batch");
+ self.ensure_capacity(total_num_groups);
+
+ let partial_counts = values[0].as_primitive::<UInt64Type>();
+ let partial_sums = values[1].as_primitive::<Float64Type>();
+
+ let counts = &mut self.counts;
+ let block_size = self.block_size;
+ let len = self.len;
+ self.null_state.accumulate(
+ group_indices,
+ partial_counts,
+ opt_filter,
+ total_num_groups,
+ |group_index, partial_count| {
+ debug_assert!(group_index < len);
+ let block_idx = group_index / block_size;
+ let value_idx = group_index % block_size;
+ counts[block_idx][value_idx] += partial_count;
+ },
+ );
+
+ let sums = &mut self.sums;
+ self.null_state.accumulate(
+ group_indices,
+ partial_sums,
+ opt_filter,
+ total_num_groups,
+ |group_index, partial_sum| {
+ debug_assert!(group_index < len);
+ let block_idx = group_index / block_size;
+ let value_idx = group_index % block_size;
+ sums[block_idx][value_idx] += partial_sum;
+ },
+ );
+
+ Ok(())
+ }
+
+ fn convert_to_state(
+ &self,
+ values: &[ArrayRef],
+ opt_filter: Option<&BooleanArray>,
+ ) -> Result<Vec<ArrayRef>> {
+ assert_eq!(values.len(), 1, "single argument to convert_to_state");
+ let values = values[0].as_primitive::<Float64Type>();
+ let mut counts = Vec::with_capacity(values.len());
+ let mut sums = Vec::with_capacity(values.len());
+ let mut nulls = NullBufferBuilder::new(values.len());
+
+ for row in 0..values.len() {
+ if opt_filter
+ .map(|filter| filter.is_valid(row) && filter.value(row))
+ .unwrap_or(true)
+ && values.is_valid(row)
+ {
+ counts.push(1);
+ sums.push(values.value(row));
+ nulls.append_non_null();
+ } else {
+ counts.push(0);
Review Comment:
this can use `collect` rather than push
--
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]