提交 0622d70e authored 作者: Noel Grandin's avatar Noel Grandin

Merge pull request #165 from Myp/master

Fix compatibility postgresql function string_agg
......@@ -2293,8 +2293,9 @@ public class Parser {
}
} else if (aggregateType == Aggregate.GROUP_CONCAT) {
Aggregate agg = null;
if (equalsToken("GROUP_CONCAT", aggregateName)) {
boolean distinct = readIf("DISTINCT");
if (equalsToken("GROUP_CONCAT", aggregateName)) {
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 posgresql 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论