diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 932a6100b9..6a42ddb62a 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1620,6 +1620,10 @@ func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident) if err != nil { return errFileNotFound.GenByArgs(oldIdent.Schema, oldIdent.Name) } + if 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 04fb0cceb2..fb0fa9c1c8 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -1535,6 +1535,7 @@ func (s *testDBSuite) testRenameTable(c *C, sql string) { c.Assert(newTblInfo.Meta().ID, Equals, oldTblID) s.tk.MustQuery("select * from t1").Check(testkit.Rows("1 1", "2 2")) s.tk.MustExec("use test") + // Make sure t doesn't exist. s.tk.MustExec("create table t (c1 int, c2 int)") s.tk.MustExec("drop table t") @@ -1554,12 +1555,19 @@ func (s *testDBSuite) testRenameTable(c *C, sql string) { // for failure case failSQL := fmt.Sprintf(sql, "test_not_exist.t", "test_not_exist.t") s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) + failSQL = fmt.Sprintf(sql, "test.test_not_exist", "test.test_not_exist") + s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test_not_exist.t") 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)") + s.tk.MustExec(fmt.Sprintf(sql, "test1.t", "t")) + s.tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.t1")) s.tk.MustExec("drop database test1") }