提交 2fd0055f authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Sort also distinct values for ARRAY_AGG and GROUP_CONCAT

上级 d29ae257
...@@ -12,6 +12,7 @@ import java.util.HashMap; ...@@ -12,6 +12,7 @@ import java.util.HashMap;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.dml.Select; import org.h2.command.dml.Select;
import org.h2.command.dml.SelectOrderBy; import org.h2.command.dml.SelectOrderBy;
import org.h2.engine.Database;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.index.Cursor; import org.h2.index.Cursor;
import org.h2.index.Index; import org.h2.index.Index;
...@@ -259,12 +260,22 @@ public class Aggregate extends Expression { ...@@ -259,12 +260,22 @@ public class Aggregate extends Expression {
private void sortWithOrderBy(Value[] array) { private void sortWithOrderBy(Value[] array) {
final SortOrder sortOrder = orderBySort; final SortOrder sortOrder = orderBySort;
Arrays.sort(array, new Comparator<Value>() { if (sortOrder != null) {
@Override Arrays.sort(array, new Comparator<Value>() {
public int compare(Value v1, Value v2) { @Override
return sortOrder.compare(((ValueArray) v1).getList(), ((ValueArray) v2).getList()); public int compare(Value v1, Value v2) {
} return sortOrder.compare(((ValueArray) v1).getList(), ((ValueArray) v2).getList());
}); }
});
} else {
final Database database = select.getSession().getDatabase();
Arrays.sort(array, new Comparator<Value> () {
@Override
public int compare(Value v1, Value v2) {
return database.compare(v1, v2);
}
});
}
} }
@Override @Override
...@@ -375,7 +386,7 @@ public class Aggregate extends Expression { ...@@ -375,7 +386,7 @@ public class Aggregate extends Expression {
if (array == null) { if (array == null) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
if (orderByList != null) { if (orderByList != null || distinct) {
sortWithOrderBy(array); sortWithOrderBy(array);
} }
StatementBuilder buff = new StatementBuilder(); StatementBuilder buff = new StatementBuilder();
...@@ -402,7 +413,7 @@ public class Aggregate extends Expression { ...@@ -402,7 +413,7 @@ public class Aggregate extends Expression {
if (array == null) { if (array == null) {
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
} }
if (orderByList != null) { if (orderByList != null || distinct) {
sortWithOrderBy(array); sortWithOrderBy(array);
} }
if (orderByList != null) { if (orderByList != null) {
......
...@@ -37,6 +37,32 @@ select ARRAY_AGG(v order by v asc), ...@@ -37,6 +37,32 @@ select ARRAY_AGG(v order by v asc),
> (1, 2, 3, 4, 5, 6, 7, 8, 9) (9, 8, 7, 6, 5, 4) > (1, 2, 3, 4, 5, 6, 7, 8, 9) (9, 8, 7, 6, 5, 4)
> rows (ordered): 1 > rows (ordered): 1
drop table test;
> ok
create table test (id int auto_increment primary key, v int);
> ok
insert into test(v) values (7), (2), (8), (3), (7), (3), (9), (-1);
> update count: 8
select array_agg(v) from test;
> ARRAY_AGG(V)
> -------------------------
> (7, 2, 8, 3, 7, 3, 9, -1)
> rows: 1
select array_agg(distinct v) from test;
> ARRAY_AGG(DISTINCT V)
> ---------------------
> (-1, 2, 3, 7, 8, 9)
> rows: 1
select array_agg(distinct v order by v desc) from test;
> ARRAY_AGG(DISTINCT V ORDER BY V DESC)
> -------------------------------------
> (9, 8, 7, 3, 2, -1)
> rows (ordered): 1
drop table test; drop table test;
> ok > ok
...@@ -37,6 +37,32 @@ select group_concat(v order by v asc separator '-'), ...@@ -37,6 +37,32 @@ select group_concat(v order by v asc separator '-'),
> 1-2-3-4-5-6-7-8-9 9-8-7-6-5-4 > 1-2-3-4-5-6-7-8-9 9-8-7-6-5-4
> rows (ordered): 1 > rows (ordered): 1
drop table test;
> ok
create table test (id int auto_increment primary key, v int);
> ok
insert into test(v) values (7), (2), (8), (3), (7), (3), (9), (-1);
> update count: 8
select group_concat(v) from test;
> GROUP_CONCAT(V)
> ----------------
> 7,2,8,3,7,3,9,-1
> rows: 1
select group_concat(distinct v) from test;
> GROUP_CONCAT(DISTINCT V)
> ------------------------
> -1,2,3,7,8,9
> rows: 1
select group_concat(distinct v order by v desc) from test;
> GROUP_CONCAT(DISTINCT V ORDER BY V DESC)
> ----------------------------------------
> 9,8,7,3,2,-1
> rows (ordered): 1
drop table test; drop table test;
> ok > ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论