Skip to content

Commit

Permalink
allow distinct in aggregate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Mar 15, 2023
1 parent be3b1b2 commit 5427a61
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/ORM/Query/AST/Functions/ArrayAgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,33 @@
* @see https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
*
* @example ARRAY_AGG(entity.field)
* @example ARRAY_AGG(DISTINCT entity.field)
* @example ARRAY_AGG(entity.field) FILTER (WHERE entity.field IS NOT NULL)
* @example ARRAY_AGG(DISTINCT entity.field) FILTER (WHERE entity.field IS NOT NULL)
*/
final class ArrayAgg extends AggregateWithFilterFunction
{
private bool $distinct = false;

private Node $expr;

public function parseFunction(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$lexer = $parser->getLexer();
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
$parser->match(Lexer::T_DISTINCT);
$this->distinct = true;
}

$this->expr = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getFunctionSql(SqlWalker $sqlWalker): string
{
return "ARRAY_AGG({$this->expr->dispatch($sqlWalker)})";
return sprintf('ARRAY_AGG(%s%s)', $this->distinct ? 'DISTINCT ' : '', $this->expr->dispatch($sqlWalker));
}
}
13 changes: 12 additions & 1 deletion src/ORM/Query/AST/Functions/JsonAgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,33 @@
* @see https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
*
* @example JSON_AGG(entity.field)
* @example JSON_AGG(DISTINCT entity.field)
* @example JSON_AGG(entity.field) FILTER (WHERE entity.field IS NOT NULL)
* @example JSON_AGG(DISTINCT entity.field) FILTER (WHERE entity.field IS NOT NULL)
*/
final class JsonAgg extends AggregateWithFilterFunction
{
private bool $distinct = false;

private Node $expr;

public function parseFunction(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$lexer = $parser->getLexer();
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
$parser->match(Lexer::T_DISTINCT);
$this->distinct = true;
}

$this->expr = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getFunctionSql(SqlWalker $sqlWalker): string
{
return "JSON_AGG({$this->expr->dispatch($sqlWalker)})";
return sprintf('JSON_AGG(%s%s)', $this->distinct ? 'DISTINCT ' : '', $this->expr->dispatch($sqlWalker));
}
}
11 changes: 10 additions & 1 deletion src/ORM/Query/AST/Functions/JsonbAgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@
*/
final class JsonbAgg extends AggregateWithFilterFunction
{
private bool $distinct = false;

private Node $expr;

public function parseFunction(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$lexer = $parser->getLexer();
if ($lexer->isNextToken(Lexer::T_DISTINCT)) {
$parser->match(Lexer::T_DISTINCT);
$this->distinct = true;
}

$this->expr = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

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

0 comments on commit 5427a61

Please sign in to comment.