22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
44use std:: sync:: Arc ;
5+ use std:: sync:: LazyLock ;
56
67use vortex_array:: stats:: rewrite:: falsify;
78use vortex_error:: VortexExpect ;
@@ -15,6 +16,14 @@ use crate::aggregate_fn::fns::all_nan::AllNan;
1516use crate :: aggregate_fn:: fns:: all_non_nan:: AllNonNan ;
1617use crate :: aggregate_fn:: fns:: all_non_null:: AllNonNull ;
1718use crate :: aggregate_fn:: fns:: all_null:: AllNull ;
19+ use crate :: aggregate_fn:: fns:: max:: MAX_SKIP_NANS ;
20+ use crate :: aggregate_fn:: fns:: max:: Max ;
21+ use crate :: aggregate_fn:: fns:: min:: MIN_SKIP_NANS ;
22+ use crate :: aggregate_fn:: fns:: min:: Min ;
23+ use crate :: aggregate_fn:: fns:: nan_count:: NAN_COUNT ;
24+ use crate :: aggregate_fn:: fns:: nan_count:: NanCount ;
25+ use crate :: aggregate_fn:: fns:: null_count:: NULL_COUNT ;
26+ use crate :: aggregate_fn:: fns:: null_count:: NullCount ;
1827use crate :: dtype:: DType ;
1928use crate :: expr:: BoundExpression ;
2029use crate :: expr:: bound:: and;
@@ -30,7 +39,6 @@ use crate::expr::bound::lt;
3039use crate :: expr:: bound:: lt_eq;
3140use crate :: expr:: bound:: or;
3241use crate :: expr:: bound:: or_collect;
33- use crate :: expr:: stats:: Stat ;
3442use crate :: scalar:: StringLike ;
3543use crate :: scalar_fn:: EmptyOptions ;
3644use crate :: scalar_fn:: ScalarFnId ;
@@ -55,6 +63,13 @@ use crate::stats::bound::stat;
5563use crate :: stats:: rewrite:: StatsRewriteRule ;
5664use crate :: stats:: session:: StatsSession ;
5765
66+ static ALL_NULL : LazyLock < AggregateFnRef > = LazyLock :: new ( || AllNull . bind ( AggregateEmptyOptions ) ) ;
67+ static ALL_NON_NULL : LazyLock < AggregateFnRef > =
68+ LazyLock :: new ( || AllNonNull . bind ( AggregateEmptyOptions ) ) ;
69+ static ALL_NAN : LazyLock < AggregateFnRef > = LazyLock :: new ( || AllNan . bind ( AggregateEmptyOptions ) ) ;
70+ static ALL_NON_NAN : LazyLock < AggregateFnRef > =
71+ LazyLock :: new ( || AllNonNan . bind ( AggregateEmptyOptions ) ) ;
72+
5873/// Register built-in stats rewrite rules.
5974pub ( crate ) fn register_builtins ( session : & StatsSession ) {
6075 session. register_rewrite ( BinaryNanCountStatsRewrite ) ;
@@ -523,7 +538,7 @@ fn list_contains_falsify<P: NonNanProof>(
523538 let list = expr. child ( 0 ) ;
524539 let needle = expr. child ( 1 ) ;
525540
526- let Some ( list_scalar) = literal_stat ( list, Stat :: Min ) else {
541+ let Some ( list_scalar) = literal_stat ( list, & MIN_SKIP_NANS ) else {
527542 return Ok ( None ) ;
528543 } ;
529544 let elements = list_scalar
@@ -618,35 +633,35 @@ fn dynamic_comparison_falsify<P: NonNanProof>(
618633}
619634
620635fn min ( expr : & BoundExpression ) -> Option < BoundExpression > {
621- stat_expr ( expr, Stat :: Min )
636+ stat_expr ( expr, & MIN_SKIP_NANS )
622637}
623638
624639fn max ( expr : & BoundExpression ) -> Option < BoundExpression > {
625- stat_expr ( expr, Stat :: Max )
640+ stat_expr ( expr, & MAX_SKIP_NANS )
626641}
627642
628643fn null_count ( expr : & BoundExpression ) -> Option < BoundExpression > {
629- stat_expr ( expr, Stat :: NullCount )
644+ stat_expr ( expr, & NULL_COUNT )
630645}
631646
632647fn nan_count ( expr : & BoundExpression ) -> Option < BoundExpression > {
633- stat_expr ( expr, Stat :: NaNCount )
648+ stat_expr ( expr, & NAN_COUNT )
634649}
635650
636651fn all_null ( expr : & BoundExpression ) -> BoundExpression {
637- stat_fn ( expr. clone ( ) , AllNull . bind ( AggregateEmptyOptions ) )
652+ stat_fn ( expr. clone ( ) , ALL_NULL . clone ( ) )
638653}
639654
640655fn all_non_null ( expr : & BoundExpression ) -> BoundExpression {
641- stat_fn ( expr. clone ( ) , AllNonNull . bind ( AggregateEmptyOptions ) )
656+ stat_fn ( expr. clone ( ) , ALL_NON_NULL . clone ( ) )
642657}
643658
644659fn all_nan ( expr : & BoundExpression ) -> BoundExpression {
645- stat_fn ( expr. clone ( ) , AllNan . bind ( AggregateEmptyOptions ) )
660+ stat_fn ( expr. clone ( ) , ALL_NAN . clone ( ) )
646661}
647662
648663fn all_non_nan ( expr : & BoundExpression ) -> BoundExpression {
649- stat_fn ( expr. clone ( ) , AllNonNan . bind ( AggregateEmptyOptions ) )
664+ stat_fn ( expr. clone ( ) , ALL_NON_NAN . clone ( ) )
650665}
651666
652667enum NanCheck {
@@ -667,7 +682,7 @@ impl NonNanProof for NanCountProof {
667682 const EMIT_UNGUARDED_REWRITES : bool = true ;
668683
669684 fn check ( expr : & BoundExpression ) -> VortexResult < NanCheck > {
670- non_nan_check ( expr, |expr| match stat_expr ( expr, Stat :: NaNCount ) {
685+ non_nan_check ( expr, |expr| match stat_expr ( expr, & NAN_COUNT ) {
671686 Some ( nan_count) => NanCheck :: Check ( eq ( nan_count, lit ( 0u64 ) ) ) ,
672687 None => NanCheck :: Unavailable ,
673688 } )
@@ -681,7 +696,7 @@ impl NonNanProof for AllNonNanProof {
681696
682697 fn check ( expr : & BoundExpression ) -> VortexResult < NanCheck > {
683698 non_nan_check ( expr, |expr| {
684- NanCheck :: Check ( stat_fn ( expr. clone ( ) , AllNonNan . bind ( AggregateEmptyOptions ) ) )
699+ NanCheck :: Check ( stat_fn ( expr. clone ( ) , ALL_NON_NAN . clone ( ) ) )
685700 } )
686701 }
687702}
@@ -723,8 +738,8 @@ fn has_nans(dtype: &DType) -> bool {
723738 dtype. is_float ( )
724739}
725740
726- fn stat_expr ( expr : & BoundExpression , stat : Stat ) -> Option < BoundExpression > {
727- if let Some ( literal) = literal_stat ( expr, stat ) {
741+ fn stat_expr ( expr : & BoundExpression , aggregate : & AggregateFnRef ) -> Option < BoundExpression > {
742+ if let Some ( literal) = literal_stat ( expr, aggregate ) {
728743 return Some ( literal) ;
729744 }
730745
@@ -736,17 +751,16 @@ fn stat_expr(expr: &BoundExpression, stat: Stat) -> Option<BoundExpression> {
736751 }
737752
738753 if let Some ( dtype) = expr. as_opt :: < Cast > ( ) {
739- return cast_stat ( expr. child ( 0 ) , dtype, stat ) ;
754+ return cast_stat ( expr. child ( 0 ) , dtype, aggregate ) ;
740755 }
741756
742- let aggregate_fn = stat. aggregate_fn ( ) ?;
743757 // The aggregate may not support the expression's dtype, e.g. min/max over structs,
744758 // even when the predicate itself is well-typed. Such stats cannot be lowered later,
745759 // so do not reference them in the rewrite.
746- aggregate_fn
760+ aggregate
747761 . return_dtype ( expr. dtype ( ) )
748762 . is_some ( )
749- . then ( || stat_fn ( expr. clone ( ) , aggregate_fn ) )
763+ . then ( || stat_fn ( expr. clone ( ) , aggregate . clone ( ) ) )
750764}
751765
752766fn with_non_nan_guards < ' a , P : NonNanProof > (
@@ -773,32 +787,34 @@ fn with_non_nan_guards<'a, P: NonNanProof>(
773787 } )
774788}
775789
776- fn literal_stat ( expr : & BoundExpression , stat : Stat ) -> Option < BoundExpression > {
790+ fn literal_stat ( expr : & BoundExpression , aggregate : & AggregateFnRef ) -> Option < BoundExpression > {
777791 let scalar = expr. as_opt :: < Literal > ( ) ?;
778- match stat {
779- Stat :: Min | Stat :: Max => Some ( lit ( scalar. clone ( ) ) ) ,
780- Stat :: NullCount => Some ( lit ( if scalar . is_null ( ) { 1u64 } else { 0u64 } ) ) ,
781- Stat :: NaNCount => {
782- let value = scalar . as_primitive_opt ( ) ? ;
783- if !value . ptype ( ) . is_float ( ) {
784- return None ;
785- }
786-
787- Some ( lit ( if value. is_nan ( ) { 1u64 } else { 0u64 } ) )
792+ if aggregate . is :: < Min > ( ) || aggregate . is :: < Max > ( ) {
793+ return Some ( lit ( scalar. clone ( ) ) ) ;
794+ }
795+ if aggregate . is :: < NullCount > ( ) {
796+ return Some ( lit ( if scalar . is_null ( ) { 1u64 } else { 0u64 } ) ) ;
797+ }
798+ if aggregate . is :: < NanCount > ( ) {
799+ let value = scalar . as_primitive_opt ( ) ? ;
800+ if value . ptype ( ) . is_float ( ) {
801+ return Some ( lit ( if value. is_nan ( ) { 1u64 } else { 0u64 } ) ) ;
788802 }
789- Stat :: IsConstant
790- | Stat :: IsSorted
791- | Stat :: IsStrictSorted
792- | Stat :: Sum
793- | Stat :: UncompressedSizeInBytes => None ,
794803 }
804+ None
795805}
796806
797- fn cast_stat ( expr : & BoundExpression , dtype : & DType , stat : Stat ) -> Option < BoundExpression > {
798- match stat {
799- Stat :: Min | Stat :: Max => stat_expr ( expr, stat) . map ( |stat| cast ( stat, dtype. clone ( ) ) ) ,
800- Stat :: NaNCount | Stat :: Sum | Stat :: UncompressedSizeInBytes => stat_expr ( expr, stat) ,
801- Stat :: NullCount | Stat :: IsConstant | Stat :: IsSorted | Stat :: IsStrictSorted => None ,
807+ fn cast_stat (
808+ expr : & BoundExpression ,
809+ dtype : & DType ,
810+ aggregate : & AggregateFnRef ,
811+ ) -> Option < BoundExpression > {
812+ if aggregate. is :: < Min > ( ) || aggregate. is :: < Max > ( ) {
813+ stat_expr ( expr, aggregate) . map ( |stat| cast ( stat, dtype. clone ( ) ) )
814+ } else if aggregate. is :: < NanCount > ( ) {
815+ stat_expr ( expr, aggregate)
816+ } else {
817+ None
802818 }
803819}
804820
0 commit comments