Skip to content

Commit 65ea070

Browse files
committed
Push decimal aggregates through Widen
Decimal widening preserves order, equality, and validity, so ordered aggregates can use child partials without widening every input value. Reuse exact cached child extrema for MinMax. Compute decimal Sum in the logical sum dtype to avoid overflow at the child precision. Serve it only through the legacy Stat::Sum slot, whose independent batch totals preserve canonical overflow behavior. Keep the running-sum variants on the fallback path. Cover batch equivalence, logical precision, overflow, and child statistics without repeating partial-combination tests or adding benchmark cases. Signed-off-by: Connor Tsui <[email protected]>
1 parent e59b236 commit 65ea070

4 files changed

Lines changed: 488 additions & 31 deletions

File tree

‎vortex-array/src/arrays/widen/compute/aggregate.rs‎

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
//! Aggregates over the narrow child of a [`Widen`] array.
5+
//!
6+
//! Results must match aggregation over the widened values. Widening preserves order and equality,
7+
//! but sums also depend on the logical dtype's precision and overflow rules.
8+
49
use vortex_error::VortexExpect;
510
use vortex_error::VortexResult;
611

712
use crate::ArrayRef;
13+
use crate::Columnar;
814
use crate::ExecutionCtx;
15+
use crate::aggregate_fn::AggregateDTypes;
916
use crate::aggregate_fn::AggregateFnRef;
17+
use crate::aggregate_fn::AggregateFnVTable;
18+
use crate::aggregate_fn::NumericalAggregateOpts;
19+
use crate::aggregate_fn::fns::is_constant::IsConstant;
20+
use crate::aggregate_fn::fns::is_sorted::IsSorted;
21+
use crate::aggregate_fn::fns::max::Max;
22+
use crate::aggregate_fn::fns::min::Min;
23+
use crate::aggregate_fn::fns::min_max::MinMax;
24+
use crate::aggregate_fn::fns::min_max::MinMaxResult;
25+
use crate::aggregate_fn::fns::min_max::min_max;
1026
use crate::aggregate_fn::fns::sum::Sum;
1127
use crate::aggregate_fn::kernels::DynAggregateKernel;
1228
use crate::arrays::Widen;
@@ -15,11 +31,7 @@ use crate::dtype::DType;
1531
use crate::expr::stats::Stat;
1632
use crate::scalar::Scalar;
1733

18-
/// Runs an aggregate of a [`Widen`] on its values and widens the partial to the logical dtype.
19-
///
20-
/// The values run the same accumulator code as the canonical array, so edge cases such as an empty
21-
/// batch or a single null match by construction, and the values keep their own cached statistics
22-
/// and kernels. [`Sum`] is served only when [`is_exact_sum`] holds.
34+
/// Computes aggregate partials from a [`Widen`]'s child without materializing widened values.
2335
#[derive(Debug)]
2436
pub(crate) struct WidenArrayAggregate;
2537

@@ -34,27 +46,123 @@ impl DynAggregateKernel for WidenArrayAggregate {
3446
return Ok(None);
3547
};
3648

37-
// NB: Exactness rests on integer decoding. A decimal `Widen` (CD4 in #10733) needs its own
38-
// argument, because `Sum` checks a decimal for overflow against the precision of its input.
39-
if !batch.dtype().is_int() {
49+
let values = widen.values();
50+
let logical_dtype = batch.dtype();
51+
52+
if logical_dtype.is_decimal() {
53+
return aggregate_decimal(aggregate_fn, values, logical_dtype, ctx);
54+
}
55+
56+
if !logical_dtype.is_int() {
4057
return Ok(None);
4158
}
4259

43-
let values = widen.values();
4460
if aggregate_fn.is::<Sum>() && !is_exact_sum(aggregate_fn, batch, values) {
4561
return Ok(None);
4662
}
4763

48-
let mut accumulator = aggregate_fn.accumulator(values.dtype())?;
49-
accumulator.accumulate(values, ctx)?;
64+
let partial = aggregate_values(aggregate_fn, values, logical_dtype, ctx)?;
65+
66+
Ok(Some(partial))
67+
}
68+
}
69+
70+
/// Selects decimal aggregates whose child values produce the canonical partial.
71+
fn aggregate_decimal(
72+
aggregate_fn: &AggregateFnRef,
73+
values: &ArrayRef,
74+
logical_dtype: &DType,
75+
ctx: &mut ExecutionCtx,
76+
) -> VortexResult<Option<Scalar>> {
77+
if let Some(options) = aggregate_fn.as_opt::<Sum>() {
78+
// This kernel returns a batch total. Only the Stat::Sum path sums each batch from zero.
79+
// Other variants add to the running sum and can overflow at different points.
80+
if Stat::from_aggregate_fn(aggregate_fn) != Some(Stat::Sum) {
81+
return Ok(None);
82+
}
83+
84+
let partial = decimal_sum_partial(values, options, logical_dtype, ctx)?;
85+
86+
return Ok(Some(partial));
87+
}
88+
89+
if !(aggregate_fn.is::<MinMax>()
90+
|| aggregate_fn.is::<Min>()
91+
|| aggregate_fn.is::<Max>()
92+
|| aggregate_fn.is::<IsConstant>()
93+
|| aggregate_fn.is::<IsSorted>())
94+
{
95+
return Ok(None);
96+
}
97+
98+
let partial = aggregate_values(aggregate_fn, values, logical_dtype, ctx)?;
5099

51-
let partial_dtype = aggregate_fn.state_dtype(batch.dtype()).vortex_expect(
52-
"an aggregate that accepts the values accepts the logical dtype, which is a wider \
53-
integer of the same signedness",
54-
);
100+
Ok(Some(partial))
101+
}
55102

