Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: support prepare DDL statements with no parameters #10144

Merged
merged 6 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 6 additions & 3 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,19 @@ 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
_, ok := stmt.(ast.DDLNode)
ian-p-cooke marked this conversation as resolved.
Show resolved Hide resolved
if 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))'")
}