Bug #49504 Inconsistency in handling range predicates with NULL constants
Submitted: 7 Dec 2009 12:54 Modified: 7 Dec 2009 15:42
Reporter: Alexey Kopytov Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.1+ OS:Any
Assigned to: CPU Architecture:Any

[7 Dec 2009 12:54] Alexey Kopytov
Description:
Followup to Igor's comments on bug #47123:

"
Alexey,

After your patch, for your test case, we have:

mysql> EXPLAIN SELECT * FROM t1 WHERE a <> NULL;
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra 
                                             |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL |
Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
1 row in set (10.68 sec)

Yet, if we drop the index we get:

mysql > drop index a on t1;
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

(I've added two extra records to t1 with values '2' and '3').

mysql> EXPLAIN SELECT * FROM t1 WHERE a <> NULL;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra 
     |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using
where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

This seems to me to be counter-intuitive. I would expect to get "Impossible where" in
this case too.
"

How to repeat:
CREATE TABLE t1(a INT, KEY(a));
INSERT INTO t1 VALUES (1), (NULL);
EXPLAIN SELECT * FROM t1 WHERE a <> NULL;
DROP INDEX a ON t1;
EXPLAIN SELECT * FROM t1 WHERE a <> NULL;

Suggested fix:
In addition to catching null-rejecting predicates returning UNKNOWN in get_mm_leaf(), do the same in optimize_cond() so that we have "imp. where" for both indexed and non-indexed columns.
[7 Dec 2009 15:42] MySQL Verification Team
Thank you for the bug report.