56-
Ok(Some(accumulator.flush()?.cast(&partial_dtype)?))
103+
/// Computes a child partial and casts it to the logical aggregate state dtype.
104+
fn aggregate_values(
105+
aggregate_fn: &AggregateFnRef,
106+
values: &ArrayRef,
107+
logical_dtype: &DType,
108+
ctx: &mut ExecutionCtx,
109+
) -> VortexResult<Scalar> {
110+
let partial_dtype = aggregate_fn.state_dtype(logical_dtype).vortex_expect(
111+
"the served aggregate accepts the logical dtype when it accepts the narrower values",
112+
);
113+
114+
if let Some(options) = aggregate_fn.as_opt::<MinMax>() {
115+
return min_max_partial(values, *options, logical_dtype, partial_dtype, ctx);
57116
}
117+
118+
let mut accumulator = aggregate_fn.accumulator(values.dtype())?;
119+
accumulator.accumulate(values, ctx)?;
120+
121+
accumulator.flush()?.cast(&partial_dtype)
122+
}
123+
124+
/// Computes a decimal [`Sum`] batch partial without materializing the widened array.
125+
///
126+
/// Checked arithmetic overflow or a batch total outside the result precision produces null.
127+
/// Null rows are skipped, and empty or all-null batches produce zero.
128+
fn decimal_sum_partial(
129+
values: &ArrayRef,
130+
options: &NumericalAggregateOpts,
131+
logical_dtype: &DType,
132+
ctx: &mut ExecutionCtx,
133+
) -> VortexResult<Scalar> {
134+
// Sum converts each stored value to the accumulator width selected by these logical dtypes
135+
// before adding it. The child width never limits the sum. Widen preserves values, scale, and
136+
// validity, so the additions, precision checks, and output dtype match the widened array.
137+
let dtypes = AggregateDTypes::try_new(&Sum, options, logical_dtype.clone())?;
138+
let args = dtypes.args(options);
139+
let values = values.clone().execute::<Columnar>(ctx)?;
140+
141+
let mut partial = Sum.empty_partial(args)?;
142+
Sum.accumulate(args, &mut partial, &values, ctx)?;
143+
144+
Sum.to_scalar(args, &partial)
145+
}
146+
147+
/// Builds a logical [`MinMax`] partial from the child's cached or computed extrema.
148+
///
149+
/// [`min_max`] uses exact cached extrema before scanning the child. Extrema have the non-nullable
150+
/// logical dtype, even for nullable inputs. A batch with no valid values has a null partial.
151+
fn min_max_partial(
152+
values: &ArrayRef,
153+
options: NumericalAggregateOpts,
154+
logical_dtype: &DType,
155+
partial_dtype: DType,
156+
ctx: &mut ExecutionCtx,
157+
) -> VortexResult<Scalar> {
158+
let Some(MinMaxResult { min, max }) = min_max(values, ctx, options)? else {
159+
return Ok(Scalar::null(partial_dtype));
160+
};
161+
162+
let element_dtype = logical_dtype.as_nonnullable();
163+
let extrema = vec![min.cast(&element_dtype)?, max.cast(&element_dtype)?];
164+
165+
Ok(Scalar::struct_(partial_dtype, extrema))
58166
}
59167

