Skip to content

Commit b5cb4ae

Browse files
committed
Fixed bug MDEV-14368 Improper error for a grouping query that
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'.
1 parent 36f8474 commit b5cb4ae

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

mysql-test/r/having.result

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,18 @@ id column_1
697697
1 80a12660d24a72460e5e292fe33f870276d7f40a
698698
expected -- 1 row(s) returned not ER_BAD_FIELD_ERROR
699699
drop table t1;
700+
#
701+
# mdev-14368: grouping query with alias for aggregate function in HAVING
702+
# when sql_mode = 'ONLY_FULL_GROUP_BY'
703+
set @save_sql_mode= @@sql_mode;
704+
set sql_mode = 'ONLY_FULL_GROUP_BY';
705+
create table t1(a int);
706+
insert t1 values (4),(1),(2),(1), (3),(4);
707+
SELECT a, COUNT(a) as ct FROM t1 GROUP BY a HAVING ct>0;
708+
a ct
709+
1 2
710+
2 1
711+
3 1
712+
4 2
713+
set sql_mode=@save_sql_mode;
714+
drop table t1;

mysql-test/t/having.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,20 @@ HAVING UPPER(`column_1`) LIKE '8%';
727727
--echo expected -- 1 row(s) returned not ER_BAD_FIELD_ERROR
728728

729729
drop table t1;
730+
731+
--echo #
732+
--echo # mdev-14368: grouping query with alias for aggregate function in HAVING
733+
--echo # when sql_mode = 'ONLY_FULL_GROUP_BY'
734+
735+
736+
set @save_sql_mode= @@sql_mode;
737+
set sql_mode = 'ONLY_FULL_GROUP_BY';
738+
739+
create table t1(a int);
740+
insert t1 values (4),(1),(2),(1), (3),(4);
741+
742+
SELECT a, COUNT(a) as ct FROM t1 GROUP BY a HAVING ct>0;
743+
744+
set sql_mode=@save_sql_mode;
745+
746+
drop table t1;

sql/item.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4771,7 +4771,8 @@ resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
47714771

47724772
if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY &&
47734773
select->having_fix_field &&
4774-
select_ref != not_found_item && !group_by_ref)
4774+
select_ref != not_found_item && !group_by_ref &&
4775+
!ref->alias_name_used)
47754776
{
47764777
/*
47774778
Report the error if fields was found only in the SELECT item list and

0 commit comments

Comments
 (0)