Skip to content

Commit

Permalink
executor: fix the wrong output when the whole data of partition table…
Browse files Browse the repository at this point in the history
… falls in one region
  • Loading branch information
winoros committed Apr 4, 2023
1 parent c9d322c commit 998c5f1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4388,7 +4388,7 @@ func (builder *dataReaderBuilder) buildIndexReaderForIndexJoin(ctx context.Conte
if err != nil {
return nil, err
}
err = e.open(ctx, kvRanges)
err = e.open(ctx, kv.NewNonParitionedKeyRanges(kvRanges))
return e, err
}

Expand Down
19 changes: 12 additions & 7 deletions executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ func (e *IndexReaderExecutor) Open(ctx context.Context) error {
}

sc := e.ctx.GetSessionVars().StmtCtx
var kvRanges []kv.KeyRange
var kvRanges *kv.KeyRanges
if len(e.partitions) > 0 {
keyRanges := make([][]kv.KeyRange, 0, len(e.partitions))
for _, p := range e.partitions {
partRange := e.ranges
if pRange, ok := e.partRangeMap[p.GetPhysicalID()]; ok {
Expand All @@ -281,10 +282,15 @@ func (e *IndexReaderExecutor) Open(ctx context.Context) error {
if err != nil {
return err
}
kvRanges = append(kvRanges, kvRange...)
keyRanges = append(keyRanges, kvRange)
e.kvRanges = append(e.kvRanges, kvRange...)
}
kvRanges = kv.NewPartitionedKeyRanges(keyRanges)
} else {
kvRanges, err = e.buildKeyRanges(sc, e.ranges, e.physicalTableID)
var keyRanges []kv.KeyRange
keyRanges, err = e.buildKeyRanges(sc, e.ranges, e.physicalTableID)
kvRanges = kv.NewNonParitionedKeyRanges(keyRanges)
e.kvRanges = keyRanges
}
if err != nil {
return err
Expand All @@ -293,7 +299,7 @@ func (e *IndexReaderExecutor) Open(ctx context.Context) error {
return e.open(ctx, kvRanges)
}

func (e *IndexReaderExecutor) open(ctx context.Context, kvRanges []kv.KeyRange) error {
func (e *IndexReaderExecutor) open(ctx context.Context, kvRanges *kv.KeyRanges) error {
var err error
if e.corColInFilter {
e.dagPB.Executors, err = constructDistExec(e.ctx, e.plans)
Expand All @@ -306,7 +312,6 @@ func (e *IndexReaderExecutor) open(ctx context.Context, kvRanges []kv.KeyRange)
collExec := true
e.dagPB.CollectExecutionSummaries = &collExec
}
e.kvRanges = kvRanges
// Treat temporary table as dummy table, avoid sending distsql request to TiKV.
// In a test case IndexReaderExecutor is mocked and e.table is nil.
// Avoid sending distsql request to TIKV.
Expand All @@ -320,11 +325,11 @@ func (e *IndexReaderExecutor) open(ctx context.Context, kvRanges []kv.KeyRange)
e.memTracker = memory.NewTracker(e.id, -1)
}
e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker)
slices.SortFunc(kvRanges, func(i, j kv.KeyRange) bool {
kvRanges.SortByFunc(func(i, j kv.KeyRange) bool {
return bytes.Compare(i.StartKey, j.StartKey) < 0
})
var builder distsql.RequestBuilder
builder.SetKeyRanges(kvRanges).
builder.SetWrappedKeyRanges(kvRanges).
SetDAGRequest(e.dagPB).
SetStartTS(e.startTS).
SetDesc(e.desc).
Expand Down
2 changes: 1 addition & 1 deletion executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func TestOrderByandLimit(t *testing.T) {
x := rand.Intn(1099)
y := rand.Intn(2000) + 1
queryRangePartition := fmt.Sprintf("select /*+ LIMIT_TO_COP() */ a from trange use index(idx_a) where a > %v order by a limit %v;", x, y)
queryHashPartition := fmt.Sprintf("select /*+ LIMIT_TO_COP() */ a from trange use index(idx_a) where a > %v order by a limit %v;", x, y)
queryHashPartition := fmt.Sprintf("select /*+ LIMIT_TO_COP() */ a from thash use index(idx_a) where a > %v order by a limit %v;", x, y)
queryRegular := fmt.Sprintf("select a from tregular use index(idx_a) where a > %v order by a limit %v;", x, y)
require.True(t, tk.HasPlan(queryRangePartition, "IndexReader")) // check if indexReader is used
require.True(t, tk.HasPlan(queryHashPartition, "IndexReader"))
Expand Down

0 comments on commit 998c5f1

Please sign in to comment.