diff --git a/pkg/ddl/column.go b/pkg/ddl/column.go index e8ccb08e0b1c4..4f8dae39d061f 100644 --- a/pkg/ddl/column.go +++ b/pkg/ddl/column.go @@ -2039,3 +2039,12 @@ func getChangingColumnOriginName(changingColumn *model.ColumnInfo) string { } return columnName[:pos] } + +func getExpressionIndexOriginName(expressionIdx *model.ColumnInfo) string { + columnName := strings.TrimPrefix(expressionIdx.Name.O, expressionIndexPrefix+"_") + var pos int + if pos = strings.LastIndex(columnName, "_"); pos == -1 { + return columnName + } + return columnName[:pos] +} diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index b14790e3cdd57..60de335247114 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -398,6 +398,8 @@ func onRenameIndex(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) } renameIndexes(tblInfo, from, to) + renameHiddenColumns(tblInfo, from, to) + if ver, err = updateVersionAndTableInfo(d, t, job, tblInfo, true); err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -2534,3 +2536,12 @@ func renameIndexes(tblInfo *model.TableInfo, from, to model.CIStr) { } } } + +func renameHiddenColumns(tblInfo *model.TableInfo, from, to model.CIStr) { + for _, col := range tblInfo.Columns { + if col.Hidden && getExpressionIndexOriginName(col) == 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/tests/integrationtest/r/ddl/db_rename.result b/tests/integrationtest/r/ddl/db_rename.result index 2b71ae58e7ca0..1191ed4b23e0e 100644 --- a/tests/integrationtest/r/ddl/db_rename.result +++ b/tests/integrationtest/r/ddl/db_rename.result @@ -13,3 +13,17 @@ Error 1061 (42000): Duplicate key name 'k2' alter table t rename index k2 to K2; alter table t rename key k3 to K2; Error 1061 (42000): Duplicate key name 'K2' +drop table t; +create table t(j json); +alter table t add index idx1((cast(j as char(10) array))); +alter table t rename index idx1 to idx2; +alter table t add index idx1((cast(j as char(10) array))); +insert into t values ('["1"]'); +alter table t add index IDX3((cast(j as char(10) array))); +alter table t rename index IDX3 to IDX4; +alter table t add index IDX3((cast(j as char(10) array))); +insert into t values ('["2"]'); +select * from t; +j +["1"] +["2"] diff --git a/tests/integrationtest/t/ddl/db_rename.test b/tests/integrationtest/t/ddl/db_rename.test index 85e8d37c2be71..05f3814f24e61 100644 --- a/tests/integrationtest/t/ddl/db_rename.test +++ b/tests/integrationtest/t/ddl/db_rename.test @@ -13,3 +13,15 @@ alter table t rename index k2 to K2; -- error 1061 alter table t rename key k3 to K2; +# TestIssue51431 +drop table t; +create table t(j json); +alter table t add index idx1((cast(j as char(10) array))); +alter table t rename index idx1 to idx2; +alter table t add index idx1((cast(j as char(10) array))); +insert into t values ('["1"]'); +alter table t add index IDX3((cast(j as char(10) array))); +alter table t rename index IDX3 to IDX4; +alter table t add index IDX3((cast(j as char(10) array))); +insert into t values ('["2"]'); +select * from t;