提交 0a8c8b08 authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

Add support for PostgreSQL STRING_AGG function. Patch by Fred Aquiles.

上级 64040855
...@@ -59,6 +59,7 @@ Change Log ...@@ -59,6 +59,7 @@ Change Log
throw an exception. throw an exception.
</li><li>Fix bug in MVStore when creating lots of temporary tables, where we could run out of </li><li>Fix bug in MVStore when creating lots of temporary tables, where we could run out of
transaction IDs. transaction IDs.
</li><li>Add support for PostgreSQL STRING_AGG function. Patch by Fred Aquiles.
</li></ul> </li></ul>
<h2>Version 1.4.186 Beta (2015-03-02)</h2> <h2>Version 1.4.186 Beta (2015-03-02)</h2>
......
...@@ -2270,7 +2270,7 @@ public class Parser { ...@@ -2270,7 +2270,7 @@ public class Parser {
} }
} }
private Expression readAggregate(int aggregateType) { private Expression readAggregate(int aggregateType, String aggregateName) {
if (currentSelect == null) { if (currentSelect == null) {
throw getSyntaxError(); throw getSyntaxError();
} }
...@@ -2293,16 +2293,26 @@ public class Parser { ...@@ -2293,16 +2293,26 @@ public class Parser {
} }
} }
} else if (aggregateType == Aggregate.GROUP_CONCAT) { } else if (aggregateType == Aggregate.GROUP_CONCAT) {
Aggregate agg = null;
if (equalsToken("GROUP_CONCAT", aggregateName)) {
boolean distinct = readIf("DISTINCT"); boolean distinct = readIf("DISTINCT");
Aggregate agg = new Aggregate(Aggregate.GROUP_CONCAT, agg = new Aggregate(Aggregate.GROUP_CONCAT,
readExpression(), currentSelect, distinct); readExpression(), currentSelect, distinct);
if (readIf("ORDER")) { if (readIf("ORDER")) {
read("BY"); read("BY");
agg.setGroupConcatOrder(parseSimpleOrderList()); agg.setGroupConcatOrder(parseSimpleOrderList());
} }
if (readIf("SEPARATOR")) { if (readIf("SEPARATOR")) {
agg.setGroupConcatSeparator(readExpression()); agg.setGroupConcatSeparator(readExpression());
} }
} else if (equalsToken("STRING_AGG", aggregateName)) {
// PostgreSQL compatibility: string_agg(expression, delimiter)
agg = new Aggregate(Aggregate.GROUP_CONCAT,
readExpression(), currentSelect, false);
read(",");
agg.setGroupConcatSeparator(readExpression());
}
r = agg; r = agg;
} else { } else {
boolean distinct = readIf("DISTINCT"); boolean distinct = readIf("DISTINCT");
...@@ -2382,7 +2392,7 @@ public class Parser { ...@@ -2382,7 +2392,7 @@ public class Parser {
} }
int agg = getAggregateType(name); int agg = getAggregateType(name);
if (agg >= 0) { if (agg >= 0) {
return readAggregate(agg); return readAggregate(agg, name);
} }
Function function = Function.getFunction(database, name); Function function = Function.getFunction(database, name);
if (function == null) { if (function == null) {
......
...@@ -163,6 +163,8 @@ public class Aggregate extends Expression { ...@@ -163,6 +163,8 @@ public class Aggregate extends Expression {
addAggregate("MAX", MAX); addAggregate("MAX", MAX);
addAggregate("AVG", AVG); addAggregate("AVG", AVG);
addAggregate("GROUP_CONCAT", GROUP_CONCAT); addAggregate("GROUP_CONCAT", GROUP_CONCAT);
// PostgreSQL compatibility: string_agg(expression, delimiter)
addAggregate("STRING_AGG", GROUP_CONCAT);
addAggregate("STDDEV_SAMP", STDDEV_SAMP); addAggregate("STDDEV_SAMP", STDDEV_SAMP);
addAggregate("STDDEV", STDDEV_SAMP); addAggregate("STDDEV", STDDEV_SAMP);
addAggregate("STDDEV_POP", STDDEV_POP); addAggregate("STDDEV_POP", STDDEV_POP);
......
...@@ -8188,6 +8188,12 @@ SELECT GROUP_CONCAT(ID ORDER BY ID) FROM TEST; ...@@ -8188,6 +8188,12 @@ SELECT GROUP_CONCAT(ID ORDER BY ID) FROM TEST;
> 1,2,3,4,5,6,7,8,9 > 1,2,3,4,5,6,7,8,9
> rows (ordered): 1 > rows (ordered): 1
SELECT STRING_AGG(ID,';') FROM TEST;
> GROUP_CONCAT(ID SEPARATOR ';')
> ------------------------------
> 1;2;3;4;5;6;7;8;9
> rows: 1
SELECT DISTINCT NAME FROM TEST; SELECT DISTINCT NAME FROM TEST;
> NAME > NAME
> -------- > --------
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论