diff --git a/pkg/store/copr/coprocessor_test.go b/pkg/store/copr/coprocessor_test.go index fe061b5698660..a3fef83fd785b 100644 --- a/pkg/store/copr/coprocessor_test.go +++ b/pkg/store/copr/coprocessor_test.go @@ -439,6 +439,12 @@ func TestSplitKeyRangesByLocationsWithoutBuckets(t *testing.T) { rangeEqual(t, locRanges[1].Ranges.ToRanges(), "g", "h", "h", "m", "n") rangeEqual(t, locRanges[2].Ranges.ToRanges(), "n", "t") rangeEqual(t, locRanges[3].Ranges.ToRanges(), "v", "w") + + locRanges, err = cache.SplitKeyRangesByLocationsWithoutBuckets(bo, NewKeyRanges(BuildKeyRanges("a", "b", "v", "w")), UnspecifiedLimit) + require.NoError(t, err) + require.Len(t, locRanges, 2) + rangeEqual(t, locRanges[0].Ranges.ToRanges(), "a", "b") + rangeEqual(t, locRanges[1].Ranges.ToRanges(), "v", "w") } func TestSplitKeyRanges(t *testing.T) { diff --git a/pkg/store/copr/region_cache.go b/pkg/store/copr/region_cache.go index 7883dc13c0166..eb64e29dde464 100644 --- a/pkg/store/copr/region_cache.go +++ b/pkg/store/copr/region_cache.go @@ -154,9 +154,11 @@ func (c *RegionCache) splitKeyRangesByLocation(loc *tikv.KeyLocation, ranges *Ke } } else { // rs[i] is not in the region. - taskRanges := ranges.Slice(0, i) - res = append(res, &LocationKeyRanges{Location: loc, Ranges: taskRanges}) - ranges = ranges.Slice(i, ranges.Len()) + if i > 0 { + taskRanges := ranges.Slice(0, i) + res = append(res, &LocationKeyRanges{Location: loc, Ranges: taskRanges}) + ranges = ranges.Slice(i, ranges.Len()) + } } return res, ranges, false } @@ -206,12 +208,12 @@ func (c *RegionCache) SplitKeyRangesByLocationsWithoutBuckets(bo *Backoffer, ran } res := make([]*LocationKeyRanges, 0, resCap) + nextLocIndex := 0 for ranges.Len() > 0 { if limit != UnspecifiedLimit && len(res) >= limit { break } - nextLocIndex := len(res) if nextLocIndex >= len(locs) { err = errors.Errorf("Unexpected loc index %d, which should less than %d", nextLocIndex, len(locs)) return nil, err @@ -223,6 +225,7 @@ func (c *RegionCache) SplitKeyRangesByLocationsWithoutBuckets(bo *Backoffer, ran res = append(res, &LocationKeyRanges{Location: loc, Ranges: ranges}) break } + nextLocIndex++ isBreak := false res, ranges, isBreak = c.splitKeyRangesByLocation(loc, ranges, res)