Skip to content

Commit

Permalink
planner: fix same index read plan but has different plan digest (#31560)
Browse files Browse the repository at this point in the history
close #31438
  • Loading branch information
crazycs520 authored Jan 13, 2022
1 parent c6d7ba3 commit 0a3bcc6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion planner/core/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (p *PhysicalIndexReader) ExplainInfo() string {

// ExplainNormalizedInfo implements Plan interface.
func (p *PhysicalIndexReader) ExplainNormalizedInfo() string {
return p.ExplainInfo()
return "index:" + p.indexPlan.TP()
}

func (p *PhysicalIndexReader) accessObject(sctx sessionctx.Context) string {
Expand Down
13 changes: 10 additions & 3 deletions planner/core/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
Expand All @@ -35,7 +36,7 @@ import (
"github.com/pingcap/tidb/util/testutil"
)

var _ = Suite(&testPlanNormalize{})
var _ = SerialSuites(&testPlanNormalize{})

type testPlanNormalize struct {
store kv.Storage
Expand Down Expand Up @@ -286,6 +287,14 @@ func (s *testPlanNormalize) TestNormalizedDigest(c *C) {
s_dist_10 char(24) DEFAULT NULL,
PRIMARY KEY ( s_w_id , s_i_id )
);`)

err := failpoint.Enable("github.com/pingcap/tidb/planner/mockRandomPlanID", "return(true)")
c.Assert(err, IsNil)
defer func() {
err = failpoint.Disable("github.com/pingcap/tidb/planner/mockRandomPlanID")
c.Assert(err, IsNil)
}()

normalizedDigestCases := []struct {
sql1 string
sql2 string
Expand Down Expand Up @@ -404,15 +413,13 @@ func (s *testPlanNormalize) TestNormalizedDigest(c *C) {
}

func testNormalizeDigest(tk *testkit.TestKit, c *C, sql1, sql2 string, isSame bool) {
tk.Se.GetSessionVars().PlanID = 0
tk.MustQuery(sql1)
info := tk.Se.ShowProcess()
c.Assert(info, NotNil)
physicalPlan, ok := info.Plan.(core.PhysicalPlan)
c.Assert(ok, IsTrue)
normalized1, digest1 := core.NormalizePlan(physicalPlan)

tk.Se.GetSessionVars().PlanID = 0
tk.MustQuery(sql2)
info = tk.Se.ShowProcess()
c.Assert(info, NotNil)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/testdata/plan_normalized_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"SQL": "select a+1,b+2 from t1 use index(b) where b=3",
"Plan": [
" Projection root plus(test.t1.a, ?), plus(test.t1.b, ?)",
" └─IndexReader root index:IndexRangeScan_5",
" └─IndexReader root index:IndexRangeScan",
" └─IndexRangeScan cop table:t1, index:b(b), range:[?,?], keep order:false"
]
},
Expand Down
5 changes: 5 additions & 0 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math"
"math/rand"
"runtime/trace"
"strings"
"sync"
Expand Down Expand Up @@ -316,6 +317,10 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
hintProcessor := &hint.BlockHintProcessor{Ctx: sctx}
node.Accept(hintProcessor)

failpoint.Inject("mockRandomPlanID", func() {
sctx.GetSessionVars().PlanID = rand.Intn(1000) // nolint:gosec
})

builder := planBuilderPool.Get().(*plannercore.PlanBuilder)
defer planBuilderPool.Put(builder.ResetForReuse())

Expand Down

0 comments on commit 0a3bcc6

Please sign in to comment.