60168
/// Returns whether the [`Sum`] of the values of `batch` equals the sum of the canonical batch.

‎vortex-array/src/arrays/widen/tests/aggregates.rs‎

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
//! Tests for aggregate results and child-statistics reuse.
5+
//!
6+
//! Shared batch assertions compare integer and decimal [`Widen`] arrays with fresh canonical inputs.
7+
8+
use std::fmt::Debug;
49
use std::num::NonZeroUsize;
510

611
use rstest::rstest;
12+
use vortex_error::VortexExpect;
713
use vortex_error::VortexResult;
814

915
use super::SESSION;
@@ -32,21 +38,27 @@ use crate::aggregate_fn::fns::last::Last;
3238
use crate::aggregate_fn::fns::max::Max;
3339
use crate::aggregate_fn::fns::min::Min;
3440
use crate::aggregate_fn::fns::min_max::MinMax;
41+
use crate::aggregate_fn::fns::min_max::make_minmax_dtype;
3542
use crate::aggregate_fn::fns::null_count::NullCount;
3643
use crate::aggregate_fn::fns::sum::Sum;
3744
use crate::aggregate_fn::fns::sum_v2::SumV2;
3845
use crate::aggregate_fn::fns::uncompressed_size_in_bytes::UncompressedSizeInBytes;
3946
use crate::arrays::ChunkedArray;
4047
use crate::arrays::ConstantArray;
48+
use crate::arrays::DecimalArray;
4149
use crate::arrays::PrimitiveArray;
4250
use crate::arrays::Widen;
4351
use crate::arrays::WidenArray;
4452
use crate::builtins::ArrayBuiltins;
4553
use crate::dtype::DType;
54+
use crate::dtype::DecimalDType;
4655
use crate::dtype::Nullability;
4756
use crate::dtype::Nullability::NonNullable;
4857
use crate::dtype::Nullability::Nullable;
4958
use crate::dtype::PType;
59+
use crate::expr::stats::Precision;
60+
use crate::expr::stats::Stat;
61+
use crate::scalar::DecimalValue;
5062
use crate::scalar::Scalar;
5163

5264
/// Every aggregate that applies to integers, and whether the `Widen` aggregate kernel serves it.
@@ -89,9 +101,8 @@ fn canonical_batch(
89101
.execute::<PrimitiveArray>(ctx)
90102
}
91103

