Behavior on VARCHAR data type of comparison operator '=' is different from mysql
Description
Please answer these questions before submitting your issue. Thanks!
- What did you do? If possible, provide a recipe for reproducing the error.
create table t (a char(10), b varchar(10), c binary(10), d varbinary(10));
insert into t values ('text ','text ','text ','text '); <-----there're trailing spaces
mysql> desc t;
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| a | char(10) | YES | | NULL | |
| b | varchar(10) | YES | | NULL | |
| c | binary(10) | YES | | NULL | |
| d | varbinary(10) | YES | | NULL | |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from t;
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.00 sec)
- What did you expect to see?
MySQL
mysql> select * from t where a = 'text';
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.00 sec)
mysql> select * from t where b = 'text';
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.00 sec)
mysql> select * from t where c = 'text';
Empty set (0.00 sec)
mysql> select * from t where d = 'text';
Empty set (0.00 sec)
- What did you see instead?
TiDB treats CHAR and VARCHAR in different ways, so I guess operator '=' is not designed to strictly compare the trailing spaces.
MySQL [gin]> select * from t where a='text';
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.00 sec)
MySQL [gin]> select * from t where b='text';
Empty set (0.00 sec)
MySQL [gin]> select * from t where c='text';
Empty set (0.01 sec)
MySQL [gin]> select * from t where d='text';
Empty set (0.00 sec)
- What version of TiDB are you using (
tidb-server -V)? Git Commit Hash: 365e4ab491b683a69c2004f30b1ae4f86256e2ad Git Commit Branch: master UTC Build Time: 2017-08-16 01:24:27
SIG slack channel
Score
- 900
Mentor
- @lzmhhh123
Thanks for your report. Previous related issue: https://github.com/pingcap/tidb/issues/3660 We need to implement equality comparison in the way of ignoring trailing whitespace.
I can confirm this can still be reproduced:
create table t (a char(10), b varchar(10), c binary(10), d varbinary(10));
insert into t values ('text ','text ','text ','text ');
select * from t where a='text';
select * from t where b='text';
select * from t where c='text';
select * from t where d='text';
MySQL:
mysql57> create table t (a char(10), b varchar(10), c binary(10), d varbinary(10));
Query OK, 0 rows affected (0.08 sec)
mysql57> insert into t values ('text ','text ','text ','text ');
Query OK, 1 row affected (0.01 sec)
mysql57> select * from t where a='text';
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.00 sec)
mysql57> select * from t where b='text';
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.00 sec)
mysql57> select * from t where c='text';
Empty set (0.00 sec)
mysql57> select * from t where d='text';
Empty set (0.00 sec)
TiDB:
MySQL [test]> create table t (a char(10), b varchar(10), c binary(10), d varbinary(10));
Query OK, 0 rows affected (0.32 sec)
MySQL [test]> insert into t values ('text ','text ','text ','text ');
Query OK, 1 row affected (0.05 sec)
MySQL [test]> select * from t where a='text';
+------+--------+------------+--------+
| a | b | c | d |
+------+--------+------------+--------+
| text | text | text | text |
+------+--------+------------+--------+
1 row in set (0.01 sec)
MySQL [test]> select * from t where b='text';
Empty set (0.00 sec)
MySQL [test]> select * from t where c='text';
Empty set (0.00 sec)
MySQL [test]> select * from t where d='text';
Empty set (0.00 sec)
@zz-jason PTAL
This bug can still be reproduced on Master.
It's fixed after introducing the new collation. I believe that @qw4990 doesn't enable the new collation when reproduces it.