Commit 21c6145a authored by evgen@moonbone.local's avatar evgen@moonbone.local
Browse files

Bug#27219: Aggregate functions in ORDER BY.

Mixing aggregate functions and non-grouping columns is not allowed in the
ONLY_FULL_GROUP_BY mode. However in some cases the error wasn't thrown because
of insufficient check.

In order to check more thoroughly the new algorithm employs a list of outer
fields used in a sum function and a SELECT_LEX::full_group_by_flag.
Each non-outer field checked to find out whether it's aggregated or not and
the current select is marked accordingly.
All outer fields that are used under an aggregate function are added to the
Item_sum::outer_fields list and later checked by the Item_sum::check_sum_func
function.
parent 3a87bbfe
Loading
Loading
Loading
Loading
+146 −0
Original line number Diff line number Diff line
@@ -1213,4 +1213,150 @@ FROM t1;
ERROR 21000: Subquery returns more than 1 row
DROP TABLE t1;
SET @@sql_mode = @old_sql_mode;
#
# Bug#27219: Aggregate functions in ORDER BY.  
#
SET @save_sql_mode=@@sql_mode;
SET @@sql_mode='ONLY_FULL_GROUP_BY';
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
CREATE TABLE t2 SELECT * FROM t1;
SELECT 1 FROM t1 ORDER BY COUNT(*);
1
1
SELECT 1 FROM t1 ORDER BY COUNT(*) + 1;
1
1
SELECT 1 FROM t1 ORDER BY COUNT(*) + a;
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT 1 FROM t1 ORDER BY COUNT(*), 1;
1
1
SELECT 1 FROM t1 ORDER BY COUNT(*), a;
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT 1 FROM t1 ORDER BY SUM(a);
1
1
SELECT 1 FROM t1 ORDER BY SUM(a + 1);
1
1
SELECT 1 FROM t1 ORDER BY SUM(a) + 1;
1
1
SELECT 1 FROM t1 ORDER BY SUM(a), b;
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT a FROM t1 ORDER BY COUNT(b);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2);
a
3
2
3
2
3
4
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a), t2.a FROM t2);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 ORDER BY (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
a
2
3
4
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ANY (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1
1
1
1
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1
1
1
1
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1
1
1
1
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY t2.a LIMIT 1);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT 1 FROM t1 GROUP BY t1.a
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT t1.a FROM t1 
WHERE t1.a = (SELECT t2.a FROM t2 GROUP BY t2.a
ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1);
a
4
SELECT t1.a, SUM(t1.b) FROM t1 
WHERE t1.a = (SELECT SUM(t2.b) FROM t2 GROUP BY t2.a
ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1)
GROUP BY t1.a;
a	SUM(t1.b)
4	4
SELECT t1.a, SUM(t1.b) FROM t1 
WHERE t1.a = (SELECT SUM(t2.b) FROM t2
ORDER BY SUM(t2.b) + SUM(t1.b) LIMIT 1)
GROUP BY t1.a;
a	SUM(t1.b)
SELECT t1.a, SUM(t1.b) FROM t1 
WHERE t1.a = (SELECT SUM(t2.b) FROM t2
ORDER BY SUM(t2.b + t1.a) LIMIT 1)
GROUP BY t1.a;
a	SUM(t1.b)
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
a
select avg (
(select
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
from t1 as outr order by outr.a limit 1))
from t1 as most_outer;
avg (
(select
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
from t1 as outr order by outr.a limit 1))
29.0000
select avg (
(select (
(select sum(outr.a + innr.a) from t1 as innr limit 1)) as tt
from t1 as outr order by count(outr.a) limit 1)) as tt
from t1 as most_outer;
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;
tt
29
29
35
35
35
41
SET sql_mode=@save_sql_mode;
DROP TABLE t1, t2;
End of 5.0 tests
+117 −0
Original line number Diff line number Diff line
@@ -893,4 +893,121 @@ FROM t1;
DROP TABLE t1;
SET @@sql_mode = @old_sql_mode;

--echo #
--echo # Bug#27219: Aggregate functions in ORDER BY.  
--echo #
SET @save_sql_mode=@@sql_mode;
SET @@sql_mode='ONLY_FULL_GROUP_BY';

CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
CREATE TABLE t2 SELECT * FROM t1;

SELECT 1 FROM t1 ORDER BY COUNT(*);
SELECT 1 FROM t1 ORDER BY COUNT(*) + 1;
--error 1140
SELECT 1 FROM t1 ORDER BY COUNT(*) + a;
SELECT 1 FROM t1 ORDER BY COUNT(*), 1;
--error 1140
SELECT 1 FROM t1 ORDER BY COUNT(*), a;

SELECT 1 FROM t1 ORDER BY SUM(a);
SELECT 1 FROM t1 ORDER BY SUM(a + 1);
SELECT 1 FROM t1 ORDER BY SUM(a) + 1;
--error 1140
SELECT 1 FROM t1 ORDER BY SUM(a), b;

--error 1140
SELECT a FROM t1 ORDER BY COUNT(b);

SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2);

