Skip to content

Commit 7c91e28

Browse files
committed
Bug#18694751 72512: Non-aggregated query with set function in ORDER BY should be rejected
A non-aggregated query with an ORDER BY applied to it cannot contain aggregate functions. This patch prevents such queries. A new error message is added: ER_AGGREGATE_ORDER_NON_AGG_QUERY. Notice that one test case with HAVING and an aggregated argument fails but should not. This is because of a wrong resolve order in SELECT_LEX::prepare().
1 parent fa96a02 commit 7c91e28

22 files changed

+469
-167
lines changed

mysql-test/include/order_by.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ DROP TABLE t1;
17111711
--echo # VALGRIND/CRASH WHEN ORDERING BY MULTIPLE AGGREGATE FUNCTIONS
17121712
--echo #
17131713

1714-
select 1 order by max(1) + min(1);
1714+
select count(*) order by max(1) + min(1);
17151715

17161716
--echo End of 5.1 tests
17171717

mysql-test/include/subquery.inc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,14 +3939,14 @@ CREATE TABLE t3 LIKE t1;
39393939

39403940
--echo # should have 1 impossible where and 2 dependent subqueries
39413941
EXPLAIN
3942-
SELECT 1 FROM t1
3942+
SELECT COUNT(*) FROM t1
39433943
WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
3944-
ORDER BY count(*);
3944+
ORDER BY COUNT(*);
39453945

39463946
--echo # should not crash the next statement
3947-
SELECT 1 FROM t1
3947+
SELECT COUNT(*) FROM t1
39483948
WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
3949-
ORDER BY count(*);
3949+
ORDER BY COUNT(*);
39503950

