Commit 50d4d1ca authored by Igor Babaev's avatar Igor Babaev

Fixed bug mdev-4367.

When calculating selectivity of conditions one should take into account
the cases when some tables to be joined are empty.
parent d62ee4e9
...@@ -750,4 +750,18 @@ drop table t1; ...@@ -750,4 +750,18 @@ drop table t1;
set histogram_size=@save_histogram_size; set histogram_size=@save_histogram_size;
set histogram_type=@save_histogram_type; set histogram_type=@save_histogram_type;
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; 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 use_stat_tables=@save_use_stat_tables;
...@@ -757,6 +757,20 @@ drop table t1; ...@@ -757,6 +757,20 @@ drop table t1;
set histogram_size=@save_histogram_size; set histogram_size=@save_histogram_size;
set histogram_type=@save_histogram_type; set histogram_type=@save_histogram_type;
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; 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 use_stat_tables=@save_use_stat_tables;
set optimizer_switch=@save_optimizer_switch_for_selectivity_test; set optimizer_switch=@save_optimizer_switch_for_selectivity_test;
SET SESSION STORAGE_ENGINE=DEFAULT; SET SESSION STORAGE_ENGINE=DEFAULT;
...@@ -336,4 +336,26 @@ set histogram_size=@save_histogram_size; ...@@ -336,4 +336,26 @@ set histogram_size=@save_histogram_size;
set histogram_type=@save_histogram_type; set histogram_type=@save_histogram_type;
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; 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; set use_stat_tables=@save_use_stat_tables;
...@@ -3324,6 +3324,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) ...@@ -3324,6 +3324,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
DBUG_ENTER("calculate_cond_selectivity_for_table"); DBUG_ENTER("calculate_cond_selectivity_for_table");
table->cond_selectivity= 1.0; table->cond_selectivity= 1.0;
#if 0
#else
if (table_records == 0)
DBUG_RETURN(FALSE);
#endif
if (thd->variables.optimizer_use_condition_selectivity > 2 && if (thd->variables.optimizer_use_condition_selectivity > 2 &&
!bitmap_is_clear_all(used_fields)) !bitmap_is_clear_all(used_fields))
...@@ -3363,11 +3369,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) ...@@ -3363,11 +3369,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
double rows; double rows;
if (*key) if (*key)
{ {
#if 0
rows= records_in_column_ranges(&param, idx, *key);
if (rows != HA_POS_ERROR)
(*key)->field->cond_selectivity= rows/table_records;
#else
table->reginfo.impossible_range= 0; table->reginfo.impossible_range= 0;
if ((*key)->type == SEL_ARG::IMPOSSIBLE) if ((*key)->type == SEL_ARG::IMPOSSIBLE)
{ {
...@@ -3381,7 +3382,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) ...@@ -3381,7 +3382,6 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
if (rows != HA_POS_ERROR) if (rows != HA_POS_ERROR)
(*key)->field->cond_selectivity= rows/table_records; (*key)->field->cond_selectivity= rows/table_records;
} }
#endif
} }
} }
......
...@@ -3795,7 +3795,7 @@ make_join_statistics(JOIN *join, List<TABLE_LIST> &tables_list, ...@@ -3795,7 +3795,7 @@ make_join_statistics(JOIN *join, List<TABLE_LIST> &tables_list,
*/ */
add_group_and_distinct_keys(join, s); 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). Perform range analysis if there are keys it could use (1).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment