Skip to content

Commit 232d49d

Browse files
committed
e
2 parents eea3038 + 0347432 commit 232d49d

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

h2/src/docsrc/html/changelog.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ <h1>Change Log</h1>
2121

2222
<h2>Next Version (unreleased)</h2>
2323
<ul>
24+
<li>Pull request #165: Fix compatibility postgresql function string_agg
25+
</li>
2426
<li>Pull request #163: improved performance when not using the default timezone.
2527
</li>
2628
<li>Local temporary tables with many rows did not work correctly due to automatic analyze.

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

0 commit comments

Comments
 (0)