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

planner: fix same index read plan but has different plan digest (#31560) #31654

Open
wants to merge 2 commits into
base: release-5.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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