WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@fivetran-BradfordPaskewitz
Copy link
Collaborator

@fivetran-BradfordPaskewitz fivetran-BradfordPaskewitz commented Dec 2, 2025

Issue: https://fivetran.atlassian.net/browse/RD-1066808

Problem: In BigQuery, when you UNNEST an ARRAY<STRUCT<...>>, the struct fields can be referenced as unqualified columns (e.g., WHERE type = 'x'). This worked in simple queries but failed in correlated subqueries because sqlglot's type annotation couldn't resolve columns from outer scopes, so qualify_columns didn't know what struct fields to expose.

Solution: Add a fallback mechanism in qualify_columns.py that queries the schema directly when type annotation fails. When processing an UNNEST source, if the expression isn't typed, traverse parent scopes to find the column's type definition in the schema (handling both table sources and CTEs), then extract and expose the struct field names as available columns in that scope.

@tobymao
Copy link
Owner

tobymao commented Dec 2, 2025

can you fix the type annotation instead?

@georgesittas
Copy link
Collaborator

Yeah, I second that suggestion in favor of avoiding making this more complicated than necessary. Keen to understand more if that is tricky to do; let me know and we can hop in a call and chat about this.

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

@fivetran-BradfordPaskewitz keep in mind that you'll need to rebase and apply these changes to resolver.py due to 625654a.

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

@fivetran-BradfordPaskewitz another quick round of comments from me, but this should be good to merge soon.

@tobymao wanna take a look as well? Another pair of eyes is a good idea for this one.

@georgesittas georgesittas requested a review from tobymao December 10, 2025 13:57
Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

Great work @fivetran-BradfordPaskewitz, looks reasonable 👍

@georgesittas georgesittas merged commit 2fd14ed into main Dec 11, 2025
8 checks passed
@georgesittas georgesittas deleted the fix_qualify_unnest branch December 11, 2025 11:59
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.

4 participants