Skip to content

Commit

Permalink
cherry pick pingcap#22101 to release-5.0-rc
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
wshwsh12 authored and ti-srebot committed Jan 4, 2021
1 parent 10c5cf7 commit c6e5103
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1115,16 +1115,20 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
MaxExecutionTime: maxExecutionTime,
RedactSQL: s.sessionVars.EnableRedactLog,
}
oldPi := s.ShowProcess()
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
}
}
// We set process info before building plan, so we extended execution time.
if oldPi != nil && oldPi.Info == pi.Info {
pi.Time = oldPi.Time
}
_, pi.Digest = s.sessionVars.StmtCtx.SQLDigest()
// DO NOT reset the currentPlan to nil until this query finishes execution, otherwise reentrant calls
// of SetProcessInfo would override Plan and PlanExplainRows to nil.
Expand Down Expand Up @@ -1227,6 +1231,10 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex
return nil, err
}

// Uncorrelated subqueries will execute once when building plan, so we reset process info before building plan.
cmd32 := atomic.LoadUint32(&s.GetSessionVars().CommandValue)
s.SetProcessInfo(stmtNode.Text(), time.Now(), byte(cmd32), 0)

// Transform abstract syntax tree to a physical plan(stored in executor.ExecStmt).
compiler := executor.Compiler{Ctx: s}
stmt, err := compiler.Compile(ctx, stmtNode)
Expand Down
36 changes: 36 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3830,3 +3830,39 @@ func (s *testSessionSerialSuite) TestDefaultWeekFormat(c *C) {
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustQuery("select week('2020-02-02'), @@default_week_format, week('2020-02-02');").Check(testkit.Rows("6 4 6"))
}
<<<<<<< HEAD
=======

func (s *testSessionSerialSuite) TestIssue21944(c *C) {
tk1 := testkit.NewTestKitWithInit(c, s.store)
_, err := tk1.Exec("set @@tidb_current_ts=1;")
c.Assert(err.Error(), Equals, "[variable:1238]Variable 'tidb_current_ts' is a read only variable")
}

func (s *testSessionSerialSuite) TestIssue21943(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
_, err := tk.Exec("set @@last_plan_from_binding='123';")
c.Assert(err.Error(), Equals, "[variable:1238]Variable 'last_plan_from_binding' is a read only variable")

_, err = tk.Exec("set @@last_plan_from_cache='123';")
c.Assert(err.Error(), Equals, "[variable:1238]Variable 'last_plan_from_cache' is a read only variable")
}

func (s *testSessionSerialSuite) TestProcessInfoIssue22068(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t(a int)")
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
tk.MustQuery("select 1 from t where a = (select sleep(5));").Check(testkit.Rows())
wg.Done()
}()
time.Sleep(2 * time.Second)
pi := tk.Se.ShowProcess()
c.Assert(pi, NotNil)
c.Assert(pi.Info, Equals, "select 1 from t where a = (select sleep(5));")
c.Assert(pi.Plan, IsNil)
wg.Wait()
}
>>>>>>> 57cd69473... session: set process info before building plan (#22101)

0 comments on commit c6e5103

Please sign in to comment.