Skip to content

Use AccumulatorArgs::is_reversed in NthValueAgg #11669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions datafusion/functions-aggregate/src/nth_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValu
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
use datafusion_expr::utils::format_state_name;
use datafusion_expr::{
Accumulator, AggregateUDF, AggregateUDFImpl, Expr, ReversedUDAF, Signature,
Volatility,
Accumulator, AggregateUDFImpl, Expr, ReversedUDAF, Signature, Volatility,
};
use datafusion_physical_expr_common::aggregate::merge_arrays::merge_ordered_arrays;
use datafusion_physical_expr_common::aggregate::utils::ordering_fields;
Expand All @@ -53,24 +52,15 @@ make_udaf_expr_and_func!(
#[derive(Debug)]
pub struct NthValueAgg {
signature: Signature,
/// Determines whether `N` is relative to the beginning or the end
/// of the aggregation. When set to `true`, then `N` is from the end.
reversed: bool,
}

impl NthValueAgg {
/// Create a new `NthValueAgg` aggregate function
pub fn new() -> Self {
Self {
signature: Signature::any(2, Volatility::Immutable),
reversed: false,
}
}

pub fn with_reversed(mut self, reversed: bool) -> Self {
self.reversed = reversed;
self
}
}

impl Default for NthValueAgg {
Expand Down Expand Up @@ -99,7 +89,7 @@ impl AggregateUDFImpl for NthValueAgg {
fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
let n = match acc_args.input_exprs[1] {
Expr::Literal(ScalarValue::Int64(Some(value))) => {
if self.reversed {
if acc_args.is_reversed {
Ok(-value)
} else {
Ok(value)
Expand Down Expand Up @@ -157,9 +147,7 @@ impl AggregateUDFImpl for NthValueAgg {
}

fn reverse_expr(&self) -> ReversedUDAF {
ReversedUDAF::Reversed(Arc::from(AggregateUDF::from(
Self::new().with_reversed(!self.reversed),
)))
ReversedUDAF::Reversed(nth_value_udaf())
}
}

Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-expr-common/src/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ impl AggregateFunctionExpr {
self.ignore_nulls
}

/// Return if the aggregation is distinct
/// Return if the aggregation is reversed
pub fn is_reversed(&self) -> bool {
self.is_reversed
}
Expand Down