Skip to content

Commit 8a557a7

Browse files
committed
fix!: unnest deep stuff
1 parent 6b7084d commit 8a557a7

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

sqlglot/optimizer/qualify_tables.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def _qualify(table: exp.Table) -> None:
7575
_qualify(node)
7676

7777
for scope in traverse_scope(expression):
78+
for query in scope.subqueries:
79+
subquery = query.parent
80+
if isinstance(subquery, exp.Subquery):
81+
subquery.unwrap().replace(subquery)
82+
7883
for derived_table in scope.derived_tables:
7984
unnested = derived_table.unnest()
8085
if isinstance(unnested, exp.Table):

sqlglot/optimizer/scope.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _collect(self):
156156
self._ctes.append(node)
157157
elif _is_derived_table(node) and _is_from_or_join(node):
158158
self._derived_tables.append(node)
159-
elif isinstance(node, exp.UNWRAPPED_QUERIES):
159+
elif isinstance(node, exp.UNWRAPPED_QUERIES) and not _is_from_or_join(node):
160160
self._subqueries.append(node)
161161
elif isinstance(node, exp.TableColumn):
162162
self._table_columns.append(node)
@@ -821,7 +821,7 @@ def _traverse_udtfs(scope):
821821

822822
sources = {}
823823
for expression in expressions:
824-
if _is_derived_table(expression):
824+
if isinstance(expression, exp.Subquery):
825825
top = None
826826
for child_scope in _traverse_scope(
827827
scope.branch(
@@ -870,10 +870,7 @@ def walk_in_scope(expression, bfs=True, prune=None):
870870

871871
if (
872872
isinstance(node, exp.CTE)
873-
or (
874-
isinstance(node.parent, (exp.From, exp.Join, exp.Subquery))
875-
and _is_derived_table(node)
876-
)
873+
or (isinstance(node.parent, (exp.From, exp.Join)) and _is_derived_table(node))
877874
or (isinstance(node.parent, exp.UDTF) and isinstance(node, exp.Query))
878875
or isinstance(node, exp.UNWRAPPED_QUERIES)
879876
):

tests/fixtures/optimizer/qualify_columns.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ SELECT _q_0.a AS a FROM (SELECT x.a AS a FROM x AS x UNION SELECT x.a AS a FROM
337337

338338
# title: nested subqueries in union
339339
((select a from x where a < 1)) UNION ((select a from x where a > 2));
340-
((SELECT x.a AS a FROM x AS x WHERE x.a < 1)) UNION ((SELECT x.a AS a FROM x AS x WHERE x.a > 2));
340+
(SELECT x.a AS a FROM x AS x WHERE x.a < 1) UNION (SELECT x.a AS a FROM x AS x WHERE x.a > 2);
341341

342342

343343
# dialect: bigquery
@@ -443,6 +443,10 @@ SELECT _q_1.a AS a FROM (SELECT x.a AS a FROM x AS x) AS _q_1 WHERE _q_1.a IN (S
443443
SELECT * FROM table_a as A WHERE A.col1 IN (SELECT MAX(B.col2) FROM table_b as B UNION ALL SELECT MAX(C.col2) FROM table_b as C);
444444
SELECT * FROM table_a AS `A` WHERE `A`.col1 IN (SELECT MAX(`B`.col2) AS _col_0 FROM table_b AS `B` UNION ALL SELECT MAX(`C`.col2) AS _col_0 FROM table_b AS `C`);
445445

446+
# Title: Unnest deep subquery
447+
select * from x where b in ((((select b from y))));
448+
SELECT x.a AS a, x.b AS b FROM x AS x WHERE x.b IN (SELECT y.b AS b FROM y AS y);
449+
446450
--------------------------------------
447451
-- Correlated subqueries
448452
--------------------------------------

0 commit comments

Comments
 (0)