diff --git a/myisam/mi_key.c b/myisam/mi_key.c index a9b5a8b279f..e06239d3bba 100644 --- a/myisam/mi_key.c +++ b/myisam/mi_key.c @@ -168,13 +168,18 @@ uint _mi_make_key(register MI_INFO *info, uint keynr, uchar *key, } continue; } -#ifdef NOT_YET_FIXED_LENGTH_KEY if (char_length && length > char_length) { char_length= my_charpos(cs, pos, pos+length, char_length); - set_if_smaller(length, char_length); + if (char_length < length) + { + uint diff= length - char_length; + memcpy((byte*) key, pos, char_length); + cs->cset->fill(cs, key + char_length, diff, ' '); + key+= length; + continue; + } } -#endif memcpy((byte*) key, pos, length); key+= length; } diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result index 4d1b5d54bda..8ad8be26b62 100644 --- a/mysql-test/r/ctype_utf8.result +++ b/mysql-test/r/ctype_utf8.result @@ -276,3 +276,26 @@ select c cb20 from t1 where c=repeat('b',20); cb20 bbbbbbbbbbbbbbbbbbbb drop table t1; +create table t1 (c char(3) character set utf8, unique (c(2))); +insert into t1 values ('1'),('2'),('3'),('4'),('x'),('y'),('z'); +insert into t1 values ('a'); +insert into t1 values ('aa'); +insert into t1 values ('aaa'); +ERROR 23000: Duplicate entry 'aaa' for key 1 +insert into t1 values ('b'); +insert into t1 values ('bb'); +insert into t1 values ('bbb'); +ERROR 23000: Duplicate entry 'bbb' for key 1 +insert into t1 values ('а'); +insert into t1 values ('аа'); +insert into t1 values ('ааа'); +ERROR 23000: Duplicate entry 'ааа' for key 1 +insert into t1 values ('б'); +insert into t1 values ('бб'); +insert into t1 values ('ббб'); +ERROR 23000: Duplicate entry 'ббб' for key 1 +insert into t1 values ('ꪪ'); +insert into t1 values ('ꪪꪪ'); +insert into t1 values ('ꪪꪪꪪ'); +ERROR 23000: Duplicate entry 'ꪪꪪ' for key 1 +drop table t1; diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test index 4e130440a24..f25a1ecfd0c 100644 --- a/mysql-test/t/ctype_utf8.test +++ b/mysql-test/t/ctype_utf8.test @@ -187,3 +187,30 @@ select c cz from t1 where c='z'; select c ca10 from t1 where c='aaaaaaaaaa'; select c cb20 from t1 where c=repeat('b',20); drop table t1; + +# +# Bug 4521: unique key prefix interacts poorly with utf8 +# Check fixed length keys +create table t1 (c char(3) character set utf8, unique (c(2))); +insert into t1 values ('1'),('2'),('3'),('4'),('x'),('y'),('z'); +insert into t1 values ('a'); +insert into t1 values ('aa'); +--error 1062 +insert into t1 values ('aaa'); +insert into t1 values ('b'); +insert into t1 values ('bb'); +--error 1062 +insert into t1 values ('bbb'); +insert into t1 values ('а'); +insert into t1 values ('аа'); +--error 1062 +insert into t1 values ('ааа'); +insert into t1 values ('б'); +insert into t1 values ('бб'); +--error 1062 +insert into t1 values ('ббб'); +insert into t1 values ('ꪪ'); +insert into t1 values ('ꪪꪪ'); +--error 1062 +insert into t1 values ('ꪪꪪꪪ'); +drop table t1;