From 31d87cd50c672662e06871325803408e4ac9da60 Mon Sep 17 00:00:00 2001
From: "serg@serg.mysql.com" <>
Date: Sun, 2 Mar 2003 14:07:32 +0100
Subject: [PATCH] optimizer should check for "field LIKE const" not "field like
 STRING"

---
 .bzrignore                    |  2 ++
 mysql-test/r/func_like.result | 10 ++++++++++
 mysql-test/t/func_like.test   | 11 +++++++----
 sql/item_cmpfunc.cc           | 12 ++++++++----
 4 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/.bzrignore b/.bzrignore
index f5e9775ee8..78d44a7704 100644
--- a/.bzrignore
+++ b/.bzrignore
@@ -528,3 +528,5 @@ support-files/MacOSX/Info.plist
 support-files/MacOSX/StartupParameters.plist
 support-files/MacOSX/postinstall
 support-files/MacOSX/preinstall
+configure.lineno
+innobase/configure.lineno
diff --git a/mysql-test/r/func_like.result b/mysql-test/r/func_like.result
index c2085ba12d..f923c16b2a 100644
--- a/mysql-test/r/func_like.result
+++ b/mysql-test/r/func_like.result
@@ -1,10 +1,20 @@
 drop table if exists t1;
 create table t1 (a varchar(10), key(a));
 insert into t1 values ("a"),("abc"),("abcd"),("hello"),("test");
+explain select * from t1 where a like 'abc%';
+table	type	possible_keys	key	key_len	ref	rows	Extra
+t1	range	a	a	11	NULL	1	Using where; Using index
+explain select * from t1 where a like concat('abc','%');
+table	type	possible_keys	key	key_len	ref	rows	Extra
+t1	range	a	a	11	NULL	1	Using where; Using index
 select * from t1 where a like "abc%";
 a
 abc
 abcd
+select * from t1 where a like concat("abc","%");
+a
+abc
+abcd
 select * from t1 where a like "ABC%";
 a
 abc
diff --git a/mysql-test/t/func_like.test b/mysql-test/t/func_like.test
index 09746fcc81..a5d1193fd7 100644
--- a/mysql-test/t/func_like.test
+++ b/mysql-test/t/func_like.test
@@ -5,10 +5,13 @@
 drop table if exists t1;
 create table t1 (a varchar(10), key(a));
 insert into t1 values ("a"),("abc"),("abcd"),("hello"),("test");
-select * from t1 where a like "abc%"; 
-select * from t1 where a like "ABC%"; 
-select * from t1 where a like "test%"; 
-select * from t1 where a like "te_t"; 
+explain select * from t1 where a like 'abc%';
+explain select * from t1 where a like concat('abc','%');
+select * from t1 where a like "abc%";
+select * from t1 where a like concat("abc","%");
+select * from t1 where a like "ABC%";
+select * from t1 where a like "test%";
+select * from t1 where a like "te_t";
 
 #
 # The following will test the Turbo Boyer-Moore code
diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc
index 55e8ef7c4b..d96069a17a 100644
--- a/sql/item_cmpfunc.cc
+++ b/sql/item_cmpfunc.cc
@@ -1409,12 +1409,16 @@ longlong Item_func_like::val_int()
 
 Item_func::optimize_type Item_func_like::select_optimize() const
 {
-  if (args[1]->type() == STRING_ITEM)
+  if (args[1]->const_item())
   {
-    if (((Item_string *) args[1])->str_value[0] != wild_many)
+    String* res2= args[1]->val_str((String *)&tmp_value2);
+
+    if (!res2)
+      return OPTIMIZE_NONE;
+
+    if (*res2->ptr() != wild_many)
     {
-      if ((args[0]->result_type() != STRING_RESULT) ||
-	  ((Item_string *) args[1])->str_value[0] != wild_one)
+      if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != wild_one)
 	return OPTIMIZE_OP;
     }
   }
-- 
2.30.9