feat: support reference rel with outer references#977
Draft
yongchul wants to merge 1 commit intosubstrait-io:mainfrom
Draft
feat: support reference rel with outer references#977yongchul wants to merge 1 commit intosubstrait-io:mainfrom
yongchul wants to merge 1 commit intosubstrait-io:mainfrom
Conversation
b32b72c to
de7c2b6
Compare
yongchul
commented
Feb 25, 2026
| // The outer reference being declared. This must be a FieldReference with | ||
| // root_type set to OuterReference, matching a FieldReference used inside | ||
| // the Rel of this PlanRel. | ||
| Expression.FieldReference outer_reference = 1; |
Contributor
Author
There was a problem hiding this comment.
Or we could employ more explicit convention like OuterReferencePlaceHolder, which has (rel index, index in the declaration) to reference the declaration in the PlanRel.
The problem of this approach is that it will require rewriting the plan to replace the existing reference with the placeholders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Currently, a
Plancan hold sharedRelsubtrees that are referenced viaReferenceRel(subtree_ordinal)to express DAGs and multi-query optimizations. However, when the sharedRelcontainsOuterReferencefield references (e.g., from a correlated subquery), those outer references become free variables at the plan level — they refer to scopes that don't exist in thePlanRelcontext. EachReferenceRelusage site may appear in a different relational context, so the bindings for those outer references must be supplied per-reference.There is currently no mechanism to express this, which means correlated subqueries with outer references cannot be factored out as shared common subexpressions.
Changes
PlanRel(plan.proto)Added a repeated
OuterReferenceDeclarationfield. Each declaration pairs:FieldReference— identifying the free variable as it appears inside theRelType— the expected type of the valueThis serves as the parameter signature for a parameterized common subexpression. Declarations are required when the
Relcontains outer references.ReferenceRel(algebra.proto)Added a
repeated Expression.FieldReference outer_reference_bindingsfield. Entries are positionally matched to theouter_reference_declarationsin the referencedPlanRel:outer_reference_bindings[i]provides the concrete value forouter_reference_declarations[i], evaluated in theReferenceRel's local relational context.Documentation (logical_relations.md)
Added a section explaining parameterized common subexpressions with a worked example: a single query where two structurally identical correlated subqueries (at different nesting levels, accessing different fields) are factored into a shared
PlanRelwith twoReferenceRelusages providing different positional bindings.AI disclaimer
This PR was assisted by Claude Sonnet 4.6.
This change is