From b2bca120f78ae2c1013940416ee29ea338006d22 Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Tue, 12 May 2020 20:37:11 +0800 Subject: [PATCH 1/6] Fix it. --- planner/core/common_plans.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index dfc896e9b18e3..cf45c46da260a 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -831,7 +831,9 @@ func (e *Explain) RenderResult() error { return err } case ast.ExplainFormatDOT: - e.prepareDotInfo(e.TargetPlan.(PhysicalPlan)) + if physicalPlan, ok := e.TargetPlan.(PhysicalPlan); ok { + e.prepareDotInfo(physicalPlan) + } case ast.ExplainFormatHint: hints := GenHintsFromPhysicalPlan(e.TargetPlan) hints = append(hints, hint.ExtractTableHintsFromStmtNode(e.ExecStmt)...) From 134a972db1963813174c55e3d8e152137c0a2521 Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Tue, 12 May 2020 20:37:49 +0800 Subject: [PATCH 2/6] Add comment for a previous code. --- planner/core/common_plans.go | 1 + 1 file changed, 1 insertion(+) diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index cf45c46da260a..885fe555e49bd 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -944,6 +944,7 @@ func (e *Explain) explainPlanInRowFormat(p Plan, taskType, driverSide, indent st // prepareOperatorInfo generates the following information for every plan: // operator id, estimated rows, task type, access object and other operator info. func (e *Explain) prepareOperatorInfo(p Plan, taskType, driverSide, indent string, isLastChild bool) { + // The `_0` come form a Execute or other plans, which we don't need to explain it. if p.ExplainID().String() == "_0" { return } From 9794329aeb7eb7629da2722683781a97a4f022fa Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Tue, 12 May 2020 21:28:47 +0800 Subject: [PATCH 3/6] Add test. --- executor/explainfor_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index 96ceb1768ae1b..a25dae48a2782 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -185,3 +185,21 @@ func (s *testPrepareSerialSuite) TestExplainForConnPlanCache(c *C) { " └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo", )) } + +func (s *testPrepareSerialSuite) TestExplainDotForExplainPlan(c *C) { + tk := testkit.NewTestKit(c, s.store) + + rows := tk.MustQuery("select connection_id()").Rows() + c.Assert(len(rows), Equals, 1) + connID := rows[0][0].(string) + tk.MustQuery("explain select 1").Check(testkit.Rows( + "Projection_3 1.00 root 1->Column#1", + "└─TableDual_4 1.00 root rows:1", + )) + + tkProcess := tk.Se.ShowProcess() + ps := []*util.ProcessInfo{tkProcess} + tk.Se.SetSessionManager(&mockSessionManager1{PS: ps}) + + tk.MustQuery(fmt.Sprintf("explain for connection %s", connID)).Check(nil) +} From ef3f518c03de61c61a8ed839653cac5df337177e Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Tue, 12 May 2020 21:42:10 +0800 Subject: [PATCH 4/6] remove this comment, so that we can cherry-pick this pr. --- planner/core/common_plans.go | 1 - 1 file changed, 1 deletion(-) diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index 885fe555e49bd..cf45c46da260a 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -944,7 +944,6 @@ func (e *Explain) explainPlanInRowFormat(p Plan, taskType, driverSide, indent st // prepareOperatorInfo generates the following information for every plan: // operator id, estimated rows, task type, access object and other operator info. func (e *Explain) prepareOperatorInfo(p Plan, taskType, driverSide, indent string, isLastChild bool) { - // The `_0` come form a Execute or other plans, which we don't need to explain it. if p.ExplainID().String() == "_0" { return } From 75883d96f019a3c9c120241031caf35c09cef4be Mon Sep 17 00:00:00 2001 From: Zhi Qi <30543181+LittleFall@users.noreply.github.com> Date: Tue, 12 May 2020 21:47:32 +0800 Subject: [PATCH 5/6] Update executor/explainfor_test.go Co-authored-by: Feng Liyuan --- executor/explainfor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index a25dae48a2782..56db1160976b2 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -192,7 +192,7 @@ func (s *testPrepareSerialSuite) TestExplainDotForExplainPlan(c *C) { rows := tk.MustQuery("select connection_id()").Rows() c.Assert(len(rows), Equals, 1) connID := rows[0][0].(string) - tk.MustQuery("explain select 1").Check(testkit.Rows( + tk.MustQuery("explain format="dot" select 1").Check(testkit.Rows( "Projection_3 1.00 root 1->Column#1", "└─TableDual_4 1.00 root rows:1", )) From 65c97840a2bebde1a5ecec503e17e565b4d18427 Mon Sep 17 00:00:00 2001 From: Zhi Qi Date: Tue, 12 May 2020 21:53:55 +0800 Subject: [PATCH 6/6] Add slash for quotation. --- executor/explainfor_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index 56db1160976b2..4a9bb14eabe98 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -192,7 +192,7 @@ func (s *testPrepareSerialSuite) TestExplainDotForExplainPlan(c *C) { rows := tk.MustQuery("select connection_id()").Rows() c.Assert(len(rows), Equals, 1) connID := rows[0][0].(string) - tk.MustQuery("explain format="dot" select 1").Check(testkit.Rows( + tk.MustQuery("explain select 1").Check(testkit.Rows( "Projection_3 1.00 root 1->Column#1", "└─TableDual_4 1.00 root rows:1", )) @@ -201,5 +201,5 @@ func (s *testPrepareSerialSuite) TestExplainDotForExplainPlan(c *C) { ps := []*util.ProcessInfo{tkProcess} tk.Se.SetSessionManager(&mockSessionManager1{PS: ps}) - tk.MustQuery(fmt.Sprintf("explain for connection %s", connID)).Check(nil) + tk.MustQuery(fmt.Sprintf("explain format=\"dot\" for connection %s", connID)).Check(nil) }