[MDEV-25871] compare error bigint to varchar Created: 2021-06-07  Updated: 2021-06-21  Resolved: 2021-06-17

Status: Closed
Project: MariaDB Server
Component/s: Data types, OTHER
Affects Version/s: 10.2.38
Fix Version/s: N/A

Type: Bug Priority: Critical
Reporter: Bernd Buffen Assignee: Alice Sherepa
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Linux jitsi.kgs-born.de 4.15.0-144-generic #148-Ubuntu SMP Sat May 8 02:33:43 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux


Issue Links:
Relates
relates to MDEV-21445 Strange/inconsistent behavior of IN c... Closed
relates to MDEV-25862 BIGINT UNSIGNED treated as SIGNED Open

 Description   

If i compare a bigint with a varchar i get a wrong result

here a sample:

This query should only find 1 row, but it returns 2 rows

MariaDB [bernd]> desc big;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| bi    | bigint(11)       | YES  |     | NULL    |                |
| vc    | varchar(20)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
 
MariaDB [bernd]> select * from big;
+----+--------------------+--------------------+
| id | bi                 | vc                 |
+----+--------------------+--------------------+
|  1 | 137226000008073943 | 137226000008073943 |
|  2 | 137226000008073929 | 137226000008073929 |
+----+--------------------+--------------------+
2 rows in set (0.05 sec)
 
MariaDB [bernd]> select * from big where vc = 137226000008073943;
+----+--------------------+--------------------+
| id | bi                 | vc                 |
+----+--------------------+--------------------+
|  1 | 137226000008073943 | 137226000008073943 |
|  2 | 137226000008073929 | 137226000008073929 |
+----+--------------------+--------------------+
2 rows in set (0.00 sec)
 
MariaDB [bernd]> 



 Comments   
Comment by Alice Sherepa [ 2021-06-08 ]

MariaDB [test]> select '137226000008073943'  = 137226000008073929;
+--------------------------------------------+
| '137226000008073943'  = 137226000008073929 |
+--------------------------------------------+
|                                          1 |
+--------------------------------------------+
1 row in set (0.00 sec)

I guess the problem is described here https://mariadb.com/kb/en/type-conversion/#rules-for-conversion-on-comparison and https://dev.mysql.com/doc/refman/8.0/en/type-conversion.html
please use CAST to avoid the problem (select * from t1 where vc = CAST(137226000008073943 AS CHAR); )

Generated at Thu Feb 08 09:41:02 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.