Skip to content

Commit ee1ac25

Browse files
authored
Avoid single-chunk grouped accumulator states (#9597)
## Summary - return the sole grouped partial state directly from `GroupedAccumulator::flush` - preserve the existing `ChunkedArray` fallback for zero or multiple partial states This avoids routing single-batch grouped aggregate finalization through a one-chunk wrapper and its optimizer pushdown. Signed-off-by: Matt Katz <[email protected]>
1 parent 8d2f1d9 commit ee1ac25

1 file changed

Lines changed: 69 additions & 1 deletion

File tree

‎vortex-array/src/aggregate_fn/accumulator_grouped.rs‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ impl<V: AggregateFnVTable> DynGroupedAccumulator for GroupedAccumulator<V> {
273273
}
274274

275275
fn flush(&mut self) -> VortexResult<ArrayRef> {
276-
let states = std::mem::take(&mut self.partials);
276+
let mut states = std::mem::take(&mut self.partials);
277+
if states.len() == 1 {
278+
return Ok(states.pop().vortex_expect("checked one partial"));
279+
}
277280
Ok(ChunkedArray::try_new(states, self.partial_dtype.clone())?.into_array())
278281
}
279282

@@ -418,3 +421,68 @@ fn fixed_size_list_group_ranges(groups: &FixedSizeListArray) -> GroupRanges {
418421
size: groups.list_size() as usize,
419422
}
420423
}
424+
425+
#[cfg(test)]
426+
mod tests {
427+
use vortex_error::VortexResult;
428+
429+
use crate::ArrayRef;
430+
use crate::IntoArray;
431+
use crate::aggregate_fn::DynGroupedAccumulator;
432+
use crate::aggregate_fn::GroupedAccumulator;
433+
use crate::aggregate_fn::NumericalAggregateOpts;
434+
use crate::aggregate_fn::fns::count::Count;
435+
use crate::arrays::Chunked;
436+
use crate::arrays::PrimitiveArray;
437+
use crate::dtype::DType;
438+
use crate::dtype::Nullability::NonNullable;
439+
use crate::dtype::PType;
440+
441+
fn accumulator() -> VortexResult<GroupedAccumulator<Count>> {
442+
GroupedAccumulator::try_new(
443+
Count,
444+
NumericalAggregateOpts::default(),
445+
DType::Primitive(PType::I32, NonNullable),
446+
)
447+
}
448+
449+
fn state(values: impl IntoIterator<Item = u64>) -> ArrayRef {
450+
PrimitiveArray::from_iter(values).into_array()
451+
}
452+
453+
#[test]
454+
fn test_flush_single_partial_returns_original_array() -> VortexResult<()> {
455+
let mut accumulator = accumulator()?;
456+
let state = state([1, 2, 3]);
457+
accumulator.push_result(state.clone())?;
458+
459+
let flushed = accumulator.flush()?;
460+
461+
assert!(ArrayRef::ptr_eq(&flushed, &state));
462+
Ok(())
463+
}
464+
465+
#[test]
466+
fn test_flush_multiple_partials_returns_chunked_array() -> VortexResult<()> {
467+
let mut accumulator = accumulator()?;
468+
accumulator.push_result(state([1, 2]))?;
469+
accumulator.push_result(state([3, 4]))?;
470+
471+
let flushed = accumulator.flush()?;
472+
473+
assert!(flushed.is::<Chunked>());
474+
assert_eq!(flushed.len(), 4);
475+
Ok(())
476+
}
477+
478+
#[test]
479+
fn test_flush_without_partials_returns_empty_chunked_array() -> VortexResult<()> {
480+
let mut accumulator = accumulator()?;
481+
482+
let flushed = accumulator.flush()?;
483+
484+
assert!(flushed.is::<Chunked>());
485+
assert!(flushed.is_empty());
486+
Ok(())
487+
}
488+
}

0 commit comments

Comments
 (0)