Loading mysql-test/r/group_by.result +36 −9 Original line number Diff line number Diff line Loading @@ -629,15 +629,6 @@ explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using filesort DROP TABLE t1; create table t1 ( col1 int, col2 int ); insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2); select group_concat( distinct col1 ) as alias from t1 group by col2 having alias like '%'; alias 1,2 1,2 1 drop table t1; create table t1 (a int); insert into t1 values(null); select min(a) is null from t1; Loading @@ -650,3 +641,39 @@ select 1 and min(a) is null from t1; 1 and min(a) is null 1 drop table t1; create table t1 ( col1 int, col2 int ); insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2); select group_concat( distinct col1 ) as alias from t1 group by col2 having alias like '%'; alias 1,2 1,2 1 drop table t1; create table t1 (a integer, b integer, c integer); insert into t1 (a,b) values (1,2),(1,3),(2,5); select a, 0.1*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; a r2 r1 1 1.0 2 select a, rand()*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; a r2 r1 1 1 2 select a,sum(b) from t1 where a=1 group by c; a sum(b) 1 5 select a*sum(b) from t1 where a=1 group by c; a*sum(b) 5 select sum(a)*sum(b) from t1 where a=1 group by c; sum(a)*sum(b) 10 select a,sum(b) from t1 where a=1 group by c having a=1; a sum(b) 1 5 select a as d,sum(b) from t1 where a=1 group by c having d=1; d sum(b) 1 5 select sum(a)*sum(b) as d from t1 where a=1 group by c having d > 0; d 10 drop table t1; mysql-test/r/user_var.result +2 −2 Original line number Diff line number Diff line Loading @@ -109,8 +109,8 @@ select @a:=0; select @a, @a:=@a+count(*), count(*), @a from t1 group by i; @a @a:=@a+count(*) count(*) @a 0 1 1 0 0 2 2 0 0 3 3 0 0 3 2 0 0 6 3 0 select @a:=0; @a:=0 0 Loading mysql-test/t/group_by.test +21 −7 Original line number Diff line number Diff line Loading @@ -457,6 +457,14 @@ SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; DROP TABLE t1; #Test for BUG#6976: Aggregate functions have incorrect NULL-ness create table t1 (a int); insert into t1 values(null); select min(a) is null from t1; select min(a) is null or null from t1; select 1 and min(a) is null from t1; drop table t1; # Test for BUG#5400: GROUP_CONCAT returns everything twice. create table t1 ( col1 int, col2 int ); insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2); Loading @@ -465,12 +473,18 @@ select group_concat( distinct col1 ) as alias from t1 drop table t1; # # Test BUG#8216 when referring in HAVING to n alias which is rand() function # #Test for BUG#6976: Aggregate functions have incorrect NULL-ness create table t1 (a int); insert into t1 values(null); select min(a) is null from t1; select min(a) is null or null from t1; select 1 and min(a) is null from t1; create table t1 (a integer, b integer, c integer); insert into t1 (a,b) values (1,2),(1,3),(2,5); select a, 0.1*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; select a, rand()*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; select a,sum(b) from t1 where a=1 group by c; select a*sum(b) from t1 where a=1 group by c; select sum(a)*sum(b) from t1 where a=1 group by c; select a,sum(b) from t1 where a=1 group by c having a=1; select a as d,sum(b) from t1 where a=1 group by c having d=1; select sum(a)*sum(b) as d from t1 where a=1 group by c having d > 0; drop table t1; sql/item_cmpfunc.cc +44 −2 Original line number Diff line number Diff line Loading @@ -1964,6 +1964,36 @@ bool Item_cond::walk(Item_processor processor, byte *arg) return Item_func::walk(processor, arg); } /* Move SUM items out from item tree and replace with reference SYNOPSIS split_sum_func() thd Thread handler ref_pointer_array Pointer to array of reference fields fields All fields in select NOTES This function is run on all expression (SELECT list, WHERE, HAVING etc) that have or refer (HAVING) to a SUM expression. The split is done to get an unique item for each SUM function so that we can easily find and calculate them. (Calculation done by update_sum_func() and copy_sum_funcs() in sql_select.cc) All found SUM items are added FIRST in the fields list and we replace the item with a reference. We also replace all functions without side effects (like RAND() or UDF's) that uses columns as arguments. For functions with side effects, we just remember any fields referred by the function to ensure that we get a copy of the field value for the first accepted row. This ensures that we can do things like SELECT a*SUM(b) FROM t1 WHERE a=1 */ void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields) { Loading @@ -1973,10 +2003,22 @@ void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array, const_item_cache=0; while ((item=li++)) { if (item->with_sum_func && item->type() != SUM_FUNC_ITEM) /* with_sum_func is set for items that contains a SUM expression */ if (item->type() != SUM_FUNC_ITEM && (item->with_sum_func || (item->used_tables() & PSEUDO_TABLE_BITS))) item->split_sum_func(thd, ref_pointer_array, fields); else if (item->used_tables() || item->type() == SUM_FUNC_ITEM) else if (item->type() == SUM_FUNC_ITEM || (item->used_tables() && item->type() != REF_ITEM)) { /* Replace item with a reference so that we can easily calculate it (in case of sum functions) or copy it (in case of fields) The test above is to ensure we don't do a reference for things that are constants or are not yet calculated as in: SELECT RAND() as r1, SUM(a) as r2 FROM t1 HAVING r1 > 1 AND r2 > 0 */ Item **ref= li.ref(); uint el= fields.elements; ref_pointer_array[el]= item; Loading sql/item_func.cc +6 −2 Original line number Diff line number Diff line Loading @@ -351,6 +351,7 @@ bool Item_func::walk (Item_processor processor, byte *argument) return (this->*processor)(argument); } void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields) { Loading @@ -358,9 +359,12 @@ void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array, for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++) { Item *item=* arg; if (item->with_sum_func && item->type() != SUM_FUNC_ITEM) if (item->type() != SUM_FUNC_ITEM && (item->with_sum_func || (item->used_tables() & PSEUDO_TABLE_BITS))) item->split_sum_func(thd, ref_pointer_array, fields); else if (item->used_tables() || item->type() == SUM_FUNC_ITEM) else if (item->type() == SUM_FUNC_ITEM || (item->used_tables() && item->type() != REF_ITEM)) { uint el= fields.elements; ref_pointer_array[el]= item; Loading Loading
mysql-test/r/group_by.result +36 −9 Original line number Diff line number Diff line Loading @@ -629,15 +629,6 @@ explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using filesort DROP TABLE t1; create table t1 ( col1 int, col2 int ); insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2); select group_concat( distinct col1 ) as alias from t1 group by col2 having alias like '%'; alias 1,2 1,2 1 drop table t1; create table t1 (a int); insert into t1 values(null); select min(a) is null from t1; Loading @@ -650,3 +641,39 @@ select 1 and min(a) is null from t1; 1 and min(a) is null 1 drop table t1; create table t1 ( col1 int, col2 int ); insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2); select group_concat( distinct col1 ) as alias from t1 group by col2 having alias like '%'; alias 1,2 1,2 1 drop table t1; create table t1 (a integer, b integer, c integer); insert into t1 (a,b) values (1,2),(1,3),(2,5); select a, 0.1*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; a r2 r1 1 1.0 2 select a, rand()*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; a r2 r1 1 1 2 select a,sum(b) from t1 where a=1 group by c; a sum(b) 1 5 select a*sum(b) from t1 where a=1 group by c; a*sum(b) 5 select sum(a)*sum(b) from t1 where a=1 group by c; sum(a)*sum(b) 10 select a,sum(b) from t1 where a=1 group by c having a=1; a sum(b) 1 5 select a as d,sum(b) from t1 where a=1 group by c having d=1; d sum(b) 1 5 select sum(a)*sum(b) as d from t1 where a=1 group by c having d > 0; d 10 drop table t1;
mysql-test/r/user_var.result +2 −2 Original line number Diff line number Diff line Loading @@ -109,8 +109,8 @@ select @a:=0; select @a, @a:=@a+count(*), count(*), @a from t1 group by i; @a @a:=@a+count(*) count(*) @a 0 1 1 0 0 2 2 0 0 3 3 0 0 3 2 0 0 6 3 0 select @a:=0; @a:=0 0 Loading
mysql-test/t/group_by.test +21 −7 Original line number Diff line number Diff line Loading @@ -457,6 +457,14 @@ SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; DROP TABLE t1; #Test for BUG#6976: Aggregate functions have incorrect NULL-ness create table t1 (a int); insert into t1 values(null); select min(a) is null from t1; select min(a) is null or null from t1; select 1 and min(a) is null from t1; drop table t1; # Test for BUG#5400: GROUP_CONCAT returns everything twice. create table t1 ( col1 int, col2 int ); insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2); Loading @@ -465,12 +473,18 @@ select group_concat( distinct col1 ) as alias from t1 drop table t1; # # Test BUG#8216 when referring in HAVING to n alias which is rand() function # #Test for BUG#6976: Aggregate functions have incorrect NULL-ness create table t1 (a int); insert into t1 values(null); select min(a) is null from t1; select min(a) is null or null from t1; select 1 and min(a) is null from t1; create table t1 (a integer, b integer, c integer); insert into t1 (a,b) values (1,2),(1,3),(2,5); select a, 0.1*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; select a, rand()*0+1 r2, sum(1) r1 from t1 where a = 1 group by a having r1>1 and r2=1; select a,sum(b) from t1 where a=1 group by c; select a*sum(b) from t1 where a=1 group by c; select sum(a)*sum(b) from t1 where a=1 group by c; select a,sum(b) from t1 where a=1 group by c having a=1; select a as d,sum(b) from t1 where a=1 group by c having d=1; select sum(a)*sum(b) as d from t1 where a=1 group by c having d > 0; drop table t1;
sql/item_cmpfunc.cc +44 −2 Original line number Diff line number Diff line Loading @@ -1964,6 +1964,36 @@ bool Item_cond::walk(Item_processor processor, byte *arg) return Item_func::walk(processor, arg); } /* Move SUM items out from item tree and replace with reference SYNOPSIS split_sum_func() thd Thread handler ref_pointer_array Pointer to array of reference fields fields All fields in select NOTES This function is run on all expression (SELECT list, WHERE, HAVING etc) that have or refer (HAVING) to a SUM expression. The split is done to get an unique item for each SUM function so that we can easily find and calculate them. (Calculation done by update_sum_func() and copy_sum_funcs() in sql_select.cc) All found SUM items are added FIRST in the fields list and we replace the item with a reference. We also replace all functions without side effects (like RAND() or UDF's) that uses columns as arguments. For functions with side effects, we just remember any fields referred by the function to ensure that we get a copy of the field value for the first accepted row. This ensures that we can do things like SELECT a*SUM(b) FROM t1 WHERE a=1 */ void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields) { Loading @@ -1973,10 +2003,22 @@ void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array, const_item_cache=0; while ((item=li++)) { if (item->with_sum_func && item->type() != SUM_FUNC_ITEM) /* with_sum_func is set for items that contains a SUM expression */ if (item->type() != SUM_FUNC_ITEM && (item->with_sum_func || (item->used_tables() & PSEUDO_TABLE_BITS))) item->split_sum_func(thd, ref_pointer_array, fields); else if (item->used_tables() || item->type() == SUM_FUNC_ITEM) else if (item->type() == SUM_FUNC_ITEM || (item->used_tables() && item->type() != REF_ITEM)) { /* Replace item with a reference so that we can easily calculate it (in case of sum functions) or copy it (in case of fields) The test above is to ensure we don't do a reference for things that are constants or are not yet calculated as in: SELECT RAND() as r1, SUM(a) as r2 FROM t1 HAVING r1 > 1 AND r2 > 0 */ Item **ref= li.ref(); uint el= fields.elements; ref_pointer_array[el]= item; Loading
sql/item_func.cc +6 −2 Original line number Diff line number Diff line Loading @@ -351,6 +351,7 @@ bool Item_func::walk (Item_processor processor, byte *argument) return (this->*processor)(argument); } void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields) { Loading @@ -358,9 +359,12 @@ void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array, for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++) { Item *item=* arg; if (item->with_sum_func && item->type() != SUM_FUNC_ITEM) if (item->type() != SUM_FUNC_ITEM && (item->with_sum_func || (item->used_tables() & PSEUDO_TABLE_BITS))) item->split_sum_func(thd, ref_pointer_array, fields); else if (item->used_tables() || item->type() == SUM_FUNC_ITEM) else if (item->type() == SUM_FUNC_ITEM || (item->used_tables() && item->type() != REF_ITEM)) { uint el= fields.elements; ref_pointer_array[el]= item; Loading