Skip to content

fix(substrait): Correctly parse field references in subqueries#20439

Open
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/substrait-fix-subquery
Open

fix(substrait): Correctly parse field references in subqueries#20439
neilconway wants to merge 2 commits intoapache:mainfrom
neilconway:neilc/substrait-fix-subquery

Conversation

@neilconway
Copy link
Contributor

@neilconway neilconway commented Feb 19, 2026

Which issue does this PR close?

Rationale for this change

The substrait consumer parsed field references in correlated subqueries incorrectly. Field references were always resolved relative to the schema of the current (innermost) subquery, leading to incorrect results.

What changes are included in this PR?

We now maintain a stack of outer query schemas, and pushes/pops elements from it as we traverse subqueries. When resolving field references, we now use FieldReference.root_type to detect outer query field references and resolve them against the appropriate schema.

This commit updates the expected results for parsing TPC-H queries, because several of them were parsed incorrectly (the misparsing was probably not detected because the incorrect parse didn't result in any illegal queries, by sheer luck). This also means we can enable Q17, which failed to parse before.

Are these changes tested?

Yes. Test results updated to reflect new, correct behavior, and new unit tests added.

Are there any user-facing changes?

The behavior of the substrait consumer has changed, although the previous behavior was wrong and it seems a bit unlikely anyone would have dependend on it. The DefaultSubstraitConsumer API is slightly changed (new private field).

@neilconway neilconway changed the title fix(substrait): correctly parse field references in subqueries fix(substrait): Correctly parse field references in subqueries Feb 19, 2026
@github-actions github-actions bot added the substrait Changes to the substrait crate label Feb 19, 2026
@neilconway neilconway force-pushed the neilc/substrait-fix-subquery branch from e100c43 to d0437e0 Compare February 20, 2026 15:02
@neilconway
Copy link
Contributor Author

Hi @gabotechs @waynexia Would you have time to take a quick look at this when you get a chance? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

substrait Changes to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Substrait consumer incorrectly parses references to outer query fields

1 participant

Comments