From 48e011dbbd39ed907e56f05a6b399d012a7a79ad Mon Sep 17 00:00:00 2001 From: Lynn Date: Wed, 3 Jul 2019 11:02:02 +0800 Subject: [PATCH] ddl: fix the issue that the length of the index is 0. --- ddl/db_integration_test.go | 29 +++++++++++++++++++++++++++-- ddl/index.go | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 6312bb6354fa1..3fdba111cfcbd 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -268,13 +268,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 *testIntegrationSuite3) 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 *testIntegrationSuite1) TestIssue2858And2717(c *C) { diff --git a/ddl/index.go b/ddl/index.go index 1e3f44562b268..036617496ecf3 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -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)) }