Skip to content

Commit

Permalink
fix: don't unnest subqueries when the parent select doesn't have a fr…
Browse files Browse the repository at this point in the history
…om since the subquery can't be joined (#2233)
  • Loading branch information
ginter authored Sep 15, 2023
1 parent c2238a5 commit 8509d52
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqlglot/optimizer/unnest_subqueries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def unnest(select, parent_select, next_alias_name):
predicate = select.find_ancestor(exp.Condition)
alias = next_alias_name()

if not predicate or parent_select is not predicate.parent_select:
if (
not predicate
or parent_select is not predicate.parent_select
or not parent_select.args.get("from")
):
return

# This subquery returns a scalar and can just be converted to a cross join
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/optimizer/unnest_subqueries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,15 @@ WHERE
)
AND ARRAY_ALL(_u_19."", _x -> _x = x.a)
AND x.a > COALESCE(_u_21.d, 0);
SELECT
CAST((
SELECT
x.a AS a
FROM x
) AS TEXT) AS a;
SELECT
CAST((
SELECT
x.a AS a
FROM x
) AS TEXT) AS a;

0 comments on commit 8509d52

Please sign in to comment.