Bug #121893 Indexed integer equality matches a fractional DOUBLE value
Description:
An indexed equality lookup on a SMALLINT column incorrectly matches integer row 0 when compared with the exactly representable DOUBLE value 0.25. The equivalent table scan returns no rows, as does the indexed DECIMAL control.
The observed counts are real_index=1, real_scan=0, and decimal_index=0. EXPLAIN reports a covering index lookup on t1 using k1 with `(v = ifnull(0.25e0,0.5e0))`.
The same wrong result occurs with a prepared DOUBLE parameter: executions with 0.25 and 1.0 return counts 1 and 1, respectively, instead of 0 and 1. The result is therefore access-path dependent.
Related issue #121226 also concerns conversion of a nonintegral DOUBLE/REAL value into an integer index key and loss of the residual predicate. This report additionally demonstrates the problem with a runtime CAST expression and a prepared DOUBLE parameter.
How to repeat:
CREATE DATABASE db1;
USE db1;
CREATE TABLE t1 (
v SMALLINT NOT NULL,
KEY k1 (v)
);
INSERT INTO t1 VALUES (0),(1),(2);
SET SESSION optimizer_switch='hypergraph_optimizer=off';
SELECT 'real_index' AS test_case, COUNT(*) AS actual
FROM t1 FORCE INDEX (k1)
WHERE v=IFNULL(0.25e0,0.5e0);
-- returns 1, expected 0
SELECT 'real_scan' AS test_case, COUNT(*) AS actual
FROM t1 IGNORE INDEX (k1)
WHERE v=IFNULL(0.25e0,0.5e0);
-- returns 0
SELECT 'decimal_index' AS test_case, COUNT(*) AS actual
FROM t1 FORCE INDEX (k1)
WHERE v=IFNULL(0.25,0.5);
-- returns 0
EXPLAIN FORMAT=TREE
SELECT v
FROM t1 FORCE INDEX (k1)
WHERE v=IFNULL(0.25e0,0.5e0);
PREPARE stmt1 FROM
'SELECT COUNT(*) AS actual FROM t1 FORCE INDEX (k1) WHERE v=IFNULL(CAST(? AS DOUBLE),0.5e0)';
SET @v1=0.25e0;
EXECUTE stmt1 USING @v1;
-- returns 1, expected 0
SET @v1=1.0e0;
EXECUTE stmt1 USING @v1;
-- returns 1, expected 1
DEALLOCATE PREPARE stmt1;
DROP DATABASE db1;
Suggested fix:
Do not subsume the residual equality predicate after converting a REAL_RESULT value to an integer index key unless the conversion is verified to be lossless.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.047 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.