From db397d16ad3d6e18971e11fb7d5f3245d4e28928 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 30 Jun 2007 02:09:50 +0500 Subject: [PATCH] Fixed bug #29205. When a UNION statement forced conversion of an UTF8 charset value to a binary charset value, the byte length of the result values was truncated to the CHAR_LENGTH of the original UTF8 value. sql/item.cc: Fixed bug #29205. The calculation of data length was modified in the Item_type_holder::join_types method to take into account possible conversion of a multibyte charset value to a binary charset value, when each multibyte character is converted into a sequence of bytes (not to a single byte of binary charset). mysql-test/t/ctype_utf8.test: Updated test case for bug #29205. mysql-test/r/ctype_utf8.result: Updated test case for bug #29205. --- mysql-test/r/ctype_utf8.result | 37 ++++++++++++++++++++++++++++++++++ mysql-test/t/ctype_utf8.test | 25 +++++++++++++++++++++++ sql/item.cc | 12 ++++++++--- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result index 216b5f393fb..d38480dced1 100644 --- a/mysql-test/r/ctype_utf8.result +++ b/mysql-test/r/ctype_utf8.result @@ -1657,3 +1657,40 @@ colA colB colA colB 1 foo 1 foo 2 foo bar 2 foo bar DROP TABLE t1, t2; +SELECT 'н1234567890' UNION SELECT _binary '1'; +н1234567890 +н1234567890 +1 +SELECT 'н1234567890' UNION SELECT 1; +н1234567890 +н1234567890 +1 +SELECT '1' UNION SELECT 'н1234567890'; +1 +1 +н1234567890 +SELECT 1 UNION SELECT 'н1234567890'; +1 +1 +н1234567890 +CREATE TABLE t1 (c VARCHAR(11)) CHARACTER SET utf8; +CREATE TABLE t2 (b CHAR(1) CHARACTER SET binary, i INT); +INSERT INTO t1 (c) VALUES ('н1234567890'); +INSERT INTO t2 (b, i) VALUES ('1', 1); +SELECT c FROM t1 UNION SELECT b FROM t2; +c +н1234567890 +1 +SELECT c FROM t1 UNION SELECT i FROM t2; +c +н1234567890 +1 +SELECT b FROM t2 UNION SELECT c FROM t1; +b +1 +н1234567890 +SELECT i FROM t2 UNION SELECT c FROM t1; +i +1 +н1234567890 +DROP TABLE t1, t2; diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test index c4637d14edc..0f1a7cf8c84 100644 --- a/mysql-test/t/ctype_utf8.test +++ b/mysql-test/t/ctype_utf8.test @@ -1338,3 +1338,28 @@ INSERT INTO t2 (colA, colB) VALUES (1, 'foo'),(2, 'foo bar'); SELECT * FROM t1 JOIN t2 ON t1.colA=t2.colA AND t1.colB=t2.colB WHERE t1.colA < 3; DROP TABLE t1, t2; + +# +# Bug#29205: truncation of UTF8 values when the UNION statement +# forces collation to the binary charset +# + +SELECT 'н1234567890' UNION SELECT _binary '1'; +SELECT 'н1234567890' UNION SELECT 1; + +SELECT '1' UNION SELECT 'н1234567890'; +SELECT 1 UNION SELECT 'н1234567890'; + +CREATE TABLE t1 (c VARCHAR(11)) CHARACTER SET utf8; +CREATE TABLE t2 (b CHAR(1) CHARACTER SET binary, i INT); + +INSERT INTO t1 (c) VALUES ('н1234567890'); +INSERT INTO t2 (b, i) VALUES ('1', 1); + +SELECT c FROM t1 UNION SELECT b FROM t2; +SELECT c FROM t1 UNION SELECT i FROM t2; + +SELECT b FROM t2 UNION SELECT c FROM t1; +SELECT i FROM t2 UNION SELECT c FROM t1; + +DROP TABLE t1, t2; diff --git a/sql/item.cc b/sql/item.cc index a334028fd64..52389eece10 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -6595,9 +6595,15 @@ bool Item_type_holder::join_types(THD *thd, Item *item) expansion of the size of the values because of character set conversions. */ - max_length= max(old_max_chars * collation.collation->mbmaxlen, - display_length(item) / item->collation.collation->mbmaxlen * - collation.collation->mbmaxlen); + if (collation.collation != &my_charset_bin) + { + max_length= max(old_max_chars * collation.collation->mbmaxlen, + display_length(item) / + item->collation.collation->mbmaxlen * + collation.collation->mbmaxlen); + } + else + set_if_bigger(max_length, display_length(item)); break; } case REAL_RESULT: