Apache DataFusion SQL Query Engine
fix: forward `create_sliding_accumulator` to the inner UDAF when aliased (#26480)
Cheng-Yi Shih673b64971de0
diff --git a/datafusion/core/tests/user_defined/user_defined_aggregates.rs b/datafusion/core/tests/user_defined/user_defined_aggregates.rsindex 0f153ebbd3..9b9e173947 100644--- a/datafusion/core/tests/user_defined/user_defined_aggregates.rs+++ b/datafusion/core/tests/user_defined/user_defined_aggregates.rs@@ -59,6 +59,8 @@ use datafusion_expr::{WindowFunctionDefinition, col, create_udaf, function::AccumulatorArgs,};use datafusion_functions_aggregate::average::AvgAccumulator;+use datafusion_functions_aggregate::min_max::{max_udaf, min_udaf};+use datafusion_functions_aggregate::sum::sum_udaf;/// Test to show the contents of the setup#[tokio::test]@@ -396,6 +398,114 @@ async fn test_user_defined_functions_with_alias() -> Result<()> {Ok(())}+/// Creates a context with table `t` (single `Int32` column `i` holding 1, 2, 3, 4).+fn context_with_int_table() -> Result<SessionContext> {+ let ctx = SessionContext::new();+ let arr = Int32Array::from(vec![1, 2, 3, 4]);+ let batch = RecordBatch::try_from_iter(vec![("i", Arc::new(arr) as _)])?;+ ctx.register_batch("t", batch)?;+ Ok(ctx)+}++/// An aliased aggregate over a sliding frame must still get the inner+/// function's sliding accumulator. Windows are [1], [1,2], [2,3], [3,4].+#[tokio::test]+async fn test_aliased_udaf_as_window_with_frame() -> Result<()> {+ let ctx = context_with_int_table()?;+ ctx.register_udaf(sum_udaf().as_ref().clone().with_aliases(["my_sum"]));+ ctx.register_udaf(min_udaf().as_ref().clone().with_aliases(["my_min"]));+ ctx.register_udaf(max_udaf().as_ref().clone().with_aliases(["my_max"]));++ let frame = "OVER (ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)";++ let sql = format!("SELECT my_sum(i) {frame} AS r FROM t");+ let result = plan_and_collect(&ctx, &sql).await?;+ insta::assert_snapshot!(batches_to_string(&result), @r"+ +---++ | r |+ +---++ | 1 |+ | 3 |+ | 5 |+ | 7 |+ +---++ ");++ let sql = format!("SELECT my_min(i) {frame} AS r FROM t");+ let result = plan_and_collect(&ctx, &sql).await?;+ insta::assert_snapshot!(batches_to_string(&result), @r"+ +---++ | r |+ +---++ | 1 |+ | 1 |+ | 2 |+ | 3 |+ +---++ ");++ let sql = format!("SELECT my_max(i) {frame} AS r FROM t");+ let result = plan_and_collect(&ctx, &sql).await?;+ insta::assert_snapshot!(batches_to_string(&result), @r"+ +---++ | r |+ +---++ | 1 |+ | 2 |+ | 3 |+ | 4 |+ +---++ ");++ Ok(())+}++/// `name()` forwards to the inner function, so registering the aliased `sum`+/// overwrites the built-in `sum` as well: the original name is affected too.+#[tokio::test]+async fn test_aliased_udaf_as_window_with_frame_original_name() -> Result<()> {+ let ctx = context_with_int_table()?;+ ctx.register_udaf(sum_udaf().as_ref().clone().with_aliases(["my_sum"]));++ let sql = "SELECT sum(i) OVER (ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS r FROM t";+ let result = plan_and_collect(&ctx, sql).await?;+ insta::assert_snapshot!(batches_to_string(&result), @r"+ +---++ | r |+ +---++ | 1 |+ | 3 |+ | 5 |+ | 7 |+ +---++ ");++ Ok(())+}++/// Control: an `UNBOUNDED PRECEDING` frame uses the plain accumulator, so this+/// passes both before and after the fix.+#[tokio::test]+async fn test_aliased_udaf_as_window_unbounded_preceding() -> Result<()> {+ let ctx = context_with_int_table()?;+ ctx.register_udaf(sum_udaf().as_ref().clone().with_aliases(["my_sum"]));++ let sql = "SELECT my_sum(i) OVER (ORDER BY i ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS r FROM t";+ let result = plan_and_collect(&ctx, sql).await?;+ insta::assert_snapshot!(batches_to_string(&result), @r"+ +----++ | r |+ +----++ | 1 |+ | 3 |+ | 6 |+ | 10 |+ +----++ ");++ Ok(())+}+#[tokio::test]async fn test_groups_accumulator() -> Result<()> {let ctx = SessionContext::new();diff --git a/datafusion/expr/src/udaf.rs b/datafusion/expr/src/udaf.rsindex 7358568f0a..5bfc76242c 100644--- a/datafusion/expr/src/udaf.rs+++ b/datafusion/expr/src/udaf.rs@@ -1626,7 +1626,7 @@ impl AggregateUDFImpl for AliasedAggregateUDFImpl {&self,args: AccumulatorArgs,) -> Result<Box<dyn Accumulator>> {- self.inner.accumulator(args)+ self.inner.create_sliding_accumulator(args)}fn with_beneficial_ordering(@@ -1761,8 +1761,9 @@ pub enum DistinctHandling {#[cfg(test)]mod test {use crate::{AggregateUDF, AggregateUDFImpl};- use arrow::datatypes::{DataType, Field, FieldRef};- use datafusion_common::Result;+ use arrow::array::ArrayRef;+ use arrow::datatypes::{DataType, Field, FieldRef, Schema};+ use datafusion_common::{Result, ScalarValue, not_impl_err};use datafusion_expr_common::accumulator::Accumulator;use datafusion_expr_common::signature::{Signature, Volatility};use datafusion_functions_aggregate_common::accumulator::{@@ -1885,6 +1886,103 @@ mod test {}}+ /// Panics if driven: the test only checks which inner method is forwarded to.+ #[derive(Debug)]+ struct PanicAccumulator;++ impl Accumulator for PanicAccumulator {+ fn update_batch(&mut self, _values: &[ArrayRef]) -> Result<()> {+ unimplemented!()+ }+ fn evaluate(&mut self) -> Result<ScalarValue> {+ unimplemented!()+ }+ fn size(&self) -> usize {+ size_of::<Self>()+ }+ fn state(&mut self) -> Result<Vec<ScalarValue>> {+ unimplemented!()+ }+ fn merge_batch(&mut self, _states: &[ArrayRef]) -> Result<()> {+ unimplemented!()+ }+ }++ /// Distinguishable accumulators: the plain one fails, the sliding one succeeds.+ #[derive(Debug, Clone, PartialEq, Eq, Hash)]+ struct SlidingOnlyUdf {+ signature: Signature,+ }++ impl SlidingOnlyUdf {+ fn new() -> Self {+ Self {+ signature: Signature::uniform(+ 1,+ vec![DataType::Float64],+ Volatility::Immutable,+ ),+ }+ }+ }++ impl AggregateUDFImpl for SlidingOnlyUdf {+ fn name(&self) -> &str {+ "sliding_only"+ }+ fn signature(&self) -> &Signature {+ &self.signature+ }+ fn return_type(&self, _args: &[DataType]) -> Result<DataType> {+ Ok(DataType::Int64)+ }+ fn accumulator(+ &self,+ _acc_args: AccumulatorArgs,+ ) -> Result<Box<dyn Accumulator>> {+ not_impl_err!("no plain accumulator")+ }+ fn create_sliding_accumulator(+ &self,+ _args: AccumulatorArgs,+ ) -> Result<Box<dyn Accumulator>> {+ Ok(Box::new(PanicAccumulator))+ }+ fn state_fields(&self, _args: StateFieldsArgs) -> Result<Vec<FieldRef>> {+ unimplemented!()+ }+ }++ #[test]+ fn test_aliased_udaf_forwards_create_sliding_accumulator() {+ let udaf = AggregateUDF::from(SlidingOnlyUdf::new()).with_aliases(["some_alias"]);++ let schema = Schema::empty();+ let args = || AccumulatorArgs {+ return_field: Arc::new(Field::new("f", DataType::Int64, true)),+ schema: &schema,+ expr_fields: &[],+ ignore_nulls: false,+ order_bys: &[],+ is_reversed: false,+ name: "sliding_only",+ is_distinct: false,+ exprs: &[],+ };++ // Asserting both directions proves the wrapper distinguishes the two+ // methods rather than routing both to the same one.+ let sliding = udaf.create_sliding_accumulator(args());+ assert!(+ sliding.is_ok(),+ "aliased UDAF must forward create_sliding_accumulator to the inner UDAF, got: {:?}",+ sliding.err()+ );+ assert!(udaf.accumulator(args()).is_err());++ assert!(udaf.aliases().iter().any(|a| a == "some_alias"));+ }+#[test]fn test_partial_eq() {let a1 = AggregateUDF::from(AMeanUdf::new());