Skip to content

Commit

Permalink
nested selectp parsing fixes #454
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Sep 20, 2022
1 parent c38fa8c commit 45603f1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
28 changes: 11 additions & 17 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def _parse_create(self):
this if isinstance(this, exp.Schema) else None
)
if self._match(TokenType.ALIAS):
expression = self._parse_select()
expression = self._parse_select(nested=True)

return self.expression(
exp.Create,
Expand Down Expand Up @@ -823,7 +823,7 @@ def _parse_insert(self):
this=self._parse_table(schema=True),
exists=self._parse_exists(),
partition=self._parse_partition(),
expression=self._parse_select(),
expression=self._parse_select(nested=True),
overwrite=overwrite,
)

Expand Down Expand Up @@ -877,7 +877,7 @@ def _parse_cache(self):
this=table,
lazy=lazy,
options=options,
expression=self._parse_select(),
expression=self._parse_select(nested=True),
)

def _parse_partition(self):
Expand Down Expand Up @@ -906,9 +906,7 @@ def _parse_value(self):
self._match_r_paren()
return self.expression(exp.Tuple, expressions=expressions)

def _parse_select(self, table=None):
index = self._index

def _parse_select(self, nested=False, table=False):
if self._match(TokenType.SELECT):
hint = self._parse_hint()
all_ = self._match(TokenType.ALL)
Expand Down Expand Up @@ -972,15 +970,11 @@ def _parse_select(self, table=None):
)
else:
self.raise_error(f"{this.key} does not support CTE")
elif self._match(TokenType.L_PAREN):
this = self._parse_table() if table else self._parse_select()

if this:
self._parse_query_modifiers(this)
self._match_r_paren()
this = self._parse_subquery(this)
else:
self._retreat(index)
elif (table or nested) and self._match(TokenType.L_PAREN):
this = self._parse_table() if table else self._parse_select(nested=True)
self._parse_query_modifiers(this)
self._match_r_paren()
this = self._parse_subquery(this)
elif self._match(TokenType.VALUES):
this = self.expression(
exp.Values, expressions=self._parse_csv(self._parse_value)
Expand Down Expand Up @@ -1377,7 +1371,7 @@ def _parse_set_operations(self, this):
expression,
this=this,
distinct=self._match(TokenType.DISTINCT) or not self._match(TokenType.ALL),
expression=self._parse_select(),
expression=self._parse_select(nested=True),
)

def _parse_expression(self):
Expand Down Expand Up @@ -1626,7 +1620,7 @@ def _parse_primary(self):
self._match_r_paren()

if isinstance(this, exp.Subqueryable):
return self._parse_subquery(this)
return self._parse_set_operations(self._parse_subquery(this))
if len(expressions) > 1:
return self.expression(exp.Tuple, expressions=expressions)
return self.expression(exp.Paren, this=this)
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ CACHE LAZY TABLE x OPTIONS('storageLevel' = 'value') AS SELECT 1
CACHE LAZY TABLE x OPTIONS('storageLevel' = 'value') AS WITH a AS (SELECT 1) SELECT a.* FROM a
CACHE LAZY TABLE x AS WITH a AS (SELECT 1) SELECT a.* FROM a
CACHE TABLE x AS WITH a AS (SELECT 1) SELECT a.* FROM a
CACHE TABLE x AS (SELECT 1 AS y)
CALL catalog.system.iceberg_procedure_name(named_arg_1 => 'arg_1', named_arg_2 => 'arg_2')
INSERT OVERWRITE TABLE a.b PARTITION(ds) SELECT x FROM y
INSERT OVERWRITE TABLE a.b PARTITION(ds='YYYY-MM-DD') SELECT x FROM y
Expand Down Expand Up @@ -513,3 +514,4 @@ SELECT ? AS ? FROM x WHERE b BETWEEN ? AND ? GROUP BY ?, 1 LIMIT ?
WITH a AS ((SELECT b.foo AS foo, b.bar AS bar FROM b) UNION ALL (SELECT c.foo AS foo, c.bar AS bar FROM c)) SELECT * FROM a
WITH a AS ((SELECT 1 AS b) UNION ALL (SELECT 1 AS b)) SELECT * FROM a
SELECT (WITH x AS (SELECT 1 AS y) SELECT * FROM x) AS z
SELECT ((SELECT 1) + 1)

0 comments on commit 45603f1

Please sign in to comment.