39513951
--echo # should not crash: the crash is caused by the previous statement
39523952
SELECT 1;
@@ -4658,11 +4658,13 @@ DROP TABLE t1, t2;
46584658
#
46594659
CREATE TABLE t1 (a ENUM('rainbow'));
46604660
INSERT INTO t1 VALUES (),(),(),(),();
4661-
SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
4661+
SELECT 1
4662+
FROM t1 GROUP BY (SELECT COUNT(*) FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
46624663
DROP TABLE t1;
46634664
CREATE TABLE t1 (a LONGBLOB);
46644665
INSERT INTO t1 SET a = 'aaaa';
46654666
INSERT INTO t1 SET a = 'aaaa';
4667+
--error ER_AGGREGATE_ORDER_NON_AGG_QUERY
46664668
SELECT 1 FROM t1 GROUP BY
46674669
(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
46684670
DROP TABLE t1;

mysql-test/r/func_group.result

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,11 +1481,9 @@ CREATE TABLE t1 (a INT);
14811481
INSERT INTO t1 values (),(),();
14821482
SELECT (SELECT SLEEP(0) FROM t1 ORDER BY AVG(DISTINCT a) ) as x FROM t1
14831483
GROUP BY x;
1484-
x
1485-
0
1484+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
14861485
SELECT 1 FROM t1 GROUP BY (SELECT SLEEP(0) FROM t1 ORDER BY AVG(DISTINCT a) );
1487-
1
1488-
1
1486+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
14891487
DROP TABLE t1;
14901488
CREATE TABLE t1 (a int, b date NOT NULL, KEY k1 (a,b));
14911489
SELECT MIN(b) FROM t1 WHERE a=1 AND b>'2007-08-01';
@@ -1879,8 +1877,7 @@ End of 5.1 tests
18791877
CREATE TABLE t1(a int, KEY(a));
18801878
INSERT INTO t1 VALUES (1), (2);
18811879
SELECT 1 FROM t1 ORDER BY AVG(DISTINCT a);
1882-
1
1883-
1
1880+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
18841881
DROP TABLE t1;
18851882
#
18861883
# Bug#55648: Server crash on MIN/MAX on maximum time value

mysql-test/r/group_by.result

Lines changed: 175 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,32 +1172,33 @@ SET @@sql_mode='ONLY_FULL_GROUP_BY';
11721172
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
11731173
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
11741174
CREATE TABLE t2 SELECT * FROM t1;
1175-
SELECT 1 FROM t1 ORDER BY COUNT(*);
1176-
1
1177-
1
1178-
SELECT 1 FROM t1 ORDER BY COUNT(*) + 1;
1179-
1
1180-
1
1181-
SELECT 1 FROM t1 ORDER BY COUNT(*) + a;
1182-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1183-
SELECT 1 FROM t1 ORDER BY COUNT(*), 1;
1184-
1
1185-
1
1186-
SELECT 1 FROM t1 ORDER BY COUNT(*), a;
1175+
SELECT COUNT(*) FROM t1 ORDER BY COUNT(*);
1176+
COUNT(*)
1177+
6
1178+
SELECT COUNT(*) FROM t1 ORDER BY COUNT(*) + 1;
1179+
COUNT(*)
1180+
6
1181+
SELECT COUNT(*) FROM t1 ORDER BY COUNT(*) + a;
11871182
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1188-
SELECT 1 FROM t1 ORDER BY SUM(a);
1189-
1
1190-
1
1191-
SELECT 1 FROM t1 ORDER BY SUM(a + 1);
1192-
1
1193-
1
1194-
SELECT 1 FROM t1 ORDER BY SUM(a) + 1;
1195-
1
1196-
1
1197-
SELECT 1 FROM t1 ORDER BY SUM(a), b;
1183+
SELECT COUNT(*) FROM t1 ORDER BY COUNT(*), 1;
1184+
COUNT(*)
1185+
6
1186+
SELECT COUNT(*) FROM t1 ORDER BY COUNT(*), a;
11981187
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1199-
SELECT a FROM t1 ORDER BY COUNT(b);
1188+
SELECT COUNT(*) FROM t1 ORDER BY SUM(a);
1189+
COUNT(*)
1190+
6
1191+
SELECT COUNT(*) FROM t1 ORDER BY SUM(a + 1);
1192+
COUNT(*)
1193+
6
1194+
SELECT COUNT(*) FROM t1 ORDER BY SUM(a) + 1;
1195+
COUNT(*)
1196+
6
1197+
SELECT COUNT(*) FROM t1 ORDER BY SUM(a), b;
12001198
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1199+
SELECT SUM(a) FROM t1 ORDER BY COUNT(b);
1200+
SUM(a)
1201+
17
12011202
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2);
12021203
a
12031204
3
@@ -1211,13 +1212,13 @@ ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP col
12111212
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
12121213
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
12131214
SELECT t1.a FROM t1 ORDER BY (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
1214-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1215+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
12151216
SELECT t1.a FROM t1
12161217
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
1217-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1218+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
12181219
SELECT t1.a FROM t1 GROUP BY t1.a
12191220
HAVING t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1220-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1221+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
12211222
SELECT t1.a FROM t1 GROUP BY t1.a
12221223
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
12231224
a
@@ -1226,13 +1227,13 @@ a
12261227
4
12271228
SELECT t1.a FROM t1 GROUP BY t1.a
12281229
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
1229-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1230+
ERROR HY000: Expression #2 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
12301231
SELECT t1.a FROM t1 GROUP BY t1.a
12311232
HAVING t1.a > ANY (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
1232-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1233+
ERROR HY000: Expression #2 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
12331234
SELECT t1.a FROM t1
12341235
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
1235-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1236+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
12361237
SELECT 1 FROM t1 GROUP BY t1.a
12371238
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
12381239
1
@@ -1299,7 +1300,7 @@ select avg (
12991300
(select sum(outr.a + innr.a) from t1 as innr limit 1)) as tt
13001301
from t1 as outr order by count(outr.a) limit 1)) as tt
13011302
from t1 as most_outer;
1302-
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1303+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
13031304
select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;
13041305
tt
13051306
29
@@ -2849,3 +2850,147 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
28492850
Warnings:
28502851
Note 1003 /* select#1 */ select `test`.`t1`.`kp1` AS `kp1`,sum(`test`.`t1`.`kp2`) AS `SUM(kp2)` from `test`.`t1` group by `test`.`t1`.`kp1`
28512852
DROP TABLE t1;
2853+
# Bug#72512/18694751: Non-aggregated query with set function in
2854+
# ORDER BY should be rejected
2855+
CREATE TABLE t1(a INTEGER);
2856+
INSERT INTO t1 VALUES (1), (2);
2857+
# Non-aggregated queries
2858+
SELECT a FROM t1 ORDER BY COUNT(*);
2859+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2860+
SELECT a FROM t1 WHERE a > 0 ORDER BY COUNT(*);
2861+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2862+
# Implicitly grouped query
2863+
SELECT SUM(a) FROM t1 ORDER BY COUNT(*);
2864+
SUM(a)
2865+
3
2866+
SELECT COUNT(*) FROM t1 ORDER BY COUNT(*);
2867+
COUNT(*)
2868+
2
2869+
SELECT COUNT(*) AS c FROM t1 ORDER BY COUNT(*);
2870+
c
2871+
2
2872+
SELECT COUNT(*) AS c FROM t1 ORDER BY c;
2873+
c
2874+
2
2875+
# Explicitly grouped query
2876+
SELECT a, COUNT(*) FROM t1 GROUP BY a ORDER BY COUNT(*);
2877+
a COUNT(*)
2878+
1 1
2879+
2 1
2880+
SELECT a, COUNT(*) AS c FROM t1 GROUP BY a ORDER BY COUNT(*);
2881+
a c
2882+
1 1
2883+
2 1
2884+
SELECT a, COUNT(*) AS c FROM t1 GROUP BY a ORDER BY c;
2885+
a c
2886+
1 1
2887+
2 1
2888+
SELECT a AS c FROM t1 GROUP BY a ORDER BY COUNT(*);
2889+
c
2890+
1
2891+
2
2892+
# Query with HAVING,
2893+
# Should succeed, but GROUP - HAVING - ORDER are resolved in wrong order
2894+
SELECT 1 FROM t1 HAVING COUNT(*) > 1 ORDER BY COUNT(*);
2895+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2896+
# Subquery, ORDER BY contains outer reference
2897+
SELECT (SELECT 1 AS foo ORDER BY a) AS x
2898+
FROM t1;
2899+
x
2900+
1
2901+
1
2902+
SELECT (SELECT 1 AS foo ORDER BY t1.a) AS x
2903+
FROM t1;
2904+
x
2905+
1
2906+
1
2907+
# Subquery, ORDER BY contains set function with outer reference
2908+
SELECT (SELECT 1 AS foo ORDER BY COUNT(a)) AS x
2909+
FROM t1;
2910+
x
2911+
1
2912+
SELECT (SELECT 1 AS foo ORDER BY COUNT(t1.a)) AS x
2913+
FROM t1;
2914+
x
2915+
1
2916+
# Subquery, ORDER BY contains set function with local reference
2917+
SELECT (SELECT 1 AS foo ORDER BY COUNT(*)) AS x
2918+
FROM t1;
2919+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2920+
# Subquery in ORDER BY with outer reference
2921+
SELECT a FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
2922+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2923+
SELECT SUM(a) FROM t1 ORDER BY (SELECT COUNT(t1.a) FROM t1 AS t2);
2924+
SUM(a)
2925+
3
2926+
# Query with ORDER BY inside UNION
2927+
# (Notice that ORDER BY is ignored for UNION component queries)
2928+
(SELECT a FROM t1 ORDER BY COUNT(*))
2929+
UNION
2930+
SELECT a FROM t1;
2931+
a
2932+
1
2933+
2
2934+
(SELECT a FROM t1 ORDER BY COUNT(*))
2935+
UNION ALL
2936+
SELECT a FROM t1;
2937+
a
2938+
1
2939+
2
2940+
1
2941+
2
2942+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1)
2943+
UNION
2944+
SELECT a FROM t1;
2945+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2946+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1)
2947+
UNION ALL
2948+
SELECT a FROM t1;
2949+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2950+
SELECT a FROM t1
2951+
UNION
2952+
(SELECT a FROM t1 ORDER BY COUNT(*));
2953+
a
2954+
1
2955+
2
2956+
SELECT a FROM t1
2957+
UNION ALL
2958+
(SELECT a FROM t1 ORDER BY COUNT(*));
2959+
a
2960+
1
2961+
2
2962+
1
2963+
2
2964+
SELECT a FROM t1
2965+
UNION
2966+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1 OFFSET 1);
2967+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2968+
SELECT a FROM t1
2969+
UNION ALL
2970+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1 OFFSET 1);
2971+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2972+
(SELECT a FROM t1 ORDER BY COUNT(*))
2973+
UNION
2974+
(SELECT a FROM t1 ORDER BY COUNT(*));
2975+
a
2976+
1
2977+
2
2978+
(SELECT a FROM t1 ORDER BY COUNT(*))
2979+
UNION ALL
2980+
(SELECT a FROM t1 ORDER BY COUNT(*));
2981+
a
2982+
1
2983+
2
2984+
1
2985+
2
2986+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1)
2987+
UNION
2988+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1 OFFSET 1);
2989+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2990+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1)
2991+
UNION ALL
2992+
(SELECT a FROM t1 ORDER BY COUNT(*) LIMIT 1 OFFSET 1);
2993+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2994+
(SELECT COUNT(*) FROM t1 ORDER BY a) ORDER BY COUNT(*);
2995+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
2996+
DROP TABLE t1;

