Skip to content

Feat(snowflake): improve transpilation of unnested object lookup#5234

Merged
georgesittas merged 2 commits intomainfrom
jo/transpile_unnest_obj_access_bq_to_sf
Jun 17, 2025
Merged

Feat(snowflake): improve transpilation of unnested object lookup#5234
georgesittas merged 2 commits intomainfrom
jo/transpile_unnest_obj_access_bq_to_sf

Conversation

@georgesittas
Copy link
Collaborator

@georgesittas georgesittas commented Jun 16, 2025

This PR:

  • Introduces a new transformation for Snowflake, in order to transpile BigQuery queries with unnests to it
  • Modifies the walk_in_scope logic to stop considering plain UDTFs without Query children as scope boundaries

We incorrectly transpile BigQuery queries like the following to Snowflake today:

import sqlglot
print(sqlglot.transpile("select _u.foo from unnest([struct('x' as foo, 'y' AS bar)]) as _u", "bigquery", "snowflake")[0])
# SELECT _u.foo FROM TABLE(FLATTEN(INPUT => [OBJECT_CONSTRUCT('foo', 'x', 'bar', 'y')])) AS _t0(seq, key, path, index, _u, this)

The resulting query fails with Error: invalid identifier '_U.FOO' (line 1), because the value _u is a VARIANT, and so accessing its contents using the dot notation is not allowed. Instead, Snowflake requires that we use either the colon (x:y) or the bracket syntax x['y'] for this reason.

To address this, the new transformation looks for unnests that have a single column alias, and then replaces any columns in scope that are qualified with said alias.

I looked into Snowflake's docs for TABLE(FLATTEN(...)), and I believe the expected receivers of the lookup operation should be OBJECT or VARIANT values, for which it is safe to replace the dot syntax with the bracket notation.

@georgesittas georgesittas force-pushed the jo/transpile_unnest_obj_access_bq_to_sf branch from 660ac91 to e78f8c8 Compare June 17, 2025 10:05
@georgesittas georgesittas merged commit 62da84a into main Jun 17, 2025
7 checks passed
@georgesittas georgesittas deleted the jo/transpile_unnest_obj_access_bq_to_sf branch June 17, 2025 10:07
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.

2 participants