Skip to content

Commit 0622d70

Browse files
committed
Merge pull request h2database#165 from Myp/master
Fix compatibility postgresql function string_agg
2 parents 11ef349 + 32fa278 commit 0622d70

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

h2/src/main/org/h2/command/Parser.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,8 +2293,9 @@ private Expression readAggregate(int aggregateType, String aggregateName) {
22932293
}
22942294
} else if (aggregateType == Aggregate.GROUP_CONCAT) {
22952295
Aggregate agg = null;
2296+
boolean distinct = readIf("DISTINCT");
2297+
22962298
if (equalsToken("GROUP_CONCAT", aggregateName)) {
2297-
boolean distinct = readIf("DISTINCT");
22982299
agg = new Aggregate(Aggregate.GROUP_CONCAT,
22992300
readExpression(), currentSelect, distinct);
23002301
if (readIf("ORDER")) {
@@ -2308,9 +2309,14 @@ private Expression readAggregate(int aggregateType, String aggregateName) {
23082309
} else if (equalsToken("STRING_AGG", aggregateName)) {
23092310
// PostgreSQL compatibility: string_agg(expression, delimiter)
23102311
agg = new Aggregate(Aggregate.GROUP_CONCAT,
2311-
readExpression(), currentSelect, false);
2312+
readExpression(), currentSelect, distinct);
23122313
read(",");
23132314
agg.setGroupConcatSeparator(readExpression());
2315+
2316+
if(readIf("ORDER")) {
2317+
read("BY");
2318+
agg.setGroupConcatOrder(parseSimpleOrderList());
2319+
}
23142320
}
23152321
r = agg;
23162322
} else {

h2/src/test/org/h2/test/TestAll.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
import org.h2.test.synth.TestOuterJoins;
149149
import org.h2.test.synth.TestRandomCompare;
150150
import org.h2.test.synth.TestRandomSQL;
151+
import org.h2.test.synth.TestStringAggCompatibility;
151152
import org.h2.test.synth.TestTimer;
152153
import org.h2.test.synth.sql.TestSynth;
153154
import org.h2.test.synth.thread.TestMulti;
@@ -759,6 +760,7 @@ private void test() throws SQLException {
759760
addTest(new TestMultiThreaded());
760761
addTest(new TestOuterJoins());
761762
addTest(new TestNestedJoins());
763+
addTest(new TestStringAggCompatibility());
762764

763765
runAddedTests();
764766

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.h2.test.synth;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
import org.h2.test.TestBase;
9+
10+
/**
11+
* Test for check compatibility with posgresql function string_agg()
12+
*
13+
* */
14+
public class TestStringAggCompatibility extends TestBase {
15+
16+
public static final String STRING_AGG_DB = "stringAgg";
17+
18+
private Connection conn;
19+
20+
public static void main(String[] args) throws Exception {
21+
TestBase.createCaller().init().test();
22+
}
23+
24+
@Override
25+
public void test() throws Exception {
26+
deleteDb(STRING_AGG_DB);
27+
28+
conn = getConnection(STRING_AGG_DB);
29+
30+
prepareDb();
31+
32+
33+
34+
35+
36+
testWhenOrderByMissing();
37+
testWithOrderBy();
38+
}
39+
40+
private void testWithOrderBy() throws SQLException {
41+
ResultSet result = query("select string_agg(b, ', ' order by b desc) from stringAgg group by a; ");
42+
43+
assertTrue(result.next());
44+
assertEquals("3, 2, 1", result.getString(1));
45+
}
46+
47+
private void testWhenOrderByMissing() throws SQLException {
48+
ResultSet result = query("select string_agg(b, ', ') from stringAgg group by a; ");
49+
50+
assertTrue(result.next());
51+
assertEquals("1, 2, 3", result.getString(1));
52+
}
53+
54+
55+
private ResultSet query(String q) throws SQLException {
56+
PreparedStatement st = conn.prepareStatement(q);
57+
58+
st.execute();
59+
60+
return st.getResultSet();
61+
}
62+
63+
private void prepareDb() throws SQLException {
64+
exec("create table stringAgg(\n" +
65+
" a int not null,\n" +
66+
" b varchar(50) not null\n" +
67+
");");
68+
69+
exec("insert into stringAgg values(1, '1')");
70+
exec("insert into stringAgg values(1, '2')");
71+
exec("insert into stringAgg values(1, '3')");
72+
73+
}
74+
75+
private void exec(String sql) throws SQLException {
76+
conn.prepareStatement(sql).execute();
77+
}
78+
}

0 commit comments

Comments
 (0)