Commit 2a00a073 authored by unknown's avatar unknown
Browse files

Bug #21540: Subqueries with no from and aggregate functions return

            wrong results
 Mark the containing Item(s) (Item_subselect descendant usually) of 
 a subselect as containing aggregate functions if it has references
 to aggregates functions that are calculated outside its context.
 This tels end_send_group() not to make an Item_subselect descendant in
 select list a copy and causes the correct value being returned.


mysql-test/r/func_group.result:
  Bug #21540: Subqueries with no from and aggregate functions return
              wrong results
   - test cases
mysql-test/r/subselect.result:
  Bug #21540: Subqueries with no from and aggregate functions return
              wrong results
   - fixed the result of an existing testcase.
mysql-test/t/subselect.test:
  Bug #21540: Subqueries with no from and aggregate functions return
              wrong results
   - test cases
sql/item_sum.cc:
  Bug #21540: Subqueries with no from and aggregate functions return
              wrong results
   Mark the containing Item (Item_subselect descendant usually) of 
   a subselect as containing aggregate functions if it has references
   to aggregates functions that are calculated outside its context.
   This tels end_send_group() not to make an Item_subselect descendant in
   select list a copy and causes the correct value being returned.
parent c17b36d9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -997,7 +997,7 @@ SELECT SQL_NO_CACHE
WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
FROM t1 t, t2 c WHERE t.a = c.b;
minid
NULL
1
DROP TABLE t1,t2;
create table t1 select variance(0);
show create table t1;
+61 −0
Original line number Diff line number Diff line
@@ -3368,3 +3368,64 @@ ORDER BY t1.t DESC LIMIT 1);
i1	i2	t	i1	i2	t
24	1	2005-05-27 12:40:30	24	1	2006-06-20 12:29:40
DROP TABLE t1, t2;
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (a INT);
INSERT INTO t2 values (1);
INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
(SELECT COUNT(DISTINCT t1.b) from t2)
2
1
1
SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
FROM t1 GROUP BY t1.a;
(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
2
1
1
SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b))
2	2
1	1
1	1
SELECT COUNT(DISTINCT t1.b), 
(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
FROM t1 GROUP BY t1.a;
COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
2	2
1	1
1	1
SELECT (
SELECT (
SELECT COUNT(DISTINCT t1.b)
)
) 
FROM t1 GROUP BY t1.a;
(
SELECT (
SELECT COUNT(DISTINCT t1.b)
)
)
2
1
1
SELECT (
SELECT (
SELECT (
SELECT COUNT(DISTINCT t1.b)
)
) 
FROM t1 GROUP BY t1.a LIMIT 1) 
FROM t1 t2
GROUP BY t2.a;
(
SELECT (
SELECT (
SELECT COUNT(DISTINCT t1.b)
)
) 
FROM t1 GROUP BY t1.a LIMIT 1)
2
2
2
DROP TABLE t1,t2;
+31 −0
Original line number Diff line number Diff line
@@ -2280,3 +2280,34 @@ SELECT * FROM t1,t2
                    ORDER BY t1.t DESC LIMIT 1);

DROP TABLE t1, t2;

#
# Bug #21540: Subqueries with no from and aggregate functions return 
#              wrong results
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (a INT);
INSERT INTO t2 values (1);
INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  FROM t1 GROUP BY t1.a;
SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
SELECT COUNT(DISTINCT t1.b), 
       (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  FROM t1 GROUP BY t1.a;
SELECT (
    SELECT (
      SELECT COUNT(DISTINCT t1.b)
    )
) 
FROM t1 GROUP BY t1.a;
SELECT (
  SELECT (
      SELECT (
        SELECT COUNT(DISTINCT t1.b)
      )
  ) 
  FROM t1 GROUP BY t1.a LIMIT 1) 
FROM t1 t2
GROUP BY t2.a;
DROP TABLE t1,t2;  
+21 −1
Original line number Diff line number Diff line
@@ -246,7 +246,27 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref)
      aggr_sl->inner_sum_func_list->next= this;
    }
    aggr_sl->inner_sum_func_list= this;
    aggr_sl->with_sum_func= 1;

    /* 
      Mark Item_subselect(s) as containing aggregate function all the way up
      to aggregate function's calculation context.
      Note that we must not mark the Item of calculation context itself
      because with_sum_func on the calculation context st_select_lex is
      already set above.

      with_sum_func being set for an Item means that this Item refers 
      (somewhere in it, e.g. one of its arguments if it's a function) directly
      or through intermediate items to an aggregate function that is calculated
      in a context "outside" of the Item (e.g. in the current or outer select).

      with_sum_func being set for an st_select_lex means that this st_select_lex
      has aggregate functions directly referenced (i.e. not through a sub-select).
    */
    for (sl= thd->lex->current_select; 
         sl && sl != aggr_sl && sl->master_unit()->item;
         sl= sl->master_unit()->outer_select() )
      sl->master_unit()->item->with_sum_func= 1;
  }
  return FALSE;
}