release-20.2: opt: prevent cycle in memo created by SplitDisjunction #58437
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 #58434.
/cc @cockroachdb/release
opt: prevent cycle in memo created by SplitDisjunction
Previously, the
SplitDisjunction
rule would reuse table and column IDsfrom the rule's input expressions in the left side of the generated
UnionAll
expression. This was incorrectly believed to be safe. Theaddition of partial indexes has provided an example that highlights the
flaw: this can create cycles in the memo.
Consider the table and query below:
This leads to a cycle in the memo because
tab_c_idx
can be scanned toretrieve rows where
a > 1
and wherea > 1 OR b > 1
. Notice the cyclein the memo below:
G2 -> G6 -> G10 -> G2
.G2
includes both anunconstrained partial index scan a
DistinctOn
created bySplitDisjunction
. The child of theDistinctOn
is aUnionAll
(G7
)with a left child of
G10
.G10
has two expressions, one of which is aSelect
with an input ofG2
.In order to prevent this cycle,
SplitDisjunction
now creates new tableand column IDs for both its left and right inputs. I've been unable to
find an example where table and column ID reuse in
SplitDisjuctionAddKey
causes problems, but I've updated that rule toerr on the side of caution.
Fixes #58390
Release justification: This is a critical fix for a bug that causes
errors querying tables with disjunctive filters that are the same or
similar to the predicate of one of the table's partial indexes.
Release note (bug fix): A bug has been fixed which caused errors when
querying a table with a disjunctive filter (an
OR
expression) that isthe same or similar to the predicate of one of the table's partial
indexes.