17
17
18
18
//! Defines physical expressions that can evaluated at runtime during query execution
19
19
20
+ use core:: { f32, f64} ;
20
21
use std:: any:: Any ;
21
22
use std:: convert:: TryFrom ;
22
23
use std:: sync:: Arc ;
@@ -111,6 +112,9 @@ impl AggregateExpr for Max {
111
112
) -> arrow:: error:: Result < Option < Box < dyn GroupsAccumulator > > > {
112
113
macro_rules! make_max_accumulator {
113
114
( $T: ty) => {
115
+ make_max_accumulator!( $T, <$T as ArrowPrimitiveType >:: Native :: MIN )
116
+ } ;
117
+ ( $T: ty, $STARTING_VALUE: expr) => {
114
118
Box :: new(
115
119
PrimitiveGroupsAccumulator :: <$T, $T, _, _>:: new(
116
120
& <$T as ArrowPrimitiveType >:: DATA_TYPE ,
@@ -123,13 +127,17 @@ impl AggregateExpr for Max {
123
127
* x = ( * x) . max( y) ;
124
128
} ,
125
129
)
126
- . with_starting_value( <$T as ArrowPrimitiveType > :: Native :: MIN ) ,
130
+ . with_starting_value( $STARTING_VALUE ) ,
127
131
)
128
132
} ;
129
133
}
130
134
let acc: Box < dyn GroupsAccumulator > = match & self . data_type {
131
- DataType :: Float64 => make_max_accumulator ! ( arrow:: datatypes:: Float64Type ) ,
132
- DataType :: Float32 => make_max_accumulator ! ( arrow:: datatypes:: Float32Type ) ,
135
+ DataType :: Float64 => {
136
+ make_max_accumulator ! ( arrow:: datatypes:: Float64Type , f64 :: NEG_INFINITY )
137
+ }
138
+ DataType :: Float32 => {
139
+ make_max_accumulator ! ( arrow:: datatypes:: Float32Type , f32 :: NEG_INFINITY )
140
+ }
133
141
DataType :: Int64 => make_max_accumulator ! ( arrow:: datatypes:: Int64Type ) ,
134
142
DataType :: Int96 => make_max_accumulator ! ( arrow:: datatypes:: Int96Type ) ,
135
143
DataType :: Int64Decimal ( 0 ) => {
@@ -628,6 +636,9 @@ impl AggregateExpr for Min {
628
636
) -> arrow:: error:: Result < Option < Box < dyn GroupsAccumulator > > > {
629
637
macro_rules! make_min_accumulator {
630
638
( $T: ty) => {
639
+ make_min_accumulator!( $T, <$T as ArrowPrimitiveType >:: Native :: MAX )
640
+ } ;
641
+ ( $T: ty, $STARTING_VALUE: expr) => {
631
642
Box :: new(
632
643
PrimitiveGroupsAccumulator :: <$T, $T, _, _>:: new(
633
644
& <$T as ArrowPrimitiveType >:: DATA_TYPE ,
@@ -640,14 +651,18 @@ impl AggregateExpr for Min {
640
651
* x = ( * x) . min( y) ;
641
652
} ,
642
653
)
643
- . with_starting_value( <$T as ArrowPrimitiveType > :: Native :: MAX ) ,
654
+ . with_starting_value( $STARTING_VALUE ) ,
644
655
)
645
656
} ;
646
657
}
647
658
648
659
let acc: Box < dyn GroupsAccumulator > = match & self . data_type {
649
- DataType :: Float64 => make_min_accumulator ! ( arrow:: datatypes:: Float64Type ) ,
650
- DataType :: Float32 => make_min_accumulator ! ( arrow:: datatypes:: Float32Type ) ,
660
+ DataType :: Float64 => {
661
+ make_min_accumulator ! ( arrow:: datatypes:: Float64Type , f64 :: INFINITY )
662
+ }
663
+ DataType :: Float32 => {
664
+ make_min_accumulator ! ( arrow:: datatypes:: Float32Type , f32 :: INFINITY )
665
+ }
651
666
DataType :: Int64 => make_min_accumulator ! ( arrow:: datatypes:: Int64Type ) ,
652
667
DataType :: Int96 => make_min_accumulator ! ( arrow:: datatypes:: Int96Type ) ,
653
668
DataType :: Int64Decimal ( 0 ) => {
@@ -770,9 +785,13 @@ impl Accumulator for MinAccumulator {
770
785
771
786
#[ cfg( test) ]
772
787
mod tests {
788
+ use core:: f64;
789
+
773
790
use super :: * ;
791
+ use crate :: generic_grouped_test_op;
774
792
use crate :: physical_plan:: expressions:: col;
775
793
use crate :: physical_plan:: expressions:: tests:: aggregate;
794
+ use crate :: physical_plan:: expressions:: tests:: grouped_aggregate;
776
795
use crate :: { error:: Result , generic_test_op} ;
777
796
use arrow:: datatypes:: * ;
778
797
use arrow:: record_batch:: RecordBatch ;
@@ -974,6 +993,30 @@ mod tests {
974
993
)
975
994
}
976
995
996
+ #[ test]
997
+ fn max_f64_infinity ( ) -> Result < ( ) > {
998
+ let a: ArrayRef = Arc :: new ( Float64Array :: from ( vec ! [ f64 :: NEG_INFINITY ] ) ) ;
999
+ generic_test_op ! (
1000
+ a,
1001
+ DataType :: Float64 ,
1002
+ Max ,
1003
+ ScalarValue :: from( f64 :: NEG_INFINITY ) ,
1004
+ DataType :: Float64
1005
+ )
1006
+ }
1007
+
1008
+ #[ test]
1009
+ fn max_f64_infinity_grouped ( ) -> Result < ( ) > {
1010
+ let a: ArrayRef = Arc :: new ( Float64Array :: from ( vec ! [ f64 :: NEG_INFINITY ] ) ) ;
1011
+ generic_grouped_test_op ! (
1012
+ a,
1013
+ DataType :: Float64 ,
1014
+ Max ,
1015
+ ScalarValue :: from( f64 :: NEG_INFINITY ) ,
1016
+ DataType :: Float64
1017
+ )
1018
+ }
1019
+
977
1020
#[ test]
978
1021
fn min_f64 ( ) -> Result < ( ) > {
979
1022
let a: ArrayRef =
@@ -986,4 +1029,28 @@ mod tests {
986
1029
DataType :: Float64
987
1030
)
988
1031
}
1032
+
1033
+ #[ test]
1034
+ fn min_f64_infinity ( ) -> Result < ( ) > {
1035
+ let a: ArrayRef = Arc :: new ( Float64Array :: from ( vec ! [ f64 :: INFINITY ] ) ) ;
1036
+ generic_test_op ! (
1037
+ a,
1038
+ DataType :: Float64 ,
1039
+ Min ,
1040
+ ScalarValue :: from( f64 :: INFINITY ) ,
1041
+ DataType :: Float64
1042
+ )
1043
+ }
1044
+
1045
+ #[ test]
1046
+ fn min_f64_infinity_grouped ( ) -> Result < ( ) > {
1047
+ let a: ArrayRef = Arc :: new ( Float64Array :: from ( vec ! [ f64 :: INFINITY ] ) ) ;
1048
+ generic_grouped_test_op ! (
1049
+ a,
1050
+ DataType :: Float64 ,
1051
+ Min ,
1052
+ ScalarValue :: from( f64 :: INFINITY ) ,
1053
+ DataType :: Float64
1054
+ )
1055
+ }
989
1056
}
0 commit comments