Skip to content

Commit

Permalink
planner: the issue that incorrect result is returned for indeterminis…
Browse files Browse the repository at this point in the history
…tic function when executing a prepared point-get update. (#21883)
  • Loading branch information
bb7133 committed Dec 19, 2020
1 parent 8af4c30 commit 69f05ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
30 changes: 27 additions & 3 deletions executor/seqtest/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,19 +532,19 @@ func (s *seqTestSuite) TestPreparedUpdate(c *C) {
if flag {
counter.Write(pb)
hit := pb.GetCounter().GetValue()
c.Check(hit, Equals, float64(2))
c.Check(hit, Equals, float64(0))
}
tk.MustExec(`set @a=2,@b=200; execute stmt_update using @b,@a;`)
if flag {
counter.Write(pb)
hit := pb.GetCounter().GetValue()
c.Check(hit, Equals, float64(3))
c.Check(hit, Equals, float64(1))
}
tk.MustExec(`set @a=3,@b=300; execute stmt_update using @b,@a;`)
if flag {
counter.Write(pb)
hit := pb.GetCounter().GetValue()
c.Check(hit, Equals, float64(4))
c.Check(hit, Equals, float64(2))
}

result := tk.MustQuery("select id, c1 from prepare_test where id = ?", 1)
Expand All @@ -556,6 +556,30 @@ func (s *seqTestSuite) TestPreparedUpdate(c *C) {
}
}

func (s *seqTestSuite) TestIssue21884(c *C) {
orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
}()
plannercore.SetPreparedPlanCache(false)

tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists prepare_test")
tk.MustExec("create table prepare_test(a bigint primary key, status bigint, last_update_time datetime)")
tk.MustExec("insert into prepare_test values (100, 0, '2020-12-18 20:00:00')")
tk.MustExec("prepare stmt from 'update prepare_test set status = ?, last_update_time = now() where a = 100'")
tk.MustExec("set @status = 1")
tk.MustExec("execute stmt using @status")
updateTime := tk.MustQuery("select last_update_time from prepare_test").Rows()[0][0]
// Sleep 1 second to make sure `last_update_time` is updated.
time.Sleep(1 * time.Second)
tk.MustExec("execute stmt using @status")
newUpdateTime := tk.MustQuery("select last_update_time from prepare_test").Rows()[0][0]
c.Assert(updateTime == newUpdateTime, IsFalse)
}

func (s *seqTestSuite) TestPreparedDelete(c *C) {
orgEnable := plannercore.PreparedPlanCacheEnabled()
defer func() {
Expand Down
20 changes: 11 additions & 9 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,17 @@ func (e *Execute) tryCachePointPlan(ctx context.Context, sctx sessionctx.Context
return err
}
case *Update:
ok, err = IsPointUpdateByAutoCommit(sctx, p)
if err != nil {
return err
}
if ok {
// make constant expression store paramMarker
sctx.GetSessionVars().StmtCtx.PointExec = true
p, names, err = OptimizeAstNode(ctx, sctx, prepared.Stmt, is)
}
// Temporarily turn off the cache for UPDATE to solve #21884.

//ok, err = IsPointUpdateByAutoCommit(sctx, p)
//if err != nil {
// return err
//}
//if ok {
// // make constant expression store paramMarker
// sctx.GetSessionVars().StmtCtx.PointExec = true
// p, names, err = OptimizeAstNode(ctx, sctx, prepared.Stmt, is)
//}
}
if ok {
// just cache point plan now
Expand Down

0 comments on commit 69f05ea

Please sign in to comment.