Skip to content
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
4 changes: 4 additions & 0 deletions datafusion/functions-aggregate/src/string_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ impl AggregateUDFImpl for StringAgg {
)))
}

fn reverse_expr(&self) -> datafusion_expr::ReversedUDAF {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add this on main first?
This would make it impossible that anyone updating from 49.0.1 to a newer DF version experiences a behavioral regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR open: #17165

datafusion_expr::ReversedUDAF::Reversed(string_agg_udaf())
}

fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
Expand Down
100 changes: 100 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -6028,6 +6028,106 @@ GROUP BY dummy
----
text1


# Test string_agg with ORDER BY clasuses (issue #17011)
statement ok
create table t (k varchar, v int);

statement ok
insert into t values ('a', 2), ('b', 3), ('c', 1), ('d', null);

query T
select string_agg(k, ',' order by k) from t;
----
a,b,c,d

query T
select string_agg(k, ',' order by k desc) from t;
----
d,c,b,a

query T
select string_agg(k, ',' order by v) from t;
----
c,a,b,d

query T
select string_agg(k, ',' order by v nulls first) from t;
----
d,c,a,b

query T
select string_agg(k, ',' order by v desc) from t;
----
d,b,a,c

query T
select string_agg(k, ',' order by v desc nulls last) from t;
----
b,a,c,d

query T
-- odd indexes should appear first, ties solved by v
select string_agg(k, ',' order by v % 2 == 0, v) from t;
----
c,b,a,d

query T
-- odd indexes should appear first, ties solved by v desc
select string_agg(k, ',' order by v % 2 == 0, v desc) from t;
----
b,c,a,d

query T
select string_agg(k, ',' order by
case
when k = 'a' then 3
when k = 'b' then 0
when k = 'c' then 2
when k = 'd' then 1
end)
from t;
----
b,d,c,a

query T
select string_agg(k, ',' order by
case
when k = 'a' then 3
when k = 'b' then 0
when k = 'c' then 2
when k = 'd' then 1
end desc)
from t;
----
a,c,d,b

query TT
explain select string_agg(k, ',' order by v) from t;
----
logical_plan
01)Aggregate: groupBy=[[]], aggr=[[string_agg(t.k, Utf8(",")) ORDER BY [t.v ASC NULLS LAST]]]
02)--TableScan: t projection=[k, v]
physical_plan
01)AggregateExec: mode=Single, gby=[], aggr=[string_agg(t.k,Utf8(",")) ORDER BY [t.v ASC NULLS LAST]]
02)--SortExec: expr=[v@1 ASC NULLS LAST], preserve_partitioning=[false]
03)----DataSourceExec: partitions=1, partition_sizes=[1]

query TT
explain select string_agg(k, ',' order by v desc) from t;
----
logical_plan
01)Aggregate: groupBy=[[]], aggr=[[string_agg(t.k, Utf8(",")) ORDER BY [t.v DESC NULLS FIRST]]]
02)--TableScan: t projection=[k, v]
physical_plan
01)AggregateExec: mode=Single, gby=[], aggr=[string_agg(t.k,Utf8(",")) ORDER BY [t.v DESC NULLS FIRST]]
02)--SortExec: expr=[v@1 DESC], preserve_partitioning=[false]
03)----DataSourceExec: partitions=1, partition_sizes=[1]

statement ok
drop table t;


# Tests for aggregating with NaN values
statement ok
CREATE TABLE float_table (
Expand Down
Loading