92-
/// Runs the kernel that the session registers for `aggregate` on `Widen` over `array`, and returns
93-
/// its partial, or `None` when no kernel is registered or the kernel declines.
94-
fn kernel_partial(
104+
/// Returns the registered [`Widen`] kernel's partial, or `None` if it is absent or declines.
105+
pub(super) fn kernel_partial(
95106
aggregate: &AggregateFnRef,
96107
array: &ArrayRef,
97108
ctx: &mut ExecutionCtx,
@@ -105,40 +116,40 @@ fn kernel_partial(
105116
}
106117
}
107118

108-
/// Accumulates `batches` of `dtype` with `aggregate` once as [`WidenArray::encode`] encodes them
109-
/// and once as canonical arrays, and asserts that the running partials and the results are equal.
119+
/// Checks encoded aggregates against canonical inputs, including kernel selection and partials.
110120
///
111-
/// On each batch that encodes to a `Widen`, the kernel must return a partial exactly when
112-
/// `served`, and the partial must equal the partial of a fresh accumulator on the canonical batch.
113-
/// Encoding and each accumulator get their own canonical batch, because both cache statistics on
114-
/// the array they read, and an accumulator reads a cached statistic instead of the data.
121+
/// `served` says whether the [`Widen`] kernel must return a partial. `build_batch` **must** return a
122+
/// fresh array with its own statistics on every call. Reusing arrays could let aggregation read a
123+
/// cached answer instead of testing the kernel.
115124
#[track_caller]
116-
fn assert_aggregate_batches(
125+
pub(super) fn assert_aggregate_batches<T: Debug, C: IntoArray>(
117126
aggregate: &AggregateFnRef,
118127
served: bool,
119-
batches: &[Vec<Option<i64>>],
128+
batches: &[T],
120129
dtype: &DType,
130+
mut build_batch: impl FnMut(&T, &DType, &mut ExecutionCtx) -> VortexResult<C>,
131+
mut encode: impl FnMut(C, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
121132
ctx: &mut ExecutionCtx,
122133
) -> VortexResult<()> {
123134
let mut actual = aggregate.accumulator(dtype)?;
124135
let mut expected = aggregate.accumulator(dtype)?;
125136

126137
for values in batches {
127-
let encoded = WidenArray::encode(canonical_batch(values, dtype, ctx)?, ctx)?;
138+
let encoded = encode(build_batch(values, dtype, ctx)?, ctx)?;
128139

129140
if encoded.is::<Widen>() {
130141
let partial = kernel_partial(aggregate, &encoded, ctx)?;
131142
assert_eq!(partial.is_some(), served, "{aggregate} on {values:?}");
132143

133144
if let Some(partial) = partial {
134145
let mut reference = aggregate.accumulator(dtype)?;
135-
reference.accumulate(&canonical_batch(values, dtype, ctx)?.into_array(), ctx)?;
146+
reference.accumulate(&build_batch(values, dtype, ctx)?.into_array(), ctx)?;
136147
assert_eq!(partial, reference.flush()?, "{aggregate} on {values:?}");
137148
}
138149
}
139150

140151
actual.accumulate(&encoded, ctx)?;
141-
expected.accumulate(&canonical_batch(values, dtype, ctx)?.into_array(), ctx)?;
152+
expected.accumulate(&build_batch(values, dtype, ctx)?.into_array(), ctx)?;
142153
assert_eq!(
143154
actual.partial_scalar()?,
144155
expected.partial_scalar()?,
@@ -230,7 +241,15 @@ fn test_aggregate_partials(
230241
let dtype = DType::Primitive(logical, nullability);
231242

232243
for (aggregate, served) in aggregates() {
233-
assert_aggregate_batches(&aggregate, served, &batches, &dtype, &mut ctx)?;
244+
assert_aggregate_batches(
245+
&aggregate,
246+
served,
247+
&batches,
248+
&dtype,
249+
|values, dtype, ctx| canonical_batch(values, dtype, ctx),
250+
WidenArray::encode,
251+
&mut ctx,
252+
)?;
234253
}
235254

236255
Ok(())
@@ -304,3 +323,46 @@ fn test_chunked_matches_canonical() -> VortexResult<()> {
304323

305324
Ok(())
306325
}
326+
327+
/// Cached extrema deliberately differ from the data, so the result proves the cache was read.
328+
#[rstest]
329+
#[case::integer(
330+
PrimitiveArray::from_iter([1i8, 2, 3]).into_array(),
331+
DType::Primitive(PType::I64, NonNullable),
332+
Scalar::primitive(-100i8, NonNullable),
333+
Scalar::primitive(100i8, NonNullable),
334+
)]
335+
#[case::decimal(
336+
DecimalArray::from_option_iter([Some(1i16), None], DecimalDType::new(4, 2)).into_array(),
337+
DType::Decimal(DecimalDType::new(18, 2), Nullable),
338+
Scalar::decimal(DecimalValue::I16(-9_999), DecimalDType::new(4, 2), NonNullable),
339+
Scalar::decimal(DecimalValue::I16(9_999), DecimalDType::new(4, 2), NonNullable),
340+
)]
341+
fn test_min_max_reads_values_extrema(
342+
#[case] values: ArrayRef,
343+
#[case] logical_dtype: DType,
344+
#[case] min: Scalar,
345+
#[case] max: Scalar,
346+
) -> VortexResult<()> {
347+
let mut ctx = SESSION.create_execution_ctx();
348+
349+
for (stat, extremum) in [(Stat::Min, &min), (Stat::Max, &max)] {
350+
let value = extremum.value().cloned();
351+
let value = value.vortex_expect("the planted extremum is not null");
352+
values.statistics().set(stat, Precision::Exact(value));
353+
}
354+
355+
let array = WidenArray::try_new(values, logical_dtype.clone())?.into_array();
356+
let aggregate = MinMax.bind(NumericalAggregateOpts::default());
357+
358+
let partial = kernel_partial(&aggregate, &array, &mut ctx)?;
359+
360+
let element_dtype = logical_dtype.as_nonnullable();
361+
let expected = Scalar::struct_(
362+
make_minmax_dtype(&logical_dtype),
363+
vec![min.cast(&element_dtype)?, max.cast(&element_dtype)?],
364+
);
365+
assert_eq!(partial, Some(expected));
366+
367+
Ok(())
368+
}

0 commit comments

Comments
 (0)