@@ -39,12 +39,16 @@ use datafusion_common::ScalarValue;
3939use datafusion_common:: { downcast_value, DataFusionError , Result } ;
4040use datafusion_expr:: Accumulator ;
4141
42- use crate :: aggregate:: row_accumulator:: RowAccumulator ;
42+ use crate :: aggregate:: row_accumulator:: {
43+ is_row_accumulator_support_dtype, RowAccumulator ,
44+ } ;
4345use crate :: expressions:: format_state_name;
4446use arrow:: array:: Array ;
4547use arrow:: array:: Decimal128Array ;
4648use datafusion_row:: accessor:: RowAccessor ;
4749
50+ use super :: moving_min_max;
51+
4852// Min/max aggregation can take Dictionary encode input but always produces unpacked
4953// (aka non Dictionary) output. We need to adjust the output data type to reflect this.
5054// The reason min/max aggregate produces unpacked output because there is only one
@@ -117,19 +121,7 @@ impl AggregateExpr for Max {
117121 }
118122
119123 fn row_accumulator_supported ( & self ) -> bool {
120- matches ! (
121- self . data_type,
122- DataType :: UInt8
123- | DataType :: UInt16
124- | DataType :: UInt32
125- | DataType :: UInt64
126- | DataType :: Int8
127- | DataType :: Int16
128- | DataType :: Int32
129- | DataType :: Int64
130- | DataType :: Float32
131- | DataType :: Float64
132- )
124+ is_row_accumulator_support_dtype ( & self . data_type )
133125 }
134126
135127 fn create_row_accumulator (
@@ -141,6 +133,10 @@ impl AggregateExpr for Max {
141133 self . data_type . clone ( ) ,
142134 ) ) )
143135 }
136+
137+ fn create_sliding_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
138+ Ok ( Box :: new ( SlidingMaxAccumulator :: try_new ( & self . data_type ) ?) )
139+ }
144140}
145141
146142// Statically-typed version of min/max(array) -> ScalarValue for string types.
@@ -577,6 +573,62 @@ impl Accumulator for MaxAccumulator {
577573 }
578574}
579575
576+ /// An accumulator to compute the maximum value
577+ #[ derive( Debug ) ]
578+ pub struct SlidingMaxAccumulator {
579+ max : ScalarValue ,
580+ moving_max : moving_min_max:: MovingMax < ScalarValue > ,
581+ }
582+
583+ impl SlidingMaxAccumulator {
584+ /// new max accumulator
585+ pub fn try_new ( datatype : & DataType ) -> Result < Self > {
586+ Ok ( Self {
587+ max : ScalarValue :: try_from ( datatype) ?,
588+ moving_max : moving_min_max:: MovingMax :: < ScalarValue > :: new ( ) ,
589+ } )
590+ }
591+ }
592+
593+ impl Accumulator for SlidingMaxAccumulator {
594+ fn update_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
595+ for idx in 0 ..values[ 0 ] . len ( ) {
596+ let val = ScalarValue :: try_from_array ( & values[ 0 ] , idx) ?;
597+ self . moving_max . push ( val) ;
598+ }
599+ if let Some ( res) = self . moving_max . max ( ) {
600+ self . max = res. clone ( ) ;
601+ }
602+ Ok ( ( ) )
603+ }
604+
605+ fn retract_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
606+ for _idx in 0 ..values[ 0 ] . len ( ) {
607+ ( self . moving_max ) . pop ( ) ;
608+ }
609+ if let Some ( res) = self . moving_max . max ( ) {
610+ self . max = res. clone ( ) ;
611+ }
612+ Ok ( ( ) )
613+ }
614+
615+ fn merge_batch ( & mut self , states : & [ ArrayRef ] ) -> Result < ( ) > {
616+ self . update_batch ( states)
617+ }
618+
619+ fn state ( & self ) -> Result < Vec < ScalarValue > > {
620+ Ok ( vec ! [ self . max. clone( ) ] )
621+ }
622+
623+ fn evaluate ( & self ) -> Result < ScalarValue > {
624+ Ok ( self . max . clone ( ) )
625+ }
626+
627+ fn size ( & self ) -> usize {
628+ std:: mem:: size_of_val ( self ) - std:: mem:: size_of_val ( & self . max ) + self . max . size ( )
629+ }
630+ }
631+
580632#[ derive( Debug ) ]
581633struct MaxRowAccumulator {
582634 index : usize ,
@@ -679,19 +731,7 @@ impl AggregateExpr for Min {
679731 }
680732
681733 fn row_accumulator_supported ( & self ) -> bool {
682- matches ! (
683- self . data_type,
684- DataType :: UInt8
685- | DataType :: UInt16
686- | DataType :: UInt32
687- | DataType :: UInt64
688- | DataType :: Int8
689- | DataType :: Int16
690- | DataType :: Int32
691- | DataType :: Int64
692- | DataType :: Float32
693- | DataType :: Float64
694- )
734+ is_row_accumulator_support_dtype ( & self . data_type )
695735 }
696736
697737 fn create_row_accumulator (
@@ -703,6 +743,10 @@ impl AggregateExpr for Min {
703743 self . data_type . clone ( ) ,
704744 ) ) )
705745 }
746+
747+ fn create_sliding_accumulator ( & self ) -> Result < Box < dyn Accumulator > > {
748+ Ok ( Box :: new ( SlidingMinAccumulator :: try_new ( & self . data_type ) ?) )
749+ }
706750}
707751
708752/// An accumulator to compute the minimum value
@@ -745,6 +789,67 @@ impl Accumulator for MinAccumulator {
745789 }
746790}
747791
792+ /// An accumulator to compute the minimum value
793+ #[ derive( Debug ) ]
794+ pub struct SlidingMinAccumulator {
795+ min : ScalarValue ,
796+ moving_min : moving_min_max:: MovingMin < ScalarValue > ,
797+ }
798+
799+ impl SlidingMinAccumulator {
800+ /// new min accumulator
801+ pub fn try_new ( datatype : & DataType ) -> Result < Self > {
802+ Ok ( Self {
803+ min : ScalarValue :: try_from ( datatype) ?,
804+ moving_min : moving_min_max:: MovingMin :: < ScalarValue > :: new ( ) ,
805+ } )
806+ }
807+ }
808+
809+ impl Accumulator for SlidingMinAccumulator {
810+ fn state ( & self ) -> Result < Vec < ScalarValue > > {
811+ Ok ( vec ! [ self . min. clone( ) ] )
812+ }
813+
814+ fn update_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
815+ for idx in 0 ..values[ 0 ] . len ( ) {
816+ let val = ScalarValue :: try_from_array ( & values[ 0 ] , idx) ?;
817+ if !val. is_null ( ) {
818+ self . moving_min . push ( val) ;
819+ }
820+ }
821+ if let Some ( res) = self . moving_min . min ( ) {
822+ self . min = res. clone ( ) ;
823+ }
824+ Ok ( ( ) )
825+ }
826+
827+ fn retract_batch ( & mut self , values : & [ ArrayRef ] ) -> Result < ( ) > {
828+ for idx in 0 ..values[ 0 ] . len ( ) {
829+ let val = ScalarValue :: try_from_array ( & values[ 0 ] , idx) ?;
830+ if !val. is_null ( ) {
831+ ( self . moving_min ) . pop ( ) ;
832+ }
833+ }
834+ if let Some ( res) = self . moving_min . min ( ) {
835+ self . min = res. clone ( ) ;
836+ }
837+ Ok ( ( ) )
838+ }
839+
840+ fn merge_batch ( & mut self , states : & [ ArrayRef ] ) -> Result < ( ) > {
841+ self . update_batch ( states)
842+ }
843+
844+ fn evaluate ( & self ) -> Result < ScalarValue > {
845+ Ok ( self . min . clone ( ) )
846+ }
847+
848+ fn size ( & self ) -> usize {
849+ std:: mem:: size_of_val ( self ) - std:: mem:: size_of_val ( & self . min ) + self . min . size ( )
850+ }
851+ }
852+
748853#[ derive( Debug ) ]
749854struct MinRowAccumulator {
750855 index : usize ,
0 commit comments