[MDEV-29259] Comparison semantic of int = string changes with creation of an index Created: 2022-08-05  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: None
Affects Version/s: 10.4.24, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9
Fix Version/s: 10.4, 10.5, 10.6

Type: Bug Priority: Major
Reporter: Andrei Lurie Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-21445 Strange/inconsistent behavior of IN c... Closed

 Description   

Per: https://mariadb.com/kb/en/type-conversion/
"If one argument is string and the other argument is integer, they are compared as decimals. "
However, if an index is created on the integer column and that index is picked by the optimizer, the comparison looks to be carried out as integer. This is incorrect, since it changes the comparison semantics just by creating/dropping an index.
There should not be such side effect from create/drop index.

We use 10.4.24 but same occurs on 10.6.5.

Example:

MariaDB [test]> create table t1(id int primary key, c1 int);
Query OK, 0 rows affected (0.030 sec)
 
MariaDB [test]> insert into t1 values(1, 1),(2, 2);
Query OK, 2 rows affected (0.003 sec)
Records: 2  Duplicates: 0  Warnings: 0
 
MariaDB [test]> select * from t1;
+----+------+
| id | c1   |
+----+------+
|  1 |    1 |
|  2 |    2 |
+----+------+
2 rows in set (0.001 sec)
 
MariaDB [test]> select * from t1 where c1 = '1.5';
Empty set (0.001 sec)
 
-- Note: this is as expected because column values 1,2 converted to decimal are 1.0 and 2.0
-- and neither is equal to 1.5 (string '1.5' converted to decimal).
 
MariaDB [test]> create index idx1 on t1(c1);
Query OK, 0 rows affected (0.032 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> select * from t1 where c1 = '1.5';
+----+------+
| id | c1   |
+----+------+
|  2 |    2 |
+----+------+
1 row in set (0.001 sec)
 
-- Note: this was unexpected. It looks as if string '1.5' is converted to decimal first (as it should) but then is cast to int (causing the rounding to occur).
-- This seems wrong because index scan vs table scan should not change the semantics of the data type resolution. And it is possible to implement 
-- this so that index is still used but semantics is consistent with table scan path (and documentation) - hence sounds like an oversight? 
 
MariaDB [test]> select * from t1 ignore index(idx1) where c1 = '1.5';
Empty set (0.001 sec)
 
-- Note: back to expected when index is not picked (or can also drop the index).


P.S.
could you also please clarify whether the int = string is using decimal and not double?
Because the following warning seems to imply that it is using double:

MariaDB [test]> select * from t1 where c1 = '1.5A';
Empty set (Engine=InnoDB), 1 warning (0.001 sec)
 
MariaDB [test]> show warnings;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '1.5A' |
+---------+------+------------------------------------------+



 Comments   
Comment by Alice Sherepa [ 2022-08-08 ]

Thanks for the report!
Repeatable on 10.3-10.10, the same on Mysql 5.6,5.7, but not on Mysql 8.0

Comment by Alexander Barkov [ 2022-08-18 ]

could you also please clarify whether the int = string is using decimal and not double?
Because the following warning seems to imply that it is using double:

Versions prior to 10.3.36 compared INT=STRING as DOUBLE.
The comparison INT=STRING was changed from DOUBLE to DECIMAL just recently, in June 2022, under terms of this bug report: MDEV-21445.

MariaDB-10.3.36 returns the following warning:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (id int primary key, c1 int);
INSERT INTO t1 VALUES (1,1), (2,2);
SELECT * FROM t1 WHERE c1 = '1.5x';
SHOW WARNINGS;

+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1292 | Truncated incorrect DECIMAL value: '1.5x' |
+---------+------+-------------------------------------------+

Notice "incorrect DECIMAL" in the warning.

Generated at Thu Feb 08 10:07:07 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.