Bug #121954 BIGINT/VARCHAR ref lookup misses rows that compare equal

Submitted: 27 Nov 3:20
Reporter: QiFan Liu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: BIGINT, force index, ref lookup, varchar, wrong result

[27 Nov 3:20] QiFan Liu
Description:
A mixed BIGINT/VARCHAR equality predicate returns different results depending on the access path. For BIGINT 9007199254740993 and VARCHAR ' 9.007199254740992e15 ', direct comparison returns 1 and a scan-based join finds one row, but a forced covering-index lookup finds zero rows.

The observed output is:
semantic_comparison	1
scan_count	indexed_count
1	0

EXPLAIN FORMAT=TREE confirms the ref path:
-> Nested loop inner join  (cost=0.7 rows=1)
    -> Table scan on o  (cost=0.35 rows=1)
    -> Filter: (cast(i.n as double) = cast(o.s as double))  (cost=0.35 rows=1)
        -> Covering index lookup on i using k1 (n = o.s)  (cost=0.35 rows=1)

The same divergence occurs at the unsigned BIGINT boundary: 18446744073709551615 compares equal to VARCHAR '18446744073709551616'; the scan returns one row while the forced index lookup returns zero.

Bug #36448705 may also involve ref-key numeric conversion; this report specifically demonstrates access-path-dependent results for mixed BIGINT/VARCHAR equality.

How to repeat:
Run as root in the mysql client:

CREATE DATABASE db1;
USE db1;

CREATE TABLE i1 (
  id INT PRIMARY KEY,
  n BIGINT NOT NULL,
  KEY k1 (n)
);
CREATE TABLE o1 (
  id INT PRIMARY KEY,
  s VARCHAR(40) NOT NULL
);
INSERT INTO i1 VALUES (1, 9007199254740993);
INSERT INTO o1 VALUES (1, ' 9.007199254740992e15 ');

SELECT 'semantic_comparison' AS label, i.n = o.s AS equal_result
FROM o1 AS o
CROSS JOIN i1 AS i;
-- returns: semantic_comparison  1

EXPLAIN FORMAT=TREE
SELECT STRAIGHT_JOIN i.id
FROM o1 AS o
JOIN i1 AS i FORCE INDEX (k1) ON i.n = o.s;

SET @indexed = (
  SELECT COUNT(*)
  FROM o1 AS o
  JOIN i1 AS i FORCE INDEX (k1) ON i.n = o.s
);
SET @scan = (
  SELECT COUNT(*)
  FROM o1 AS o
  JOIN i1 AS i IGNORE INDEX (k1) ON i.n = o.s
);
SELECT @scan AS scan_count, @indexed AS indexed_count;
-- returns: 1  0; both counts should be 1

-- Variant using the unsigned BIGINT boundary:
CREATE TABLE i2 (
  id INT PRIMARY KEY,
  n BIGINT UNSIGNED NOT NULL,
  KEY k2 (n)
);
CREATE TABLE o2 (
  id INT PRIMARY KEY,
  s VARCHAR(40) NOT NULL
);
INSERT INTO i2 VALUES (1, 18446744073709551615);
INSERT INTO o2 VALUES (1, '18446744073709551616');

SELECT 'semantic_comparison' AS label, i.n = o.s AS equal_result
FROM o2 AS o
CROSS JOIN i2 AS i;
-- returns: semantic_comparison  1

SET @indexed = (
  SELECT COUNT(*)
  FROM o2 AS o
  JOIN i2 AS i FORCE INDEX (k2) ON i.n = o.s
);
SET @scan = (
  SELECT COUNT(*)
  FROM o2 AS o
  JOIN i2 AS i IGNORE INDEX (k2) ON i.n = o.s
);
SELECT @scan AS scan_count, @indexed AS indexed_count;
-- returns: 1  0; both counts should be 1

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.049 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.