release-21.1: sql: fix apply joins when inner plans have subqueries #67570
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.
Backport 1/1 commits from #66442.
Backport 1/1 commits from #67569.
Two commits are squashed.
/cc @cockroachdb/release
Release justification: fix to a bug that could lead to crashes or correctness issues.
Fix is a low risk.
Previously, whenever the inner plan of an apply join was executed, it
would try to refer to the results of the subqueries of the outer plan
because the way we "connect" the subqueries with their results is a bit
fragile - by having an index into subquery plans slice that lives on the
planner
. This could be incorrect when the "inner" plan has its ownsubqueries.
In case when the "inner" plan is referring to the "inner" subqueries,
there are two bug-scenarios:
plan could use the incorrect result, and the whole query silently would
return an incorrect result too;
internal error by the vectorized engine) would occur.
This commit partially mitigates the problem by detecting the scenario
when both "inner" and "outer" plans have subqueries (and returning an
unsupported error) and by updating the planner to point to the "inner"
subqueries when there are no "outer" subqueries.
Fixes: #39433.
Release note (bug fix): Correlated subqueries that couldn't be
decorrelated and that have their own subqueries are now executed
correctly when supported. Note that it is an edge case of an edge case,
so it's unlikely the users have hit this bug (it was found by the
randomized testing).