@@ -30,8 +30,7 @@ use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValu
30
30
use datafusion_expr:: function:: { AccumulatorArgs , StateFieldsArgs } ;
31
31
use datafusion_expr:: utils:: format_state_name;
32
32
use datafusion_expr:: {
33
- Accumulator , AggregateUDF , AggregateUDFImpl , Expr , ReversedUDAF , Signature ,
34
- Volatility ,
33
+ Accumulator , AggregateUDFImpl , Expr , ReversedUDAF , Signature , Volatility ,
35
34
} ;
36
35
use datafusion_physical_expr_common:: aggregate:: merge_arrays:: merge_ordered_arrays;
37
36
use datafusion_physical_expr_common:: aggregate:: utils:: ordering_fields;
@@ -53,24 +52,15 @@ make_udaf_expr_and_func!(
53
52
#[ derive( Debug ) ]
54
53
pub struct NthValueAgg {
55
54
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 ,
59
55
}
60
56
61
57
impl NthValueAgg {
62
58
/// Create a new `NthValueAgg` aggregate function
63
59
pub fn new ( ) -> Self {
64
60
Self {
65
61
signature : Signature :: any ( 2 , Volatility :: Immutable ) ,
66
- reversed : false ,
67
62
}
68
63
}
69
-
70
- pub fn with_reversed ( mut self , reversed : bool ) -> Self {
71
- self . reversed = reversed;
72
- self
73
- }
74
64
}
75
65
76
66
impl Default for NthValueAgg {
@@ -99,7 +89,7 @@ impl AggregateUDFImpl for NthValueAgg {
99
89
fn accumulator ( & self , acc_args : AccumulatorArgs ) -> Result < Box < dyn Accumulator > > {
100
90
let n = match acc_args. input_exprs [ 1 ] {
101
91
Expr :: Literal ( ScalarValue :: Int64 ( Some ( value) ) ) => {
102
- if self . reversed {
92
+ if acc_args . is_reversed {
103
93
Ok ( -value)
104
94
} else {
105
95
Ok ( value)
@@ -154,9 +144,7 @@ impl AggregateUDFImpl for NthValueAgg {
154
144
}
155
145
156
146
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 ( ) )
160
148
}
161
149
}
162
150
0 commit comments