From f4c09baf1067e9c649b11cb7e414cb074af47706 Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Fri, 3 Feb 2023 14:20:22 +0200 Subject: MDEV-30525: Assertion `ranges > 0' fails in IO_AND_CPU_COST handler::keyread_time Make get_best_group_min_max() exit early if the table has table->records()=0. Attempting to compute loose scan over 0 groups eventually causes an assert when trying to get the cost of reading 0 ranges. --- mysql-test/main/merge.result | 9 +++++++++ mysql-test/main/merge.test | 9 +++++++++ sql/opt_range.cc | 2 ++ 3 files changed, 20 insertions(+) diff --git a/mysql-test/main/merge.result b/mysql-test/main/merge.result index 335d06fd062..307b445ab50 100644 --- a/mysql-test/main/merge.result +++ b/mysql-test/main/merge.result @@ -3936,5 +3936,14 @@ SELECT a, COUNT(*) FROM t1 WHERE a >= '2000-01-01 00:00:00' GROUP BY a; a COUNT(*) DROP TABLE t1; # +# MDEV-30525: Assertion `ranges > 0' fails in IO_AND_CPU_COST handler::keyread_time +# +CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM; +CREATE TABLE t2 (a INT, KEY(a)) ENGINE=MyISAM; +CREATE TABLE tm (a INT, KEY(a)) ENGINE=MRG_MyISAM UNION=(t1,t2); +SELECT DISTINCT a FROM tm WHERE a > 50; +a +DROP TABLE tm, t1, t2; +# # End of 11.0 tests # diff --git a/mysql-test/main/merge.test b/mysql-test/main/merge.test index aef2fa364d6..83173a46a8f 100644 --- a/mysql-test/main/merge.test +++ b/mysql-test/main/merge.test @@ -2896,6 +2896,15 @@ explain SELECT a, COUNT(*) FROM t1 WHERE a >= '2000-01-01 00:00:00' GROUP BY a; SELECT a, COUNT(*) FROM t1 WHERE a >= '2000-01-01 00:00:00' GROUP BY a; DROP TABLE t1; +--echo # +--echo # MDEV-30525: Assertion `ranges > 0' fails in IO_AND_CPU_COST handler::keyread_time +--echo # +CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM; +CREATE TABLE t2 (a INT, KEY(a)) ENGINE=MyISAM; +CREATE TABLE tm (a INT, KEY(a)) ENGINE=MRG_MyISAM UNION=(t1,t2); +SELECT DISTINCT a FROM tm WHERE a > 50; +DROP TABLE tm, t1, t2; + --echo # --echo # End of 11.0 tests --echo # diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 93e37d732d4..f2696683124 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -14049,6 +14049,8 @@ get_best_group_min_max(PARAM *param, SEL_TREE *tree, double read_time) else if (join->conds && join->conds->used_tables() & OUTER_REF_TABLE_BIT) // Cannot execute with correlated conditions. cause= "correlated conditions"; + else if (table->stat_records() == 0) + cause= "Empty table"; // Exit now, records=0 messes up cost computations if (cause) { -- cgit v1.2.1