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

ddl, planner: support ALTER TABLE without use db #18784

Merged
merged 15 commits into from
Aug 26, 2020
7 changes: 7 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,13 @@ func (p *preprocessor) resolveAlterTableStmt(node *ast.AlterTableStmt) {
if spec.Tp == ast.AlterTableRenameTable {
p.flag |= inCreateOrDropTable
break
xhebox marked this conversation as resolved.
Show resolved Hide resolved
} else if spec.Tp == ast.AlterTableAddConstraint {
xhebox marked this conversation as resolved.
Show resolved Hide resolved
if spec.Constraint.Refer != nil {
xhebox marked this conversation as resolved.
Show resolved Hide resolved
table := spec.Constraint.Refer.Table
if table.Schema.L == "" && node.Table.Schema.L != "" {
table.Schema = model.NewCIStr(node.Table.Schema.L)
}
}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions planner/core/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (s *testValidatorSuite) TestValidator(c *C) {
inPrepare bool
err error
}{
// issue 18756
{"ALTER TABLE test.t ADD CONSTRAINT fk FOREIGN KEY (c2) REFERENCES t (c1)", false, nil},

{"select ?", false, parser.ErrSyntax},
{"select ?", true, nil},
{"create table t(id int not null auto_increment default 2, key (id))", true,
Expand Down Expand Up @@ -228,11 +231,14 @@ func (s *testValidatorSuite) TestValidator(c *C) {
}()
se, err := session.CreateSession4Test(store)
c.Assert(err, IsNil)
_, err = se.Execute(context.Background(), "use test")
c.Assert(err, IsNil)
ctx := se.(sessionctx.Context)
is := infoschema.MockInfoSchema([]*model.TableInfo{core.MockSignedTable()})
for _, tt := range tests {
for k, tt := range tests {
// the first test should be executed without selecting a database
if k == 1 {
_, err = se.Execute(context.Background(), "use test")
c.Assert(err, IsNil)
}
xhebox marked this conversation as resolved.
Show resolved Hide resolved
stmts, err1 := session.Parse(ctx, tt.sql)
c.Assert(err1, IsNil)
c.Assert(stmts, HasLen, 1)
Expand Down