mysql-test/r/order_by_all.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,8 +2656,8 @@ DROP TABLE t1;
26562656
# Bug#11765255 58201:
26572657
# VALGRIND/CRASH WHEN ORDERING BY MULTIPLE AGGREGATE FUNCTIONS
26582658
#
2659-
select 1 order by max(1) + min(1);
2660-
1
2659+
select count(*) order by max(1) + min(1);
2660+
count(*)
26612661
1
26622662
End of 5.1 tests
26632663
#

mysql-test/r/order_by_icp_mrr.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,8 +2656,8 @@ DROP TABLE t1;
26562656
# Bug#11765255 58201:
26572657
# VALGRIND/CRASH WHEN ORDERING BY MULTIPLE AGGREGATE FUNCTIONS
26582658
#
2659-
select 1 order by max(1) + min(1);
2660-
1
2659+
select count(*) order by max(1) + min(1);
2660+
count(*)
26612661
1
26622662
End of 5.1 tests
26632663
#

mysql-test/r/order_by_none.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,8 +2655,8 @@ DROP TABLE t1;
26552655
# Bug#11765255 58201:
26562656
# VALGRIND/CRASH WHEN ORDERING BY MULTIPLE AGGREGATE FUNCTIONS
26572657
#
2658-
select 1 order by max(1) + min(1);
2659-
1
2658+
select count(*) order by max(1) + min(1);
2659+
count(*)
26602660
1
26612661
End of 5.1 tests
26622662
#

mysql-test/r/query_cache.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,8 @@ End of 5.0 tests
16901690
SET GLOBAL query_cache_size=1024*1024*512;
16911691
CREATE TABLE t1 (a ENUM('rainbow'));
16921692
INSERT INTO t1 VALUES (),(),(),(),();
1693-
SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
1693+
SELECT 1 FROM t1
1694+
GROUP BY (SELECT COUNT(*) FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
16941695
1
16951696
1
16961697
DROP TABLE t1;
@@ -1699,8 +1700,7 @@ INSERT INTO t1 SET a = 'aaaa';
16991700
INSERT INTO t1 SET a = 'aaaa';
17001701
SELECT 1 FROM t1 GROUP BY
17011702
(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
1702-
1
1703-
1
1703+
ERROR HY000: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query
17041704
DROP TABLE t1;
17051705
SET GLOBAL query_cache_size= default;
17061706
CREATE TABLE t1( a INT );

0 commit comments

Comments
 (0)