Skip to content

Commit 1de4d0e

Browse files
authored
fix: support within_group (#16538)
* fix: reject within_group for non ordered aggregate function * update error * support within
1 parent 8d34abb commit 1de4d0e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

datafusion/sql/src/expr/function.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
404404
}
405405
(!within_group.is_empty()).then_some(within_group)
406406
} else {
407+
let order_by = if !order_by.is_empty() {
408+
order_by
409+
} else {
410+
within_group
411+
};
407412
let order_by = self.order_by_to_sort_expr(
408413
order_by,
409414
schema,

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7040,3 +7040,16 @@ VALUES
70407040
) GROUP BY 1 ORDER BY 1;
70417041
----
70427042
x 1
7043+
7044+
query ?
7045+
SELECT array_agg(a_varchar) WITHIN GROUP (ORDER BY a_varchar)
7046+
FROM (VALUES ('a'), ('d'), ('c'), ('a')) t(a_varchar);
7047+
----
7048+
[a, a, c, d]
7049+
7050+
query ?
7051+
SELECT array_agg(DISTINCT a_varchar) WITHIN GROUP (ORDER BY a_varchar)
7052+
FROM (VALUES ('a'), ('d'), ('c'), ('a')) t(a_varchar);
7053+
----
7054+
[a, c, d]
7055+

0 commit comments

Comments
 (0)