Skip to content

Commit

Permalink
ddl: index must be non-zero for blob or text(close pingcap#11049)
Browse files Browse the repository at this point in the history
Signed-off-by: Rustin-Liu <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Feb 15, 2020
1 parent c6e4325 commit 11d9d59
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
10 changes: 6 additions & 4 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,16 @@ func (s *testIntegrationSuite5) TestMySQLErrorCode(c *C) {
tk.MustGetErrCode(sql, mysql.ErrWrongAutoKey)
sql = "create table t2 (id int auto_increment, a int key);"
tk.MustGetErrCode(sql, mysql.ErrWrongAutoKey)
sql = "create table t2 (a datetime(2) default current_timestamp(3))"
sql = "create table t2 (a datetime(2) default current_timestamp(3));"
tk.MustGetErrCode(sql, mysql.ErrInvalidDefault)
sql = "create table t2 (a datetime(2) default current_timestamp(2) on update current_timestamp)"
sql = "create table t2 (a datetime(2) default current_timestamp(2) on update current_timestamp);"
tk.MustGetErrCode(sql, mysql.ErrInvalidOnUpdate)
sql = "create table t2 (a datetime default current_timestamp on update current_timestamp(2))"
sql = "create table t2 (a datetime default current_timestamp on update current_timestamp(2));"
tk.MustGetErrCode(sql, mysql.ErrInvalidOnUpdate)
sql = "create table t2 (a datetime(2) default current_timestamp(2) on update current_timestamp(3))"
sql = "create table t2 (a datetime(2) default current_timestamp(2) on update current_timestamp(3));"
tk.MustGetErrCode(sql, mysql.ErrInvalidOnUpdate)
sql = "create table t(a blob(10), index(a(0)));"
tk.MustGetErrCode(sql, mysql.ErrKeyPart0)

sql = "create table t2 (id int primary key , age int);"
tk.MustExec(sql)
Expand Down
1 change: 1 addition & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ var (
errUnsupportedCharset = terror.ClassDDL.New(mysql.ErrUnsupportedDDLOperation, fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation], "charset %s and collate %s"))
errUnsupportedShardRowIDBits = terror.ClassDDL.New(mysql.ErrUnsupportedDDLOperation, fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUnsupportedDDLOperation], "shard_row_id_bits for table with primary key as row id"))
errBlobKeyWithoutLength = terror.ClassDDL.New(mysql.ErrBlobKeyWithoutLength, mysql.MySQLErrName[mysql.ErrBlobKeyWithoutLength])
errKeyPart0 = terror.ClassDDL.New(mysql.ErrKeyPart0, mysql.MySQLErrName[mysql.ErrKeyPart0])
errIncorrectPrefixKey = terror.ClassDDL.New(mysql.ErrWrongSubKey, mysql.MySQLErrName[mysql.ErrWrongSubKey])
errTooLongKey = terror.ClassDDL.New(mysql.ErrTooLongKey,
fmt.Sprintf(mysql.MySQLErrName[mysql.ErrTooLongKey], maxPrefixLength))
Expand Down
11 changes: 8 additions & 3 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ func checkIndexColumn(col *model.ColumnInfo, ic *ast.IndexPartSpecification) err
return errors.Trace(errJSONUsedAsKey.GenWithStackByArgs(col.Name.O))
}

// Length must be specified for BLOB and TEXT column indexes.
if types.IsTypeBlob(col.FieldType.Tp) && ic.Length == types.UnspecifiedLength {
return errors.Trace(errBlobKeyWithoutLength.GenWithStackByArgs(col.Name.O))
// Length must be specified and non-zero for BLOB and TEXT column indexes.
if types.IsTypeBlob(col.FieldType.Tp) {
if ic.Length == types.UnspecifiedLength {
return errors.Trace(errBlobKeyWithoutLength.GenWithStackByArgs(col.Name.O))
}
if ic.Length == types.ErrorLength {
return errors.Trace(errKeyPart0.GenWithStackByArgs(col.Name.O))
}
}

// Length can only be specified for specifiable types.
Expand Down
7 changes: 4 additions & 3 deletions types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
)

// UnspecifiedLength is unspecified length.
const (
UnspecifiedLength = -1
)
const UnspecifiedLength = -1

// ErrorLength is error length for blob or text.
const ErrorLength = 0

// FieldType records field type information.
type FieldType = ast.FieldType
Expand Down

0 comments on commit 11d9d59

Please sign in to comment.