Skip to content

fix(bigquery): star referencing outer-scope from correlated subquery - #8291

Open
treysp wants to merge 10 commits into
mainfrom
trey/bq-corr-star
Open

fix(bigquery): star referencing outer-scope from correlated subquery#8291
treysp wants to merge 10 commits into
mainfrom
trey/bq-corr-star

Conversation

@treysp

@treysp treysp commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

SQLGlot incorrectly raises OptimizeError for valid BigQuery syntax when a star expression inside a correlated subquery references an outer-scope table alias or STRUCT path.

This PR preserves unresolved star expressions in correlatable BigQuery scopes during qualification, leaving validation to Bigquery itself.

Examples

Table reference:
SELECT (SELECT AS STRUCT x.* EXCEPT (a)) AS s FROM x;

Struct reference:
SELECT (SELECT AS STRUCT one.* EXCEPT (a_1)) AS s FROM structs;

Common dbt pattern:
SELECT * REPLACE((SELECT AS STRUCT payload.* EXCEPT (x)) AS payload) FROM my_table AS t;

@treysp treysp changed the title Trey/bq corr star fix(bigquery): star referencing outer-scope from correlated subquery Aug 31, 2026
@treysp
treysp requested a lite review from Copilot August 31, 2026 20:51

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@georgesittas georgesittas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@treysp can't we expand the inner/correlated stars as well?

@treysp
treysp marked this pull request as draft September 2, 2026 14:22
@treysp treysp changed the title fix(bigquery): star referencing outer-scope from correlated subquery fix(bigquery): star referencing outer-scope from correlated subquery [CODEX] Sep 3, 2026
@treysp treysp changed the title fix(bigquery): star referencing outer-scope from correlated subquery [CODEX] fix(bigquery): star referencing outer-scope from correlated subquery Sep 3, 2026
@treysp
treysp requested a balanced review from Copilot September 3, 2026 20:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new qualification paths lack regression tests for the documented BigQuery cases.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread sqlglot/optimizer/qualify_columns.py Outdated
treysp and others added 5 commits September 3, 2026 17:30
A star whose qualifier names a source of an outer scope must be expanded in its own
scope, so it is resolved against the resolvers of the scopes it can be correlated
with, reusing the ordering guarantees of the post-order scope traversal. This replaces
the dialect-gated approach and also makes struct stars honor EXCEPT and REPLACE.
@georgesittas
georgesittas marked this pull request as ready for review September 7, 2026 13:44
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