Skip to content

Apache DataFusion SQL Query Engine

fix: respect ORDER BY in array_agg(DISTINCT) when the aggregate is reversed (#26440)

raams9520463f3def9a
diff --git a/datafusion/functions-aggregate/src/array_agg.rs b/datafusion/functions-aggregate/src/array_agg.rs
index 0e49abd5bb..f81a60c222 100644
--- a/datafusion/functions-aggregate/src/array_agg.rs
+++ b/datafusion/functions-aggregate/src/array_agg.rs
@@ -192,7 +192,15 @@ impl AggregateUDFImpl for ArrayAgg {
// ARRAY_AGG(DISTINCT col ORDER BY other_col) <- Invalid
// ARRAY_AGG(DISTINCT col ORDER BY concat(col, '')) <- Invalid
let sort_option = match acc_args.order_bys {
- [single] if single.expr.eq(&acc_args.exprs[0]) => Some(single.options),
+ // This accumulator sorts the values itself, so a reversed
+ // aggregate must still sort them in the requested order.
+ [single] if single.expr.eq(&acc_args.exprs[0]) => {
+ Some(if acc_args.is_reversed {
+ !single.options
+ } else {
+ single.options
+ })
+ }
[] => None,
_ => {
return exec_err!(
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index 356f0b705e..bbbf8ebea9 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -8369,6 +8369,11 @@ SELECT STRING_AGG(DISTINCT x,'|' ORDER BY x) FROM strings
----
a|b|i|j|p|x|y|z
+query TT
+SELECT STRING_AGG(x,'|' ORDER BY x DESC), STRING_AGG(DISTINCT x,'|' ORDER BY x) FROM strings
+----
+z|y|x|p|j|i|b|a a|b|i|j|p|x|y|z
+
query error This feature is not implemented: string_agg delimiter must be a string literal
SELECT STRING_AGG(DISTINCT x,y) FROM strings
diff --git a/datafusion/sqllogictest/test_files/array_agg.slt b/datafusion/sqllogictest/test_files/array_agg.slt
index a3d839c113..8342380e31 100644
--- a/datafusion/sqllogictest/test_files/array_agg.slt
+++ b/datafusion/sqllogictest/test_files/array_agg.slt
@@ -473,6 +473,19 @@ select array_agg(DISTINCT column2 order by column2) from array_agg_order_list_ta
[1, 2]
[1, 2]
+# array_agg(DISTINCT .. ORDER BY ..) keeps its own order when another aggregate
+# needs the input sorted the opposite way
+query ??
+select array_agg(x order by x desc), array_agg(DISTINCT x order by x) from (values (2), (null), (1), (2)) t(x);
+----
+[NULL, 2, 2, 1] [1, 2, NULL]
+
+query T?? rowsort
+select column1, array_agg(column2 order by column2 desc), array_agg(DISTINCT column2 order by column2) from array_agg_order_list_table group by column1;
+----
+b [2, 1] [1, 2]
+w [2, 1, 1] [1, 2]
+
statement error In an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
select array_agg(DISTINCT column2 order by column1) from array_agg_order_list_table;