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

planner: fix incorrect results when using a prefix index with OR condition (#21251) #21287

Merged
merged 6 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions util/ranger/detacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func detachDNFCondAndBuildRangeForIndex(sctx sessionctx.Context, condition *expr
}
}

// Take prefix index into consideration.
if hasPrefix(lengths) {
fixPrefixColRange(totalRanges, lengths, newTpSlice)
}
totalRanges, err := UnionRanges(sc, totalRanges)
if err != nil {
return nil, nil, false, errors.Trace(err)
Expand Down
36 changes: 36 additions & 0 deletions util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,42 @@ func (s *testRangerSuite) TestIndexStringIsTrueRange(c *C) {
}
}

func (s *testRangerSuite) TestPrefixIndexMultiColDNF(c *C) {
defer testleak.AfterTest(c)()
dom, store, err := newDomainStoreWithBootstrap(c)
defer func() {
dom.Close()
store.Close()
}()
c.Assert(err, IsNil)
testKit := testkit.NewTestKit(c, store)
testKit.MustExec("use test;")
testKit.MustExec("drop table if exists t2;")
testKit.MustExec("create table t2 (id int unsigned not null auto_increment primary key, t text, index(t(3)));")
testKit.MustExec("insert into t2 (t) values ('aaaa'),('a');")

var input []string
var output []struct {
SQL string
Plan []string
Result []string
}
s.testData.GetTestCases(c, &input, &output)
inputLen := len(input)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = s.testData.ConvertRowsToStrings(testKit.MustQuery("explain " + tt).Rows())
output[i].Result = s.testData.ConvertRowsToStrings(testKit.MustQuery(tt).Rows())
})
testKit.MustQuery("explain " + tt).Check(testkit.Rows(output[i].Plan...))
testKit.MustQuery(tt).Check(testkit.Rows(output[i].Result...))
if i+1 == inputLen/2 {
testKit.MustExec("analyze table t2;")
}
}
}

func (s *testRangerSuite) TestIndexRangeForYear(c *C) {
defer testleak.AfterTest(c)()
dom, store, err := newDomainStoreWithBootstrap(c)
Expand Down
9 changes: 9 additions & 0 deletions util/ranger/testdata/ranger_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@
"explain select * from t0 where c0 and c0 in ('123','456','789')",
"explain SELECT * FROM t0 WHERE ('a' != t0.c0) AND t0.c0;"
]
},
{
"name": "TestPrefixIndexMultiColDNF",
"cases": [
"select * from t2 where t='aaaa';",
"select * from t2 where t='aaaa' or t = 'a';",
"select * from t2 where t='aaaa';",
"select * from t2 where t='aaaa' or t = 'a';"
]
}
]
53 changes: 53 additions & 0 deletions util/ranger/testdata/ranger_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,58 @@
]
}
]
},
{
"Name": "TestPrefixIndexMultiColDNF",
"Cases": [
{
"SQL": "select * from t2 where t='aaaa';",
"Plan": [
"IndexLookUp_11 10.00 root ",
"├─IndexRangeScan_8(Build) 10.00 cop[tikv] table:t2, index:t(t) range:[\"aaa\",\"aaa\"], keep order:false, stats:pseudo",
"└─Selection_10(Probe) 10.00 cop[tikv] eq(test.t2.t, \"aaaa\")",
" └─TableRowIDScan_9 10.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
],
"Result": [
"1 aaaa"
]
},
{
"SQL": "select * from t2 where t='aaaa' or t = 'a';",
"Plan": [
"IndexLookUp_11 16.00 root ",
"├─IndexRangeScan_8(Build) 20.00 cop[tikv] table:t2, index:t(t) range:[\"a\",\"a\"], [\"aaa\",\"aaa\"], keep order:false, stats:pseudo",
"└─Selection_10(Probe) 16.00 cop[tikv] or(eq(test.t2.t, \"aaaa\"), eq(test.t2.t, \"a\"))",
" └─TableRowIDScan_9 20.00 cop[tikv] table:t2 keep order:false, stats:pseudo"
],
"Result": [
"1 aaaa",
"2 a"
]
},
{
"SQL": "select * from t2 where t='aaaa';",
"Plan": [
"TableReader_7 1.00 root data:Selection_6",
"└─Selection_6 1.00 cop[tikv] eq(test.t2.t, \"aaaa\")",
" └─TableRangeScan_5 2.00 cop[tikv] table:t2 range:[0,+inf], keep order:false"
],
"Result": [
"1 aaaa"
]
},
{
"SQL": "select * from t2 where t='aaaa' or t = 'a';",
"Plan": [
"TableReader_7 1.60 root data:Selection_6",
"└─Selection_6 1.60 cop[tikv] or(eq(test.t2.t, \"aaaa\"), eq(test.t2.t, \"a\"))",
" └─TableRangeScan_5 2.00 cop[tikv] table:t2 range:[0,+inf], keep order:false"
],
"Result": [
"1 aaaa",
"2 a"
]
}
]
}
]