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 uncorrect behavior of index join #11724

Merged
merged 7 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,4 +1390,13 @@ func (s *testSuite2) TestIssue11544(c *C) {
tk.MustExec("insert into 11544t values(1)")
tk.MustExec("insert into 11544tt values(1, 'aaaaaaa'), (1, 'aaaabbb'), (1, 'aaaacccc')")
tk.MustQuery("select /*+ TIDB_INLJ(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Rows("1 1 aaaaaaa", "1 1 aaaabbb"))
tk.MustQuery("select /*+ TIDB_INLJ(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc')").Check(testkit.Rows("1 1 aaaaaaa", "1 1 aaaabbb", "1 1 aaaacccc"))
}

func (s *testSuite2) TestIssue11390(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table 11390t (k1 int unsigned, k2 int unsigned, key(k1, k2))")
tk.MustExec("insert into 11390t values(1, 1)")
tk.MustQuery("select /*+ TIDB_INLJ(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1;").Check(testkit.Rows("1 1 1 1"))
}
37 changes: 11 additions & 26 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,42 +950,27 @@ func (ijHelper *indexJoinBuildHelper) buildTemplateRange(matchedKeyCnt int, eqAn
HighVal: make([]types.Datum, pointLength, pointLength),
})
}
emptyRow := chunk.Row{}
sc := ijHelper.join.ctx.GetSessionVars().StmtCtx
for i, j := 0, 0; j < len(eqAndInFuncs); i++ {
// This position is occupied by join key.
if ijHelper.curIdxOff2KeyOff[i] != -1 {
continue
}
sf := eqAndInFuncs[j].(*expression.ScalarFunction)
// Deal with the first two args.
if _, ok := sf.GetArgs()[0].(*expression.Column); ok {
for _, ran := range ranges {
ran.LowVal[i], err = sf.GetArgs()[1].Eval(emptyRow)
if err != nil {
return nil, err
}
ran.HighVal[i] = ran.LowVal[i]
}
} else {
for _, ran := range ranges {
ran.LowVal[i], err = sf.GetArgs()[0].Eval(emptyRow)
if err != nil {
return nil, err
}
ran.HighVal[i] = ran.LowVal[i]
}
oneColumnRan, err := ranger.BuildColumnRange([]expression.Expression{eqAndInFuncs[j]}, sc, ijHelper.curNotUsedIndexCols[j].RetType, ijHelper.curNotUsedColLens[j])
if err != nil {
return nil, err
}
for _, ran := range ranges {
ran.LowVal[i] = oneColumnRan[0].LowVal[0]
ran.HighVal[i] = oneColumnRan[0].HighVal[0]
}
// If the length of in function's constant list is more than one, we will expand ranges.
curRangeLen := len(ranges)
for argIdx := 2; argIdx < len(sf.GetArgs()); argIdx++ {
for ranIdx := 1; ranIdx < len(oneColumnRan); ranIdx++ {
newRanges := make([]*ranger.Range, 0, curRangeLen)
for oldRangeIdx := 0; oldRangeIdx < curRangeLen; oldRangeIdx++ {
newRange := ranges[oldRangeIdx].Clone()
newRange.LowVal[i], err = sf.GetArgs()[argIdx].Eval(emptyRow)
if err != nil {
return nil, err
}
newRange.HighVal[i] = newRange.LowVal[i]
newRange.LowVal[i] = oneColumnRan[ranIdx].LowVal[0]
newRange.HighVal[i] = oneColumnRan[ranIdx].HighVal[0]
newRanges = append(newRanges, newRange)
}
ranges = append(ranges, newRanges...)
Expand Down
9 changes: 9 additions & 0 deletions util/ranger/ranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,15 @@ func (s *testRangerSuite) TestColumnRange(c *C) {
resultStr: "[[\"a\",\"a\"]]",
length: 1,
},
// This test case cannot be simplified to [1, 3] otherwise the index join will executes wrongly.
Copy link
Member Author

Choose a reason for hiding this comment

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

Use this test to assert that there won't be range merging in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why can't merge to [1,3]? Because of the prefix index?

{
colPos: 0,
exprStr: "a in (1, 2, 3)",
accessConds: "[in(test.t.a, 1, 2, 3)]",
filterConds: "",
resultStr: "[[1,1] [2,2] [3,3]]",
length: types.UnspecifiedLength,
},
}

ctx := context.Background()
Expand Down