Skip to content

Conversation

@georgesittas
Copy link
Collaborator

Transpiling BigQuery queries to Snowflake is not always correct today. One example is:

-- BigQuery input
SELECT x FROM UNNEST([STRUCT('x' AS x)])

-- Snowflake output, produced by SQLGlot's transpiler
SELECT x FROM TABLE(FLATTEN(INPUT => [OBJECT_CONSTRUCT('x', 'x')])) AS _u(seq, key, path, index, value, this)

The transpiler's output is not valid Snowflake, because the identifier x referenced in the projection list does not exist. Instead, only the identifiers mentioned in the table alias are visible, and accessing x requires extracting it from value.

To address this issue, I implemented a rule that conditionally qualifies UNNEST sources and the columns they produce, if we can statically determine them.

After this rule is applied, a followup rule, already in effect today, takes care of converting the qualified columns into the proper bracket expressions used to extract values out of VARIANT objects, which leads to a correct transpilation.

@georgesittas georgesittas force-pushed the jo/transpile_bq_unnest_unqualified_cols_snowflake branch from 9a2d50a to 4f3d04e Compare July 15, 2025 17:52
@georgesittas georgesittas merged commit a95993a into main Jul 16, 2025
6 checks passed
@georgesittas georgesittas deleted the jo/transpile_bq_unnest_unqualified_cols_snowflake branch July 16, 2025 08:37
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.

3 participants