@@ -11,6 +11,8 @@ pub mod primitive;
1111mod struct_;
1212mod varbin;
1313
14+ use std:: sync:: LazyLock ;
15+
1416use vortex_error:: VortexExpect ;
1517use vortex_error:: VortexResult ;
1618use vortex_error:: vortex_bail;
@@ -33,9 +35,15 @@ use crate::IntoArray;
3335use crate :: aggregate_fn:: Accumulator ;
3436use crate :: aggregate_fn:: AggregateArgs ;
3537use crate :: aggregate_fn:: AggregateFnId ;
38+ use crate :: aggregate_fn:: AggregateFnRef ;
3639use crate :: aggregate_fn:: AggregateFnVTable ;
40+ use crate :: aggregate_fn:: AggregateFnVTableExt ;
3741use crate :: aggregate_fn:: DynAccumulator ;
3842use crate :: aggregate_fn:: EmptyOptions ;
43+ use crate :: aggregate_fn:: fns:: max:: MAX_SKIP_NANS ;
44+ use crate :: aggregate_fn:: fns:: min:: MIN_SKIP_NANS ;
45+ use crate :: aggregate_fn:: fns:: nan_count:: NAN_COUNT ;
46+ use crate :: aggregate_fn:: fns:: nan_count:: NanCount ;
3947use crate :: arrays:: Constant ;
4048use crate :: arrays:: Null ;
4149use crate :: builtins:: ArrayBuiltins ;
@@ -44,9 +52,6 @@ use crate::dtype::FieldNames;
4452use crate :: dtype:: Nullability ;
4553use crate :: dtype:: StructFields ;
4654use crate :: expr:: stats:: Precision ;
47- use crate :: expr:: stats:: Stat ;
48- use crate :: expr:: stats:: StatsProvider ;
49- use crate :: expr:: stats:: StatsProviderExt ;
5055use crate :: scalar:: Scalar ;
5156use crate :: scalar_fn:: fns:: operators:: Operator ;
5257
@@ -81,17 +86,15 @@ fn arrays_value_equal(a: &ArrayRef, b: &ArrayRef, ctx: &mut ExecutionCtx) -> Vor
8186 Ok ( eq_result. true_count ( ) == valid_count)
8287}
8388
89+ static IS_CONSTANT : LazyLock < AggregateFnRef > = LazyLock :: new ( || IsConstant . bind ( EmptyOptions ) ) ;
90+
8491/// Compute whether an array has constant values.
8592///
86- /// An array is constant IFF at least one of the following conditions apply:
87- /// 1. It has at least one element (**Note** - an empty array isn't constant).
88- /// 2. It's encoded as a [`ConstantArray`](crate::arrays::ConstantArray) or [`NullArray`](crate::arrays::NullArray)
89- /// 3. Has an exact statistic attached to it, saying its constant.
90- /// 4. Is all invalid.
91- /// 5. Is all valid AND has minimum and maximum statistics that are equal.
93+ /// Empty arrays are not constant. Cached results, constant encodings, validity facts, and equal
94+ /// cached extrema can avoid scanning the values.
9295pub fn is_constant ( array : & ArrayRef , ctx : & mut ExecutionCtx ) -> VortexResult < bool > {
93- // Short-circuit using cached array statistics .
94- if let Precision :: Exact ( value) = array. statistics ( ) . get_as :: < bool > ( Stat :: IsConstant ) {
96+ // Short-circuit using the cached aggregate result .
97+ if let Precision :: Exact ( value) = array. aggregations ( ) . get_result_as :: < bool > ( & IS_CONSTANT ) ? {
9598 return Ok ( value) ;
9699 }
97100
@@ -103,24 +106,24 @@ pub fn is_constant(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<boo
103106 // Array of length 1 is always constant.
104107 if array. len ( ) == 1 {
105108 array
106- . statistics ( )
107- . set ( Stat :: IsConstant , Precision :: Exact ( true . into ( ) ) ) ;
109+ . aggregations ( )
110+ . insert_result ( IS_CONSTANT . clone ( ) , Precision :: Exact ( true . into ( ) ) ) ;
108111 return Ok ( true ) ;
109112 }
110113
111114 // Constant and null arrays are always constant.
112115 if array. is :: < Constant > ( ) || array. is :: < Null > ( ) {
113116 array
114- . statistics ( )
115- . set ( Stat :: IsConstant , Precision :: Exact ( true . into ( ) ) ) ;
117+ . aggregations ( )
118+ . insert_result ( IS_CONSTANT . clone ( ) , Precision :: Exact ( true . into ( ) ) ) ;
116119 return Ok ( true ) ;
117120 }
118121
119122 let all_invalid = array. all_invalid ( ctx) ?;
120123 if all_invalid {
121124 array
122- . statistics ( )
123- . set ( Stat :: IsConstant , Precision :: Exact ( true . into ( ) ) ) ;
125+ . aggregations ( )
126+ . insert_result ( IS_CONSTANT . clone ( ) , Precision :: Exact ( true . into ( ) ) ) ;
124127 return Ok ( true ) ;
125128 }
126129
@@ -129,24 +132,26 @@ pub fn is_constant(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<boo
129132 // If we have some nulls but not all nulls, array can't be constant.
130133 if !all_valid && !all_invalid {
131134 array
132- . statistics ( )
133- . set ( Stat :: IsConstant , Precision :: Exact ( false . into ( ) ) ) ;
135+ . aggregations ( )
136+ . insert_result ( IS_CONSTANT . clone ( ) , Precision :: Exact ( false . into ( ) ) ) ;
134137 return Ok ( false ) ;
135138 }
136139
137140 // We already know here that the array is all valid, so we check for min/max stats.
138- let min_stat = array. statistics ( ) . get ( Stat :: Min ) ;
139- let max_stat = array. statistics ( ) . get ( Stat :: Max ) ;
141+ let min_stat = array. aggregations ( ) . get_result ( & MIN_SKIP_NANS ) ;
142+ let max_stat = array. aggregations ( ) . get_result ( & MAX_SKIP_NANS ) ;
140143
141144 if let Precision :: Exact ( min) = min_stat. as_ref ( )
142145 && let Precision :: Exact ( max) = max_stat. as_ref ( )
143146 && min == max
144- && ( Stat :: NaNCount . dtype ( array. dtype ( ) ) . is_none ( )
145- || array. statistics ( ) . get_as :: < u64 > ( Stat :: NaNCount ) == Precision :: exact ( 0u64 ) )
147+ && ( NanCount
148+ . return_dtype ( & EmptyOptions , array. dtype ( ) )
149+ . is_none ( )
150+ || array. aggregations ( ) . get_result_as :: < u64 > ( & NAN_COUNT ) ? == Precision :: exact ( 0u64 ) )
146151 {
147152 array
148- . statistics ( )
149- . set ( Stat :: IsConstant , Precision :: Exact ( true . into ( ) ) ) ;
153+ . aggregations ( )
154+ . insert_result ( IS_CONSTANT . clone ( ) , Precision :: Exact ( true . into ( ) ) ) ;
150155 return Ok ( true ) ;
151156 }
152157
@@ -166,10 +171,10 @@ pub fn is_constant(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<boo
166171
167172 let result = result_scalar. as_bool ( ) . value ( ) . unwrap_or ( false ) ;
168173
169- // Cache the computed is_constant as a statistic .
174+ // Retain the finalized result without retaining the comparison value .
170175 array
171- . statistics ( )
172- . set ( Stat :: IsConstant , Precision :: Exact ( result. into ( ) ) ) ;
176+ . aggregations ( )
177+ . insert_result ( IS_CONSTANT . clone ( ) , Precision :: Exact ( result. into ( ) ) ) ;
173178
174179 Ok ( result)
175180}
@@ -248,8 +253,7 @@ impl IsConstantPartial {
248253 }
249254}
250255
251- static NAMES : std:: sync:: LazyLock < FieldNames > =
252- std:: sync:: LazyLock :: new ( || FieldNames :: from ( [ "is_constant" , "value" ] ) ) ;
256+ static NAMES : LazyLock < FieldNames > = LazyLock :: new ( || FieldNames :: from ( [ "is_constant" , "value" ] ) ) ;
253257
254258pub fn make_is_constant_partial_dtype ( element_dtype : & DType ) -> DType {
255259 DType :: Struct (
0 commit comments