diff --git a/mysql-test/r/selectivity.result b/mysql-test/r/selectivity.result index 22efaa480c6..bd29affc724 100644 --- a/mysql-test/r/selectivity.result +++ b/mysql-test/r/selectivity.result @@ -750,4 +750,18 @@ drop table t1; 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 +# when 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; +INSERT INTO t2 VALUES ('x'),('y'); +CREATE TABLE t3 (c varchar(1), KEY(c)) ENGINE=MyISAM; +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; 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 10e7b6a03c2..736ee98e41a 100644 --- a/mysql-test/r/selectivity_innodb.result +++ b/mysql-test/r/selectivity_innodb.result @@ -757,6 +757,20 @@ drop table t1; 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 +# when 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; +INSERT INTO t2 VALUES ('x'),('y'); +CREATE TABLE t3 (c varchar(1), KEY(c)) ENGINE=MyISAM; +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; 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 31ca9eefdbf..0614920dc13 100644 --- a/mysql-test/t/selectivity.test +++ b/mysql-test/t/selectivity.test @@ -336,4 +336,26 @@ set histogram_size=@save_histogram_size; 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 # when optimizer_use_condition_selectivity=3 +--echo # + +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; +INSERT INTO t2 VALUES ('x'),('y'); + +CREATE TABLE t3 (c varchar(1), KEY(c)) ENGINE=MyISAM; + +SELECT * FROM t1 STRAIGHT_JOIN (t2 JOIN t3 ON c = b AND b > 'z'); + +DROP TABLE t1,t2,t3; + +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 44c5f619f37..3c65d99038a 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -3324,6 +3324,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) DBUG_ENTER("calculate_cond_selectivity_for_table"); 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)) @@ -3363,11 +3369,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) double rows; if (*key) { -#if 0 - rows= records_in_column_ranges(¶m, idx, *key); - if (rows != HA_POS_ERROR) - (*key)->field->cond_selectivity= rows/table_records; -#else table->reginfo.impossible_range= 0; if ((*key)->type == SEL_ARG::IMPOSSIBLE) { @@ -3381,7 +3382,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) if (rows != HA_POS_ERROR) (*key)->field->cond_selectivity= rows/table_records; } -#endif } } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c5671544972..109c7f22edc 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -3795,7 +3795,7 @@ make_join_statistics(JOIN *join, List &tables_list, */ add_group_and_distinct_keys(join, s); - table->cond_selectivity= 1.0; + s->table->cond_selectivity= 1.0; /* Perform range analysis if there are keys it could use (1).