Skip to content

Commit

Permalink
executor: fix issue of explain for connection can't show the last que…
Browse files Browse the repository at this point in the history
…ry plan (#21242) (#21315)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Nov 26, 2020
1 parent 1f90ac8 commit f567009
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,28 @@ func (cli *testServerClient) runTestConcurrentUpdate(c *C) {
})
}

func (cli *testServerClient) runTestExplainForConn(c *C) {
cli.runTestsOnNewDB(c, nil, "explain_for_conn", func(dbt *DBTest) {
dbt.mustExec("drop table if exists t")
dbt.mustExec("create table t (a int key, b int)")
dbt.mustExec("insert t values (1, 1)")
rows := dbt.mustQuery("select connection_id();")
c.Assert(rows.Next(), IsTrue)
var connID int64
err := rows.Scan(&connID)
c.Assert(err, IsNil)
c.Assert(rows.Close(), IsNil)
dbt.mustQuery("select * from t where a=1")
rows = dbt.mustQuery("explain for connection " + strconv.Itoa(int(connID)))
c.Assert(rows.Next(), IsTrue)
row := make([]string, 9)
err = rows.Scan(&row[0], &row[1], &row[2], &row[3], &row[4], &row[5], &row[6], &row[7], &row[8])
c.Assert(err, IsNil)
c.Assert(strings.Join(row, ","), Matches, "Point_Get_1,1.00,1,root,table:t,time.*loop.*handle:1.*")
c.Assert(rows.Close(), IsNil)
})
}

func (cli *testServerClient) runTestErrorCode(c *C) {
cli.runTestsOnNewDB(c, nil, "ErrorCode", func(dbt *DBTest) {
dbt.mustExec("create table test (c int PRIMARY KEY);")
Expand Down
4 changes: 4 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (ts *tidbTestSerialSuite) TestLoadData(c *C) {
ts.runTestLoadDataForSlowLog(c, ts.server)
}

func (ts *tidbTestSerialSuite) TestExplainFor(c *C) {
ts.runTestExplainForConn(c)
}

func (ts *tidbTestSerialSuite) TestStmtCount(c *C) {
ts.runTestStmtCount(c)
}
Expand Down
10 changes: 10 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,16 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
MaxExecutionTime: maxExecutionTime,
RedactSQL: s.sessionVars.EnableRedactLog,
}
if p == nil {
// Store the last valid plan when the current plan is nil.
// This is for `explain for connection` statement has the ability to query the last valid plan.
oldPi := s.ShowProcess()
if oldPi != nil && oldPi.Plan != nil && len(oldPi.PlanExplainRows) > 0 {
pi.Plan = oldPi.Plan
pi.PlanExplainRows = oldPi.PlanExplainRows
pi.RuntimeStatsColl = oldPi.RuntimeStatsColl
}
}
_, pi.Digest = s.sessionVars.StmtCtx.SQLDigest()
s.currentPlan = nil
if s.sessionVars.User != nil {
Expand Down

0 comments on commit f567009

Please sign in to comment.