提交 232d49d4 authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

Merge branch 'master' of https://github.com/h2database/h2database

e
......@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>Pull request #165: Fix compatibility postgresql function string_agg
</li>
<li>Pull request #163: improved performance when not using the default timezone.
</li>
<li>Local temporary tables with many rows did not work correctly due to automatic analyze.
......
......@@ -2293,8 +2293,9 @@ public class Parser {
}
} else if (aggregateType == Aggregate.GROUP_CONCAT) {
Aggregate agg = null;
boolean distinct = readIf("DISTINCT");
if (equalsToken("GROUP_CONCAT", aggregateName)) {
boolean distinct = readIf("DISTINCT");
agg = new Aggregate(Aggregate.GROUP_CONCAT,
readExpression(), currentSelect, distinct);
if (readIf("ORDER")) {
......@@ -2308,9 +2309,14 @@ public class Parser {
} else if (equalsToken("STRING_AGG", aggregateName)) {
// PostgreSQL compatibility: string_agg(expression, delimiter)
agg = new Aggregate(Aggregate.GROUP_CONCAT,
readExpression(), currentSelect, false);
readExpression(), currentSelect, distinct);
read(",");
agg.setGroupConcatSeparator(readExpression());
if(readIf("ORDER")) {
read("BY");
agg.setGroupConcatOrder(parseSimpleOrderList());
}
}
r = agg;
} else {
......
......@@ -148,6 +148,7 @@ import org.h2.test.synth.TestNestedJoins;
import org.h2.test.synth.TestOuterJoins;
import org.h2.test.synth.TestRandomCompare;
import org.h2.test.synth.TestRandomSQL;
import org.h2.test.synth.TestStringAggCompatibility;
import org.h2.test.synth.TestTimer;
import org.h2.test.synth.sql.TestSynth;
import org.h2.test.synth.thread.TestMulti;
......@@ -759,6 +760,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
addTest(new TestMultiThreaded());
addTest(new TestOuterJoins());
addTest(new TestNestedJoins());
addTest(new TestStringAggCompatibility());
runAddedTests();
......
package org.h2.test.synth;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.h2.test.TestBase;
/**
* Test for check compatibility with PostgreSQL function string_agg()
*/
public class TestStringAggCompatibility extends TestBase {
public static final String STRING_AGG_DB = "stringAgg";
private Connection conn;
public static void main(String[] args) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws Exception {
deleteDb(STRING_AGG_DB);
conn = getConnection(STRING_AGG_DB);
prepareDb();
testWhenOrderByMissing();
testWithOrderBy();
}
private void testWithOrderBy() throws SQLException {
ResultSet result = query("select string_agg(b, ', ' order by b desc) from stringAgg group by a; ");
assertTrue(result.next());
assertEquals("3, 2, 1", result.getString(1));
}
private void testWhenOrderByMissing() throws SQLException {
ResultSet result = query("select string_agg(b, ', ') from stringAgg group by a; ");
assertTrue(result.next());
assertEquals("1, 2, 3", result.getString(1));
}
private ResultSet query(String q) throws SQLException {
PreparedStatement st = conn.prepareStatement(q);
st.execute();
return st.getResultSet();
}
private void prepareDb() throws SQLException {
exec("create table stringAgg(\n" + " a int not null,\n" + " b varchar(50) not null\n" + ");");
exec("insert into stringAgg values(1, '1')");
exec("insert into stringAgg values(1, '2')");
exec("insert into stringAgg values(1, '3')");
}
private void exec(String sql) throws SQLException {
conn.prepareStatement(sql).execute();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论