Commit 76221343 authored by Alexander Barkov's avatar Alexander Barkov

Bug#44131 Binary-mode "order by" returns records in incorrect order for UTF-8 strings

Problem: Item_char_typecast reported wrong max_length when
casting to BINARY, which lead, in particular, in wrong
"ORDER BY BINARY(char_column)" results.

Fix: making Item_char_typecast report correct max_length.

  @ mysql-test/r/ctype_utf16.result
    Fixing old incorrect test result.
  @ mysql-test/r/ctype_utf32.result
    Fixing old incorrect test result.
  @ mysql-test/r/ctype_utf8.result
    Adding new test
  @ mysql-test/t/ctype_utf8.test
    Adding new test
  @ sql/item_timefunc.cc
    Making Item_char_typecast report correct max_length
    when cast is done to BINARY.
parent 1eda9dc7
......@@ -1848,6 +1848,24 @@ select hex(_utf8 B'001111111111');
ERROR HY000: Invalid utf8 character string: 'FF'
select (_utf8 X'616263FF');
ERROR HY000: Invalid utf8 character string: 'FF'
#
# Bug#44131 Binary-mode "order by" returns records in incorrect order for UTF-8 strings
#
CREATE TABLE t1 (id int not null primary key, name varchar(10)) character set utf8;
INSERT INTO t1 VALUES
(2,'一二三01'),(3,'一二三09'),(4,'一二三02'),(5,'一二三08'),
(6,'一二三11'),(7,'一二三91'),(8,'一二三21'),(9,'一二三81');
SELECT * FROM t1 ORDER BY BINARY(name);
id name
2 一二三01
4 一二三02
5 一二三08
3 一二三09
6 一二三11
8 一二三21
9 一二三81
7 一二三91
DROP TABLE t1;
CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL);
INSERT INTO t1 VALUES (70000, 1092), (70001, 1085), (70002, 1065);
SELECT CONVERT(a, CHAR), CONVERT(b, CHAR) FROM t1 GROUP BY b;
......
......@@ -1440,6 +1440,16 @@ select hex(_utf8 B'001111111111');
--error ER_INVALID_CHARACTER_STRING
select (_utf8 X'616263FF');
--echo #
--echo # Bug#44131 Binary-mode "order by" returns records in incorrect order for UTF-8 strings
--echo #
CREATE TABLE t1 (id int not null primary key, name varchar(10)) character set utf8;
INSERT INTO t1 VALUES
(2,'一二三01'),(3,'一二三09'),(4,'一二三02'),(5,'一二三08'),
(6,'一二三11'),(7,'一二三91'),(8,'一二三21'),(9,'一二三81');
SELECT * FROM t1 ORDER BY BINARY(name);
DROP TABLE t1;
#
# Bug #36772: When using UTF8, CONVERT with GROUP BY returns truncated results
#
......
......@@ -2554,9 +2554,9 @@ void Item_char_typecast::fix_length_and_dec()
from_cs != &my_charset_bin &&
cast_cs != &my_charset_bin);
collation.set(cast_cs, DERIVATION_IMPLICIT);
char_length= (cast_length >= 0) ?
cast_length :
args[0]->max_length / args[0]->collation.collation->mbmaxlen;
char_length= (cast_length >= 0) ? cast_length :
args[0]->max_length /
(cast_cs == &my_charset_bin ? 1 : args[0]->collation.collation->mbmaxlen);
max_length= char_length * cast_cs->mbmaxlen;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment