Skip to content

Commit

Permalink
Fixed bug MDEV-14368 Improper error for a grouping query that
Browse files Browse the repository at this point in the history
uses alias in HAVING when sql_mode = 'ONLY_FULL_GROUP_BY'

This patch corrects the patch for bug#18739: non-standard
HAVING extension was allowed in strict ANSI sql mode
added in 2006 by commit 4b7c4cd.
As a result of incompleteness of the fix in the above commit
if a query with GROUP BY contained an aggregate function with an
alias and this alias was used in the HAVING clause of the query
the server reported an error when sql_mode was set to
'ONLY_FULL_GROUP_BY'.
  • Loading branch information
igorbabaev committed Nov 11, 2017
1 parent 36f8474 commit b5cb4ae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions mysql-test/r/having.result
Expand Up @@ -697,3 +697,18 @@ id column_1
1 80a12660d24a72460e5e292fe33f870276d7f40a
expected -- 1 row(s) returned not ER_BAD_FIELD_ERROR
drop table t1;
#
# mdev-14368: grouping query with alias for aggregate function in HAVING
# when sql_mode = 'ONLY_FULL_GROUP_BY'
set @save_sql_mode= @@sql_mode;
set sql_mode = 'ONLY_FULL_GROUP_BY';
create table t1(a int);
insert t1 values (4),(1),(2),(1), (3),(4);
SELECT a, COUNT(a) as ct FROM t1 GROUP BY a HAVING ct>0;
a ct
1 2
2 1
3 1
4 2
set sql_mode=@save_sql_mode;
drop table t1;
17 changes: 17 additions & 0 deletions mysql-test/t/having.test
Expand Up @@ -727,3 +727,20 @@ HAVING UPPER(`column_1`) LIKE '8%';
--echo expected -- 1 row(s) returned not ER_BAD_FIELD_ERROR

drop table t1;

--echo #
--echo # mdev-14368: grouping query with alias for aggregate function in HAVING
--echo # when sql_mode = 'ONLY_FULL_GROUP_BY'


set @save_sql_mode= @@sql_mode;
set sql_mode = 'ONLY_FULL_GROUP_BY';

create table t1(a int);
insert t1 values (4),(1),(2),(1), (3),(4);

SELECT a, COUNT(a) as ct FROM t1 GROUP BY a HAVING ct>0;

set sql_mode=@save_sql_mode;

drop table t1;
3 changes: 2 additions & 1 deletion sql/item.cc
Expand Up @@ -4771,7 +4771,8 @@ resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)

if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY &&
select->having_fix_field &&
select_ref != not_found_item && !group_by_ref)
select_ref != not_found_item && !group_by_ref &&
!ref->alias_name_used)
{
/*
Report the error if fields was found only in the SELECT item list and
Expand Down

0 comments on commit b5cb4ae

Please sign in to comment.