-
Notifications
You must be signed in to change notification settings - Fork 702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
semantically incorrect transpilation of unnest from duckdb to bigquery #2227
Comments
You can use SELECT
s.col[SAFE_ORDINAL(i)] as col,
s.col_2[SAFE_ORDINAL(i)] as col_2
FROM (
SELECT
[1, 2, 3] as col,
[4, 5] as col_2
) as s CROSS JOIN UNNEST(GENERATE_ARRAY(1, GREATEST(ARRAY_LENGTH([1, 2, 3]), ARRAY_LENGTH([4, 5])))) AS i |
Postgres and DuckDB behave the same here:
|
FWIW I am happy to see sqlglot attempting to handle the unnest-column-to-cross-join problem. It's been a pain the butt to handle in ibis. |
I guess there's implicit unnesting, which when combined with
|
You can reduce it further: SELECT
[1, 2, 3][SAFE_ORDINAL(i)] AS col,
[4, 5][SAFE_ORDINAL(i)] AS col_2
FROM
UNNEST(GENERATE_ARRAY(1, GREATEST(ARRAY_LENGTH([1, 2, 3]), ARRAY_LENGTH([4, 5])))) AS i The general pattern is:
|
There are a few dialects that I am aware of that need this transformation because they don't support
|
this is the best way imo select x, y, z
from
unnest(array[1, 2, 3, 4]) x with offset o1
left join unnest(array[4, 5, 6]) y with offset o2
on o1 = o2
left join unnest(array[7, 8]) z with offset o3
on o1 = o3 |
also remove with ordinality with just offset for consistency closes #2227
also remove with ordinality with just offset for consistency closes #2227
also remove with ordinality with just offset for consistency closes #2227
also remove with ordinality with just offset for consistency closes #2227
also remove with ordinality with just offset for consistency closes #2227
also remove with ordinality with just offset for consistency closes #2227
also remove with ordinality with just offset for consistency closes #2227
Fully reproducible code snippet
Please include a fully reproducible code snippet or the input sql, dialect, and expected output.
First, the Python:
Running this in DuckDB:
And in the BigQuery UI, because
bq shell
doesn't accept the syntax:This should probably compile to something like this:
which gives the same output as DuckDB:
Official Documentation
https://duckdb.org/docs/sql/query_syntax/unnest.html#unnesting-lists
See the second example
The text was updated successfully, but these errors were encountered: