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 ;
49use std:: num:: NonZeroUsize ;
510
611use rstest:: rstest;
12+ use vortex_error:: VortexExpect ;
713use vortex_error:: VortexResult ;
814
915use super :: SESSION ;
@@ -32,21 +38,27 @@ use crate::aggregate_fn::fns::last::Last;
3238use crate :: aggregate_fn:: fns:: max:: Max ;
3339use crate :: aggregate_fn:: fns:: min:: Min ;
3440use crate :: aggregate_fn:: fns:: min_max:: MinMax ;
41+ use crate :: aggregate_fn:: fns:: min_max:: make_minmax_dtype;
3542use crate :: aggregate_fn:: fns:: null_count:: NullCount ;
3643use crate :: aggregate_fn:: fns:: sum:: Sum ;
3744use crate :: aggregate_fn:: fns:: sum_v2:: SumV2 ;
3845use crate :: aggregate_fn:: fns:: uncompressed_size_in_bytes:: UncompressedSizeInBytes ;
3946use crate :: arrays:: ChunkedArray ;
4047use crate :: arrays:: ConstantArray ;
48+ use crate :: arrays:: DecimalArray ;
4149use crate :: arrays:: PrimitiveArray ;
4250use crate :: arrays:: Widen ;
4351use crate :: arrays:: WidenArray ;
4452use crate :: builtins:: ArrayBuiltins ;
4553use crate :: dtype:: DType ;
54+ use crate :: dtype:: DecimalDType ;
4655use crate :: dtype:: Nullability ;
4756use crate :: dtype:: Nullability :: NonNullable ;
4857use crate :: dtype:: Nullability :: Nullable ;
4958use crate :: dtype:: PType ;
59+ use crate :: expr:: stats:: Precision ;
60+ use crate :: expr:: stats:: Stat ;
61+ use crate :: scalar:: DecimalValue ;
5062use 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