提交 c32e8dc1 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix GROUP_CONCAT with variable separator

上级 f7c66675
...@@ -3545,6 +3545,7 @@ GROUP_CONCAT ( [ DISTINCT|ALL ] string ...@@ -3545,6 +3545,7 @@ GROUP_CONCAT ( [ DISTINCT|ALL ] string
[FILTER (WHERE expression)] [OVER windowNameOrSpecification] [FILTER (WHERE expression)] [OVER windowNameOrSpecification]
"," ","
Concatenates strings with a separator. Concatenates strings with a separator.
Separator must be the same for all rows in the same group.
The default separator is a ',' (without space). The default separator is a ',' (without space).
This method returns a string. This method returns a string.
If no rows are selected, the result is NULL. If no rows are selected, the result is NULL.
......
...@@ -181,6 +181,10 @@ public class Aggregate extends AbstractAggregate { ...@@ -181,6 +181,10 @@ public class Aggregate extends AbstractAggregate {
if (v != ValueNull.INSTANCE) { if (v != ValueNull.INSTANCE) {
v = updateCollecting(session, v.convertTo(Value.STRING), remembered); v = updateCollecting(session, v.convertTo(Value.STRING), remembered);
} }
if (args.length >= 2) {
((AggregateDataCollecting) data).setSharedArgument(
remembered != null ? remembered[1] : args[1].getValue(session));
}
break; break;
case ARRAY_AGG: case ARRAY_AGG:
if (v != ValueNull.INSTANCE) { if (v != ValueNull.INSTANCE) {
...@@ -415,7 +419,8 @@ public class Aggregate extends AbstractAggregate { ...@@ -415,7 +419,8 @@ public class Aggregate extends AbstractAggregate {
} }
private Value getGroupConcat(Session session, AggregateData data) { private Value getGroupConcat(Session session, AggregateData data) {
Value[] array = ((AggregateDataCollecting) data).getArray(); AggregateDataCollecting collectingData = (AggregateDataCollecting) data;
Value[] array = collectingData.getArray();
if (array == null) { if (array == null) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
...@@ -423,7 +428,7 @@ public class Aggregate extends AbstractAggregate { ...@@ -423,7 +428,7 @@ public class Aggregate extends AbstractAggregate {
sortWithOrderBy(array); sortWithOrderBy(array);
} }
StatementBuilder buff = new StatementBuilder(); StatementBuilder buff = new StatementBuilder();
String sep = args.length < 2 ? "," : args[1].getValue(session).getString(); String sep = args.length < 2 ? "," : collectingData.getSharedArgument().getString();
for (Value val : array) { for (Value val : array) {
String s; String s;
if (val.getValueType() == Value.ARRAY) { if (val.getValueType() == Value.ARRAY) {
......
...@@ -67,3 +67,32 @@ select group_concat(distinct v order by v desc) from test; ...@@ -67,3 +67,32 @@ select group_concat(distinct v order by v desc) from test;
drop table test; drop table test;
> ok > ok
create table test(g varchar, v int) as values ('-', 1), ('-', 2), ('-', 3), ('|', 4), ('|', 5), ('|', 6), ('*', null);
> ok
select g, group_concat(v separator g) from test group by g;
> G GROUP_CONCAT(V SEPARATOR G)
> - ---------------------------
> * null
> - 1-2-3
> | 4|5|6
> rows: 3
select g, group_concat(v separator g) over (partition by g) from test order by v;
> G GROUP_CONCAT(V SEPARATOR G) OVER (PARTITION BY G)
> - -------------------------------------------------
> * null
> - 1-2-3
> - 1-2-3
> - 1-2-3
> | 4|5|6
> | 4|5|6
> | 4|5|6
> rows (ordered): 7
select g, group_concat(v separator v) from test group by g;
> exception INVALID_VALUE_2
drop table test;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论