@@ -1172,32 +1172,33 @@ SET @@sql_mode='ONLY_FULL_GROUP_BY';
1172
1172
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
1173
1173
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
1174
1174
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;
1187
1182
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;
1198
1187
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;
1200
1198
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
1201
1202
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2);
1202
1203
a
1203
1204
3
@@ -1211,13 +1212,13 @@ ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP col
1211
1212
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
1212
1213
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1213
1214
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
1215
1216
SELECT t1.a FROM t1
1216
1217
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
1218
1219
SELECT t1.a FROM t1 GROUP BY t1.a
1219
1220
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
1221
1222
SELECT t1.a FROM t1 GROUP BY t1.a
1222
1223
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
1223
1224
a
@@ -1226,13 +1227,13 @@ a
1226
1227
4
1227
1228
SELECT t1.a FROM t1 GROUP BY t1.a
1228
1229
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
1230
1231
SELECT t1.a FROM t1 GROUP BY t1.a
1231
1232
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
1233
1234
SELECT t1.a FROM t1
1234
1235
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
1236
1237
SELECT 1 FROM t1 GROUP BY t1.a
1237
1238
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1238
1239
1
@@ -1299,7 +1300,7 @@ select avg (
1299
1300
(select sum(outr.a + innr.a) from t1 as innr limit 1)) as tt
1300
1301
from t1 as outr order by count(outr.a) limit 1)) as tt
1301
1302
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
1303
1304
select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;
1304
1305
tt
1305
1306
29
@@ -2849,3 +2850,147 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
2849
2850
Warnings:
2850
2851
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`
2851
2852
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;
0 commit comments