From ebc168be65c43dffc56fab179f362d0355853d3e Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Mon, 17 Dec 2018 19:31:11 +1100 Subject: [PATCH] Impossible WHERE for a!=a, aa For a table column `a`, the above expressions logically equate to false in all cases. With this patch the optimizer knows about this and queries like: SELECT * FROM t1 WHERE a!=a no longer need to evaluate a!=a for every row. The same applies if the expression was `aa` An `EXPLAIN SELECT COUNT(*) FROM t1 WHERE a(`test`.`t1`.`a`,(/* select#2 */ select `test`.`t2`.`c` from `test`.`t2` where ((`test`.`t1`.`a`) = `test`.`t2`.`c`)))) DROP TABLE t1, t2; # End of test for Bug#23209903 +# +# Expect "Impossible WHERE" for never true values like a!=a, aa +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2),(3); +EXPLAIN SELECT * FROM t1 WHERE a!=a; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +Warnings: +Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0 +SELECT COUNT(*) FROM t1 WHERE a!=a; +COUNT(*) +0 +EXPLAIN SELECT * FROM t1 WHERE a>a; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +Warnings: +Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0 +SELECT COUNT(*) FROM t1 WHERE a>a; +COUNT(*) +0 +EXPLAIN SELECT COUNT(*) FROM t1 WHERE aa; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +Warnings: +Note 1003 /* select#1 */ select count(0) AS `COUNT(*)` from `test`.`t1` where 0 +SELECT COUNT(*) FROM t1 WHERE a>a; +COUNT(*) +0 +EXPLAIN SELECT COUNT(*) FROM t1 WHERE aa-1; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 3 33.33 Using where +Warnings: +Note 1003 /* select#1 */ select count(0) AS `COUNT(*)` from `test`.`t1` where (`test`.`t1`.`a` > (`test`.`t1`.`a` - 1)) +SELECT COUNT(*) FROM t1 WHERE a>a-1; +COUNT(*) +3 +EXPLAIN SELECT COUNT(*) FROM t1 WHERE aa +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2),(3); + +EXPLAIN SELECT * FROM t1 WHERE a!=a; +SELECT COUNT(*) FROM t1 WHERE a!=a; +EXPLAIN SELECT * FROM t1 WHERE a>a; +SELECT COUNT(*) FROM t1 WHERE a>a; +EXPLAIN SELECT COUNT(*) FROM t1 WHERE aa; +SELECT COUNT(*) FROM t1 WHERE a>a; +EXPLAIN SELECT COUNT(*) FROM t1 WHERE aa-1; +SELECT COUNT(*) FROM t1 WHERE a>a-1; +EXPLAIN SELECT COUNT(*) FROM t1 WHERE a(cond)->arguments()[0]; Item *right_item = down_cast(cond)->arguments()[1]; if (left_item->eq(right_item, 1)) { - if (!left_item->maybe_null || + if (*cond_value == Item::COND_FALSE || + !left_item->maybe_null || down_cast(cond)->functype() == Item_func::EQUAL_FUNC) { *retcond = NULL; return false; // Compare of identical items