diff --git a/pkg/ddl/column.go b/pkg/ddl/column.go index ee2e1304ebae5..f575aaf255e91 100644 --- a/pkg/ddl/column.go +++ b/pkg/ddl/column.go @@ -1304,8 +1304,8 @@ func getChangingColumnOriginName(changingColumn *model.ColumnInfo) string { return columnName[:pos] } -func getExpressionIndexOriginName(expressionIdx *model.ColumnInfo) string { - columnName := strings.TrimPrefix(expressionIdx.Name.O, expressionIndexPrefix+"_") +func getExpressionIndexOriginName(originalName pmodel.CIStr) string { + columnName := strings.TrimPrefix(originalName.O, expressionIndexPrefix+"_") var pos int if pos = strings.LastIndex(columnName, "_"); pos == -1 { return columnName diff --git a/pkg/ddl/db_integration_test.go b/pkg/ddl/db_integration_test.go index ae397fc44ea9d..1d8821f4a3bf8 100644 --- a/pkg/ddl/db_integration_test.go +++ b/pkg/ddl/db_integration_test.go @@ -1333,7 +1333,8 @@ func assertAlterWarnExec(tk *testkit.TestKit, t *testing.T, sql string) { } func TestAlterAlgorithm(t *testing.T) { - store := testkit.CreateMockStore(t, mockstore.WithDDLChecker()) + store, dom := testkit.CreateMockStoreAndDomain(t, mockstore.WithDDLChecker()) + ddlChecker := dom.DDL().(*schematracker.Checker) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") @@ -1376,6 +1377,20 @@ func TestAlterAlgorithm(t *testing.T) { tk.MustExec("alter table t rename index idx_c2 to idx_c, ALGORITHM=INSTANT") tk.MustExec("alter table t rename index idx_c to idx_c1, ALGORITHM=DEFAULT") + // Test corner case for renameIndexes + tk.MustExec(`create table tscalar(c1 int, col_1_1 int, key col_1(col_1_1))`) + tk.MustExec("alter table tscalar rename index col_1 to col_2") + tk.MustExec("admin check table tscalar") + tk.MustExec("drop table tscalar") + + // Test rename index with scalar function + ddlChecker.Disable() + tk.MustExec(`create table tscalar(id int, col_1 json, KEY idx_1 ((cast(col_1 as char(64) array))))`) + tk.MustExec("alter table tscalar rename index idx_1 to idx_1_1") + tk.MustExec("admin check table tscalar") + tk.MustExec("drop table tscalar") + ddlChecker.Enable() + // partition. assertAlterWarnExec(tk, t, "alter table t ALGORITHM=COPY, truncate partition p1") assertAlterWarnExec(tk, t, "alter table t ALGORITHM=INPLACE, truncate partition p2") diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index 897f788c26542..2f292703bff7d 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -2611,12 +2611,19 @@ func renameIndexes(tblInfo *model.TableInfo, from, to pmodel.CIStr) { idx.Name.L = strings.Replace(idx.Name.L, from.L, to.L, 1) idx.Name.O = strings.Replace(idx.Name.O, from.O, to.O, 1) } + for _, col := range idx.Columns { + originalCol := tblInfo.Columns[col.Offset] + if originalCol.Hidden && getExpressionIndexOriginName(col.Name) == from.O { + col.Name.L = strings.Replace(col.Name.L, from.L, to.L, 1) + col.Name.O = strings.Replace(col.Name.O, from.O, to.O, 1) + } + } } } func renameHiddenColumns(tblInfo *model.TableInfo, from, to pmodel.CIStr) { for _, col := range tblInfo.Columns { - if col.Hidden && getExpressionIndexOriginName(col) == from.O { + if col.Hidden && getExpressionIndexOriginName(col.Name) == from.O { col.Name.L = strings.Replace(col.Name.L, from.L, to.L, 1) col.Name.O = strings.Replace(col.Name.O, from.O, to.O, 1) } diff --git a/pkg/ddl/schematracker/BUILD.bazel b/pkg/ddl/schematracker/BUILD.bazel index de05d2d58abcb..209b1af2b649e 100644 --- a/pkg/ddl/schematracker/BUILD.bazel +++ b/pkg/ddl/schematracker/BUILD.bazel @@ -47,7 +47,7 @@ go_test( ], embed = [":schematracker"], flaky = True, - shard_count = 15, + shard_count = 16, deps = [ "//pkg/executor", "//pkg/infoschema", diff --git a/pkg/ddl/schematracker/checker.go b/pkg/ddl/schematracker/checker.go index 4742209f46799..e2d09a1027e55 100644 --- a/pkg/ddl/schematracker/checker.go +++ b/pkg/ddl/schematracker/checker.go @@ -223,7 +223,7 @@ func (*Checker) RecoverSchema(_ sessionctx.Context, _ *ddl.RecoverSchemaInfo) (e // CreateTable implements the DDL interface. func (d *Checker) CreateTable(ctx sessionctx.Context, stmt *ast.CreateTableStmt) error { err := d.realExecutor.CreateTable(ctx, stmt) - if err != nil { + if err != nil || d.closed.Load() { return err } @@ -332,7 +332,7 @@ func (d *Checker) DropIndex(ctx sessionctx.Context, stmt *ast.DropIndexStmt) err // AlterTable implements the DDL interface. func (d *Checker) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt *ast.AlterTableStmt) error { err := d.realExecutor.AlterTable(ctx, sctx, stmt) - if err != nil { + if err != nil || d.closed.Load() { return err } diff --git a/pkg/ddl/schematracker/dm_tracker_test.go b/pkg/ddl/schematracker/dm_tracker_test.go index 89b47bc8b21a5..9ab0e28f90a67 100644 --- a/pkg/ddl/schematracker/dm_tracker_test.go +++ b/pkg/ddl/schematracker/dm_tracker_test.go @@ -219,6 +219,24 @@ func TestIndexLength(t *testing.T) { checkShowCreateTable(t, tblInfo, expected) } +func TestCreateTableWithIndex(t *testing.T) { + // See issue 56045 + sql := "create table test.t(col_1 json, KEY idx_1 ((cast(col_1 as char(64) array))))" + tracker := schematracker.NewSchemaTracker(2) + tracker.CreateTestDB(nil) + execCreate(t, tracker, sql) + + sql = "alter table test.t rename index idx_1 to idx_1_1" + execAlter(t, tracker, sql) + + tblInfo := mustTableByName(t, tracker, "test", "t") + expected := "CREATE TABLE `t` (\n" + + " `col_1` json DEFAULT NULL,\n" + + " KEY `idx_1_1` ((cast(`col_1` as char(64) array)))\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" + checkShowCreateTable(t, tblInfo, expected) +} + func TestIssue5092(t *testing.T) { // copy TestIssue5092 in db_integration_test.go sql := "create table test.t (a int)"