-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
release-22.1: opt: do not generate unnecessary cross-joins on join input #79504
release-22.1: opt: do not generate unnecessary cross-joins on join input #79504
Conversation
This commit fixes a bug that caused unnecessary cross-joins on the input of lookup joins, causing both suboptimal query plans and incorrect query results. The bug only affected lookup joins with lookup expressions. Fixes cockroachdb#79384 Release note (bug fix): A bug has been fixed that caused the optimizer to generate query plans with logically incorrect lookup joins. The bug can only occur in queries with an inner join, e.g., `t1 JOIN t2`, if all of the following are true: 1. The join contains an equality condition between columns of both tables, e.g., `t1.a = t2.a`. 2. A query filter or `CHECK` constraint constrains a column to a set of specific values, e.g., `t2.b IN (1, 2, 3)`. In the case of a `CHECK` constraint, the column must be `NOT NULL`. 3. A query filter or `CHECK` constraint constrains a column to a range, e.g., `t2.c > 0`. In the case of a `CHECK` constraint, the column must be `NOT NULL`. 4. An index contains a column from each of the criteria above, e.g., `INDEX t2(a, b, c)`. This bug has been present since version 21.2.0.
In cockroachdb#78685, we prevented `GenerateLookupJoins` from incorrect creating a cross-join on the input of a semi-join, addressing cockroachdb#78681. This commit addresses the same issue with `GenerateInvertedJoins`, which we originally forgot to fix. Informs cockroachdb#78681 Release note (bug fix): A bug has been fixed which caused the optimizer to generate invalid query plans which could result in incorrect query results. The bug, which has been present since version 21.1.0, can appear if all of the following conditions are true: 1. The query contains a semi-join, such as queries in the form `SELECT * FROM a WHERE EXISTS (SELECT * FROM b WHERE a.a @> b.b)`. 2. The inner table has a multi-column inverted index containing the inverted column in the filter. 3. The index prefix columns are constrained to a set of values via the filter or a `CHECK` constraint, e.g., with an `IN` operator. In the case of a `CHECK` constraint, the column is `NOT NULL`.
Thanks for opening a backport. Please check the backport criteria before merging:
If some of the basic criteria cannot be satisfied, ensure that the exceptional criteria are satisfied within.
Add a brief release justification to the body of your PR to justify this backport. Some other things to consider:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 8 of 8 files at r1, 3 of 3 files at r2, all commit messages.
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @mgartner)
Backport 2/2 commits from #79389.
/cc @cockroachdb/release
opt: do not generate unnecessary cross-joins on lookup join input
This commit fixes a bug that caused unnecessary cross-joins on the input
of lookup joins, causing both suboptimal query plans and incorrect query
results. The bug only affected lookup joins with lookup expressions.
Fixes #79384
Release note (bug fix): A bug has been fixed that caused the optimizer
to generate query plans with logically incorrect lookup joins. The bug
can only occur in queries with an inner join, e.g.,
t1 JOIN t2
, if allof the following are true:
tables, e.g.,
t1.a = t2.a
.CHECK
constraint constrains a column to a setof specific values, e.g.,
t2.b IN (1, 2, 3)
. In the case of aCHECK
constraint, the column must beNOT NULL
.CHECK
constraint constrains a column to arange, e.g.,
t2.c > 0
. In the case of aCHECK
constraint, thecolumn must be
NOT NULL
.INDEX t2(a, b, c)
.This bug has been present since version 21.2.0.
opt: do not cross-join input of inverted semi-join
In #78685, we prevented
GenerateLookupJoins
from incorrect creating across-join on the input of a semi-join, addressing #78681. This commit
addresses the same issue with
GenerateInvertedJoins
, which weoriginally forgot to fix.
Informs #78681
Release note (bug fix): A bug has been fixed which caused the optimizer
to generate invalid query plans which could result in incorrect query
results. The bug, which has been present since version 21.1.0, can
appear if all of the following conditions are true:
SELECT * FROM a WHERE EXISTS (SELECT * FROM b WHERE a.a @> b.b)
.inverted column in the filter.
filter or a
CHECK
constraint, e.g., with anIN
operator. In thecase of a
CHECK
constraint, the column isNOT NULL
.Release justification: Fixes a correctness bug in the optimizer.