Skip to content

Commit

Permalink
planner: adjust the schema when using 'explain for' for verbose format (
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent authored Aug 18, 2021
1 parent 6a1b9fe commit f7fdffc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
42 changes: 42 additions & 0 deletions executor/explainfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,48 @@ func (s *testSerialSuite) TestExplainFor(c *C) {
tkRoot.MustExec(fmt.Sprintf("explain for connection %d", tkRootProcess.ID))
}

func (s *testSerialSuite) TestExplainForVerbose(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("set @@tidb_enable_collect_execution_info=0;")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(id int);")
tk.MustQuery("select * from t1;")
tkRootProcess := tk.Se.ShowProcess()
ps := []*util.ProcessInfo{tkRootProcess}
tk.Se.SetSessionManager(&mockSessionManager1{PS: ps})
tk2.Se.SetSessionManager(&mockSessionManager1{PS: ps})

rs := tk.MustQuery("explain format = 'verbose' select * from t1").Rows()
rs2 := tk2.MustQuery(fmt.Sprintf("explain format = 'verbose' for connection %d", tkRootProcess.ID)).Rows()
c.Assert(len(rs), Equals, len(rs2))
for i := range rs {
c.Assert(rs[i], DeepEquals, rs2[i])
}

tk.MustExec("set @@tidb_enable_collect_execution_info=1;")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t2(id int);")
tk.MustQuery("select * from t2;")
tkRootProcess = tk.Se.ShowProcess()
ps = []*util.ProcessInfo{tkRootProcess}
tk.Se.SetSessionManager(&mockSessionManager1{PS: ps})
tk2.Se.SetSessionManager(&mockSessionManager1{PS: ps})
rs = tk.MustQuery("explain format = 'verbose' select * from t2").Rows()
rs2 = tk2.MustQuery(fmt.Sprintf("explain format = 'verbose' for connection %d", tkRootProcess.ID)).Rows()
c.Assert(len(rs), Equals, len(rs2))
for i := range rs {
// "id", "estRows", "estCost", "task", "access object", "operator info"
c.Assert(len(rs[i]), Equals, 6)
// "id", "estRows", "estCost", "actRows", "task", "access object", "execution info", "operator info", "memory", "disk"
c.Assert(len(rs2[i]), Equals, 10)
for j := 0; j < 3; j++ {
c.Assert(rs[i][j], Equals, rs2[i][j])
}
}
}

func (s *testSerialSuite) TestIssue11124(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
Expand Down
17 changes: 11 additions & 6 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,11 @@ func (e *Explain) prepareSchema() error {
case (format == types.ExplainFormatROW && (!e.Analyze && e.RuntimeStatsColl == nil)) || (format == types.ExplainFormatBrief):
fieldNames = []string{"id", "estRows", "task", "access object", "operator info"}
case format == types.ExplainFormatVerbose:
fieldNames = []string{"id", "estRows", "estCost", "task", "access object", "operator info"}
if e.Analyze || e.RuntimeStatsColl != nil {
fieldNames = []string{"id", "estRows", "estCost", "actRows", "task", "access object", "execution info", "operator info", "memory", "disk"}
} else {
fieldNames = []string{"id", "estRows", "estCost", "task", "access object", "operator info"}
}
case format == types.ExplainFormatROW && (e.Analyze || e.RuntimeStatsColl != nil):
fieldNames = []string{"id", "estRows", "actRows", "task", "access object", "execution info", "operator info", "memory", "disk"}
case format == types.ExplainFormatDOT:
Expand Down Expand Up @@ -1277,12 +1281,13 @@ func (e *Explain) prepareOperatorInfo(p Plan, taskType, driverSide, indent strin
estRows, estCost, accessObject, operatorInfo := e.getOperatorInfo(p, id)

var row []string
if e.Analyze {
actRows, analyzeInfo, memoryInfo, diskInfo := getRuntimeInfo(e.ctx, p, nil)
row = []string{id, estRows, actRows, taskType, accessObject, analyzeInfo, operatorInfo, memoryInfo, diskInfo}
} else if e.RuntimeStatsColl != nil {
if e.Analyze || e.RuntimeStatsColl != nil {
row = []string{id, estRows}
if e.Format == types.ExplainFormatVerbose {
row = append(row, estCost)
}
actRows, analyzeInfo, memoryInfo, diskInfo := getRuntimeInfo(e.ctx, p, e.RuntimeStatsColl)
row = []string{id, estRows, actRows, taskType, accessObject, analyzeInfo, operatorInfo, memoryInfo, diskInfo}
row = append(row, actRows, taskType, accessObject, analyzeInfo, operatorInfo, memoryInfo, diskInfo)
} else {
row = []string{id, estRows}
if e.Format == types.ExplainFormatVerbose {
Expand Down

0 comments on commit f7fdffc

Please sign in to comment.