diff --git a/mysql-test/suite/parts/r/bug87512.result b/mysql-test/suite/parts/r/bug87512.result new file mode 100644 index 0000000..22b8eae --- /dev/null +++ b/mysql-test/suite/parts/r/bug87512.result @@ -0,0 +1,13 @@ +CREATE TABLE t1( +id INT UNSIGNED NOT NULL, +dttm DATETIME NOT NULL, +PRIMARY KEY(id, dttm) +) ENGINE=InnoDB +PARTITION BY RANGE COLUMNS(dttm) ( +PARTITION pf_201612 VALUES LESS THAN ('20170101') ENGINE=InnoDB +); +SELECT * FROM t1 WHERE dttm > '2017-01-19' ORDER BY id DESC; +id dttm +SELECT * FROM t1 WHERE dttm > '2017-01-19' ORDER BY id ASC; +id dttm +DROP TABLE t1; diff --git a/mysql-test/suite/parts/t/bug87512.test b/mysql-test/suite/parts/t/bug87512.test new file mode 100644 index 0000000..25cc323 --- /dev/null +++ b/mysql-test/suite/parts/t/bug87512.test @@ -0,0 +1,20 @@ +# +# Bug #87512 "Server crashes when querying data from non-existing partition" +# + +--source include/have_innodb.inc +--source include/have_partition.inc + +CREATE TABLE t1( + id INT UNSIGNED NOT NULL, + dttm DATETIME NOT NULL, + PRIMARY KEY(id, dttm) +) ENGINE=InnoDB +PARTITION BY RANGE COLUMNS(dttm) ( + PARTITION pf_201612 VALUES LESS THAN ('20170101') ENGINE=InnoDB +); + +SELECT * FROM t1 WHERE dttm > '2017-01-19' ORDER BY id DESC; +SELECT * FROM t1 WHERE dttm > '2017-01-19' ORDER BY id ASC; + +DROP TABLE t1; diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc index 99c95ca..6cdbac5 100644 --- a/sql/ha_partition.cc +++ b/sql/ha_partition.cc @@ -5690,10 +5690,21 @@ int ha_partition::partition_scan_set_up(uchar * buf, bool idx_read_flag) get_partition_set(table,buf,active_index,&m_start_key,&m_part_spec); else { - m_part_spec.start_part= 0; - m_part_spec.end_part= m_tot_parts - 1; + m_part_spec.start_part= + bitmap_get_first_set(&m_part_info->read_partitions); + m_part_spec.end_part= m_part_spec.start_part; + + uint i= m_part_spec.end_part; + while (i != MY_BIT_NONE) + { + i= bitmap_get_next_set(&m_part_info->read_partitions, i); + if (i != MY_BIT_NONE) + m_part_spec.end_part= i; + } } - if (m_part_spec.start_part > m_part_spec.end_part) + if (m_part_spec.start_part == MY_BIT_NONE || + m_part_spec.end_part == MY_BIT_NONE || + m_part_spec.start_part > m_part_spec.end_part) { /* We discovered a partition set but the set was empty so we report