Skip to content

Commit

Permalink
Fix(parser)!: expand single VALUES clause in CTE into a SELECT * (#4617)
Browse files Browse the repository at this point in the history
* Fix(parser): expand single VALUES clause in CTE into a SELECT *

* Add an alias to the values clause
  • Loading branch information
georgesittas authored Jan 15, 2025
1 parent 9ba1db3 commit 8b465d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3197,14 +3197,19 @@ def _parse_cte(self) -> t.Optional[exp.CTE]:
else:
materialized = None

return self.expression(
cte = self.expression(
exp.CTE,
this=self._parse_wrapped(self._parse_statement),
alias=alias,
materialized=materialized,
comments=comments,
)

if isinstance(cte.this, exp.Values):
cte.set("this", exp.select("*").from_(exp.alias_(cte.this, "_values", table=True)))

return cte

def _parse_table_alias(
self, alias_tokens: t.Optional[t.Collection[TokenType]] = None
) -> t.Optional[exp.TableAlias]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def test_with(self):
)
self.validate(
"WITH A(filter) AS (VALUES 1, 2, 3) SELECT * FROM A WHERE filter >= 2",
"WITH A(filter) AS (VALUES (1), (2), (3)) SELECT * FROM A WHERE filter >= 2",
"WITH A(filter) AS (SELECT * FROM (VALUES (1), (2), (3)) AS _values) SELECT * FROM A WHERE filter >= 2",
read="presto",
)
self.validate(
Expand Down

0 comments on commit 8b465d4

Please sign in to comment.