Skip to content

Commit 204e1bc

Browse files
authored
Use AccumulatorArgs::is_reversed in NthValueAgg (#11669)
* Refactor: use `AccumulatorArgs::is_reversed` * Minor: fixes comment
1 parent 01dc3f9 commit 204e1bc

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

datafusion/functions-aggregate/src/nth_value.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValu
3030
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
3131
use datafusion_expr::utils::format_state_name;
3232
use datafusion_expr::{
33-
Accumulator, AggregateUDF, AggregateUDFImpl, Expr, ReversedUDAF, Signature,
34-
Volatility,
33+
Accumulator, AggregateUDFImpl, Expr, ReversedUDAF, Signature, Volatility,
3534
};
3635
use datafusion_physical_expr_common::aggregate::merge_arrays::merge_ordered_arrays;
3736
use datafusion_physical_expr_common::aggregate::utils::ordering_fields;
@@ -53,24 +52,15 @@ make_udaf_expr_and_func!(
5352
#[derive(Debug)]
5453
pub 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

6157
impl 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

7666
impl 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

datafusion/physical-expr-common/src/aggregate/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl AggregateFunctionExpr {
485485
self.ignore_nulls
486486
}
487487

488-
/// Return if the aggregation is distinct
488+
/// Return if the aggregation is reversed
489489
pub fn is_reversed(&self) -> bool {
490490
self.is_reversed
491491
}

0 commit comments

Comments
 (0)