Optimizer: Fix range intersection for CNF(conjunctive normal form) | tidb-test=pr/2350
What problem does this PR solve?
Issue Number: close #54337
Problem Summary:
Current code does not optimally find index range when we have the filter as complex conjuncts. A complex cojunct in this context is DNF. The code does well for each DNF and builds a union of ranges but for the CNF part, it does not do full intersection. The code does intersection only for point ranges which may not as selective as the full intersection. There is a simple example under https://github.com/tikv/tikv/issues/16798 that shows the optimization limitation.
What changed and how does it work?
We added code does full intersection of ranges. For example, if we have ((a1>1) or (a1=1 and b1 > 10)) and ((a1<10) or (a1=10 and b1 < 20)) then there are two conjuncts :
- ((a1>1) or (a1=1 and b1 > 10))
- ((a1<10) or (a1=10 and b1 < 20)) The first produces intervals : (1 10,1 +inf], (1,+inf] and second intervals is [-inf,10), [10 -inf,10 20) and their intersection is (1 10,1 +inf], (1,10), [10 -inf,10 20). Before this fix, the code just produce point ranges as [10,10], (1,1].
Check List
Tests
- [X] 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.
Optimize query performance by pushing all relevant range conditions to table and index range scans.
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.
Codecov Report
Attention: Patch coverage is 74.54545% with 42 lines in your changes missing coverage. Please review.
Project coverage is 54.8637%. Comparing base (
c71eece) to head (ee412bb). Report is 1 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #54166 +/- ##
=================================================
- Coverage 72.8111% 54.8637% -17.9475%
=================================================
Files 1534 1657 +123
Lines 435579 609485 +173906
=================================================
+ Hits 317150 334386 +17236
- Misses 98804 252537 +153733
- Partials 19625 22562 +2937
| Flag | Coverage Δ | |
|---|---|---|
| integration | 20.2130% <7.8787%> (?) |
|
| unit | 71.8181% <74.5454%> (+0.0023%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Components | Coverage Δ | |
|---|---|---|
| dumpling | 52.9656% <ø> (ø) |
|
| parser | ∅ <ø> (∅) |
|
| br | 46.3806% <ø> (+0.3196%) |
:arrow_up: |
/test mysql-test
@ghazalfamilyusa: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.
In response to this:
/test mysql-test
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.
Please supply the release note on the PR comment
Please supply the release note on the PR comment
Done
/ok-to-test
/test fast_test_tiprow
@ghazalfamilyusa: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:
/test build/test check-dev/test check-dev2/test mysql-test/test pull-br-integration-test/test pull-integration-ddl-test/test pull-lightning-integration-test/test pull-mysql-client-test/test unit-test
The following commands are available to trigger optional jobs:
/test canary-notify-when-compatibility-sections-changed/test pingcap/tidb/canary_ghpr_unit_test/test pull-common-test/test pull-e2e-test/test pull-integration-common-test/test pull-integration-copr-test/test pull-integration-jdbc-test/test pull-integration-mysql-test/test pull-integration-nodejs-test/test pull-sqllogic-test/test pull-tiflash-test
Use /test all to run the following jobs that were automatically triggered:
pingcap/tidb/ghpr_buildpingcap/tidb/ghpr_checkpingcap/tidb/ghpr_check2pingcap/tidb/ghpr_mysql_testpingcap/tidb/ghpr_unit_testpingcap/tidb/pull_integration_ddl_testpingcap/tidb/pull_mysql_client_test
In response to this:
/test fast_test_tiprow
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/test-infra repository.
/test unit-test
@ghazalfamilyusa: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:
/test fast_test_tiprow/test tidb_parser_test
Use /test all to run all jobs.
In response to this:
/test unit-test
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.
/test unit-test
@ghazalfamilyusa: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:
/test fast_test_tiprow/test tidb_parser_test
Use /test all to run all jobs.
In response to this:
/test unit-test
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.
/retest
/retest
for, "https://github.com/pingcap/tidb/pull/54166#discussion_r1662913214"
The test cases cover these combinations. I do not think it is a good idea to add examples as comments unless it is something simple and not covering combinations like this. We should have comments in the code and I hope I did that. The logic is no complicated. If one value is not equal to the other in the suffix then it is simple. The extra logic for cases where they are equal in the common length and it goes through cases of n1 == n2, n1 > n2 and n1 < n2. For each case, there is additional logic for lower/upper bounds.
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: elsa0520, time-and-fate
The full list of commands accepted by this bot can be found here.
The pull request process is described here
- ~~OWNERS~~ [elsa0520,time-and-fate]
- ~~pkg/planner/OWNERS~~ [elsa0520,time-and-fate]
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
[LGTM Timeline notifier]
Timeline:
2024-07-01 05:37:51.318121421 +0000 UTC m=+1216397.803610255: :ballot_box_with_check: agreed by elsa0520.2024-07-03 06:24:50.322838738 +0000 UTC m=+1392016.808327570: :ballot_box_with_check: agreed by time-and-fate.
/retest
/retest
