From d3c81804ba8754e3f920b266a917b7facbae23f9 Mon Sep 17 00:00:00 2001 From: Monty Date: Wed, 21 Jun 2023 14:00:18 +0300 Subject: [PATCH] MDEV-31494 Server crashes in ha_partition::index_blocks / get_key_scans_params MDEV-31445 Server crashes in ha_partition::index_blocks / cost_for_index_read The crash happened in the case where partition pruning finds 0 partitions. --- mysql-test/main/partition.result | 38 ++++++++++++++++++++++++++++++++ mysql-test/main/partition.test | 37 +++++++++++++++++++++++++++++++ sql/ha_partition.cc | 2 +- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/mysql-test/main/partition.result b/mysql-test/main/partition.result index 78d4aa3f87c..9155f90d781 100644 --- a/mysql-test/main/partition.result +++ b/mysql-test/main/partition.result @@ -2858,3 +2858,41 @@ id select_type table type possible_keys key key_len ref rows Extra 2 DEPENDENT SUBQUERY t3 ref_or_null a1,b a1 10 func,test.t2.a 198 Using where; Full scan on NULL key set optimizer_switch=@tmp_os; drop table t1,t2,t3; +# +# MDEV-31494 Server crashes in ha_partition::index_blocks / +# get_key_scans_params +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); +CREATE TABLE t2 (b INT, c INT, KEY(c)) PARTITION BY LIST (b) (PARTITION p0 VALUES IN (1,2), PARTITION p1 VALUES IN (3,4)); +INSERT INTO t2 VALUES (1,1),(2,2); +explain SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE t2.b = 5 AND t2.c = 10; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE t2.b = 5 AND t2.c = 10; +a b c +DROP TABLE t1, t2; +# +# MDEV-31445 Server crashes in ha_partition::index_blocks / +# cost_for_index_read +# +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (b INT); +CREATE TABLE t3 (c INT, d INT, KEY(d)) PARTITION BY KEY (c) PARTITIONS 2; +CREATE TABLE t4 (e INT); +INSERT INTO t1 VALUES (1),(2); +INSERT INTO t2 VALUES (1),(2); +INSERT INTO t3 VALUES (1,1),(2,2); +INSERT INTO t4 VALUES (1),(2); +explain SELECT * FROM t1 LEFT JOIN (t2 JOIN t3 ON (0 IN (SELECT e FROM t4) AND t3.d = t2.b)) ON (t3.c < t2.b); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 +1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where +1 PRIMARY t3 ref d d 5 test.t2.b 1 Using where +2 SUBQUERY t4 ALL NULL NULL NULL NULL 2 Using where +SELECT * FROM t1 LEFT JOIN (t2 JOIN t3 ON (0 IN (SELECT e FROM t4) AND t3.d = t2.b)) ON (t3.c < t2.b); +a b c d +1 NULL NULL NULL +2 NULL NULL NULL +drop table t1,t2,t3,t4; +# End of 11.0 tests diff --git a/mysql-test/main/partition.test b/mysql-test/main/partition.test index d96031a6b2b..101e696a9ff 100644 --- a/mysql-test/main/partition.test +++ b/mysql-test/main/partition.test @@ -3062,3 +3062,40 @@ set optimizer_switch=@tmp_os; drop table t1,t2,t3; + +--echo # +--echo # MDEV-31494 Server crashes in ha_partition::index_blocks / +--echo # get_key_scans_params +--echo # + +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1),(2); + +CREATE TABLE t2 (b INT, c INT, KEY(c)) PARTITION BY LIST (b) (PARTITION p0 VALUES IN (1,2), PARTITION p1 VALUES IN (3,4)); + +INSERT INTO t2 VALUES (1,1),(2,2); +explain SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE t2.b = 5 AND t2.c = 10; +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE t2.b = 5 AND t2.c = 10; +DROP TABLE t1, t2; + +--echo # +--echo # MDEV-31445 Server crashes in ha_partition::index_blocks / +--echo # cost_for_index_read +--echo # + +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (b INT); +CREATE TABLE t3 (c INT, d INT, KEY(d)) PARTITION BY KEY (c) PARTITIONS 2; +CREATE TABLE t4 (e INT); + +INSERT INTO t1 VALUES (1),(2); +INSERT INTO t2 VALUES (1),(2); +INSERT INTO t3 VALUES (1,1),(2,2); +INSERT INTO t4 VALUES (1),(2); + +explain SELECT * FROM t1 LEFT JOIN (t2 JOIN t3 ON (0 IN (SELECT e FROM t4) AND t3.d = t2.b)) ON (t3.c < t2.b); +SELECT * FROM t1 LEFT JOIN (t2 JOIN t3 ON (0 IN (SELECT e FROM t4) AND t3.d = t2.b)) ON (t3.c < t2.b); + +drop table t1,t2,t3,t4; + +--echo # End of 11.0 tests diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index a7cfbc44971..2a36436758b 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -12204,7 +12204,7 @@ ulonglong ha_partition::index_blocks(uint index, uint ranges, ha_rows rows) ulonglong blocks= 0; ulonglong active_partitions= bitmap_bits_set(&m_part_info->read_partitions); /* Assume rows are evenly dived among partitions */ - rows= (rows+ active_partitions -1) / active_partitions; + rows= (rows+ active_partitions -1) / MY_MAX(active_partitions, 1); for (uint i= bitmap_get_first_set(&m_part_info->read_partitions); i < m_tot_parts;