@@ -5,12 +5,13 @@ use num_traits::PrimInt;
55use rustc_hash:: FxBuildHasher ;
66use vortex_array:: aliases:: hash_map:: HashMap ;
77use vortex_array:: arrays:: PrimitiveArray ;
8+ use vortex_array:: stats:: Stat ;
89use vortex_array:: variants:: PrimitiveArrayTrait ;
910use vortex_array:: { Array , ToCanonical } ;
1011use vortex_dtype:: { NativePType , match_each_integer_ptype} ;
11- use vortex_error:: { VortexExpect , VortexUnwrap } ;
12+ use vortex_error:: { VortexError , VortexExpect , VortexUnwrap } ;
1213use vortex_mask:: AllOr ;
13- use vortex_scalar:: PValue ;
14+ use vortex_scalar:: { PValue , ScalarValue } ;
1415
1516use crate :: sample:: sample;
1617use crate :: { CompressorStats , GenerateStatsOptions } ;
@@ -150,11 +151,9 @@ impl CompressorStats for IntegerStats {
150151 }
151152}
152153
153- fn typed_int_stats < T : NativePType + Hash + PrimInt > (
154- array : & PrimitiveArray ,
155- count_distinct_values : bool ,
156- ) -> IntegerStats
154+ fn typed_int_stats < T > ( array : & PrimitiveArray , count_distinct_values : bool ) -> IntegerStats
157155where
156+ T : NativePType + Hash + PrimInt + for < ' a > TryFrom < & ' a ScalarValue , Error = VortexError > ,
158157 TypedStats < T > : Into < ErasedStats > ,
159158{
160159 // Special case: empty array
@@ -204,8 +203,6 @@ where
204203 let head = buffer[ head_idx] ;
205204
206205 let mut loop_state = LoopState {
207- min : head,
208- max : head,
209206 distinct_values : if count_distinct_values {
210207 HashMap :: with_capacity_and_hasher ( array. len ( ) / 2 , FxBuildHasher )
211208 } else {
@@ -281,9 +278,19 @@ where
281278 u32:: MAX
282279 } ;
283280
281+ let min = array
282+ . statistics ( )
283+ . compute_as :: < T > ( Stat :: Min )
284+ . vortex_expect ( "min should be computed" ) ;
285+
286+ let max = array
287+ . statistics ( )
288+ . compute_as :: < T > ( Stat :: Max )
289+ . vortex_expect ( "max should be computed" ) ;
290+
284291 let typed = TypedStats {
285- min : loop_state . min ,
286- max : loop_state . max ,
292+ min,
293+ max,
287294 distinct_values : loop_state. distinct_values ,
288295 top_value,
289296 top_count,
@@ -307,8 +314,6 @@ where
307314}
308315
309316struct LoopState < T > {
310- min : T ,
311- max : T ,
312317 prev : T ,
313318 runs : u32 ,
314319 distinct_values : HashMap < T , u32 , FxBuildHasher > ,
@@ -321,9 +326,6 @@ fn inner_loop_nonnull<T: PrimInt + Hash>(
321326 state : & mut LoopState < T > ,
322327) {
323328 for & value in values {
324- state. min = state. min . min ( value) ;
325- state. max = state. max . max ( value) ;
326-
327329 if count_distinct_values {
328330 * state. distinct_values . entry ( value) . or_insert ( 0 ) += 1 ;
329331 }
@@ -344,9 +346,6 @@ fn inner_loop_nullable<T: PrimInt + Hash>(
344346) {
345347 for ( idx, & value) in values. iter ( ) . enumerate ( ) {
346348 if is_valid. value ( idx) {
347- state. min = state. min . min ( value) ;
348- state. max = state. max . max ( value) ;
349-
350349 if count_distinct_values {
351350 * state. distinct_values . entry ( value) . or_insert ( 0 ) += 1 ;
352351 }
@@ -368,9 +367,6 @@ fn inner_loop_naive<T: PrimInt + Hash>(
368367) {
369368 for ( idx, & value) in values. iter ( ) . enumerate ( ) {
370369 if is_valid. value ( idx) {
371- state. min = state. min . min ( value) ;
372- state. max = state. max . max ( value) ;
373-
374370 if count_distinct_values {
375371 * state. distinct_values . entry ( value) . or_insert ( 0 ) += 1 ;
376372 }
0 commit comments