From 1b0bd6960480e08627078f9d74a976dbc923c9c5 Mon Sep 17 00:00:00 2001 From: Lynn Date: Mon, 8 Jul 2019 19:15:30 +0800 Subject: [PATCH] ddl: fix the issue that the length of the index is 0. (#11045) --- 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 317104492b459..0d9b60f453c4d 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -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) { diff --git a/ddl/index.go b/ddl/index.go index 1aaab3e0ce5af..a9e5c1ae23f58 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)) }