From fea3299e309655903aa5c54e5819096fc01ce88e Mon Sep 17 00:00:00 2001 From: Jo <46752250+GeorgeSittas@users.noreply.github.com> Date: Wed, 14 Jun 2023 20:17:44 +0300 Subject: [PATCH] Fix(parser): don't parse an alias for non-source UNNESTs (#1774) * Fix(parser): don't parse an alias for non-source UNNESTs * Add test --- sqlglot/parser.py | 7 ++++--- tests/dialects/test_bigquery.py | 1 + tests/test_parser.py | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sqlglot/parser.py b/sqlglot/parser.py index 651baec0e7..556ce260c2 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -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"): @@ -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): diff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py index 801b1f7325..26980573e3 100644 --- a/tests/dialects/test_bigquery.py +++ b/tests/dialects/test_bigquery.py @@ -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)") diff --git a/tests/test_parser.py b/tests/test_parser.py index 897357f0cf..96192cdbb3 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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))