From 0477e80522adb28108c463536d7cfa88239e4d8c Mon Sep 17 00:00:00 2001 From: sachin Date: Mon, 25 Feb 2019 00:46:48 +0530 Subject: [PATCH] Long Index is only allowed for unique keys not normal index. --- mysql-test/main/long_unique.result | 57 ++++++++++++++++++++++++++++++ mysql-test/main/long_unique.test | 30 ++++++++++++++++ sql/sql_table.cc | 8 +++-- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/mysql-test/main/long_unique.result b/mysql-test/main/long_unique.result index b9ba56b0cbe..70528e402af 100644 --- a/mysql-test/main/long_unique.result +++ b/mysql-test/main/long_unique.result @@ -1405,4 +1405,61 @@ insert into t1 values( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, insert into t1 values( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63);; ERROR 23000: Duplicate entry '0' for key 'a63' drop table t1; +create table t1(a blob , key(a)); +Warnings: +Note 1071 Specified key was too long; max key length is 1000 bytes +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` blob DEFAULT NULL, + KEY `a` (`a`(1000)) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1(a blob); +alter table t1 add index(a); +Warnings: +Note 1071 Specified key was too long; max key length is 1000 bytes +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` blob DEFAULT NULL, + KEY `a` (`a`(1000)) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1(a text, key(a)); +Warnings: +Note 1071 Specified key was too long; max key length is 1000 bytes +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` text DEFAULT NULL, + KEY `a` (`a`(1000)) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1(a varchar(4000)); +alter table t1 add index(a); +Warnings: +Warning 1071 Specified key was too long; max key length is 1000 bytes +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` varchar(4000) DEFAULT NULL, + KEY `a` (`a`(1000)) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; +create table t1 (pk int, a int, b int, primary key(pk), key(pk,a)); +alter table t1 modify a text; +ERROR 42000: Specified key was too long; max key length is 1000 bytes +alter table t1 modify a varchar(1000); +ERROR 42000: Specified key was too long; max key length is 1000 bytes +show create table t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `pk` int(11) NOT NULL, + `a` int(11) DEFAULT NULL, + `b` int(11) DEFAULT NULL, + PRIMARY KEY (`pk`), + KEY `pk` (`pk`,`a`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1; set @@GLOBAL.max_allowed_packet= @allowed_packet; diff --git a/mysql-test/main/long_unique.test b/mysql-test/main/long_unique.test index 4d5ecba799f..a6bc68f54dc 100644 --- a/mysql-test/main/long_unique.test +++ b/mysql-test/main/long_unique.test @@ -512,4 +512,34 @@ while ($count) --eval $insert_data_2 drop table t1; +# +# non-unique long indexes are automatically shortened +# +create table t1(a blob , key(a)); +show create table t1; +drop table t1; +create table t1(a blob); +alter table t1 add index(a); +show create table t1; +drop table t1; + +create table t1(a text, key(a)); +show create table t1; +drop table t1; +create table t1(a varchar(4000)); +alter table t1 add index(a); +show create table t1; +drop table t1; + +# +# somewhat inconsistently, the following is an error +# +create table t1 (pk int, a int, b int, primary key(pk), key(pk,a)); +--error ER_TOO_LONG_KEY +alter table t1 modify a text; +--error ER_TOO_LONG_KEY +alter table t1 modify a varchar(1000); +show create table t1; +drop table t1; + set @@GLOBAL.max_allowed_packet= @allowed_packet; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 412aba684d2..70a3b6044d9 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -3941,13 +3941,15 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, column->length= MAX_LEN_GEOM_POINT_FIELD; if (!column->length) { - if (key->type == Key::PRIMARY) + if (key->type == Key::UNIQUE) + is_hash_field_needed= true; + else if (key->type == Key::MULTIPLE) + column->length= file->max_key_length() + 1; + else { my_error(ER_BLOB_KEY_WITHOUT_LENGTH, MYF(0), column->field_name.str); DBUG_RETURN(TRUE); } - else - is_hash_field_needed= true; } } #ifdef HAVE_SPATIAL