1717
1818//! Defines physical expressions that can evaluated at runtime during query execution
1919
20+ use core:: { f32, f64} ;
2021use std:: any:: Any ;
2122use std:: convert:: TryFrom ;
2223use std:: sync:: Arc ;
@@ -111,6 +112,9 @@ impl AggregateExpr for Max {
111112 ) -> arrow:: error:: Result < Option < Box < dyn GroupsAccumulator > > > {
112113 macro_rules! make_max_accumulator {
113114 ( $T: ty) => {
115+ make_max_accumulator!( $T, <$T as ArrowPrimitiveType >:: Native :: MIN )
116+ } ;
117+ ( $T: ty, $STARTING_VALUE: expr) => {
114118 Box :: new(
115119 PrimitiveGroupsAccumulator :: <$T, $T, _, _>:: new(
116120 & <$T as ArrowPrimitiveType >:: DATA_TYPE ,
@@ -123,13 +127,17 @@ impl AggregateExpr for Max {
123127 * x = ( * x) . max( y) ;
124128 } ,
125129 )
126- . with_starting_value( <$T as ArrowPrimitiveType > :: Native :: MIN ) ,
130+ . with_starting_value( $STARTING_VALUE ) ,
127131 )
128132 } ;
129133 }
130134 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+ }
133141 DataType :: Int64 => make_max_accumulator ! ( arrow:: datatypes:: Int64Type ) ,
134142 DataType :: Int96 => make_max_accumulator ! ( arrow:: datatypes:: Int96Type ) ,
135143 DataType :: Int64Decimal ( 0 ) => {
@@ -628,6 +636,9 @@ impl AggregateExpr for Min {
628636 ) -> arrow:: error:: Result < Option < Box < dyn GroupsAccumulator > > > {
629637 macro_rules! make_min_accumulator {
630638 ( $T: ty) => {
639+ make_min_accumulator!( $T, <$T as ArrowPrimitiveType >:: Native :: MAX )
640+ } ;
641+ ( $T: ty, $STARTING_VALUE: expr) => {
631642 Box :: new(
632643 PrimitiveGroupsAccumulator :: <$T, $T, _, _>:: new(
633644 & <$T as ArrowPrimitiveType >:: DATA_TYPE ,
@@ -640,14 +651,18 @@ impl AggregateExpr for Min {
640651 * x = ( * x) . min( y) ;
641652 } ,
642653 )
643- . with_starting_value( <$T as ArrowPrimitiveType > :: Native :: MAX ) ,
654+ . with_starting_value( $STARTING_VALUE ) ,
644655 )
645656 } ;
646657 }
647658
648659 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+ }
651666 DataType :: Int64 => make_min_accumulator ! ( arrow:: datatypes:: Int64Type ) ,
652667 DataType :: Int96 => make_min_accumulator ! ( arrow:: datatypes:: Int96Type ) ,
653668 DataType :: Int64Decimal ( 0 ) => {
@@ -770,9 +785,13 @@ impl Accumulator for MinAccumulator {
770785
771786#[ cfg( test) ]
772787mod tests {
788+ use core:: f64;
789+
773790 use super :: * ;
791+ use crate :: generic_grouped_test_op;
774792 use crate :: physical_plan:: expressions:: col;
775793 use crate :: physical_plan:: expressions:: tests:: aggregate;
794+ use crate :: physical_plan:: expressions:: tests:: grouped_aggregate;
776795 use crate :: { error:: Result , generic_test_op} ;
777796 use arrow:: datatypes:: * ;
778797 use arrow:: record_batch:: RecordBatch ;
@@ -974,6 +993,30 @@ mod tests {
974993 )
975994 }
976995
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+
9771020 #[ test]
9781021 fn min_f64 ( ) -> Result < ( ) > {
9791022 let a: ArrayRef =
@@ -986,4 +1029,28 @@ mod tests {
9861029 DataType :: Float64
9871030 )
9881031 }
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+ }
9891056}
0 commit comments