From a2193afb370152c290115db496de6a4116b38fae 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 | 19 +++++++++++++++++++ ddl/index.go | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index f272116f30be0..1d961a6048aa5 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -197,6 +197,25 @@ func (s *testIntegrationSuite) TestEndIncluded(c *C) { tk.MustExec("admin check table t") } +func (s *testIntegrationSuite) 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 *testIntegrationSuite) TestNullGeneratedColumn(c *C) { tk := testkit.NewTestKit(c, s.store) diff --git a/ddl/index.go b/ddl/index.go index 50e13e9393bc8..2863b6c712bb9 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -62,7 +62,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)) }