Details
-
Bug
-
Status: Confirmed (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.11, 11.4, 11.8, 12.3, 13.1, 12.3.3, 13.1.1
-
Ubuntu22.04 x86_64
-
Unexpected results
-
Description
MAX(c0) with WHERE c0 <= 3 AND c0 BETWEEN 5 AND 10 returns 10, although no row satisfies both conditions and COUNT
with the same WHERE returns 0. The plan is "Select tables optimized away". With the two conditions in the other order the server reports "Impossible WHERE" and returns NULL, and with IGNORE INDEX the result is NULL. MIN() has the same problem: c0 >= 15 AND c0 BETWEEN 5 AND 10 returns 5.
The same happens when the BETWEEN is inside a derived table and the other condition outside it, which is how it was found:
SELECT MAX(c0) FROM (SELECT * FROM t WHERE c0 BETWEEN 5 AND 10) d WHERE c0 <= 3 returns 10.
MySQL 26.7.1 returns NULL for all of these.
How to reproduce
CREATE TABLE t (c0 INT PRIMARY KEY); |
INSERT INTO t VALUES (1), (5), (10), (20); |
|
|
SELECT COUNT(*) FROM t WHERE c0 <= 3 AND c0 BETWEEN 5 AND 10; |
-- 0
|
|
|
SELECT MAX(c0) FROM t WHERE c0 <= 3 AND c0 BETWEEN 5 AND 10; |
-- expected: NULL
|
-- actual: 10
|
|
|
SELECT MAX(c0) FROM t IGNORE INDEX (PRIMARY) WHERE c0 <= 3 AND c0 BETWEEN 5 AND 10; |
-- NULL correct
|
|
|
SELECT MAX(c0) FROM t WHERE c0 BETWEEN 5 AND 10 AND c0 <= 3; |
-- NULL correct (conditions in the other order)
|
|
|
SELECT MIN(c0) FROM t WHERE c0 >= 15 AND c0 BETWEEN 5 AND 10; |
-- expected: NULL
|
-- actual: 5
|
|
|
EXPLAIN SELECT MAX(c0) FROM t WHERE c0 <= 3 AND c0 BETWEEN 5 AND 10; |
-- Select tables optimized away
|
EXPLAIN SELECT MAX(c0) FROM t WHERE c0 BETWEEN 5 AND 10 AND c0 <= 3; |
-- Impossible WHERE noticed after reading const tables |
Attachments
Issue Links
- relates to
-
MDEV-41377 MIN/MAX index shortcut rounds a DECIMAL bound onto an integer key, wrong result
-
- Confirmed
-