diff --git a/mysql-test/r/derived_cond_pushdown.result b/mysql-test/r/derived_cond_pushdown.result index 1aa228249a0..32d3c88cc8d 100644 --- a/mysql-test/r/derived_cond_pushdown.result +++ b/mysql-test/r/derived_cond_pushdown.result @@ -14928,3 +14928,20 @@ SELECT * FROM t1 LEFT JOIN v2 ON (a = pk); a pk MIN(b) DROP VIEW v2; DROP TABLE t1,t2; +# +# MDEV-14994: splittable table with no rows +# +CREATE TABLE t1 (f INT PRIMARY KEY) ENGINE=MyISAM; +CREATE VIEW v1 AS SELECT a.* FROM t1 AS a STRAIGHT_JOIN t1 AS b; +CREATE VIEW v2 AS SELECT f FROM v1 GROUP BY f; +SELECT * FROM v1 JOIN v2 ON v1.f = v2.f; +f f +EXPLAIN EXTENDED +SELECT * FROM v1 JOIN v2 ON v1.f = v2.f; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +3 LATERAL DERIVED NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table +Warnings: +Note 1003 /* select#1 */ select NULL AS `f`,`v2`.`f` AS `f` from `test`.`t1` `a` straight_join `test`.`t1` `b` join `test`.`v2` where 0 +DROP VIEW v1,v2; +DROP TABLE t1; diff --git a/mysql-test/t/derived_cond_pushdown.test b/mysql-test/t/derived_cond_pushdown.test index 80403d2c35a..e728a9590a1 100644 --- a/mysql-test/t/derived_cond_pushdown.test +++ b/mysql-test/t/derived_cond_pushdown.test @@ -2598,3 +2598,18 @@ SELECT * FROM t1 LEFT JOIN v2 ON (a = pk); DROP VIEW v2; DROP TABLE t1,t2; + +--echo # +--echo # MDEV-14994: splittable table with no rows +--echo # + +CREATE TABLE t1 (f INT PRIMARY KEY) ENGINE=MyISAM; +CREATE VIEW v1 AS SELECT a.* FROM t1 AS a STRAIGHT_JOIN t1 AS b; +CREATE VIEW v2 AS SELECT f FROM v1 GROUP BY f; + +SELECT * FROM v1 JOIN v2 ON v1.f = v2.f; +EXPLAIN EXTENDED +SELECT * FROM v1 JOIN v2 ON v1.f = v2.f; + +DROP VIEW v1,v2; +DROP TABLE t1; diff --git a/sql/opt_split.cc b/sql/opt_split.cc index efc5a5e5551..8fb8b787299 100644 --- a/sql/opt_split.cc +++ b/sql/opt_split.cc @@ -645,7 +645,8 @@ double spl_postjoin_oper_cost(THD *thd, double join_record_count, uint rec_len) cost+= get_tmp_table_lookup_cost(thd, join_record_count,rec_len) * join_record_count; // cost to perform post join operation used here cost+= get_tmp_table_lookup_cost(thd, join_record_count, rec_len) + - join_record_count * log2 (join_record_count) * + (join_record_count == 0 ? 0 : + join_record_count * log2 (join_record_count)) * SORT_INDEX_CMP_COST; // cost to perform sorting return cost; } @@ -948,7 +949,9 @@ SplM_plan_info * JOIN_TAB::choose_best_splitting(double record_count, spl_plan->table= best_table; spl_plan->key= best_key; spl_plan->parts= best_key_parts; - spl_plan->split_sel= best_rec_per_key / spl_opt_info->unsplit_card; + spl_plan->split_sel= best_rec_per_key / + (spl_opt_info->unsplit_card ? + spl_opt_info->unsplit_card : 1); uint rec_len= table->s->rec_buff_length;