tidb icon indicating copy to clipboard operation
tidb copied to clipboard

Behavior on VARCHAR data type of comparison operator '=' is different from mysql

Open tshqin opened this issue 9 years ago • 5 comments

Description

Please answer these questions before submitting your issue. Thanks!

  1. 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)
  1. 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)
  1. 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)
  1. 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

#sig-planner

Score

  • 900

Mentor

  • @lzmhhh123

tshqin avatar Aug 24 '17 11:08 tshqin

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.

breezewish avatar Aug 28 '17 02:08 breezewish

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)

morgo avatar Oct 29 '18 00:10 morgo

@zz-jason PTAL

breezewish avatar Oct 29 '18 06:10 breezewish

This bug can still be reproduced on Master.

qw4990 avatar Aug 24 '20 06:08 qw4990

It's fixed after introducing the new collation. I believe that @qw4990 doesn't enable the new collation when reproduces it.

wjhuang2016 avatar Nov 03 '20 06:11 wjhuang2016