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

executor: merge overlapped ranges when build key ranges for indexJoin (#14596) #14599

Merged
merged 5 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 17 additions & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2215,10 +2215,26 @@ func buildKvRangesForIndexJoin(ctx sessionctx.Context, tableID, indexID int64, l
}
kvRanges = append(kvRanges, tmpKvRanges...)
}
// kvRanges don't overlap each other. So compare StartKey is enough.
// Sort and merge the overlapped ranges.
sort.Slice(kvRanges, func(i, j int) bool {
return bytes.Compare(kvRanges[i].StartKey, kvRanges[j].StartKey) < 0
})
if cwc != nil {
// If cwc is not nil, we need to merge the overlapped ranges here.
mergedKeyRanges := make([]kv.KeyRange, 0, len(kvRanges))
for i := range kvRanges {
if len(mergedKeyRanges) == 0 {
mergedKeyRanges = append(mergedKeyRanges, kvRanges[i])
continue
}
if bytes.Compare(kvRanges[i].StartKey, mergedKeyRanges[len(mergedKeyRanges)-1].EndKey) <= 0 {
mergedKeyRanges[len(mergedKeyRanges)-1].EndKey = kvRanges[i].EndKey
} else {
mergedKeyRanges = append(mergedKeyRanges, kvRanges[i])
}
}
return mergedKeyRanges, nil
}
return kvRanges, nil
}

Expand Down
11 changes: 11 additions & 0 deletions executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,14 @@ func (s *testSuite2) TestIndexJoinPartitionTable(c *C) {
tk.MustExec("insert into t values(1, 27, 2)")
tk.MustQuery("SELECT /*+ TIDB_INLJ(t1) */ count(1) FROM t t1 INNER JOIN (SELECT a, max(c) AS c FROM t WHERE b = 27 AND a = 1 GROUP BY a) t2 ON t1.a = t2.a AND t1.c = t2.c WHERE t1.b = 27").Check(testkit.Rows("1"))
}

func (s *testSuite2) TestIndexJoinMultiCondition(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int not null, b int not null, key idx_a_b(a,b))")
tk.MustExec("create table t2(a int not null, b int not null)")
tk.MustExec("insert into t1 values (0,1), (0,2), (0,3)")
tk.MustExec("insert into t2 values (0,1), (0,2), (0,3)")
tk.MustQuery("select /*+ TIDB_INLJ(t1) */ count(*) from t1, t2 where t1.a = t2.a and t1.b < t2.b").Check(testkit.Rows("3"))
}