Skip to content

Commit

Permalink
Fix(parser,bigquery): make separator optional in STRING_AGG parser (#…
Browse files Browse the repository at this point in the history
…1974)

* Fix(parser,bigquery): make separator optional in STRING_AGG parser

* Refactor
  • Loading branch information
georgesittas authored Jul 28, 2023
1 parent 2874ae5 commit 89c8635
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
10 changes: 4 additions & 6 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3861,13 +3861,11 @@ def _parse_string_agg(self) -> exp.Expression:
args = self._parse_csv(self._parse_conjunction)

index = self._index
if not self._match(TokenType.R_PAREN):
if not self._match(TokenType.R_PAREN) and args:
# postgres: STRING_AGG([DISTINCT] expression, separator [ORDER BY expression1 {ASC | DESC} [, ...]])
return self.expression(
exp.GroupConcat,
this=seq_get(args, 0),
separator=self._parse_order(this=seq_get(args, 1)),
)
# bigquery: STRING_AGG([DISTINCT] expression [, separator] [ORDER BY key [{ASC | DESC}] [, ... ]] [LIMIT n])
args[-1] = self._parse_limit(this=self._parse_order(this=args[-1]))
return self.expression(exp.GroupConcat, this=args[0], separator=seq_get(args, 1))

# Checks if we can parse an order clause: WITHIN GROUP (ORDER BY <order_by_expression_list> [ASC | DESC]).
# This is done "manually", instead of letting _parse_window parse it into an exp.WithinGroup node, so that
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_bigquery(self):
with self.assertRaises(ParseError):
transpile("SELECT * FROM UNNEST(x) AS x(y)", read="bigquery")

self.validate_identity("STRING_AGG(DISTINCT a ORDER BY b DESC, c DESC LIMIT 10)")
self.validate_identity("SELECT PARSE_TIMESTAMP('%c', 'Thu Dec 25 07:30:00 2008', 'UTC')")
self.validate_identity("SELECT ANY_VALUE(fruit HAVING MAX sold) FROM fruits")
self.validate_identity("SELECT ANY_VALUE(fruit HAVING MIN sold) FROM fruits")
Expand Down

0 comments on commit 89c8635

Please sign in to comment.