Skip to content

Commit 1ae75c7

Browse files
Dandandanclaude
andcommitted
feat: Use BatchedVec for count and avg accumulator storage
Extends BatchedVec chunked storage to CountGroupsAccumulator and AvgGroupsAccumulator for consistent batch-at-a-time emission. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent fdb81ac commit 1ae75c7

2 files changed

Lines changed: 54 additions & 24 deletions

File tree

‎datafusion/functions-aggregate/src/average.rs‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use datafusion_functions_aggregate_common::aggregate::avg_distinct::{
4444
DecimalDistinctAvgAccumulator, Float64DistinctAvgAccumulator,
4545
};
4646
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::accumulate::NullState;
47+
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::batched_vec::BatchedVec;
4748
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::nulls::{
4849
filtered_null_mask, set_nulls,
4950
};
@@ -52,7 +53,7 @@ use datafusion_macros::user_doc;
5253
use log::debug;
5354
use std::any::Any;
5455
use std::fmt::Debug;
55-
use std::mem::{size_of, size_of_val};
56+
use std::mem::size_of_val;
5657
use std::sync::Arc;
5758

5859
make_udaf_expr_and_func!(
@@ -763,10 +764,10 @@ where
763764
return_data_type: DataType,
764765

765766
/// Count per group (use u64 to make UInt64Array)
766-
counts: Vec<u64>,
767+
counts: BatchedVec<u64>,
767768

768769
/// Sums per group, stored as the native type
769-
sums: Vec<T::Native>,
770+
sums: BatchedVec<T::Native>,
770771

771772
/// Track nulls in the input / filters
772773
null_state: NullState,
@@ -786,11 +787,12 @@ where
786787
std::any::type_name::<T>()
787788
);
788789

790+
const DEFAULT_BATCH_SIZE: usize = 8192;
789791
Self {
790792
return_data_type: return_data_type.clone(),
791793
sum_data_type: sum_data_type.clone(),
792-
counts: vec![],
793-
sums: vec![],
794+
counts: BatchedVec::new(DEFAULT_BATCH_SIZE),
795+
sums: BatchedVec::new(DEFAULT_BATCH_SIZE),
794796
null_state: NullState::new(),
795797
avg_fn,
796798
}
@@ -813,8 +815,9 @@ where
813815
let values = values[0].as_primitive::<T>();
814816

815817
// increment counts, update sums
816-
self.counts.resize(total_num_groups, 0);
817-
self.sums.resize(total_num_groups, T::default_value());
818+
self.counts.ensure_capacity(total_num_groups, 0);
819+
self.sums
820+
.ensure_capacity(total_num_groups, T::default_value());
818821
self.null_state.accumulate(
819822
group_indices,
820823
values,
@@ -825,16 +828,23 @@ where
825828
let sum = unsafe { self.sums.get_unchecked_mut(group_index) };
826829
*sum = sum.add_wrapping(new_value);
827830

828-
self.counts[group_index] += 1;
831+
let count = unsafe { self.counts.get_unchecked_mut(group_index) };
832+
*count += 1;
829833
},
830834
);
831835

832836
Ok(())
833837
}
834838

835839
fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
836-
let counts = emit_to.take_needed(&mut self.counts);
837-
let sums = emit_to.take_needed(&mut self.sums);
840+
let counts = match emit_to {
841+
EmitTo::All => self.counts.take_all(),
842+
EmitTo::First(n) => self.counts.take_first(n),
843+
};
844+
let sums = match emit_to {
845+
EmitTo::All => self.sums.take_all(),
846+
EmitTo::First(n) => self.sums.take_first(n),
847+
};
838848
let nulls = self.null_state.build(emit_to);
839849

840850
if let Some(nulls) = &nulls {
@@ -876,10 +886,16 @@ where
876886
fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
877887
let nulls = self.null_state.build(emit_to);
878888

879-
let counts = emit_to.take_needed(&mut self.counts);
889+
let counts = match emit_to {
890+
EmitTo::All => self.counts.take_all(),
891+
EmitTo::First(n) => self.counts.take_first(n),
892+
};
880893
let counts = UInt64Array::new(counts.into(), nulls.clone()); // zero copy
881894

882-
let sums = emit_to.take_needed(&mut self.sums);
895+
let sums = match emit_to {
896+
EmitTo::All => self.sums.take_all(),
897+
EmitTo::First(n) => self.sums.take_first(n),
898+
};
883899
let sums = PrimitiveArray::<T>::new(sums.into(), nulls) // zero copy
884900
.with_data_type(self.sum_data_type.clone());
885901

@@ -901,7 +917,7 @@ where
901917
let partial_counts = values[0].as_primitive::<UInt64Type>();
902918
let partial_sums = values[1].as_primitive::<T>();
903919
// update counts with partial counts
904-
self.counts.resize(total_num_groups, 0);
920+
self.counts.ensure_capacity(total_num_groups, 0);
905921
self.null_state.accumulate(
906922
group_indices,
907923
partial_counts,
@@ -915,7 +931,8 @@ where
915931
);
916932

917933
// update sums
918-
self.sums.resize(total_num_groups, T::default_value());
934+
self.sums
935+
.ensure_capacity(total_num_groups, T::default_value());
919936
self.null_state.accumulate(
920937
group_indices,
921938
partial_sums,
@@ -956,6 +973,6 @@ where
956973
}
957974

958975
fn size(&self) -> usize {
959-
self.counts.capacity() * size_of::<u64>() + self.sums.capacity() * size_of::<T>()
976+
self.counts.size() + self.sums.size()
960977
}
961978
}

