executor,server: fix pessimistic "insert select for update" return malformed packet (#20584)

Signed-off-by: lysu <sulifx@gmail.com>
This commit is contained in:
lysu
2020-10-27 19:50:24 +08:00
committed by GitHub
parent 272f964031
commit 3ed192eb0d
2 changed files with 22 additions and 1 deletions

View File

@ -763,7 +763,7 @@ func (a *ExecStmt) buildExecutor() (Executor, error) {
}
e = executorExec.stmtExec
}
a.isSelectForUpdate = b.hasLock && (!stmtCtx.InDeleteStmt && !stmtCtx.InUpdateStmt)
a.isSelectForUpdate = b.hasLock && (!stmtCtx.InDeleteStmt && !stmtCtx.InUpdateStmt && !stmtCtx.InInsertStmt)
return e, nil
}

View File

@ -931,3 +931,24 @@ func (ts *tidbTestSuite) TestNullFlag(c *C) {
expectFlag := uint16(tmysql.NotNullFlag | tmysql.BinaryFlag)
c.Assert(dumpFlag(cols[0].Type, cols[0].Flag), Equals, expectFlag)
}
func (ts *tidbTestSuite) TestPessimisticInsertSelectForUpdate(c *C) {
qctx, err := ts.tidbdrv.OpenCtx(uint64(0), 0, uint8(tmysql.DefaultCollationID), "test", nil)
c.Assert(err, IsNil)
ctx := context.Background()
_, err = Execute(ctx, qctx, "use test;")
c.Assert(err, IsNil)
_, err = Execute(ctx, qctx, "drop table if exists t1, t2")
c.Assert(err, IsNil)
_, err = Execute(ctx, qctx, "create table t1 (id int)")
c.Assert(err, IsNil)
_, err = Execute(ctx, qctx, "create table t2 (id int)")
c.Assert(err, IsNil)
_, err = Execute(ctx, qctx, "insert into t1 select 1")
c.Assert(err, IsNil)
_, err = Execute(ctx, qctx, "begin pessimistic")
c.Assert(err, IsNil)
rs, err := Execute(ctx, qctx, "INSERT INTO t2 (id) select id from t1 where id = 1 for update")
c.Assert(err, IsNil)
c.Assert(rs, IsNil) // should be no delay
}