Bug #80682 Inconsistent behavior with GROUP BY alias, w/ different sequence in select list
Submitted: 10 Mar 2016 8:44 Modified: 10 Mar 2016 11:49
Reporter: Su Dylan Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:5.7.8, 5.7.11 OS:Any
Assigned to: CPU Architecture:Any

[10 Mar 2016 8:44] Su Dylan
Description:
Output:
=====
mysql> create table t1(c1 int, c2 int);
insert into t1 values(1,1),(2,0),(Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1,1),(2,0),(1,3);
select c1, c2 fQuery OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select c1, c2 from t1;
+------+------+
| c1   | c2   |
+------+------+
|    1 |    1 |
|    2 |    0 |
|    1 |    3 |
+------+------+
3 rows in set (0.00 sec)

mysql> select 1 as c3, c1 as c3, c2 as c3 from t1 group by c3;
+----+------+------+
| c3 | c3   | c3   |
+----+------+------+
|  1 |    1 |    1 |
+----+------+------+
1 row in set (0.00 sec)

mysql> select c1 as c3, c2 as c3, 1 as c3 from t1 group by c3;
ERROR 1052 (23000): Column 'c3' in group statement is ambiguous
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.8-rc  |
+-----------+
1 row in set (0.00 sec)

Problem:
=====
Since the last two queries differ only in sequence of the same expressions, they are expected to have same result.

How to repeat:
drop table if exists t1;
create table t1(c1 int, c2 int);
insert into t1 values(1,1),(2,0),(1,3);
select c1, c2 from t1;
select 1 as c3, c1 as c3, c2 as c3 from t1 group by c3;
select c1 as c3, c2 as c3, 1 as c3 from t1 group by c3;

Suggested fix:
The last two queries have same result.
[10 Mar 2016 11:49] MySQL Verification Team
Hello Su Dylan,

Thank you for the report.
Imho both statement should have reported error "Column 'c3' in group statement is ambiguous".

Thanks,
Umesh