Skip to content

Commit 5427a61

Browse files
committed
allow distinct in aggregate functions
1 parent be3b1b2 commit 5427a61

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/ORM/Query/AST/Functions/ArrayAgg.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,33 @@
1515
* @see https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
1616
*
1717
* @example ARRAY_AGG(entity.field)
18+
* @example ARRAY_AGG(DISTINCT entity.field)
1819
* @example ARRAY_AGG(entity.field) FILTER (WHERE entity.field IS NOT NULL)
20+
* @example ARRAY_AGG(DISTINCT entity.field) FILTER (WHERE entity.field IS NOT NULL)
1921
*/
2022
final class ArrayAgg extends AggregateWithFilterFunction
2123
{
24+
private bool $distinct = false;
25+
2226
private Node $expr;
2327

2428
public function parseFunction(Parser $parser): void
2529
{
2630
$parser->match(Lexer::T_IDENTIFIER);
2731
$parser->match(Lexer::T_OPEN_PARENTHESIS);
32+
33+
$lexer = $parser->getLexer();
34+
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
35+
$parser->match(Lexer::T_DISTINCT);
36+
$this->distinct = true;
37+
}
38+
2839
$this->expr = $parser->StringPrimary();
2940
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
3041
}
3142

3243
public function getFunctionSql(SqlWalker $sqlWalker): string
3344
{
34-
return "ARRAY_AGG({$this->expr->dispatch($sqlWalker)})";
45+
return sprintf('ARRAY_AGG(%s%s)', $this->distinct ? 'DISTINCT ' : '', $this->expr->dispatch($sqlWalker));
3546
}
3647
}

src/ORM/Query/AST/Functions/JsonAgg.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,33 @@
1515
* @see https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
1616
*
1717
* @example JSON_AGG(entity.field)
18+
* @example JSON_AGG(DISTINCT entity.field)
1819
* @example JSON_AGG(entity.field) FILTER (WHERE entity.field IS NOT NULL)
20+
* @example JSON_AGG(DISTINCT entity.field) FILTER (WHERE entity.field IS NOT NULL)
1921
*/
2022
final class JsonAgg extends AggregateWithFilterFunction
2123
{
24+
private bool $distinct = false;
25+
2226
private Node $expr;
2327

2428
public function parseFunction(Parser $parser): void
2529
{
2630
$parser->match(Lexer::T_IDENTIFIER);
2731
$parser->match(Lexer::T_OPEN_PARENTHESIS);
32+
33+
$lexer = $parser->getLexer();
34+
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
35+
$parser->match(Lexer::T_DISTINCT);
36+
$this->distinct = true;
37+
}
38+
2839
$this->expr = $parser->StringPrimary();
2940
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
3041
}
3142

3243
public function getFunctionSql(SqlWalker $sqlWalker): string
3344
{
34-
return "JSON_AGG({$this->expr->dispatch($sqlWalker)})";
45+
return sprintf('JSON_AGG(%s%s)', $this->distinct ? 'DISTINCT ' : '', $this->expr->dispatch($sqlWalker));
3546
}
3647
}

src/ORM/Query/AST/Functions/JsonbAgg.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,27 @@
1919
*/
2020
final class JsonbAgg extends AggregateWithFilterFunction
2121
{
22+
private bool $distinct = false;
23+
2224
private Node $expr;
2325

2426
public function parseFunction(Parser $parser): void
2527
{
2628
$parser->match(Lexer::T_IDENTIFIER);
2729
$parser->match(Lexer::T_OPEN_PARENTHESIS);
30+
31+
$lexer = $parser->getLexer();
32+
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
33+
$parser->match(Lexer::T_DISTINCT);
34+
$this->distinct = true;
35+
}
36+
2837
$this->expr = $parser->StringPrimary();
2938
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
3039
}
3140

3241
public function getFunctionSql(SqlWalker $sqlWalker): string
3342
{
34-
return "JSON_AGG({$this->expr->dispatch($sqlWalker)})";
43+
return sprintf('JSONB_AGG(%s%s)', $this->distinct ? 'DISTINCT ' : '', $this->expr->dispatch($sqlWalker));
3544
}
3645
}

0 commit comments

Comments
 (0)