Skip to content

fix(dialect): emit FROM UNNEST for FROM-less LATERAL VIEW EXPLODE (#8495) - #8496

Closed
inchang-ing wants to merge 1 commit into
tobymao:mainfrom
inchang-ing:fix8495
Closed

inchang-ing wants to merge 1 commit into
tobymao:mainfrom
inchang-ing:fix8495

Conversation

@inchang-ing

Copy link
Copy Markdown
Contributor

Summary

Spark allows a LATERAL VIEW EXPLODE without a FROM clause, e.g.:

SELECT n LATERAL VIEW EXPLODE(SEQUENCE(1, 3)) AS n

When transpiling to Presto/Trino/DuckDB, explode_to_unnest_sql turned this into a CROSS JOIN UNNEST(...), but with no FROM relation there is nothing on the left of the CROSS JOIN, producing invalid SQL:

SELECT n CROSS JOIN UNNEST(SEQUENCE(1, 3)) AS _t0(n)

Root cause

explode_to_unnest_sql (in sqlglot/dialects/dialect.py) always wraps the UNNEST in a CROSS JOIN. A CROSS JOIN requires a left relation, which doesn't exist when the parent SELECT has no FROM clause.

Fix

When the parent SELECT has no FROM, emit the UNNEST as the FROM clause instead:

SELECT n FROM UNNEST(SEQUENCE(1, 3)) AS _t0(n)

The existing FROM ... CROSS JOIN UNNEST(...) path (used when a FROM is present) is unchanged.

Verification

Added a regression test in tests/dialects/test_presto.py covering the spark → presto/trino/duckdb transpilation. All test_presto, test_trino, and test_spark suites pass.

Fixes #8495

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
@georgesittas

Copy link
Copy Markdown
Collaborator

Thanks, but handling this is out of scope for sqlglot. See linked issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spark FROM-less LATERAL VIEW EXPLODE produces invalid Trino CROSS JOIN UNNEST

3 participants