--error 1140
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a), t2.a FROM t2);
--error 1140
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
--error 1140
SELECT t1.a FROM t1 ORDER BY (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);

--error 1140
SELECT t1.a FROM t1
  WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
--error 1140
SELECT t1.a FROM t1 GROUP BY t1.a
  HAVING t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.a) LIMIT 1);

SELECT t1.a FROM t1 GROUP BY t1.a
  HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
--error 1140
SELECT t1.a FROM t1 GROUP BY t1.a
  HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
--error 1140
SELECT t1.a FROM t1 GROUP BY t1.a
  HAVING t1.a > ANY (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));

--error 1140
SELECT t1.a FROM t1
  WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));

SELECT 1 FROM t1 GROUP BY t1.a
  HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
SELECT 1 FROM t1 GROUP BY t1.a
  HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
SELECT 1 FROM t1 GROUP BY t1.a
  HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);

--error 1140
SELECT 1 FROM t1 GROUP BY t1.a
  HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY t2.a LIMIT 1);
--error 1140
SELECT 1 FROM t1 GROUP BY t1.a
  HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
--error 1140
SELECT 1 FROM t1 GROUP BY t1.a
  HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);

# Both SUMs are aggregated in the subquery, no mixture:
SELECT t1.a FROM t1 
  WHERE t1.a = (SELECT t2.a FROM t2 GROUP BY t2.a
                  ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1);

# SUM(t1.b) is aggregated in the subquery, no mixture:
SELECT t1.a, SUM(t1.b) FROM t1 
  WHERE t1.a = (SELECT SUM(t2.b) FROM t2 GROUP BY t2.a
                  ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1)
  GROUP BY t1.a;

# 2nd SUM(t1.b) is aggregated in the subquery, no mixture:
SELECT t1.a, SUM(t1.b) FROM t1 
  WHERE t1.a = (SELECT SUM(t2.b) FROM t2
                  ORDER BY SUM(t2.b) + SUM(t1.b) LIMIT 1)
  GROUP BY t1.a;

# SUM(t2.b + t1.a) is aggregated in the subquery, no mixture:
SELECT t1.a, SUM(t1.b) FROM t1 
  WHERE t1.a = (SELECT SUM(t2.b) FROM t2
                  ORDER BY SUM(t2.b + t1.a) LIMIT 1)
  GROUP BY t1.a;

SELECT t1.a FROM t1 GROUP BY t1.a
    HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);

select avg (
  (select
    (select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
   from t1 as outr order by outr.a limit 1))
from t1 as most_outer;

--error 1140
select avg (
  (select (
    (select sum(outr.a + innr.a) from t1 as innr limit 1)) as tt
   from t1 as outr order by count(outr.a) limit 1)) as tt
from t1 as most_outer;

select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;

SET sql_mode=@save_sql_mode;
DROP TABLE t1, t2;

--echo End of 5.0 tests
+24 −4
Original line number Diff line number Diff line
@@ -3925,9 +3925,9 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
      }
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
        goto error;
      else if (!ret)
        return FALSE;
      outer_fixed= TRUE;
      if (!ret)
        goto mark_non_agg_field;
    }
    else if (!from_field)
      goto error;
