Skip to content

Commit adbad5e

Browse files
committed
MDEV-31113 Server crashes in store_length / Type_handler_string_result::make_sort_key with DISTINCT and group function
Fix-up for commit 476b24d Author: Monty Date: Thu Feb 16 14:19:33 2023 +0200 MDEV-20057 Distinct SUM on CROSS JOIN and grouped returns wrong result which misses initializing of sorder->suffix_length. In this commit the initialization is implemented by passing MY_ZEROFILL flag to the allocation of SORT_FIELD elements
1 parent f216644 commit adbad5e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

mysql-test/main/distinct.result

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@ sum(distinct 1) sum(t1.d) > 5 c
10931093
1 1 0
10941094
1 0 5
10951095
1 1 6
1096+
SET @sort_buffer_size_save= @@sort_buffer_size;
10961097
set @@sort_buffer_size=1024;
10971098
insert into t1 select -seq,-seq from seq_1_to_100;
10981099
select distinct sum(distinct 1), sum(t1.d) > 2, length(group_concat(t1.d)) > 1000 from (t1 e join t1) group by t1.c having t1.c > -2 ;
@@ -1106,4 +1107,17 @@ sum(distinct 1) sum(t1.d) > 2 length(group_concat(t1.d)) > 1000 c
11061107
1 1 0 5
11071108
1 1 0 6
11081109
drop table t1;
1110+
set @@sort_buffer_size=@sort_buffer_size_save;
1111+
#
1112+
# MDEV-31113 Server crashes in store_length / Type_handler_string_result::make_sort_key
1113+
# with DISTINCT and group function
1114+
#
1115+
CREATE TABLE t (f INT);
1116+
INSERT INTO t VALUES (1),(2);
1117+
SELECT DISTINCT CONVERT(STDDEV(f), CHAR(16)) AS f1, UUID() AS f2 FROM t GROUP BY f2 WITH ROLLUP;
1118+
f1 f2
1119+
0.0000 #
1120+
0.0000 #
1121+
0.5000 #
1122+
DROP TABLE t;
11091123
# End of 10.4 tests

mysql-test/main/distinct.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,23 @@ select distinct sum(distinct 1), sum(t1.d) > 5 from (t1 e join t1) group by t1.c
834834
select distinct sum(distinct 1), sum(t1.d) > 5, t1.c from (t1 e join t1) group by t1.c;
835835

836836
# Force usage of remove_dup_with_compare() algorithm
837+
SET @sort_buffer_size_save= @@sort_buffer_size;
837838
set @@sort_buffer_size=1024;
838839
insert into t1 select -seq,-seq from seq_1_to_100;
839840
select distinct sum(distinct 1), sum(t1.d) > 2, length(group_concat(t1.d)) > 1000 from (t1 e join t1) group by t1.c having t1.c > -2 ;
840841
select distinct sum(distinct 1), sum(t1.d) > 2, length(group_concat(t1.d)) > 1000,t1.c from (t1 e join t1) group by t1.c having t1.c > -2;
841842
drop table t1;
843+
set @@sort_buffer_size=@sort_buffer_size_save;
844+
845+
--echo #
846+
--echo # MDEV-31113 Server crashes in store_length / Type_handler_string_result::make_sort_key
847+
--echo # with DISTINCT and group function
848+
--echo #
849+
850+
CREATE TABLE t (f INT);
851+
INSERT INTO t VALUES (1),(2);
852+
--replace_column 2 #
853+
SELECT DISTINCT CONVERT(STDDEV(f), CHAR(16)) AS f1, UUID() AS f2 FROM t GROUP BY f2 WITH ROLLUP;
854+
DROP TABLE t;
842855

843856
--echo # End of 10.4 tests

sql/sql_select.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24226,7 +24226,7 @@ JOIN_TAB::remove_duplicates()
2422624226

2422724227
if (!(sortorder= (SORT_FIELD*) my_malloc((fields->elements+1) *
2422824228
sizeof(SORT_FIELD),
24229-
MYF(MY_WME))))
24229+
MYF(MY_WME | MY_ZEROFILL))))
2423024230
DBUG_RETURN(TRUE);
2423124231

2423224232
/* Calculate how many saved fields there is in list */
@@ -24245,7 +24245,6 @@ JOIN_TAB::remove_duplicates()
2424524245
else
2424624246
{
2424724247
/* Item is not stored in temporary table, remember it */
24248-
sorder->field= 0; // Safety, not used
2424924248
sorder->item= item;
2425024249
/* Calculate sorder->length */
2425124250
item->type_handler()->sortlength(thd, item, sorder);

0 commit comments

Comments
 (0)