From 498ff4468d67499eb35e5c7d0014f935523e0158 Mon Sep 17 00:00:00 2001
From: Alexander Barkov <alexander.barkov@oracle.com>
Date: Fri, 18 Feb 2011 10:32:40 +0300
Subject: [PATCH] Bug#60101 COALESCE with cp1251 tables causes [Err] 1267 -
 Illegal mix of collations

Problem:
  IF() did not copy collation derivation and repertoire from
  an argument if the opposite argument was NULL:
    IF(cond, res1, NULL)
    IF(cond, NULL, res2)
  only CHARSET_INFO pointer was copied.
  This resulted in illegal mix of collations error.

Fix:
  copy all collation parameters from the non-NULL argument:
  CHARSET_INFO pointer, derivation, repertoire.
---
 mysql-test/r/ctype_cp1251.result | 15 +++++++++++++++
 mysql-test/t/ctype_cp1251.test   | 10 ++++++++++
 sql/item_cmpfunc.cc              |  4 ++--
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/mysql-test/r/ctype_cp1251.result b/mysql-test/r/ctype_cp1251.result
index 202dfe2b7d..24604d9d5f 100644
--- a/mysql-test/r/ctype_cp1251.result
+++ b/mysql-test/r/ctype_cp1251.result
@@ -3219,5 +3219,20 @@ maketime(`a`,`a`,`a`)
 DROP TABLE t1;
 SET sql_mode=default;
 #
+# Bug#60101 COALESCE with cp1251 tables causes [Err] 1267 - Illegal mix of collations
+#
+CREATE TABLE t1 (test1 INT, test2 VARCHAR(255));
+SHOW CREATE TABLE t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `test1` int(11) DEFAULT NULL,
+  `test2` varchar(255) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+SELECT COALESCE(IF(test1=1, 1, NULL), test2) FROM t1;
+COALESCE(IF(test1=1, 1, NULL), test2)
+SELECT COALESCE(IF(test1=1, NULL, 1), test2) FROM t1;
+COALESCE(IF(test1=1, NULL, 1), test2)
+DROP TABLE t1;
+#
 # End of 5.5 tests
 #
diff --git a/mysql-test/t/ctype_cp1251.test b/mysql-test/t/ctype_cp1251.test
index 7ffb60374e..aae5094445 100644
--- a/mysql-test/t/ctype_cp1251.test
+++ b/mysql-test/t/ctype_cp1251.test
@@ -85,6 +85,16 @@ set global LC_TIME_NAMES=convert((-8388608) using cp1251);
 
 --source include/ctype_numconv.inc
 
+--echo #
+--echo # Bug#60101 COALESCE with cp1251 tables causes [Err] 1267 - Illegal mix of collations
+--echo #
+CREATE TABLE t1 (test1 INT, test2 VARCHAR(255));
+SHOW CREATE TABLE t1;
+SELECT COALESCE(IF(test1=1, 1, NULL), test2) FROM t1;
+SELECT COALESCE(IF(test1=1, NULL, 1), test2) FROM t1;
+DROP TABLE t1;
+
+
 --echo #
 --echo # End of 5.5 tests
 --echo #
diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc
index 67635c73b4..df541f603e 100644
--- a/sql/item_cmpfunc.cc
+++ b/sql/item_cmpfunc.cc
@@ -2640,7 +2640,7 @@ Item_func_if::fix_length_and_dec()
   if (null1)
   {
     cached_result_type= arg2_type;
-    collation.set(args[2]->collation.collation);
+    collation.set(args[2]->collation);
     cached_field_type= args[2]->field_type();
     max_length= args[2]->max_length;
     return;
@@ -2649,7 +2649,7 @@ Item_func_if::fix_length_and_dec()
   if (null2)
   {
     cached_result_type= arg1_type;
-    collation.set(args[1]->collation.collation);
+    collation.set(args[1]->collation);
     cached_field_type= args[1]->field_type();
     max_length= args[1]->max_length;
     return;
-- 
2.30.9