Description:
A query using a NOT BETWEEN clause produces inconsistent results between the optimized plan and the unoptimized count when the BETWEEN operator's boundaries become NULL. This occurs when invalid date/time arithmetic (e.g., subtracting an INTERVAL from an integer) is used to define the boundaries. The optimizer correctly evaluates NOT (NULL) to NULL and excludes the row, while the unoptimized path incorrectly evaluates NOT (NULL) to TRUE, leading to an erroneous row count.
How to repeat:
CREATE TABLE t14265 (c1 MEDIUMINT UNSIGNED);
INSERT INTO t14265 (c1) VALUES (10914966);
---- Below are the SQL statements that reveal the bug.
SELECT * FROM t14265 WHERE ((NOT (((c1)) BETWEEN ((126 - INTERVAL '11.721673' SECOND_MICROSECOND)) AND (NULL))));
-- rows: 0
SELECT SUM(count) FROM (SELECT (((NOT (((c1)) BETWEEN ((126 - INTERVAL '11.721673' SECOND_MICROSECOND)) AND ((NULL)))))) IS TRUE AS count FROM t14265) AS ta_norec;
-- 1