Skip to content

Commit

Permalink
Fix(parser): don't parse an alias for non-source UNNESTs (#1774)
Browse files Browse the repository at this point in the history
* Fix(parser): don't parse an alias for non-source UNNESTs

* Add test
  • Loading branch information
georgesittas authored Jun 14, 2023
1 parent b13d0b9 commit a2deee3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2346,13 +2346,14 @@ def _parse_table(

return this

def _parse_unnest(self) -> t.Optional[exp.Unnest]:
def _parse_unnest(self, with_alias: bool = True) -> t.Optional[exp.Unnest]:
if not self._match(TokenType.UNNEST):
return None

expressions = self._parse_wrapped_csv(self._parse_type)
ordinality = self._match_pair(TokenType.WITH, TokenType.ORDINALITY)
alias = self._parse_table_alias()

alias = self._parse_table_alias() if with_alias else None

if alias and self.UNNEST_COLUMN_ONLY:
if alias.args.get("columns"):
Expand Down Expand Up @@ -2792,7 +2793,7 @@ def _parse_is(self, this: t.Optional[exp.Expression]) -> t.Optional[exp.Expressi
return self.expression(exp.Not, this=this) if negate else this

def _parse_in(self, this: t.Optional[exp.Expression], alias: bool = False) -> exp.In:
unnest = self._parse_unnest()
unnest = self._parse_unnest(with_alias=False)
if unnest:
this = self.expression(exp.In, this=this, unnest=unnest)
elif self._match(TokenType.L_PAREN):
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 @@ -6,6 +6,7 @@ class TestBigQuery(Validator):
dialect = "bigquery"

def test_bigquery(self):
self.validate_identity("SELECT foo IN UNNEST(bar) AS bla")
self.validate_identity("SELECT * FROM x-0.a")
self.validate_identity("SELECT * FROM pivot CROSS JOIN foo")
self.validate_identity("SAFE_CAST(x AS STRING)")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def test_column(self):
def test_float(self):
self.assertEqual(parse_one(".2"), parse_one("0.2"))

def test_unnest_projection(self):
expr = parse_one("SELECT foo IN UNNEST(bla) AS bar")
self.assertIsInstance(expr.selects[0], exp.Alias)
self.assertEqual(expr.selects[0].output_name, "bar")

def test_unary_plus(self):
self.assertEqual(parse_one("+15"), exp.Literal.number(15))

Expand Down

0 comments on commit a2deee3

Please sign in to comment.