Skip to content

Commit

Permalink
executor: support prepare DDL statements with no parameters (#10144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-p-cooke authored and ngaut committed May 6, 2019
1 parent 0b037aa commit c9cc3b7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion executor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
ErrGetStartTS = terror.ClassExecutor.New(codeGetStartTS, "Can not get start ts")
ErrUnknownPlan = terror.ClassExecutor.New(codeUnknownPlan, "Unknown plan")
ErrPrepareMulti = terror.ClassExecutor.New(codePrepareMulti, "Can not prepare multiple statements")
ErrPrepareDDL = terror.ClassExecutor.New(codePrepareDDL, "Can not prepare DDL statements")
ErrPrepareDDL = terror.ClassExecutor.New(codePrepareDDL, "Can not prepare DDL statements with parameters")
ErrResultIsEmpty = terror.ClassExecutor.New(codeResultIsEmpty, "result is empty")
ErrBuildExecutor = terror.ClassExecutor.New(codeErrBuildExec, "Failed to build executor")
ErrBatchInsertFail = terror.ClassExecutor.New(codeBatchInsertFail, "Batch insert failed, please clean the table and try again.")
Expand Down
8 changes: 5 additions & 3 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,18 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.RecordBatch) error {
return ErrPrepareMulti
}
stmt := stmts[0]
if _, ok := stmt.(ast.DDLNode); ok {
return ErrPrepareDDL
}
err = ResetContextOfStmt(e.ctx, stmt)
if err != nil {
return err
}
var extractor paramMarkerExtractor
stmt.Accept(&extractor)

// DDL Statements can not accept parameters
if _, ok := stmt.(ast.DDLNode); ok && len(extractor.markers) > 0 {
return ErrPrepareDDL
}

// Prepare parameters should NOT over 2 bytes(MaxUint16)
// https://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html#packet-COM_STMT_PREPARE_OK.
if len(extractor.markers) > math.MaxUint16 {
Expand Down
8 changes: 8 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ func (s *testSuite1) TestPreparedNameResolver(c *C) {
_, err = tk.Exec("prepare stmt from '(select * FROM t) union all (select * FROM t) order by a limit ?'")
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'order clause'")
}

// a 'create table' DDL statement should be accepted if it has no parameters.
func (s *testSuite1) TestPreparedDDL(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("prepare stmt from 'create table t (id int, KEY id (id))'")
}

0 comments on commit c9cc3b7

Please sign in to comment.