From e8278396195c340076d3e89ce2f6afc06e80aa87 Mon Sep 17 00:00:00 2001
From: "bar@mysql.com" <>
Date: Thu, 14 Oct 2004 16:36:29 +0500
Subject: [PATCH] Bug #6019 SELECT tries to use too short prefix index on utf8
 data

---
 mysql-test/r/ctype_utf8.result | 11 +++++++++++
 mysql-test/t/ctype_utf8.test   | 15 +++++++++++++++
 strings/ctype-mb.c             |  4 ++++
 strings/ctype-utf8.c           |  4 ++--
 4 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result
index 0ed3444581c..e52b6f9a609 100644
--- a/mysql-test/r/ctype_utf8.result
+++ b/mysql-test/r/ctype_utf8.result
@@ -779,3 +779,14 @@ id	term
 2	testetest
 3	testètest
 DROP TABLE t1;
+set names utf8;
+create table t1 (
+a int primary key,
+b varchar(6),
+index b3(b(3))
+) engine=myisam character set=utf8;
+insert into t1 values(1,'foo'),(2,'foobar');
+select * from t1 where b like 'foob%';
+a	b
+2	foobar
+drop table t1;
diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test
index ae8db92e3f9..0da52054aac 100644
--- a/mysql-test/t/ctype_utf8.test
+++ b/mysql-test/t/ctype_utf8.test
@@ -620,3 +620,18 @@ SELECT id, term FROM t1 where (list_id = 1) AND (term = "test
 SELECT id, term FROM t1 where (list_id = 1) AND (term = "testetest");
 SELECT id, term FROM t1 where (list_id = 1) AND (term = "testètest");
 DROP TABLE t1;
+
+#
+# Bug #6019 SELECT tries to use too short prefix index on utf8 data
+#
+set names utf8;
+--disable_warnings
+create table t1 (
+  a int primary key,
+  b varchar(6),
+  index b3(b(3))
+) engine=myisam character set=utf8;
+--enable_warnings
+insert into t1 values(1,'foo'),(2,'foobar');
+select * from t1 where b like 'foob%';
+drop table t1;
diff --git a/strings/ctype-mb.c b/strings/ctype-mb.c
index 3bfc66029ce..a41449d5e50 100644
--- a/strings/ctype-mb.c
+++ b/strings/ctype-mb.c
@@ -499,6 +499,10 @@ my_bool my_like_range_mb(CHARSET_INFO *cs,
     {
       char buf[10];
       uint buflen;
+      uint charlen= my_charpos(cs, min_org, min_str, res_length/cs->mbmaxlen);
+      
+      if (charlen < (uint) (min_str - min_org))
+        min_str= min_org + charlen;
       
       /* Write min key  */
       *min_length= (uint) (min_str - min_org);
diff --git a/strings/ctype-utf8.c b/strings/ctype-utf8.c
index 62e97d51328..fd6610b72b1 100644
--- a/strings/ctype-utf8.c
+++ b/strings/ctype-utf8.c
@@ -2059,7 +2059,7 @@ static MY_COLLATION_HANDLER my_collation_ci_handler =
     my_strnncoll_utf8,
     my_strnncollsp_utf8,
     my_strnxfrm_utf8,
-    my_like_range_simple,
+    my_like_range_mb,
     my_wildcmp_mb,
     my_strcasecmp_utf8,
     my_instr_mb,
@@ -2119,7 +2119,7 @@ CHARSET_INFO my_charset_utf8_general_ci=
     1,                  /* mbminlen     */
     3,                  /* mbmaxlen     */
     0,                  /* min_sort_char */
-    255,                /* max_sort_char */
+    0xFFFF,             /* max_sort_char */
     &my_charset_utf8_handler,
     &my_collation_ci_handler
 };
-- 
2.30.9