Refactor how we manage outer scopes in join iteration#3428
Merged
Conversation
…ns in apply_indexes_from_outer_scope.go
zachmu
approved these changes
Feb 13, 2026
Member
zachmu
left a comment
There was a problem hiding this comment.
Certainly a big improvement on what came before. Can't shake the feeling that we need to allocate result rows once at a top level and pin all field indexes at plan construction time.
sql/analyzer/resolve_subqueries.go
Outdated
| for _, joinParent := range joinParents { | ||
| if sqa.OuterScopeVisibility && joinParent != nil { | ||
| if stripChild, ok := joinParent.Right().(*plan.StripRowNode); ok && stripChild.Child == sqa { | ||
| // if joinParent.Right() == sqa { |
sql/rowexec/join_iters.go
Outdated
| return i.fullRow[i.parentLen : i.parentLen+i.leftLen] | ||
| } | ||
|
|
||
| // leftColumns returns the values most recently yielded from the secondary/right child node. |
sql/rowexec/join_iters.go
Outdated
| i.secondary = nil | ||
| if err != nil { | ||
| return nil, err | ||
| // Close cleans up the iterator by recusrively closing the children iterators. |
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.
There were several problems with the previous join iterator implementation:
The behavior with subqueries is the main motivation for this PR.
Previously, in order to expose values from outer scopes to iterators, we would dynamically inject PrependNodes into subquery build plans. These nodes would insert values into the beginning of returned rows, allowing parent iterators to read them and use them in expressions. To compensate for this, we would also inject StripRowNodes into joins. StripRowNodes are the opposite of PrependNodes, removing columns from their iterators.
This logic was incredibly difficult to reason about correctly:
-- Lateral joins would re-include all the values from the outermost scope in the next scope, effectively doubling the number of columns with each nesting level.
-- StripRowNodes would only be generated based on the innermost scope, resulting in some injected values not being removed.
-- StripRowNodes would be generated under each join node, including between join nodes in a multi-table join. Join nodes would thus would need to re-insert these values in order to compensate... but were expected to not re-insert columns corresponding to values defined by a parent join node, except for lateral joins... reasoning about this correctly quickly becomes untenable.
Ultimately, there's no reason why join nodes can't handle this directly. And removing the StripRowNodes type and replacing it with logic in the join iterators actually makes the logic much more consistent: Parent iterators should assume that all child iterators contain prepended values for outer scopes, and values determined by the node's schema, and nothing else. And the parent iterator returns rows that also have this property.