diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 79cfa4067d953..4a8b7a8a20328 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2200,6 +2200,8 @@ func (s *testIntegrationSuite7) TestCreateExpressionIndexError(c *C) { tk.MustExec("drop table if exists t;") tk.MustGetErrCode("create table t (j json, key k (((j,j))))", errno.ErrFunctionalIndexRowValueIsNotAllowed) tk.MustExec("create table t (j json, key k ((j+1),(j+1)))") + + tk.MustGetErrCode("create table t1 (col1 int, index ((concat(''))));", errno.ErrWrongKeyColumnFunctionalIndex) } func (s *testIntegrationSuite7) TestAddExpressionIndexOnPartition(c *C) { diff --git a/ddl/error.go b/ddl/error.go index 77e1e19a32c9e..c75ccb1b6d487 100644 --- a/ddl/error.go +++ b/ddl/error.go @@ -64,6 +64,8 @@ var ( errOnlyOnRangeListPartition = terror.ClassDDL.New(mysql.ErrOnlyOnRangeListPartition, mysql.MySQLErrName[mysql.ErrOnlyOnRangeListPartition]) // errWrongKeyColumn is for table column cannot be indexed. errWrongKeyColumn = terror.ClassDDL.New(mysql.ErrWrongKeyColumn, mysql.MySQLErrName[mysql.ErrWrongKeyColumn]) + // errWrongKeyColumnFunctionalIndex is for expression cannot be indexed. + errWrongKeyColumnFunctionalIndex = terror.ClassDDL.New(mysql.ErrWrongKeyColumnFunctionalIndex, mysql.MySQLErrName[mysql.ErrWrongKeyColumnFunctionalIndex]) // errWrongFKOptionForGeneratedColumn is for wrong foreign key reference option on generated columns. errWrongFKOptionForGeneratedColumn = terror.ClassDDL.New(mysql.ErrWrongFKOptionForGeneratedColumn, mysql.MySQLErrName[mysql.ErrWrongFKOptionForGeneratedColumn]) // ErrUnsupportedOnGeneratedColumn is for unsupported actions on generated columns. diff --git a/ddl/index.go b/ddl/index.go index c94f90e53c679..7dfb922045896 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -130,6 +130,9 @@ func checkIndexPrefixLength(columns []*model.ColumnInfo, idxColumns []*model.Ind func checkIndexColumn(col *model.ColumnInfo, ic *ast.IndexPartSpecification) error { if col.Flen == 0 && (types.IsTypeChar(col.FieldType.Tp) || types.IsTypeVarchar(col.FieldType.Tp)) { + if col.GeneratedExprString != "" { + return errors.Trace(errWrongKeyColumnFunctionalIndex.GenWithStackByArgs(col.GeneratedExprString)) + } return errors.Trace(errWrongKeyColumn.GenWithStackByArgs(ic.Column.Name)) }