Skip to content

Commit

Permalink
executor: merge overlapped ranges when build key ranges for indexJoin (
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Feb 4, 2020
1 parent e304158 commit 6441e1f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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"))
}

0 comments on commit 6441e1f

Please sign in to comment.