Skip to content

Commit 3e8e4d0

Browse files
author
ram@gw.mysql.r18.ru
committed
A fix (bug #6475: Another server crash in 5.0.2
bug #6515: count(distinct...) crashes the server)
1 parent a3f4bd2 commit 3e8e4d0

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

mysql-test/r/count_distinct.result

+7
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,10 @@ select count(distinct f) from t1;
5353
count(distinct f)
5454
0
5555
drop table t1;
56+
create table t1 (a char(3), b char(20), primary key (a, b));
57+
insert into t1 values ('ABW', 'Dutch'), ('ABW', 'English');
58+
select count(distinct a) from t1 group by b;
59+
count(distinct a)
60+
1
61+
1
62+
drop table t1;

mysql-test/r/func_gconcat.result

+7
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,10 @@ group_concat(distinct b order by b)
457457
Warnings:
458458
Warning 1260 2 line(s) were cut by GROUP_CONCAT()
459459
drop table t1;
460+
create table t1 (a char(3), b char(20), primary key (a, b));
461+
insert into t1 values ('ABW', 'Dutch'), ('ABW', 'English');
462+
select group_concat(a) from t1 group by b;
463+
group_concat(a)
464+
ABW
465+
ABW
466+
drop table t1;

mysql-test/t/count_distinct.test

+8
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ create table t1 (f int);
5555
select count(distinct f) from t1;
5656
drop table t1;
5757

58+
#
59+
# Bug #6515
60+
#
61+
62+
create table t1 (a char(3), b char(20), primary key (a, b));
63+
insert into t1 values ('ABW', 'Dutch'), ('ABW', 'English');
64+
select count(distinct a) from t1 group by b;
65+
drop table t1;

mysql-test/t/func_gconcat.test

+9
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,12 @@ select group_concat(b order by b) from t1 group by a;
277277
select group_concat(distinct b order by b) from t1 group by a;
278278

279279
drop table t1;
280+
281+
#
282+
# Bug #6475
283+
#
284+
285+
create table t1 (a char(3), b char(20), primary key (a, b));
286+
insert into t1 values ('ABW', 'Dutch'), ('ABW', 'English');
287+
select group_concat(a) from t1 group by b;
288+
drop table t1;

sql/sql_select.cc

+33-11
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ static void init_tmptable_sum_functions(Item_sum **func);
192192
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
193193
static void copy_sum_funcs(Item_sum **func_ptr);
194194
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
195+
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
195196
static bool init_sum_functions(Item_sum **func, Item_sum **end);
196197
static bool update_sum_func(Item_sum **func);
197198
static void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
@@ -990,13 +991,15 @@ JOIN::optimize()
990991
if (create_sort_index(thd, this, group_list,
991992
HA_POS_ERROR, HA_POS_ERROR) ||
992993
alloc_group_fields(this, group_list) ||
993-
make_sum_func_list(all_fields, fields_list, 1))
994+
make_sum_func_list(all_fields, fields_list, 1) ||
995+
setup_sum_funcs(thd, sum_funcs))
994996
DBUG_RETURN(1);
995997
group_list=0;
996998
}
997999
else
9981000
{
999-
if (make_sum_func_list(all_fields, fields_list, 0))
1001+
if (make_sum_func_list(all_fields, fields_list, 0) ||
1002+
setup_sum_funcs(thd, sum_funcs))
10001003
DBUG_RETURN(1);
10011004
if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
10021005
{
@@ -1372,6 +1375,7 @@ JOIN::exec()
13721375
}
13731376
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
13741377
1, TRUE) ||
1378+
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
13751379
(tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table,
13761380
0)))
13771381
{
@@ -1459,7 +1463,9 @@ JOIN::exec()
14591463
set_items_ref_array(items3);
14601464

14611465
if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
1462-
1, TRUE) || thd->is_fatal_error)
1466+
1, TRUE) ||
1467+
setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
1468+
thd->is_fatal_error)
14631469
DBUG_VOID_RETURN;
14641470
}
14651471
if (curr_join->group_list || curr_join->order)
@@ -11794,9 +11800,6 @@ bool JOIN::alloc_func_list()
1179411800
before_group_by Set to 1 if this is called before GROUP BY handling
1179511801
recompute Set to TRUE if sum_funcs must be recomputed
1179611802
11797-
NOTES
11798-
Calls ::setup() for all item_sum objects in field_list
11799-
1180011803
RETURN
1180111804
0 ok
1180211805
1 error
@@ -11817,12 +11820,7 @@ bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
1181711820
while ((item=it++))
1181811821
{
1181911822
if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
11820-
{
1182111823
*func++= (Item_sum*) item;
11822-
/* let COUNT(DISTINCT) create the temporary table */
11823-
if (((Item_sum*) item)->setup(thd))
11824-
DBUG_RETURN(TRUE);
11825-
}
1182611824
}
1182711825
if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
1182811826
{
@@ -11967,6 +11965,30 @@ change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
1196711965
Code for calculating functions
1196811966
******************************************************************************/
1196911967

11968+
11969+
/*
11970+
Call ::setup for all sum functions
11971+
11972+
SYNOPSIS
11973+
setup_sum_funcs()
11974+
thd thread handler
11975+
func_ptr sum function list
11976+
11977+
RETURN
11978+
FALSE ok
11979+
TRUE error
11980+
*/
11981+
11982+
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
11983+
{
11984+
Item_sum *func;
11985+
while ((func= *(func_ptr++)))
11986+
if (func->setup(thd))
11987+
return TRUE;
11988+
return FALSE;
11989+
}
11990+
11991+
1197011992
static void
1197111993
init_tmptable_sum_functions(Item_sum **func_ptr)
1197211994
{

0 commit comments

Comments
 (0)