diff --git a/ddl/ddl.go b/ddl/ddl.go index 59b2b87081599..d19240f8cf154 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -166,7 +166,7 @@ type DDL interface { GetInformationSchema() infoschema.InfoSchema AlterTable(ctx sessionctx.Context, tableIdent ast.Ident, spec []*ast.AlterTableSpec) error TruncateTable(ctx sessionctx.Context, tableIdent ast.Ident) error - RenameTable(ctx sessionctx.Context, oldTableIdent, newTableIdent ast.Ident) error + RenameTable(ctx sessionctx.Context, oldTableIdent, newTableIdent ast.Ident, isAlterTable bool) error // SetLease will reset the lease time for online DDL change, // it's a very dangerous function and you must guarantee that all servers have the same lease time. SetLease(ctx context.Context, lease time.Duration) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 478f1ccad23eb..fe3651c84e23d 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -994,7 +994,8 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A err = d.AlterColumn(ctx, ident, spec) case ast.AlterTableRenameTable: newIdent := ast.Ident{Schema: spec.NewTable.Schema, Name: spec.NewTable.Name} - err = d.RenameTable(ctx, ident, newIdent) + isAlterTable := true + err = d.RenameTable(ctx, ident, newIdent, isAlterTable) case ast.AlterTableDropPrimaryKey: err = ErrUnsupportedModifyPrimaryKey.GenByArgs("drop") case ast.AlterTableOption: @@ -1619,7 +1620,7 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error { return errors.Trace(err) } -func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident) error { +func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident, isAlterTable bool) error { is := d.GetInformationSchema() oldSchema, ok := is.SchemaByName(oldIdent.Schema) if !ok { @@ -1629,6 +1630,10 @@ func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident) if err != nil { return errFileNotFound.GenByArgs(oldIdent.Schema, oldIdent.Name) } + if isAlterTable && newIdent.Schema.L == oldIdent.Schema.L && newIdent.Name.L == oldIdent.Name.L { + // oldIdent is equal to newIdent, do nothing + return nil + } newSchema, ok := is.SchemaByName(newIdent.Schema) if !ok { return errErrorOnRename.GenByArgs(oldIdent.Schema, oldIdent.Name, newIdent.Schema, newIdent.Name) diff --git a/ddl/ddl_db_test.go b/ddl/ddl_db_test.go index d915dedc2881d..ccf63f0285dcc 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -1609,14 +1609,16 @@ func (s *testDBSuite) TestTruncateTable(c *C) { } func (s *testDBSuite) TestRenameTable(c *C) { - s.testRenameTable(c, "rename table %s to %s") + isAlterTable := false + s.testRenameTable(c, "rename table %s to %s", isAlterTable) } func (s *testDBSuite) TestAlterTableRenameTable(c *C) { - s.testRenameTable(c, "alter table %s rename to %s") + isAlterTable := true + s.testRenameTable(c, "alter table %s rename to %s", isAlterTable) } -func (s *testDBSuite) testRenameTable(c *C, sql string) { +func (s *testDBSuite) testRenameTable(c *C, sql string, isAlterTable bool) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") // for different databases @@ -1659,8 +1661,18 @@ func (s *testDBSuite) testRenameTable(c *C, sql string) { s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) failSQL = fmt.Sprintf(sql, "test1.t2", "test_not_exist.t") s.testErrorCode(c, failSQL, tmysql.ErrErrorOnRename) - failSQL = fmt.Sprintf(sql, "test1.t2", "test1.t2") - s.testErrorCode(c, failSQL, tmysql.ErrTableExists) + + // for the same table name + s.tk.MustExec("use test1") + s.tk.MustExec("create table if not exists t (c1 int, c2 int)") + s.tk.MustExec("create table if not exists t1 (c1 int, c2 int)") + if isAlterTable { + s.tk.MustExec(fmt.Sprintf(sql, "test1.t", "t")) + s.tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.T1")) + } else { + s.testErrorCode(c, fmt.Sprintf(sql, "test1.t", "t"), tmysql.ErrTableExists) + s.testErrorCode(c, fmt.Sprintf(sql, "test1.t1", "test1.T1"), tmysql.ErrTableExists) + } s.tk.MustExec("drop database test1") } diff --git a/executor/ddl.go b/executor/ddl.go index 23bf112f73c5c..3bf220eb50d21 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -93,7 +93,8 @@ func (e *DDLExec) executeRenameTable(s *ast.RenameTableStmt) error { } oldIdent := ast.Ident{Schema: s.OldTable.Schema, Name: s.OldTable.Name} newIdent := ast.Ident{Schema: s.NewTable.Schema, Name: s.NewTable.Name} - err := domain.GetDomain(e.ctx).DDL().RenameTable(e.ctx, oldIdent, newIdent) + isAlterTable := false + err := domain.GetDomain(e.ctx).DDL().RenameTable(e.ctx, oldIdent, newIdent, isAlterTable) return errors.Trace(err) }