diff --git a/mysql-test/main/long_unique_bugs.result b/mysql-test/main/long_unique_bugs.result index 6bf3a7dcc44..3f65fb18014 100644 --- a/mysql-test/main/long_unique_bugs.result +++ b/mysql-test/main/long_unique_bugs.result @@ -809,3 +809,29 @@ hex(c1) hex(c2) c3 hex(c4) NULL NULL NULL NULL drop table t1; # End of 10.5 tests +# +# MDEV-36852 Table definition gets corrupt after adding unique hash key +# +create table t1 (a text, b int, foreign key(a) references x(x)) engine=myisam; +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, + `b` int(11) DEFAULT NULL, + KEY `a` (`a`(1000)) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci +alter table t1 add unique(a), add 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, + `b` int(11) DEFAULT NULL, + UNIQUE KEY `a` (`a`) USING HASH, + KEY `a_2` (`a`(1000)) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci +drop table t1; +# End of 10.6 tests diff --git a/mysql-test/main/long_unique_bugs.test b/mysql-test/main/long_unique_bugs.test index b1daf97d194..d8d3462c1da 100644 --- a/mysql-test/main/long_unique_bugs.test +++ b/mysql-test/main/long_unique_bugs.test @@ -772,3 +772,15 @@ select hex(c1), hex(c2), c3, hex(c4) from t1; drop table t1; --echo # End of 10.5 tests + +--echo # +--echo # MDEV-36852 Table definition gets corrupt after adding unique hash key +--echo # + +create table t1 (a text, b int, foreign key(a) references x(x)) engine=myisam; +show create table t1; +alter table t1 add unique(a), add key(a); +show create table t1; +drop table t1; + +--echo # End of 10.6 tests diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 3a963403d65..69ddea8e5e9 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -3337,8 +3337,6 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, Create_field *auto_increment_key= 0; Key_part_spec *column; - bool is_hash_field_needed= key->key_create_info.algorithm - == HA_KEY_ALG_LONG_HASH; if (key->type == Key::IGNORE_KEY) { /* ignore redundant keys */ @@ -3349,6 +3347,9 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info, break; } + bool is_hash_field_needed= key->key_create_info.algorithm + == HA_KEY_ALG_LONG_HASH; + if (key_check_without_overlaps(thd, create_info, alter_info, *key)) DBUG_RETURN(true); diff --git a/sql/table.cc b/sql/table.cc index 9bbc29d4ecf..119d949388a 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -2838,6 +2838,7 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, hash_keypart->fieldnr= hash_field_used_no + 1; hash_field= share->field[hash_field_used_no]; hash_field->flags|= LONG_UNIQUE_HASH_FIELD;//Used in parse_vcol_defs + DBUG_ASSERT(hash_field->invisible == INVISIBLE_FULL); keyinfo->flags|= HA_NOSAME; share->virtual_fields++; share->stored_fields--;