diff --git a/mysql-test/r/selectivity.result b/mysql-test/r/selectivity.result index bd29affc724..760c7796302 100644 --- a/mysql-test/r/selectivity.result +++ b/mysql-test/r/selectivity.result @@ -751,10 +751,10 @@ set histogram_size=@save_histogram_size; set histogram_type=@save_histogram_type; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; # -# Bug mdev-4367: join of a merged empty derived table +# Bug mdev-4367: 2-way join with an empty table # when optimizer_use_condition_selectivity=3 # -SET optimizer_use_condition_selectivity=3; +set optimizer_use_condition_selectivity=3; CREATE TABLE t1 (a varchar(1)) ENGINE=MyISAM; INSERT INTO t1 VALUES ('j'),('k'); CREATE TABLE t2 (b varchar(1)) ENGINE=MyISAM; @@ -764,4 +764,34 @@ SELECT * FROM t1 STRAIGHT_JOIN (t2 JOIN t3 ON c = b AND b > 'z'); a b c DROP TABLE t1,t2,t3; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; +# +# Bug mdev-4366: impossible condition on an indexed column discovered after +# substitution of constant tables +# with optimizer_use_condition_selectivity=3 +# +CREATE TABLE t1 (pk int PRIMARY KEY, a int); +INSERT INTO t1 VALUES +(1,4), (2,6), (3,3), (4,5); +CREATE TABLE t2 (b int); +INSERT INTO t2 VALUES (1), (7); +set optimizer_use_condition_selectivity=1; +EXPLAIN EXTENDED +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +Warnings: +Note 1003 select 1 AS `1` from `test`.`t1` join `test`.`t2` where 0 +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +1 +set optimizer_use_condition_selectivity=3; +EXPLAIN EXTENDED +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +Warnings: +Note 1003 select 1 AS `1` from `test`.`t1` join `test`.`t2` where 0 +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +1 +DROP TABLE t1,t2; +set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/selectivity_innodb.result b/mysql-test/r/selectivity_innodb.result index 736ee98e41a..cc9b5475ac1 100644 --- a/mysql-test/r/selectivity_innodb.result +++ b/mysql-test/r/selectivity_innodb.result @@ -758,10 +758,10 @@ set histogram_size=@save_histogram_size; set histogram_type=@save_histogram_type; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; # -# Bug mdev-4367: join of a merged empty derived table +# Bug mdev-4367: 2-way join with an empty table # when optimizer_use_condition_selectivity=3 # -SET optimizer_use_condition_selectivity=3; +set optimizer_use_condition_selectivity=3; CREATE TABLE t1 (a varchar(1)) ENGINE=MyISAM; INSERT INTO t1 VALUES ('j'),('k'); CREATE TABLE t2 (b varchar(1)) ENGINE=MyISAM; @@ -771,6 +771,36 @@ SELECT * FROM t1 STRAIGHT_JOIN (t2 JOIN t3 ON c = b AND b > 'z'); a b c DROP TABLE t1,t2,t3; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; +# +# Bug mdev-4366: impossible condition on an indexed column discovered after +# substitution of constant tables +# with optimizer_use_condition_selectivity=3 +# +CREATE TABLE t1 (pk int PRIMARY KEY, a int); +INSERT INTO t1 VALUES +(1,4), (2,6), (3,3), (4,5); +CREATE TABLE t2 (b int); +INSERT INTO t2 VALUES (1), (7); +set optimizer_use_condition_selectivity=1; +EXPLAIN EXTENDED +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +Warnings: +Note 1003 select 1 AS `1` from `test`.`t1` join `test`.`t2` where 0 +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +1 +set optimizer_use_condition_selectivity=3; +EXPLAIN EXTENDED +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +Warnings: +Note 1003 select 1 AS `1` from `test`.`t1` join `test`.`t2` where 0 +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +1 +DROP TABLE t1,t2; +set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; set use_stat_tables=@save_use_stat_tables; set optimizer_switch=@save_optimizer_switch_for_selectivity_test; SET SESSION STORAGE_ENGINE=DEFAULT; diff --git a/mysql-test/t/selectivity.test b/mysql-test/t/selectivity.test index 0614920dc13..4d0ec2d9128 100644 --- a/mysql-test/t/selectivity.test +++ b/mysql-test/t/selectivity.test @@ -337,11 +337,11 @@ set histogram_type=@save_histogram_type; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; --echo # ---echo # Bug mdev-4367: join of a merged empty derived table +--echo # Bug mdev-4367: 2-way join with an empty table --echo # when optimizer_use_condition_selectivity=3 --echo # -SET optimizer_use_condition_selectivity=3; +set optimizer_use_condition_selectivity=3; CREATE TABLE t1 (a varchar(1)) ENGINE=MyISAM; INSERT INTO t1 VALUES ('j'),('k'); @@ -357,5 +357,32 @@ DROP TABLE t1,t2,t3; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; +--echo # +--echo # Bug mdev-4366: impossible condition on an indexed column discovered after +--echo # substitution of constant tables +--echo # with optimizer_use_condition_selectivity=3 +--echo # + +CREATE TABLE t1 (pk int PRIMARY KEY, a int); +INSERT INTO t1 VALUES + (1,4), (2,6), (3,3), (4,5); + +CREATE TABLE t2 (b int); +INSERT INTO t2 VALUES (1), (7); + +set optimizer_use_condition_selectivity=1; +EXPLAIN EXTENDED +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; + +set optimizer_use_condition_selectivity=3; +EXPLAIN EXTENDED +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; +SELECT 1 FROM t1, t2 WHERE pk = 6 AND a = 2 AND b = 10; + +DROP TABLE t1,t2; + +set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; + set use_stat_tables=@save_use_stat_tables; diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 3c65d99038a..215c85cbdf5 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -3325,11 +3325,8 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) table->cond_selectivity= 1.0; -#if 0 -#else if (table_records == 0) DBUG_RETURN(FALSE); -#endif if (thd->variables.optimizer_use_condition_selectivity > 2 && !bitmap_is_clear_all(used_fields)) @@ -3338,6 +3335,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) MEM_ROOT alloc; SEL_TREE *tree; SEL_ARG **key, **end; + double rows; uint idx= 0; init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0, @@ -3364,12 +3362,18 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) if (!tree) goto free_alloc; + table->reginfo.impossible_range= 0; + if (tree->type == SEL_TREE::IMPOSSIBLE) + { + rows= 0; + table->reginfo.impossible_range= 1; + goto free_alloc; + } + for (key= tree->keys, end= key + param.keys; key != end; key++, idx++) { - double rows; if (*key) { - table->reginfo.impossible_range= 0; if ((*key)->type == SEL_ARG::IMPOSSIBLE) { rows= 0;