@@ -3939,9 +3939,9 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
      int ret;
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
        goto error;
      else if (!ret)
        return FALSE;
      outer_fixed= 1;
      if (!ret)
        goto mark_non_agg_field;
    }

    /*
@@ -4007,6 +4007,26 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
    thd->lex->current_select->non_agg_fields.push_back(this);
    marker= thd->lex->current_select->cur_pos_in_select_list;
  }
mark_non_agg_field:
  if (fixed && thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY)
  {
    /*
      Mark selects according to presence of non aggregated fields.
      Fields from outer selects added to the aggregate function
      outer_fields list as its unknown at the moment whether it's
      aggregated or not.
    */
    if (!thd->lex->in_sum_func)
      cached_table->select_lex->full_group_by_flag|= NON_AGG_FIELD_USED;
    else
    {
      if (outer_fixed)
        thd->lex->in_sum_func->outer_fields.push_back(this);
      else if (thd->lex->in_sum_func->nest_level !=
          thd->lex->current_select->nest_level)
        cached_table->select_lex->full_group_by_flag|= NON_AGG_FIELD_USED;
    }
  }
  return FALSE;

error:
+14 −0
Original line number Diff line number Diff line
@@ -1434,6 +1434,19 @@ Item_in_subselect::select_in_like_transformer(JOIN *join, Comp_creator *func)

  DBUG_ENTER("Item_in_subselect::select_in_like_transformer");

  {
    /*
      IN/SOME/ALL/ANY subqueries aren't support LIMIT clause. Without it
      ORDER BY clause becomes meaningless thus we drop it here.
    */
    SELECT_LEX *sl= current->master_unit()->first_select();
    for (; sl; sl= sl->next_select())
    {
      if (sl->join)
        sl->join->order= 0;
    }
  }

  if (changed)
  {
    DBUG_RETURN(RES_OK);
@@ -1468,6 +1481,7 @@ Item_in_subselect::select_in_like_transformer(JOIN *join, Comp_creator *func)

  transformed= 1;
  arena= thd->activate_stmt_arena_if_needed(&backup);

  /*
    Both transformers call fix_fields() only for Items created inside them,
    and all that items do not make permanent changes in current item arena
+64 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ bool Item_sum::init_sum_func_check(THD *thd)
  aggr_sel= NULL;
  max_arg_level= -1;
  max_sum_func_level= -1;
  outer_fields.empty();
  return FALSE;
}

@@ -175,6 +176,7 @@ bool Item_sum::check_sum_func(THD *thd, Item **ref)
               MYF(0));
    return TRUE;
  }

  if (in_sum_func)
  {
    /*
@@ -195,6 +197,68 @@ bool Item_sum::check_sum_func(THD *thd, Item **ref)
      set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
    set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
  }

  /*
    Check that non-aggregated fields and sum functions aren't mixed in the
    same select in the ONLY_FULL_GROUP_BY mode.
  */
  if (outer_fields.elements)
  {
    Item_field *field;
    /*
      Here we compare the nesting level of the select to which an outer field
      belongs to with the aggregation level of the sum function. All fields in
      the outer_fields list are checked.

      If the nesting level is equal to the aggregation level then the field is
        aggregated by this sum function.
      If the nesting level is less than the aggregation level then the field
        belongs to an outer select. In this case if there is an embedding sum
        function add current field to functions outer_fields list. If there is
        no embedding function then the current field treated as non aggregated
        and the select it belongs to is marked accordingly.
      If the nesting level is greater than the aggregation level then it means
        that this field was added by an inner sum function.
        Consider an example:

          select avg ( <-- we are here, checking outer.f1
            select (
              select sum(outer.f1 + inner.f1) from inner
            ) from outer)
          from most_outer;

        In this case we check that no aggregate functions are used in the
        select the field belongs to. If there are some then an error is
        raised.
    */
    List_iterator<Item_field> of(outer_fields);
    while ((field= of++))
    {
      SELECT_LEX *sel= field->cached_table->select_lex;
      if (sel->nest_level < aggr_level)
      {
        if (in_sum_func)
        {
          /*
            Let upper function decide whether this field is a non
            aggregated one.
          */
          in_sum_func->outer_fields.push_back(field);
        }
        else
          sel->full_group_by_flag|= NON_AGG_FIELD_USED;
      }
      if (sel->nest_level > aggr_level &&
          (sel->full_group_by_flag & SUM_FUNC_USED) &&
          !sel->group_list.elements)
      {
        my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
                   ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
        return TRUE;
      }
    }
  }
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
  update_used_tables();
  thd->lex->in_sum_func= in_sum_func;
  return FALSE;
Loading