@@ -30,8 +30,7 @@ use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValu
3030use datafusion_expr:: function:: { AccumulatorArgs , StateFieldsArgs } ;
3131use datafusion_expr:: utils:: format_state_name;
3232use datafusion_expr:: {
33- Accumulator , AggregateUDF , AggregateUDFImpl , Expr , ReversedUDAF , Signature ,
34- Volatility ,
33+ Accumulator , AggregateUDFImpl , Expr , ReversedUDAF , Signature , Volatility ,
3534} ;
3635use datafusion_physical_expr_common:: aggregate:: merge_arrays:: merge_ordered_arrays;
3736use datafusion_physical_expr_common:: aggregate:: utils:: ordering_fields;
@@ -53,24 +52,15 @@ make_udaf_expr_and_func!(
5352#[ derive( Debug ) ]
5453pub struct NthValueAgg {
5554 signature : Signature ,
56- /// Determines whether `N` is relative to the beginning or the end
57- /// of the aggregation. When set to `true`, then `N` is from the end.
58- reversed : bool ,
5955}
6056
6157impl NthValueAgg {
6258 /// Create a new `NthValueAgg` aggregate function
6359 pub fn new ( ) -> Self {
6460 Self {
6561 signature : Signature :: any ( 2 , Volatility :: Immutable ) ,
66- reversed : false ,
6762 }
6863 }
69-
70- pub fn with_reversed ( mut self , reversed : bool ) -> Self {
71- self . reversed = reversed;
72- self
73- }
7464}
7565
7666impl Default for NthValueAgg {
@@ -99,7 +89,7 @@ impl AggregateUDFImpl for NthValueAgg {
9989 fn accumulator ( & self , acc_args : AccumulatorArgs ) -> Result < Box < dyn Accumulator > > {
10090 let n = match acc_args. input_exprs [ 1 ] {
10191 Expr :: Literal ( ScalarValue :: Int64 ( Some ( value) ) ) => {
102- if self . reversed {
92+ if acc_args . is_reversed {
10393 Ok ( -value)
10494 } else {
10595 Ok ( value)
@@ -154,9 +144,7 @@ impl AggregateUDFImpl for NthValueAgg {
154144 }
155145
156146 fn reverse_expr ( & self ) -> ReversedUDAF {
157- ReversedUDAF :: Reversed ( Arc :: from ( AggregateUDF :: from (
158- Self :: new ( ) . with_reversed ( !self . reversed ) ,
159- ) ) )
147+ ReversedUDAF :: Reversed ( nth_value_udaf ( ) )
160148 }
161149}
162150
0 commit comments