Skip to content
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

Optimizer: Fix null filtering logic for IN list #53370

Merged
merged 3 commits into from
May 21, 2024

Conversation

ghazalfamilyusa
Copy link
Contributor

@ghazalfamilyusa ghazalfamilyusa commented May 17, 2024

What problem does this PR solve?

Issue Number: close #49476

Problem Summary:

Incorrect outer to inner join conversion for some cases of IN list incorrectly treated as null filtering.
An example in #49476

CREATE TABLE t0(c0 INT);
CREATE TABLE t1(c0 BOOLEAN);
INSERT INTO t0 (c0) VALUES (0);

SELECT * FROM t1 NATURAL RIGHT JOIN t0; -- 0
SELECT (false IN (t1.c0, t0.c0)) FROM t1 NATURAL RIGHT JOIN t0; -- 1
SELECT * FROM t1 NATURAL RIGHT JOIN t0 WHERE (false IN (t1.c0, t0.c0));
-- Expected: 0
-- Actual: Empty set

The problem is in the constant folding logic when it is used to evaluate null rejection logic.
The logic is risky and just fills in constant 1 for non-inner side. In the example above the IN list becomes
false in (null,1) which is false (null filter is true).

What changed and how does it work?

Fix done at the top level code for null filtering logic used in outer to inner. The fix use the OR logic instead of IN list.
For example, alse IN (t1.c0, t0.c0)) is checked for nullability using (false = t1.c0) or (false = t0.c0) which is not null filter.
Also, this PR consolidates the null rejection code from outer to inner code and the general utility code. This includes cleans up of previous special cases.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added release-note-none do-not-merge/needs-triage-completed sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 17, 2024
Copy link

tiprow bot commented May 17, 2024

Hi @ghazalfamilyusa. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link

codecov bot commented May 17, 2024

Codecov Report

Attention: Patch coverage is 95.16129% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 73.0911%. Comparing base (f5ac93e) to head (03dc40b).
Report is 16 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #53370        +/-   ##
================================================
+ Coverage   72.6008%   73.0911%   +0.4902%     
================================================
  Files          1505       1523        +18     
  Lines        429827     439569      +9742     
================================================
+ Hits         312058     321286      +9228     
+ Misses        98576      98362       -214     
- Partials      19193      19921       +728     
Flag Coverage Δ
integration 46.4585% <95.1612%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 53.9957% <ø> (ø)
parser ∅ <ø> (∅)
br 42.0941% <ø> (+0.6934%) ⬆️

@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 18, 2024
@hawkingrei
Copy link
Member

/ok-to-test

@winoros
Copy link
Member

winoros commented May 19, 2024

I got the idea!

Copy link

ti-chi-bot bot commented May 21, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-05-19 16:31:34.010884399 +0000 UTC m=+2016447.768019971: ☑️ agreed by winoros.
  • 2024-05-21 07:39:46.720460062 +0000 UTC m=+2157340.477595635: ☑️ agreed by qw4990.

return specialNullRejectedCase1(ctx, schema, expr) // only 1 case now
// isNullRejectedInList checks null filter for IN list using OR logic.
// Reason is that null filtering through evaluation by isNullRejectedSimpleExpr
// has problems with IN list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add a case here to explain why we have to treat IN-LIST as OR here. The rest LGTM!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if the in-list contains a column from the non-inner side, then it can never pass the NF check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation for this case is shown above and in the issue. I guess you guys think it is better to add it in the code. Added that to the comment.

Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM


// isNullRejectedSimpleExpr check whether a condition is null-rejected
// A condition would be null-rejected in one of following cases:
// If it is a predicate containing a reference to an inner table that evaluates to UNKNOWN or FALSE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the comment should be refined as well, "inner table" has no ideas with

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added explanation to inner table as "null producing side"

Copy link

ti-chi-bot bot commented May 21, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, qw4990, winoros

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot merged commit 61a2981 into pingcap:master May 21, 2024
23 checks passed
@ghazalfamilyusa ghazalfamilyusa deleted the nullability_bug branch May 21, 2024 17:06
@ghazalfamilyusa ghazalfamilyusa restored the nullability_bug branch May 21, 2024 17:06
@ghazalfamilyusa ghazalfamilyusa deleted the nullability_bug branch May 21, 2024 17:06
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #53459.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 21, 2024
RidRisR pushed a commit to RidRisR/tidb that referenced this pull request May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm ok-to-test release-note-none sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected Results of IN expression With NATURAL RIGHT JOIN
6 participants