-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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 range extraction for CNF(conjunctive normal form) | tidb-test=pr/2341 #53908
Merged
ti-chi-bot
merged 3 commits into
pingcap:master
from
ghazalfamilyusa:range_enhancements
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
pkg/planner/core/casetest/index/testdata/index_range_in.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[ | ||
{ | ||
"name": "TestRangeDerivation", | ||
"cases": [ | ||
"select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where (a1>1) or (a1=1 and b1 >= 10) -- simple DNF on (a1,b1) ", | ||
"select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1>1) or (a1=1 and b1 >= 10)) and (c1 > 10) -- -- same as previous example with another conjunct", | ||
"select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where (a1>1) or (a1=1 and b1 > 10) -- simple DNF on (a1,b1) with open interval", | ||
"select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1>1) or (a1=1 and b1 > 10)) and (c1 > 10) -- same as previous example with another conjunct", | ||
"select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1<10) or (a1=10 and b1 < 20)) -- upper bound on (a1,b1)", | ||
"select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1>1) or (a1=1 and b1 > 10)) and ((a1<10) or (a1=10 and b1 < 20)) -- upper and lower bound on (a1,b1)", | ||
"select * from t where (a,b) in ((1,1),(2,2)) and c = 3 -- IN list", | ||
"select * from tuk where a<=>null and b>0 and b<2;", | ||
"select a,b,c from tuk where a>3 and b=4 order by a,c;", | ||
// Same test cases with char type | ||
"select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where (a1>'1') or (a1='1' and b1 >= '10') -- simple DNF on (a1,b1) ", | ||
"select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1>'1') or (a1='1' and b1 >= '10')) and (c1 > '10') -- -- same as previous example with another conjunct", | ||
"select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where (a1>'1') or (a1='1' and b1 > '10') -- simple DNF on (a1,b1) with open interval", | ||
"select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1>'1') or (a1='1' and b1 > '10')) and (c1 > '10') -- same as previous example with another conjunct", | ||
"select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1<'10') or (a1='10' and b1 < '20')) -- upper bound on (a1,b1)", | ||
"select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1>'1') or (a1='1' and b1 > '10')) and ((a1<'10') or (a1='10' and b1 < '20')) -- upper and lower bound on (a1,b1)" | ||
] | ||
} | ||
] |
144 changes: 144 additions & 0 deletions
144
pkg/planner/core/casetest/index/testdata/index_range_out.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
[ | ||
{ | ||
"Name": "TestRangeDerivation", | ||
"Cases": [ | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where (a1>1) or (a1=1 and b1 >= 10) -- simple DNF on (a1,b1) ", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1 range:[1 10,1 +inf], (1,+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1>1) or (a1=1 and b1 >= 10)) and (c1 > 10) -- -- same as previous example with another conjunct", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─Selection 1118.52 cop[tikv] gt(test.t1.c1, 10)", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1 range:[1 10,1 +inf], (1,+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where (a1>1) or (a1=1 and b1 > 10) -- simple DNF on (a1,b1) with open interval", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1 range:(1 10,1 +inf], (1,+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1>1) or (a1=1 and b1 > 10)) and (c1 > 10) -- same as previous example with another conjunct", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─Selection 1118.52 cop[tikv] gt(test.t1.c1, 10)", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1 range:(1 10,1 +inf], (1,+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1<10) or (a1=10 and b1 < 20)) -- upper bound on (a1,b1)", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─TableRangeScan 3356.57 cop[tikv] table:t1 range:[-inf,10), [10 -inf,10 20), keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1,PKX) */ count(*) from t1 where ((a1>1) or (a1=1 and b1 > 10)) and ((a1<10) or (a1=10 and b1 < 20)) -- upper and lower bound on (a1,b1)", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─Selection 1122.61 cop[tikv] or(gt(test.t1.a1, 1), and(eq(test.t1.a1, 1), gt(test.t1.b1, 10))), or(lt(test.t1.a1, 10), and(eq(test.t1.a1, 10), lt(test.t1.b1, 20)))", | ||
" └─TableRangeScan 1403.26 cop[tikv] table:t1 range:[1,1], (1,10), [10,10], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select * from t where (a,b) in ((1,1),(2,2)) and c = 3 -- IN list", | ||
"Plan": [ | ||
"Selection 0.00 root eq(test.t.c, 3)", | ||
"└─Batch_Point_Get 2.00 root table:t, clustered index:PRIMARY(a, b) keep order:false, desc:false" | ||
] | ||
}, | ||
{ | ||
"SQL": "select * from tuk where a<=>null and b>0 and b<2;", | ||
"Plan": [ | ||
"IndexReader 0.25 root index:Selection", | ||
"└─Selection 0.25 cop[tikv] eq(test.tuk.b, 1)", | ||
" └─IndexRangeScan 10.00 cop[tikv] table:tuk, index:a(a, b, c) range:[NULL,NULL], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select a,b,c from tuk where a>3 and b=4 order by a,c;", | ||
"Plan": [ | ||
"IndexReader 3.33 root index:Selection", | ||
"└─Selection 3.33 cop[tikv] eq(test.tuk.b, 4)", | ||
" └─IndexRangeScan 3333.33 cop[tikv] table:tuk, index:a(a, b, c) range:(3,+inf], keep order:true, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where (a1>'1') or (a1='1' and b1 >= '10') -- simple DNF on (a1,b1) ", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1char range:[\"1\" \"10\",\"1\" +inf], (\"1\",+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1>'1') or (a1='1' and b1 >= '10')) and (c1 > '10') -- -- same as previous example with another conjunct", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─Selection 1118.52 cop[tikv] gt(test.t1char.c1, 10)", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1char range:[\"1\" \"10\",\"1\" +inf], (\"1\",+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where (a1>'1') or (a1='1' and b1 > '10') -- simple DNF on (a1,b1) with open interval", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1char range:(\"1\" \"10\",\"1\" +inf], (\"1\",+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1>'1') or (a1='1' and b1 > '10')) and (c1 > '10') -- same as previous example with another conjunct", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─Selection 1118.52 cop[tikv] gt(test.t1char.c1, 10)", | ||
" └─TableRangeScan 3366.67 cop[tikv] table:t1char range:(\"1\" \"10\",\"1\" +inf], (\"1\",+inf], keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1<'10') or (a1='10' and b1 < '20')) -- upper bound on (a1,b1)", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─TableRangeScan 3356.57 cop[tikv] table:t1char range:[-inf,\"10\"), [\"10\" -inf,\"10\" \"20\"), keep order:false, stats:pseudo" | ||
] | ||
}, | ||
{ | ||
"SQL": "select /*+ USE_INDEX(t1char,PKX) */ count(*) from t1char where ((a1>'1') or (a1='1' and b1 > '10')) and ((a1<'10') or (a1='10' and b1 < '20')) -- upper and lower bound on (a1,b1)", | ||
"Plan": [ | ||
"HashAgg 1.00 root funcs:count(Column#5)->Column#4", | ||
"└─TableReader 1.00 root data:HashAgg", | ||
" └─HashAgg 1.00 cop[tikv] funcs:count(1)->Column#5", | ||
" └─Selection 1122.61 cop[tikv] or(gt(test.t1char.a1, \"1\"), and(eq(test.t1char.a1, \"1\"), gt(test.t1char.b1, \"10\"))), or(lt(test.t1char.a1, \"10\"), and(eq(test.t1char.a1, \"10\"), lt(test.t1char.b1, \"20\")))", | ||
" └─TableRangeScan 1403.26 cop[tikv] table:t1char range:[\"1\",\"1\"], (\"1\",\"10\"), [\"10\",\"10\"], keep order:false, stats:pseudo" | ||
] | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
Could you please comment this case " (heuristics applied for long lists or we turn off the intersection)" in front of here?
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.
done