fix(dialect): emit FROM UNNEST for FROM-less LATERAL VIEW EXPLODE (#8495) - #8496
Closed
inchang-ing wants to merge 1 commit into
Closed
inchang-ing wants to merge 1 commit into
inchang-ing wants to merge 1 commit into
Conversation
Spark allows `SELECT x LATERAL VIEW EXPLODE(a) AS x` without a FROM clause. When transpiling to Presto/Trino/DuckDB, explode_to_unnest_sql rendered this as a dangling `CROSS JOIN UNNEST`, which has no left relation and is invalid SQL. When the parent SELECT has no FROM, emit the UNNEST as the FROM clause instead. Fixes tobymao#8495
robinld
approved these changes
Oct 3, 2026
Collaborator
|
Thanks, but handling this is out of scope for sqlglot. See linked issue. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Spark allows a
LATERAL VIEW EXPLODEwithout aFROMclause, e.g.:When transpiling to Presto/Trino/DuckDB,
explode_to_unnest_sqlturned this into aCROSS JOIN UNNEST(...), but with noFROMrelation there is nothing on the left of theCROSS JOIN, producing invalid SQL:Root cause
explode_to_unnest_sql(insqlglot/dialects/dialect.py) always wraps theUNNESTin aCROSS JOIN. ACROSS JOINrequires a left relation, which doesn't exist when the parentSELECThas noFROMclause.Fix
When the parent
SELECThas noFROM, emit theUNNESTas theFROMclause instead:The existing
FROM ... CROSS JOIN UNNEST(...)path (used when aFROMis present) is unchanged.Verification
Added a regression test in
tests/dialects/test_presto.pycovering thespark→presto/trino/duckdbtranspilation. Alltest_presto,test_trino, andtest_sparksuites pass.Fixes #8495