diff --git a/sqlglot/parser.py b/sqlglot/parser.py index ae3f6cde66..65f596a3df 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -3197,7 +3197,7 @@ 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, @@ -3205,6 +3205,11 @@ def _parse_cte(self) -> t.Optional[exp.CTE]: 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]: diff --git a/tests/test_transpile.py b/tests/test_transpile.py index 5e50c3c902..bb8d032f6d 100644 --- a/tests/test_transpile.py +++ b/tests/test_transpile.py @@ -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(