Bug #119035 Incorrect result for BETWEEN with mixed types on a BINARY column with a UNIQUE key
Submitted: 18 Sep 10:11
Reporter: hongtao zhou Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:9.4.0 8.4.6 OS:Any
Assigned to: CPU Architecture:Any

[18 Sep 10:11] hongtao zhou
Description:
The query fails to return all rows that satisfy the WHERE clause. The problem lies in the evaluation of the BETWEEN predicate when it involves mixed data types: a BINARY column, a string literal ('R'), and a numeric literal (1).
According to MySQL's type conversion rules for comparisons, if one of the operands is a number, the other operands are converted to numbers for the evaluation.
The BETWEEN expression is c1 BETWEEN 'R' AND 1.
The upper bound is a number (1), so all three operands are cast to numbers.
The type conversion proceeds as follows:
The BINARY value 'L' is cast to the number 0.
The BINARY value 'b' is cast to the number 0.
The string literal 'R' is cast to the number 0.
The integer literal 1 remains 1.
Therefore, for both rows, the condition is effectively 0 BETWEEN 0 AND 1. This is equivalent to 0 >= 0 AND 0 <= 1, which is unequivocally TRUE.

How to repeat:
CREATE TABLE IF NOT EXISTS t194 (c1 BINARY, UNIQUE (c1)) ;
INSERT INTO t194 (c1) VALUES ('L');
INSERT INTO t194 (c1) VALUES ('b');
SELECT c1 AS ca1 FROM t194 WHERE (((c1) BETWEEN ('R') AND (1))) ; -- b
SELECT SUM(count) FROM (SELECT ((((c1) BETWEEN ('R') AND (1)))) IS TRUE AS count FROM t194 ) AS ta_norec; -- 2