Skip to content

Commit 48f02af

Browse files
committed
MDEV-9602 crash in st_key::actual_rec_per_key when group by constant
Problem was that cost_group_min_max() could not handle if group by was optimized away.
1 parent 646c4ce commit 48f02af

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

mysql-test/r/group_by.result

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,3 +2678,17 @@ NULL
26782678
100098
26792679
100099
26802680
drop table t0,t1,t2;
2681+
#
2682+
# MDEV-9602 crash in st_key::actual_rec_per_key when group by constant
2683+
#
2684+
create table t1 (a date not null,unique (a)) engine=innodb;
2685+
Warnings:
2686+
Warning 1286 Unknown storage engine 'innodb'
2687+
Warning 1266 Using storage engine MyISAM for table 't1'
2688+
select distinct a from t1 group by 'a';
2689+
a
2690+
insert into t1 values("2001-02-02"),("2001-02-03");
2691+
select distinct a from t1 group by 'a';
2692+
a
2693+
2001-02-02
2694+
drop table t1;

mysql-test/t/group_by.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,3 +1793,13 @@ from t1
17931793
group by t1.b;
17941794

17951795
drop table t0,t1,t2;
1796+
1797+
--echo #
1798+
--echo # MDEV-9602 crash in st_key::actual_rec_per_key when group by constant
1799+
--echo #
1800+
1801+
create table t1 (a date not null,unique (a)) engine=innodb;
1802+
select distinct a from t1 group by 'a';
1803+
insert into t1 values("2001-02-02"),("2001-02-03");
1804+
select distinct a from t1 group by 'a';
1805+
drop table t1;

sql/opt_range.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13105,7 +13105,17 @@ void cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
1310513105
num_blocks= (ha_rows)(table_records / keys_per_block) + 1;
1310613106

1310713107
/* Compute the number of keys in a group. */
13108-
keys_per_group= (ha_rows) index_info->actual_rec_per_key(group_key_parts - 1);
13108+
if (!group_key_parts)
13109+
{
13110+
/* Summary over the whole table */
13111+
keys_per_group= table_records;
13112+
}
13113+
else
13114+
{
13115+
keys_per_group= (ha_rows) index_info->actual_rec_per_key(group_key_parts -
13116+
1);
13117+
}
13118+
1310913119
if (keys_per_group == 0) /* If there is no statistics try to guess */
1311013120
/* each group contains 10% of all records */
1311113121
keys_per_group= (table_records / 10) + 1;

0 commit comments

Comments
 (0)