Skip to content

Conversation

@fivetran-MichaelLee
Copy link
Collaborator

@fivetran-MichaelLee fivetran-MichaelLee commented Jan 6, 2026

When BigQuery SQL uses an UNNEST expression where the table alias matches a column name in another table, SQLGlot's qualify optimizer step fails due to perceived ambiguous column name.

To illustrate, this is valid SQL in BigQuery that fails to qualify:

WITH temp_data AS (
  SELECT DATE('2024-01-01') as timeline_date, 100 as val
)
SELECT 
  timeline_date  -- sqlgot can't resolve
FROM 
  UNNEST(GENERATE_DATE_ARRAY('2024-01-01', '2024-01-02', INTERVAL 1 DAY)) AS timeline_date
LEFT JOIN 
  temp_data ON timeline_date = temp_data.timeline_date;

To fix, resolver is updated as follows:

  • get_table will check alias.column[0] for the actual alias when UNNEST_COLUMN_ONLY is true and alias.this is None
  • _get_unambiguous_columns will check ambiguous columns against aliases in unnest sources

@fivetran-MichaelLee fivetran-MichaelLee self-assigned this Jan 6, 2026
@fivetran-MichaelLee fivetran-MichaelLee changed the title fix(optimizer)!: correctly resolve unnest alias shadowing for BigQuery fix(resolver)!: correctly resolve unnest alias shadowing for BigQuery Jan 6, 2026
@fivetran-MichaelLee fivetran-MichaelLee marked this pull request as ready for review January 6, 2026 17:28
@fivetran-MichaelLee fivetran-MichaelLee changed the title fix(resolver)!: correctly resolve unnest alias shadowing for BigQuery fix(resolver): correctly resolve unnest alias shadowing for BigQuery Jan 7, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:mlee/qualify-error, sqlglot version: mlee/qualify-error)
  • baseline (main, sqlglot version: 28.5.1.dev77)

⚠️ Limited to dialects: snowflake, duckdb

By Dialect

dialect main sqlglot:mlee/qualify-error difference links
duckdb -> duckdb 4003/4003 passed (100.0%) 4003/4003 passed (100.0%) No change full result / delta
snowflake -> duckdb 579/847 passed (68.4%) 579/847 passed (68.4%) No change full result / delta
snowflake -> snowflake 847/847 passed (100.0%) 847/847 passed (100.0%) No change full result / delta

Overall

main: 5697 total, 5429 passed (pass rate: 95.3%), sqlglot version: 28.5.1.dev77

sqlglot:mlee/qualify-error: 5697 total, 5429 passed (pass rate: 95.3%), sqlglot version: mlee/qualify-error

Difference: No change

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