Skip to content

Commit

Permalink
ddl: fix the issue that the length of the index is 0. (#11045) (#11214)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Jul 16, 2019
1 parent e3598fa commit fdeccf9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,38 @@ func (s *testIntegrationSuite2) TestIssue6101(c *C) {
tk.MustExec("drop table t1")
}

func (s *testIntegrationSuite1) TestIndexLength(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table idx_len(a int(0), b timestamp(0), c datetime(0), d time(0), f float(0), g decimal(0))")
tk.MustExec("create index idx on idx_len(a)")
tk.MustExec("alter table idx_len add index idxa(a)")
tk.MustExec("create index idx1 on idx_len(b)")
tk.MustExec("alter table idx_len add index idxb(b)")
tk.MustExec("create index idx2 on idx_len(c)")
tk.MustExec("alter table idx_len add index idxc(c)")
tk.MustExec("create index idx3 on idx_len(d)")
tk.MustExec("alter table idx_len add index idxd(d)")
tk.MustExec("create index idx4 on idx_len(f)")
tk.MustExec("alter table idx_len add index idxf(f)")
tk.MustExec("create index idx5 on idx_len(g)")
tk.MustExec("alter table idx_len add index idxg(g)")
tk.MustExec("create table idx_len1(a int(0), b timestamp(0), c datetime(0), d time(0), f float(0), g decimal(0), index(a), index(b), index(c), index(d), index(f), index(g))")
}

func (s *testIntegrationSuite4) TestIssue3833(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table issue3833 (b char(0))")
tk.MustExec("create table issue3833 (b char(0), c binary(0), d varchar(0))")
assertErrorCode(c, tk, "create index idx on issue3833 (b)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "alter table issue3833 add index idx (b)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create table issue3833_2 (b char(0), index (b))", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create table issue3833_2 (b char(0), c binary(0), d varchar(0), index(b))", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create index idx on issue3833 (c)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "alter table issue3833 add index idx (c)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create table issue3833_2 (b char(0), c binary(0), d varchar(0), index(c))", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create index idx on issue3833 (d)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "alter table issue3833 add index idx (d)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create table issue3833_2 (b char(0), c binary(0), d varchar(0), index(d))", tmysql.ErrWrongKeyColumn)
}

func (s *testIntegrationSuite10) TestIssue2858And2717(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func buildIndexColumns(columns []*model.ColumnInfo, idxColNames []*ast.IndexColN
return nil, errKeyColumnDoesNotExits.GenWithStack("column does not exist: %s", ic.Column.Name)
}

if col.Flen == 0 {
if col.Flen == 0 && (types.IsTypeChar(col.FieldType.Tp) || types.IsTypeVarchar(col.FieldType.Tp)) {
return nil, errors.Trace(errWrongKeyColumn.GenWithStackByArgs(ic.Column.Name))
}

Expand Down

0 comments on commit fdeccf9

Please sign in to comment.