From 093573b6b722b17bf550bebb2058fa8cac33c110 Mon Sep 17 00:00:00 2001
From: "bar@mysql.com" <>
Date: Tue, 7 Jun 2005 13:56:42 +0500
Subject: [PATCH] Bug#10253: compound index length and utf8 char set produces
 invalid query results

mi_key.c:
  well_formed_length should be executed before space trimming, not after.
ctype_utf8.test:
ctype_utf8.result:
  adding test.
---
 myisam/mi_key.c                |  6 +++---
 mysql-test/r/ctype_utf8.result | 11 +++++++++++
 mysql-test/t/ctype_utf8.test   | 14 ++++++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/myisam/mi_key.c b/myisam/mi_key.c
index b7240f34538..6a8d88f1117 100644
--- a/myisam/mi_key.c
+++ b/myisam/mi_key.c
@@ -84,7 +84,8 @@ uint _mi_make_key(register MI_INFO *info, uint keynr, uchar *key,
     pos= (byte*) record+keyseg->start;
     if (keyseg->flag & HA_SPACE_PACK)
     {
-      end=pos+length;
+      FIX_LENGTH(cs, pos, length, char_length);
+      end= pos + char_length;
       if (type != HA_KEYTYPE_NUM)
       {
 	while (end > pos && end[-1] == ' ')
@@ -95,8 +96,7 @@ uint _mi_make_key(register MI_INFO *info, uint keynr, uchar *key,
 	while (pos < end && pos[0] == ' ')
 	  pos++;
       }
-      length=(uint) (end-pos);
-      FIX_LENGTH(cs, pos, length, char_length);
+      char_length= (uint) (end - pos);
       store_key_length_inc(key,char_length);
       memcpy((byte*) key,(byte*) pos,(size_t) char_length);
       key+=char_length;
diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result
index b7aa7c68b67..ac421ea1717 100644
--- a/mysql-test/r/ctype_utf8.result
+++ b/mysql-test/r/ctype_utf8.result
@@ -891,3 +891,14 @@ string
 create table t1 (a varchar(255)) default character set utf8;
 insert into t1 values (1.0);
 drop table t1;
+create table t1 (
+id int not null,
+city varchar(20) not null,
+key (city(7),id)
+) character set=utf8;
+insert into t1 values (1,'Durban North');
+insert into t1 values (2,'Durban');
+select * from t1 where city = 'Durban';
+id	city
+2	Durban
+drop table t1;
diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test
index bac6e60c302..a281558e5a1 100644
--- a/mysql-test/t/ctype_utf8.test
+++ b/mysql-test/t/ctype_utf8.test
@@ -731,3 +731,17 @@ select ifnull(NULL, _utf8'string');
 create table t1 (a varchar(255)) default character set utf8;
 insert into t1 values (1.0);
 drop table t1;
+
+#
+# Bug#10253 compound index length and utf8 char set
+# produces invalid query results
+#
+create table t1 (
+ id int not null,
+ city varchar(20) not null,
+ key (city(7),id)
+) character set=utf8;
+insert into t1 values (1,'Durban North');
+insert into t1 values (2,'Durban');
+select * from t1 where city = 'Durban';
+drop table t1;