This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 1de4d0e2e1 fix: support within_group (#16538)
1de4d0e2e1 is described below

commit 1de4d0e2e1b348b27e6c82ad09d3f689e4beef42
Author: Chen Chongchen <chenkov...@qq.com>
AuthorDate: Sat Jun 28 05:16:38 2025 +0800

    fix: support within_group (#16538)
    
    * fix: reject within_group for non ordered aggregate function
    
    * update error
    
    * support within
---
 datafusion/sql/src/expr/function.rs              |  5 +++++
 datafusion/sqllogictest/test_files/aggregate.slt | 13 +++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/datafusion/sql/src/expr/function.rs 
b/datafusion/sql/src/expr/function.rs
index 15e6eb7fb9..8a4bbb683b 100644
--- a/datafusion/sql/src/expr/function.rs
+++ b/datafusion/sql/src/expr/function.rs
@@ -404,6 +404,11 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
                     }
                     (!within_group.is_empty()).then_some(within_group)
                 } else {
+                    let order_by = if !order_by.is_empty() {
+                        order_by
+                    } else {
+                        within_group
+                    };
                     let order_by = self.order_by_to_sort_expr(
                         order_by,
                         schema,
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt 
b/datafusion/sqllogictest/test_files/aggregate.slt
index 40f73a19cd..53562d019c 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -7040,3 +7040,16 @@ VALUES
 ) GROUP BY 1 ORDER BY 1;
 ----
 x 1
+
+query ?
+SELECT array_agg(a_varchar) WITHIN GROUP (ORDER BY a_varchar)
+FROM (VALUES ('a'), ('d'), ('c'), ('a')) t(a_varchar);
+----
+[a, a, c, d]
+
+query ?
+SELECT array_agg(DISTINCT a_varchar) WITHIN GROUP (ORDER BY a_varchar)
+FROM (VALUES ('a'), ('d'), ('c'), ('a')) t(a_varchar);
+----
+[a, c, d]
+


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@datafusion.apache.org
For additional commands, e-mail: commits-h...@datafusion.apache.org

Reply via email to