‎datafusion/functions-aggregate/src/count.rs‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use datafusion_functions_aggregate_common::aggregate::{
4848
count_distinct::FloatDistinctCountAccumulator,
4949
count_distinct::PrimitiveDistinctCountAccumulator,
5050
groups_accumulator::accumulate::accumulate_indices,
51+
groups_accumulator::batched_vec::BatchedVec,
5152
};
5253
use datafusion_macros::user_doc;
5354
use datafusion_physical_expr::expressions;
@@ -555,18 +556,22 @@ impl Accumulator for CountAccumulator {
555556
/// accumulator has no additional null or seen filter tracking.
556557
#[derive(Debug)]
557558
struct CountGroupsAccumulator {
558-
/// Count per group.
559+
/// Count per group, stored in batch_size chunks.
559560
///
560561
/// Note this is an i64 and not a u64 (or usize) because the
561562
/// output type of count is `DataType::Int64`. Thus by using `i64`
562563
/// for the counts, the output [`Int64Array`] can be created
563564
/// without copy.
564-
counts: Vec<i64>,
565+
counts: BatchedVec<i64>,
565566
}
566567

568+
const DEFAULT_BATCH_SIZE: usize = 8192;
569+
567570
impl CountGroupsAccumulator {
568571
pub fn new() -> Self {
569-
Self { counts: vec![] }
572+
Self {
573+
counts: BatchedVec::new(DEFAULT_BATCH_SIZE),
574+
}
570575
}
571576
}
572577

@@ -583,7 +588,7 @@ impl GroupsAccumulator for CountGroupsAccumulator {
583588

584589
// Add one to each group's counter for each non null, non
585590
// filtered value
586-
self.counts.resize(total_num_groups, 0);
591+
self.counts.ensure_capacity(total_num_groups, 0);
587592
accumulate_indices(
588593
group_indices,
589594
values.logical_nulls().as_ref(),
@@ -615,18 +620,23 @@ impl GroupsAccumulator for CountGroupsAccumulator {
615620
let partial_counts = partial_counts.values();
616621

617622
// Adds the counts with the partial counts
618-
self.counts.resize(total_num_groups, 0);
623+
self.counts.ensure_capacity(total_num_groups, 0);
619624
group_indices.iter().zip(partial_counts.iter()).for_each(
620625
|(&group_index, partial_count)| {
621-
self.counts[group_index] += partial_count;
626+
// SAFETY: group_index is guaranteed to be in bounds
627+
let count = unsafe { self.counts.get_unchecked_mut(group_index) };
628+
*count += partial_count;
622629
},
623630
);
624631

625632
Ok(())
626633
}
627634

628635
fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
629-
let counts = emit_to.take_needed(&mut self.counts);
636+
let counts = match emit_to {
637+
EmitTo::All => self.counts.take_all(),
638+
EmitTo::First(n) => self.counts.take_first(n),
639+
};
630640

631641
// Count is always non null (null inputs just don't contribute to the overall values)
632642
let nulls = None;
@@ -637,7 +647,10 @@ impl GroupsAccumulator for CountGroupsAccumulator {
637647

638648
// return arrays for counts
639649
fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
640-
let counts = emit_to.take_needed(&mut self.counts);
650+
let counts = match emit_to {
651+
EmitTo::All => self.counts.take_all(),
652+
EmitTo::First(n) => self.counts.take_first(n),
653+
};
641654
let counts: PrimitiveArray<Int64Type> = Int64Array::from(counts); // zero copy, no nulls
642655
Ok(vec![Arc::new(counts) as ArrayRef])
643656
}
@@ -708,7 +721,7 @@ impl GroupsAccumulator for CountGroupsAccumulator {
708721
}
709722

710723
fn size(&self) -> usize {
711-
self.counts.capacity() * size_of::<usize>()
724+
self.counts.size()
712725
}
713726
}
714727

0 commit comments

Comments
 (0)