File: group_by_fd_no_prot.result

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (2294 lines) | stat: -rw-r--r-- 96,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
set optimizer_trace_max_mem_size=1048576;
set end_markers_in_json=on;
set optimizer_trace="enabled=on";
SET @old_sql_mode = @@sql_mode;
SET @@sql_mode='ONLY_FULL_GROUP_BY';
#
# Bug#16021396 ONLY_FULL_GROUP_BY REJECTS VALID QUERY USING VIEW
#
create table t1(a int, b int, c int) engine=InnoDB;
create algorithm=merge view v1 as select t1.a*2 as a, t1.b*2 as b, t1.c*2 as c from t1;
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=MERGE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`a` * 2) AS `a`,(`t1`.`b` * 2) AS `b`,(`t1`.`c` * 2) AS `c` from `t1`	utf8mb4	utf8mb4_0900_ai_ci
select sin(b) as z from t1 group by sin(b);
z
select sin(b) as z from v1 group by sin(b);
z
select sin(b) as z from t1 group by b;
z
select sin(b) as z from v1 group by b;
z
select sin(b) as z from v1 group by z;
z
drop view v1;
create algorithm=temptable view v1 as select t1.a*2 as a, t1.b*2 as b, t1.c*2 as c from t1;
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`a` * 2) AS `a`,(`t1`.`b` * 2) AS `b`,(`t1`.`c` * 2) AS `c` from `t1`	utf8mb4	utf8mb4_0900_ai_ci
select sin(b) as z from t1 group by sin(b);
z
select sin(b) as z from v1 group by sin(b);
z
select sin(b) as z from t1 group by b;
z
select sin(b) as z from v1 group by b;
z
select sin(b) as z from v1 group by z;
z
drop view v1;
drop table t1;
# From testcase of Bug#16903135:
CREATE TABLE group_by_test2 (
id int unsigned primary key,
cat int unsigned not null,
name varchar(10),
num int unsigned
);
INSERT INTO group_by_test2 (id,cat,name,num) VALUES
(1,10,'foo',2),
(2,11,'foo',1),
(3,22,'bar',3),
(4,23,'bar',7),
(5,34,'test',7);
SELECT
cat,
name,
SUM(num)
FROM
group_by_test2
GROUP BY
cat;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.group_by_test2.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
ALTER TABLE group_by_test2 ADD UNIQUE INDEX (cat);
SELECT
cat,
name,
SUM(num)
FROM
group_by_test2
GROUP BY
cat;
cat	name	SUM(num)
10	foo	2
11	foo	1
22	bar	3
23	bar	7
34	test	7
SELECT
cat,
name,
SUM(num)
FROM
group_by_test2
GROUP BY
cat WITH ROLLUP;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.group_by_test2.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# Expressions of group columns are ok:
SELECT
cat,
length(cat),
SUM(num)
FROM
group_by_test2
GROUP BY
cat
WITH ROLLUP;
cat	length(cat)	SUM(num)
10	2	2
11	2	1
22	2	3
23	2	7
34	2	7
NULL	NULL	20
DROP TABLE group_by_test2;
# Test from Bug #18993257 SELECT AGGR + NON-AGGR FROM JOIN WITH VIEW IS NOT REJECTED BY ONLY_FULL_GROUP_BY
CREATE TABLE t1 (
col_int_key INT,
col_varchar_key VARCHAR(1)
);
CREATE TABLE t2 (
pk INTEGER,
col_int_key INTEGER
);
CREATE VIEW view_b AS SELECT * FROM t2;
SELECT MIN( alias2.col_int_key ),
alias2.col_int_key
FROM t1 AS alias1, t2 AS alias2
WHERE alias1.col_int_key IS NULL;
ERROR 42000: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'test.alias2.col_int_key'; this is incompatible with sql_mode=only_full_group_by
# Same with view:
SELECT MIN( alias2.col_int_key ),
alias2.col_int_key
FROM t1 AS alias1, view_b AS alias2
WHERE alias1.col_int_key IS NULL;
ERROR 42000: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'test.alias2.col_int_key'; this is incompatible with sql_mode=only_full_group_by
DROP TABLE t1, t2;
DROP VIEW view_b;
#
# WL#2489; Recognizing some functional dependencies
#
create table t1(
a int,
b int not null,
c int not null,
d int,
unique key(b,c),
unique key(b,d)
);
select sin(a) as z from t1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from t1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# {b,c} UNIQUE NOT NULL => {c,b}->a
select sin(a) as z from t1 group by c,b;
z
select sin(a+b*c) as z from t1 group by c,b;
z
# In PS mode, we see nothing below, because only_full_group_by
# checks are done at PREPARE, whereas trace below is from EXECUTE.
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.c",
                "test.t1.b"
              ] /* columns */
            } 
# With outer references:
select (select sin(a)) as z from t1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select (select sin(a)) as z from t1 group by c,b;
z
# If key columns are in function, functional dependency disappears
select sin(a) as z from t1 group by c*2,b*2;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# FDs are recognized, like in SQL standard:
# b=2 => c->{b,c} => c->a as (b,c) is unique not null.
select sin(a) as z from t1 where b=2 group by c;
z
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.c",
                "test.t1.b"
              ] /* columns */
            } 
# t2.a=t1.a => {t1.b,t1.c}->t2.a
select sin(t2.a) as z from t1, t1 as t2
where t2.a=t1.a group by t1.b,t1.c;
z
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.b",
                "test.t1.c",
                "test.t2.a"
              ] /* columns */
            } 
# t2.b=t1.b and t2.c=t1.c => {t1.b,t1.c}->{all cols of t2}
select sin(t2.a) as z from t1, t1 as t2
where t2.b=t1.b and t2.c=t1.c group by t1.b,t1.c;
z
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0,
                1
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.b",
                "test.t1.c",
                "test.t2.b",
                "test.t2.c"
              ] /* columns */
            } 
# t2.b=t1.b and t2.c=t1.c => {t1.b,t1.c}->{all cols of t2}
# Moreover, {t1.b,t1.c}->{t1.d}.
# So t3.b=t2.b and t3.c=t1.d => {t1.b,t1.c}->{all cols of t3}.
select t3.a from t1, t1 as t2, t1 as t3
where
t3.b=t2.b and t3.c=t1.d and
t2.b=t1.b and t2.c=t1.c
group by t1.b,t1.c;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0,
                1,
                2
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.b",
                "test.t1.c",
                "test.t3.c",
                "test.t2.b",
                "test.t2.c",
                "test.t3.b"
              ] /* columns */
            } 
# 3 tables:
# {t1.b,t1.c}->{t1.*}->{t2.b,t2.c}->{t2.*}->{t3.pk}->{t3.b}
create table t3(pk int primary key, b int);
select t3.b from  t1,t1 as t2,t3
where t3.pk=t2.d and t2.b=t1.b and t2.c=t1.a
group by t1.b,t1.c;
b
drop table t3;
# With subq
select (select t1.b from t1
where t2.b=t1.b
group by t1.a) from t1 as t2;
(select t1.b from t1
where t2.b=t1.b
group by t1.a)
# Outer join.
create table t2 like t1;
delete from t1;
insert into t1 (a,b) values(1,10),(2,20);
Warnings:
Warning	1364	Field 'c' doesn't have a default value
insert into t2 (a,b) values(1,-10);
Warnings:
Warning	1364	Field 'c' doesn't have a default value
# In result, t2.a is NULL for both rows, values of t1.a are 1 and 2
select t1.a,t2.a from t1 left join t2 on t2.a=t1.a and t2.b=t1.b;
a	a
1	NULL
2	NULL
# So this query would choose one arbitrary value of t1.a - wrong:
select t1.a from t1 left join t2 on t2.a=t1.a and t2.b=t1.b group by t2.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# Also problem for FD with constant:
select t1.a,t2.a from t1 left join t2 on 42=t1.a and t2.b=t1.b;
a	a
1	NULL
2	NULL
select t1.a from t1 left join t2 on 42=t1.a and t2.b=t1.b group by t2.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t1.a from t1 left join t2 on t2.b=t1.a group by t2.b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t1.a from t1 left join t2 on 42=t1.a group by t2.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t1.c from t1 left join t2 on t1.a=t1.c group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t1.b from t1 left join t2 on t1.c=t1.b and t1.a=t1.c group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t1.a,t2.c from t1 left join t2 on t1.a=t2.c
and cos(t2.c+t2.b)>0.5 and sin(t1.a+t2.d)<0.9 group by t1.a;
a	c
1	NULL
2	NULL
# with keys:
select t1.a,t2.d from t1 left join t2 on t1.a=t2.c and t1.d=t2.b
and cos(t2.c+t2.b)>0.5 and sin(t1.a+t2.d)<0.9 group by t1.a,t1.d;
a	d
1	NULL
2	NULL
# with non-determinism:
select t1.a,t2.c from t1 left join t2 on t1.a=t2.c
and cos(t2.c+rand())>0.5 group by t1.a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t2.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t1.a, ANY_VALUE(t2.c) from t1 left join t2 on t1.a=t2.c
and cos(t2.c+rand())>0.5 group by t1.a;
a	ANY_VALUE(t2.c)
1	NULL
2	NULL
# with parameter:
prepare s from 'select t1.a,t2.c from t1 left join t2 on t1.a=t2.c
and cos(t2.c+ ? )>0.5 group by t1.a';
execute s using @dummy;
a	c
1	NULL
2	NULL
# No OR
select t1.a,t2.c from t1 left join t2 on t1.a=t2.c
and cos(t2.c+t2.b)>0.5 OR sin(t2.d)<0.9 group by t1.a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t2.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# With subq
select t2.b from t1 left join t1 as t2 on t1.a=t2.b and t1.b group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t2.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t2.b from t1 left join t1 as t2 on t1.a=t2.b and (select t1.b) group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t2.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# Test ANY_VALUE:
select ANY_VALUE(t1.b) from t1 left join t2 on t1.c=t1.b and t1.a=t1.c group by t1.a;
ANY_VALUE(t1.b)
10
20
select 3+(5*t1.b) from t1 left join t2 on t1.c=t1.b and t1.a=t1.c group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select 3+(5*ANY_VALUE(t1.b)) from t1 left join t2 on t1.c=t1.b and t1.a=t1.c group by t1.a;
3+(5*ANY_VALUE(t1.b))
53
103
delete from t1;
insert into t1 (a,b) values(1,10),(1,20),(2,30),(2,40);
Warnings:
Warning	1364	Field 'c' doesn't have a default value
select a, sum(b) from t1;
ERROR 42000: In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.t1.a'; this is incompatible with sql_mode=only_full_group_by
select any_value(a), sum(b) from t1;
any_value(a)	sum(b)
1	100
# different order of input rows, different "any_value":
select any_value(a), sum(b) from (select * from t1 order by a desc) as d;
any_value(a)	sum(b)
1	100
select a,b,sum(c) from t1 group by a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select a,any_value(b),sum(c) from t1 group by a;
a	any_value(b)	sum(c)
1	10	0
2	30	0
select a,any_value(b),sum(c)
from (select * from t1 order by a desc, b desc) as d
group by a;
a	any_value(b)	sum(c)
1	10	0
2	30	0
# With view.
set @optimizer_switch_saved=@@optimizer_switch;
# Merged view
create algorithm=merge view v1 as select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1;
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=MERGE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`a` * 2) AS `a`,`t1`.`b` AS `b`,`t1`.`c` AS `c`,`t1`.`d` AS `d`,`t1`.`a` AS `e` from `t1`	utf8mb4	utf8mb4_0900_ai_ci
select sin(a) as z from v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from v1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
select (select sin(a)) as z from v1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select (select sin(a)) as z from v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
select sin(a) as z from t1 group by c*2,b*2;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a+b*c) as z from v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.v1.c",
                "test.v1.b"
              ] /* columns */
            } 
select sin(a) as z from v1 where b=2 group by c;
z
# {v1.b,v1.c}->v1.a->t2.a
select sin(t2.a) as z from v1, v1 as t2
where t2.a=v1.a group by v1.b,v1.c;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.v1.b",
                "test.v1.c",
                "test.t2.a"
              ] /* columns */
            } 
select sin(t2.a) as z from v1, v1 as t2
where t2.b=v1.b and t2.c=v1.c group by v1.b,v1.c;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
# With materialized view we get more dependencies than needed, due to implementation.
select t3.a from v1, v1 as t2, v1 as t3
where
t3.b=t2.b and t3.c=v1.d and
t2.b=v1.b and t2.c=v1.c
group by v1.b,v1.c;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0,
                1,
                2
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.v1.b",
                "test.v1.c",
                "test.t3.c",
                "test.t2.b",
                "test.t2.c",
                "test.t3.b"
              ] /* columns */
            } 
# If we simply went to real_item(), we would have WHERE 2*a=b, or
# GROUP BY 2*t1.a, so we would not find FDs. The original item
# (direct_view_ref here) must also be considered!
select a from v1 where a=b group by b;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.b",
                "test.v1.a"
              ] /* columns */
            } 
select b from v1 where a=b group by a;
b
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.a",
                "test.v1.b"
              ] /* columns */
            } 
select v1.c from v1 where v1.c=v1.a group by v1.a;
c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.a",
                "test.v1.c"
              ] /* columns */
            } 
select v1.a from v1 group by v1.e;
a
2
4
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.e",
                "test.v1.a"
              ] /* columns */
            } 
select v1.c from v1 where v1.c=v1.a group by v1.e;
c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.e",
                "test.v1.a",
                "test.v1.c"
              ] /* columns */
            } 
# View appears only in WHERE
select t2.d from v1, t1 as t2 where v1.a=t2.d and v1.e=t2.a group by t2.a;
d
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.t2.a",
                "test.v1.e",
                "test.v1.a",
                "test.t2.d"
              ] /* columns */
            } 
drop view if exists v1;
# FD due to view's WHERE:
create algorithm=merge view v1 as select t1.a*2 as a, t1.b as b from t1;
select a from v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view if exists v1;
# Merged view
create algorithm=merge view v1 as select t1.a*2 as a, t1.b as b from t1 where t1.a=t1.b;
select a from v1 group by b;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.b",
                "test.t1.a",
                "test.v1.a"
              ] /* columns */
            } 
drop view if exists v1;
# Aggregates in view
# Aggregates => skipping Merged view
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
# Aggregates + GROUP BY in view
# We group by b*5, to show that it works with GROUP expressions, not only fields.
# Aggregates => skipping Merged view
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
set optimizer_switch=@optimizer_switch_saved;
set @optimizer_switch_saved=@@optimizer_switch;
# Materialized view
create algorithm=temptable view v1 as select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1;
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`a` * 2) AS `a`,`t1`.`b` AS `b`,`t1`.`c` AS `c`,`t1`.`d` AS `d`,`t1`.`a` AS `e` from `t1`	utf8mb4	utf8mb4_0900_ai_ci
select sin(a) as z from v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from v1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
select (select sin(a)) as z from v1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select (select sin(a)) as z from v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
select sin(a) as z from t1 group by c*2,b*2;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a+b*c) as z from v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.c",
                "test.v1.b",
                "test.v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.c",
                    "test.t1.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select sin(a) as z from v1 where b=2 group by c;
z
# {v1.b,v1.c}->v1.a->t2.a
select sin(t2.a) as z from v1, v1 as t2
where t2.a=v1.a group by v1.b,v1.c;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.b",
                "test.v1.c",
                "test.v1.a",
                "test.t2.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                },
                {
                  "table": "`v1` `t2`"
                }
              ] /* searched_in_materialized_tables */
            } 
select sin(t2.a) as z from v1, v1 as t2
where t2.b=v1.b and t2.c=v1.c group by v1.b,v1.c;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
# With materialized view we get more dependencies than needed, due to implementation.
select t3.a from v1, v1 as t2, v1 as t3
where
t3.b=t2.b and t3.c=v1.d and
t2.b=v1.b and t2.c=v1.c
group by v1.b,v1.c;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.b",
                "test.v1.c",
                "test.t2.b",
                "test.t2.c",
                "test.t3.b",
                "test.v1.d",
                "test.t3.c",
                "test.t3.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                },
                {
                  "table": "`v1` `t2`",
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                },
                {
                  "table": "`v1` `t3`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
# If we simply went to real_item(), we would have WHERE 2*a=b, or
# GROUP BY 2*t1.a, so we would not find FDs. The original item
# (direct_view_ref here) must also be considered!
select a from v1 where a=b group by b;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.b",
                "test.v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select b from v1 where a=b group by a;
b
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.a",
                "test.v1.b"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select v1.c from v1 where v1.c=v1.a group by v1.a;
c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.a",
                "test.v1.c"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.c"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select v1.a from v1 group by v1.e;
a
2
4
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.e",
                "test.v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select v1.c from v1 where v1.c=v1.a group by v1.e;
c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.e",
                "test.v1.a",
                "test.v1.c"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.a",
                    "test.t1.c"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
# View appears only in WHERE
select t2.d from v1, t1 as t2 where v1.a=t2.d and v1.e=t2.a group by t2.a;
d
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.t2.a",
                "test.v1.e",
                "test.v1.a",
                "test.t2.d"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
# FD due to view's WHERE:
create algorithm=temptable view v1 as select t1.a*2 as a, t1.b as b from t1;
select a from v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view if exists v1;
# Materialized view
create algorithm=temptable view v1 as select t1.a*2 as a, t1.b as b from t1 where t1.a=t1.b;
select a from v1 group by b;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.b",
                "test.v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.b",
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
# Aggregates in view
# Materialized view
create algorithm=temptable view v1 as select sum(t1.a) as a, sum(t1.b) as b from t1;
select a from v1 group by b;
a
6
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.v1.b"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`"
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
# Aggregates + GROUP BY in view
# We group by b*5, to show that it works with GROUP expressions, not only fields.
# Materialized view
create algorithm=temptable view v1 as select a, b*5 as b, sum(t1.c) as c, sum(t1.d) as d from t1 group by a,b*5;
select a from v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c from v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c from v1 group by b,d;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select a,c from v1 group by a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select d,c from v1 group by b,a;
d	c
NULL	0
NULL	0
NULL	0
NULL	0
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.v1.b",
                "test.v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v1`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */,
                  "all_group_expressions": true
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
set optimizer_switch=@optimizer_switch_saved;
set @optimizer_switch_saved=@@optimizer_switch;
# Materialized derived table
set optimizer_switch='derived_merge=off';
select sin(a) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
select (select sin(a)) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by d,b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select (select sin(a)) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
select sin(a) as z from t1 group by c*2,b*2;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select sin(a+b*c) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by c,b;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.c",
                "v1.b",
                "v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.c",
                    "test.t1.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select sin(a) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 where b=2 group by c;
z
# {v1.b,v1.c}->v1.a->t2.a
select sin(t2.a) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1, (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as t2
where t2.a=v1.a group by v1.b,v1.c;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.b",
                "v1.c",
                "v1.a",
                "t2.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                },
                {
                  "table": " `t2`"
                }
              ] /* searched_in_materialized_tables */
            } 
select sin(t2.a) as z from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1, (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as t2
where t2.b=v1.b and t2.c=v1.c group by v1.b,v1.c;
z
0.909297426825682
0.909297426825682
-0.756802495307928
-0.756802495307928
# With materialized view we get more dependencies than needed, due to implementation.
select t3.a from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1, (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as t2, (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as t3
where
t3.b=t2.b and t3.c=v1.d and
t2.b=v1.b and t2.c=v1.c
group by v1.b,v1.c;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.b",
                "v1.c",
                "t2.b",
                "t2.c",
                "t3.b",
                "v1.d",
                "t3.c",
                "t3.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                },
                {
                  "table": " `t2`",
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                },
                {
                  "table": " `t3`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t1.b",
                    "test.t1.c"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
# If we simply went to real_item(), we would have WHERE 2*a=b, or
# GROUP BY 2*t1.a, so we would not find FDs. The original item
# (direct_view_ref here) must also be considered!
select a from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 where a=b group by b;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.b",
                "v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select b from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 where a=b group by a;
b
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.a",
                "v1.b"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select v1.c from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 where v1.c=v1.a group by v1.a;
c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.a",
                "v1.c"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.c"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select v1.a from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 group by v1.e;
a
2
4
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.e",
                "v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select v1.c from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1 where v1.c=v1.a group by v1.e;
c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.e",
                "v1.a",
                "v1.c"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.a",
                    "test.t1.c"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
# View appears only in WHERE
select t2.d from (select t1.a*2 as a, t1.b as b, t1.c as c, t1.d as d, t1.a as e from t1) as v1, t1 as t2 where v1.a=t2.d and v1.e=t2.a group by t2.a;
d
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.t2.a",
                "v1.e",
                "v1.a",
                "test.t2.d"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
# FD due to view's WHERE:
select a from (select t1.a*2 as a, t1.b as b from t1) as v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
# Derived table
select a from (select t1.a*2 as a, t1.b as b from t1 where t1.a=t1.b) as v1 group by b;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "v1.b",
                "v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.b",
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
# Aggregates in view
# Derived table
select a from (select sum(t1.a) as a, sum(t1.b) as b from t1) as v1 group by b;
a
6
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "v1.b"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`"
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
# Aggregates + GROUP BY in view
# We group by b*5, to show that it works with GROUP expressions, not only fields.
# Derived table
select a from (select a, b*5 as b, sum(t1.c) as c, sum(t1.d) as d from t1 group by a,b*5) as v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c from (select a, b*5 as b, sum(t1.c) as c, sum(t1.d) as d from t1 group by a,b*5) as v1 group by b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select c from (select a, b*5 as b, sum(t1.c) as c, sum(t1.d) as d from t1 group by a,b*5) as v1 group by b,d;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select a,c from (select a, b*5 as b, sum(t1.c) as c, sum(t1.d) as d from t1 group by a,b*5) as v1 group by a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select d,c from (select a, b*5 as b, sum(t1.c) as c, sum(t1.d) as d from t1 group by a,b*5) as v1 group by b,a;
d	c
NULL	0
NULL	0
NULL	0
NULL	0
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "v1.b",
                "v1.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `v1`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */,
                  "all_group_expressions": true
                }
              ] /* searched_in_materialized_tables */
            } 
drop view if exists v1;
Warnings:
Note	1051	Unknown table 'test.v1'
set optimizer_switch=@optimizer_switch_saved;
# Derived table in merged view
create algorithm=temptable view v2
as select a as a, 2*a as b from t1;
create algorithm=merge view v1
as select v2.a as a, 3*v2.b as b from v2;
select v1.b from v1 group by v1.a;
b
6
12
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.a",
                "test.v2.b",
                "test.v1.b"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v2`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
select t2.a from t1 as t2, v1 where t2.a=v1.b group by v1.a;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.v1.a",
                "test.v2.b",
                "test.v1.b",
                "test.t2.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`v2`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
# FDs in a view are those of the underlying query expression.
# FDs in a query expression: expressions in the SELECT list must be
# deterministic.
drop view v1;
create algorithm=merge view v1
as select v2.a as a, rand()*v2.b as b from v2;
select v1.b from v1 group by v1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view v1;
create algorithm=temptable view v1
as select v2.a as a, rand()*v2.b as b from v2;
select v1.b from v1 group by v1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view v1,v2;
# Item_direct_view_ref pointing to Item_direct_view_ref pointing to
# Item_field (a rare case!)
create algorithm=merge view v2 as select 2*a as a, 2*b as b from t1;
create algorithm=merge view v1 as select a, 3*b as b from v2 where a=b;
select 1 from (select a,b+0 from v1 group by a) as d;
1
drop view v1,v2;
# Some fun cases with aggregates in derived table.
# Inner query is valid: t1.a -> t1.b (equality in WHERE). Outer query:
# d.b -> t1.b (underlying column of d.b) -> t1.a (equality)
# -> sum(1) (because t1.a is all group columns so determines
# sum()) -> d.s (because sum() is underlying of d.s)
select d.s from
(select b, sum(1) as s from t1 where a=b group by a) as d
group by d.b;
s
alter table t1 add column pk int primary key auto_increment;
# Inner query: t1.pk -> t1.* (pk). Outer query:
# d.b,d.c -> t1.b,t1.c (their underlying columns) -> t1.pk (because
# t1.b,t1.c is unique) -> sum(1) (because t1.pk is all group columns so
# determines sum()) -> d.s (because sum() is underlying of d.s)
select d.s from
(select b, c, sum(d) as s from t1 group by pk) as d
group by d.b,d.c;
s
NULL
NULL
NULL
NULL
# Outer query:
# d.c -> t1.b*3 (underlying column of d.c) -> sum(a) (because t1.b*3
# is all group expressions) -> d.s
select d.s from
(select b*3 as c, sum(a) as s from t1 group by b*3) as d
group by d.c;
s
1
1
2
2
drop table t1,t2;
# Testcase from Reviewers
create table customer1(pk int primary key, a int);
create table customer2(pk int primary key, b int);
CREATE algorithm=merge VIEW customer as SELECT pk,a,b
FROM customer1 JOIN customer2 USING (pk);
select customer.pk, customer.b
from customer
group by customer.pk;
pk	b
# View is merged. Show FDs. Note that in --ps-protocol, the trace
# is that of execution, so contains no group-by checks.
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0,
                1
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.customer.pk",
                "test.customer2.pk"
              ] /* columns */
            } 
drop view customer;
CREATE algorithm=temptable VIEW customer as SELECT pk,a,b
FROM customer1 JOIN customer2 USING (pk);
select customer.pk, customer.b
from customer
group by customer.pk;
pk	b
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "test.customer.pk",
                "test.customer.b"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": "`customer`",
                  "all_columns_of_table_map_bits": [
                    0,
                    1
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.customer1.pk",
                    "test.customer2.pk"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
# Benefit from outer-join-to-inner conversion.
insert into customer1 values(0,10),(1,20);
insert into customer2 values(0,10),(1,20);
# 1) no conversion, no FD from customer2.b to customer1.a.
explain select customer1.a, count(*)
from customer1 left join customer2 on customer1.a=customer2.b
where customer1.pk in (7,9)
group by customer2.b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.customer1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# 2) null-complemented row can't pass WHERE => conversion
# => FD from customer2.b to customer1.a.
explain select customer1.a, count(*)
from customer1 left join customer2 on customer1.a=customer2.b
where customer2.pk in (7,9)
group by customer2.b;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	customer1	NULL	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary
1	SIMPLE	customer2	NULL	range	PRIMARY	PRIMARY	4	NULL	2	50.00	Using where; Using join buffer (hash join)
Warnings:
Note	1003	/* select#1 */ select `test`.`customer1`.`a` AS `a`,count(0) AS `count(*)` from `test`.`customer1` join `test`.`customer2` where ((`test`.`customer2`.`b` = `test`.`customer1`.`a`) and (`test`.`customer2`.`pk` in (7,9))) group by `test`.`customer2`.`b`
drop table customer1,customer2;
drop view customer;
# FDs of JOIN...USING and NATURAL JOIN
create table t1(pk int primary key, a int);
create table t2(pk int primary key, b int);
select t1.pk, t1.a from t1 join t2 on t1.pk=t2.pk group by t1.pk;
pk	a
select t1.pk, t1.a from t1 join t2 using(pk) group by t1.pk;
pk	a
select t1.pk, t1.a from t1 natural join t2 group by t1.pk;
pk	a
select t1.pk, t1.a from t1 left join t2 using(pk) group by t1.pk;
pk	a
select t1.pk, t1.a from t1 natural left join t2 group by t1.pk;
pk	a
select t1.pk, t2.b from t1 join t2 on t1.pk=t2.pk group by t1.pk;
pk	b
select t1.pk, t2.b from t1 join t2 using(pk) group by t1.pk;
pk	b
select t1.pk, t2.b from t1 natural join t2 group by t1.pk;
pk	b
select t1.pk, t2.b from t1 left join t2 using(pk) group by t1.pk;
pk	b
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0,
                1
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.pk",
                "test.t2.pk"
              ] /* columns */
            } 
select t1.pk, t2.b from t1 natural left join t2 group by t1.pk;
pk	b
# Equivalent queries, with RIGHT JOIN
select t1.pk, t2.b from t2 right join t1 using(pk) group by t1.pk;
pk	b
select t1.pk, t2.b from t2 natural right join t1 group by t1.pk;
pk	b
select t1.pk, t2.b from t1 left join t2 on t1.pk>t2.pk group by t1.pk;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t2.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# Even in --ps-protocol we see the group-by checks in trace because
# there has been no execution (due to error).
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                0
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.pk"
              ] /* columns */
            } 
select t1.pk, t2.b from t2 right join t1 on t1.pk>t2.pk group by t1.pk;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t2.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop table t1,t2;
# Cases where FDs from weak side do not propagate
create table t1(a int, b int);
insert into t1 values(null,0),(null,1);
select d.a,d.c
from t1 left join (select a, coalesce(a,3) as c from t1) as d
on t1.b>0;
a	c
NULL	3
NULL	3
NULL	NULL
# Now group it by d.a:
select d.a,d.c
from t1 left join (select a, coalesce(a,3) as c from t1) as d
on t1.b>0 group by d.a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'd.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "d.a"
              ] /* columns */
            } 
select d.a,d.c
from t1 left join (select a, count(a) as c from t1 group by a) as d
on t1.b+d.c>0;
a	c
NULL	0
NULL	NULL
# Now group it by d.a:
select d.a,d.c
from t1 left join (select a, count(a) as c from t1 group by a) as d
on t1.b+d.c>0 group by d.a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'd.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "d.a"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `d`",
                  "columns": [
                    "test.t1.a"
                  ] /* columns */,
                  "all_group_expressions": true
                }
              ] /* searched_in_materialized_tables */
            } 
select d.m,d.c
from t1 left join (select max(a) as m, count(a) as c from t1) as d
on t1.b+d.c>0;
m	c
NULL	0
NULL	NULL
# Now group it by d.m:
select d.m,d.c
from t1 left join (select max(a) as m, count(a) as c from t1) as d
on t1.b+d.c>0 group by d.m;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'd.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "d.m"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `d`"
                }
              ] /* searched_in_materialized_tables */
            } 
# Now group it by d.c which is non-nullable:
select d.m,d.c
from t1 left join (select max(a) as m, count(a) as c from t1) as d
on t1.b+d.c>0 group by d.c;
m	c
NULL	0
NULL	NULL
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                1
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "d.c"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `d`"
                }
              ] /* searched_in_materialized_tables */
            } 
drop table t1;
create table t1(pk int primary key, a int);
# Not valid per the standard, because:
# 1) In t3, t3.pk->t3.a holds.
# 2) In R1 the result of "(t2 left join t3 on 1)", t3.pk->t3.a
# holds, by application of: there is a functional dependency in the
# weak side t3, and t3.pk is not nullable in t3.
# 3) In R2 the result of "t1 left join (t2 left join t3 on 1) on 1",
# t3.pk->t3.a doesn't hold anymore, because: it's a dependency in the
# weak side (weak side is R1), and t3.pk is nullable _when
# seen as a column of R1_ (in R1 t3.pk can be NULL, if the row of t3
# is actually a null-complemented one).
# But for us it is valid, because we have refined the logic: the
# pk-based FD satisfies the requirement that a NULL value of t3.pk
# implies a NULL value of t3.a (indeed, the NULL value of t3.pk can
# only come from null-complementing of the row of t3 in R1, in which
# case t3.a is also NULL).
select t3.a
from t1 left join (t1 as t2 left join t1 as t3 on 1) on 1
group by t3.pk;
a
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                2
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t3.pk"
              ] /* columns */
            } 
# Outer reference - why we use resolved_used_tables():
select (select t1.a from t1 as t2 limit 1) from t1 group by pk;
(select t1.a from t1 as t2 limit 1)
# We don't build the FD list if not needed
select t1.a*3 from t1 group by t1.a;
t1.a*3
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
NOT FOUND
drop table t1;
# Tricky cases with "ON col=literal" propagating.
create table t1(a int, b int);
insert into t1 values();
insert into t1 values(10,11);
create table t2(c int, d int);
insert into t2 values(2,3);
select t4.d
from t1 left join (t2 as t3 join t2 as t4 on t4.d=3) on t1.a=10;
d
3
NULL
# Equivalent to T1 LJ (T2, T3) ON T4.D=3 AND T1.A=10
# (this is what simplify_joins() does).
# For T4.D=3, DJS is {T1.A} which is not group column.
select t4.d
from t1 left join (t2 as t3 join t2 as t4 on t4.d=3) on t1.a=10 group by "";
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t4.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t4.d
from t1 left join (t2 as t3 left join t2 as t4 on t4.d=3) on t1.a=10;
d
3
NULL
# For T4.D=3, DJS={}, not NULL-friendly, and embedding is on weak side
# so FD cannot propagate.
select t4.d
from t1 left join (t2 as t3 left join t2 as t4 on t4.d=3) on t1.a=10 group by "";
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t4.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select t4.d
from t1 join (t2 as t3 left join t2 as t4 on t4.d=3) on t1.a=10;
d
3
# For T4.D=3, DJS={}, not NULL-friendly, but embedding is on weak side
# so FD can propagate.
select t4.d
from t1 join (t2 as t3 left join t2 as t4 on t4.d=3) on t1.a=10 group by "";
d
3
# With a view
create view v1 as select a as a, 2*a as b, coalesce(a,3) as c from t1;
select v1.b from t1 left join v1 on 1;
b
20
20
NULL
NULL
# If v1.a is NULL then v1.b is NULL: a->b is NULL-friendly
select v1.b from t1 left join v1 on 1 group by v1.a;
b
20
NULL
select v1.c from t1 left join v1 on 1;
c
10
10
3
3
# If v1.a is NULL then v1.c may not be NULL: a->c is not NULL-friendly
select v1.c from t1 left join v1 on 1 group by v1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.v1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view v1;
# Constant view item
create view v1 as select a as a, 2 as b from t1;
# Because of BUG#17023060, the result is wrong, should be
# [10,2 ; 10,2 ; NULL,NULL], which would show that {}->{v1.b} does not
# hold in the result, even though v1.b is constant (=2) in v1.
select t1.a, v1.b from t1 left join v1 on t1.a is not null;
a	b
10	2
10	2
NULL	NULL
# We correctly reject this:
select t1.a, v1.b from t1 left join v1 on t1.a is not null group by v1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view v1;
drop table t1,t2;
create table emp(empno int, ename char(1), deptno int);
create table dept(deptno int primary key, dname char(1));
CREATE algorithm=merge VIEW empdept AS
SELECT emp.empno, emp.ename, dept.deptno, dept.dname
FROM emp LEFT OUTER JOIN dept ON (emp.deptno = dept.deptno);
EXPLAIN SELECT dname, COUNT(*)
FROM empdept
GROUP BY deptno;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	emp	NULL	ALL	NULL	NULL	NULL	NULL	1	100.00	Using temporary
1	SIMPLE	dept	NULL	eq_ref	PRIMARY	PRIMARY	4	test.emp.deptno	1	100.00	NULL
Warnings:
Note	1003	/* select#1 */ select `test`.`dept`.`dname` AS `dname`,count(0) AS `COUNT(*)` from `test`.`emp` left join `test`.`dept` on((`test`.`dept`.`deptno` = `test`.`emp`.`deptno`)) where true group by `test`.`dept`.`deptno`
# There is pk-based FD dept.Depno->dept.dname in dept
# and it propagates in the view-which-became-nest because it is
# NULL-friendly.
SELECT dname, COUNT(*)
FROM empdept
GROUP BY deptno;
dname	COUNT(*)
drop view empdept;
CREATE algorithm=temptable VIEW empdept AS
SELECT emp.empno, emp.ename, dept.deptno, dept.dname
FROM emp LEFT OUTER JOIN dept ON (emp.deptno = dept.deptno);
EXPLAIN SELECT dname, COUNT(*)
FROM empdept
GROUP BY deptno;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived2>	NULL	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary
2	DERIVED	emp	NULL	ALL	NULL	NULL	NULL	NULL	1	100.00	NULL
2	DERIVED	dept	NULL	eq_ref	PRIMARY	PRIMARY	4	test.emp.deptno	1	100.00	NULL
Warnings:
Note	1003	/* select#1 */ select `test`.`empdept`.`dname` AS `dname`,count(0) AS `COUNT(*)` from `test`.`empdept` group by `test`.`empdept`.`deptno`
# There is pk-based FD dept.Depno->dept.dname in dept
# and it propagates in the materialized view because it is
# NULL-friendly, and then in the top query because the view is not in
# the weak side of an outer join.
SELECT dname, COUNT(*)
FROM empdept
GROUP BY deptno;
dname	COUNT(*)
# More tests, for code coverage.
# UNION in derived table
select emp.ename
from
(select 1 as empno union select 2) deriv,
emp
where emp.empno=deriv.empno
group by emp.empno;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.emp.ename' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop view empdept;
# Make the key-searching loop meet view columns which don't wrap a
# column (CONCAT).
CREATE VIEW empdept AS
SELECT emp.empno, emp.ename, dept.deptno, concat("",dept.dname) as dname
FROM emp LEFT JOIN dept ON (emp.deptno = dept.deptno);
SELECT ename, COUNT(*) FROM empdept WHERE empno=dname and empno=deptno GROUP BY empno;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.empdept.ename' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
drop table emp,dept;
drop view empdept;
#
# Bug#19636980 ASSERT `TABLE->OUTER_JOIN' FAILED IN GROUP_CHECK::FIND_FD_IN_JOINED_TABLE
#
CREATE TABLE t1 (
c1 INT,
c2 INT,
c4 DATE,
c5 VARCHAR(1)
);
CREATE TABLE t2 (
c1 INT,
c2 INT,
c3 INT,
c5 VARCHAR(1)
);
# alias1.c5 is not FD, the error is detected at SELECT time
CREATE VIEW v1 AS
SELECT alias1.c4 AS field1
FROM t1 AS alias1
INNER JOIN t1 AS alias2
ON 1
GROUP BY field1
ORDER BY alias1.c5;
SELECT * FROM v1;
ERROR 42000: Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'test.alias1.c5' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
DROP VIEW v1;
# alias1.c5 is FD (WHERE contains: alias1.c5='d')
CREATE VIEW v1 AS
SELECT alias1.c4 AS field1, alias1.c4 AS field2
FROM t1 AS alias1
INNER JOIN t1 AS alias2
ON (alias2.c1 = alias1.c2)
WHERE ( NOT EXISTS (
SELECT SQ1_alias1.c5 AS SQ1_field1
FROM t2 AS SQ1_alias1
WHERE SQ1_alias1.c3 < alias1.c1
))
AND (alias1.c5 = alias1.c5
AND alias1.c5 = 'd'
    )
GROUP BY field1, field2
ORDER BY alias1.c5, field1, field2
;
SELECT * FROM v1;
field1	field2
DROP VIEW v1;
DROP TABLE t1,t2;
#
# Bug#19636409 ASSERT `(MAP_OF_NEW_EQ_FDS...` IN GROUP_CHECK::IS_FD_ON_SOURCE ON SELECT
#
CREATE TABLE t1 (
pk int NOT NULL,
c1 datetime,
c2 varchar(1),
c3 date,
c4 date,
c5 varchar(1),
PRIMARY KEY (pk)
);
CREATE VIEW v1 AS
SELECT c3 AS subfield11,
pk AS subfield12,
c2 AS subfield13
FROM t1
GROUP BY subfield11, subfield12
;
# This query is valid. Indeed: field3 i.e. t1.c2 is part of GROUP BY,
# so, in a group:
# - all rows have the same value of t1.c2, noted val_c2,
# - so all rows, which necessarily match WHERE, matched with a row of
# v1 having subfield11=val_c2 and subfield12=val_c2 (due to IN
# predicate),
# - thus both grouping columns of v1's query expression are constant
# (to val_c2)
# - thus v1.subfield13 is constant too (grouping columns determine the
# SELECT list)
# - so, due to IN, this determines the value of t1.pk (t1 is the top
# query's table).
# - so t1.pk is constant, so all columns of t1 are constant.
# Which proves that it makes sense to search FDs in semijoin
# equalities.
# In other words: a semijoin is like a join except that it eliminates
# duplicates, but duplicates are irrelevant to the decision whether a
# column is functionally dependent on a set of columns.
SELECT c5 AS field1,
c4 AS field2,
c2 AS field3,
c1 AS field4
FROM t1
WHERE ( c2, c2, pk ) IN (
SELECT * FROM v1
)
GROUP BY field1, field3, field3;
field1	field2	field3	field4
#
# Bug#19687724 FUNCTIONAL DEPENDENCIES ARE NOT RECOGNIZED IN EQUALITIES BETWEEN ROWS
#
# Inspired by query above, but with a join:
SELECT c5 AS field1,
c4 AS field2,
c2 AS field3,
c1 AS field4
FROM t1, v1
WHERE ( c2, c2, pk ) = (subfield11, subfield12, subfield13)
GROUP BY field1, field3, field3;
field1	field2	field3	field4
# With constants:
SELECT c5 AS field1,
c4 AS field2,
c2 AS field3,
c1 AS field4
FROM t1
WHERE ( c2, c2, pk ) = (1, 2, 3)
GROUP BY field1, field3, field3;
field1	field2	field3	field4
DROP TABLE t1;
CREATE TABLE t1(a INT, b INT, c INT, d INT);
SELECT a,b,c,d FROM t1 WHERE a=c AND b=d GROUP by a,b;
a	b	c	d
SELECT a,b,c,d FROM t1 WHERE (a,b)=(c,d) GROUP BY a,b;
a	b	c	d
SELECT a,b,c,d FROM t1 WHERE (a,b)=(c,d+1) GROUP BY a,b;
ERROR 42000: Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
DROP TABLE t1;
DROP VIEW v1;
#
# Bug #20031708 ASSERT ON GROUP_CHECK::IS_FD_ON_SOURCE
#
CREATE TABLE t1 (
col_varchar_10_utf8 VARCHAR(10) CHARACTER SET utf8,
col_int_key INT,
pk INT PRIMARY KEY
);
Warnings:
Warning	3719	'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
CREATE TABLE t2 (
col_varchar_10_utf8 VARCHAR(10) CHARACTER SET utf8 DEFAULT NULL,
col_int_key INT DEFAULT NULL,
pk INT PRIMARY KEY
);
Warnings:
Warning	3719	'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
CREATE VIEW v2 AS SELECT * FROM t2;
# The reporter's testcase:
SELECT COUNT(*), t1.col_int_key
FROM v2 LEFT OUTER JOIN t1
ON v2.col_varchar_10_utf8 = t1.col_varchar_10_utf8
WHERE v2.pk = 4;
ERROR 42000: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'test.t1.col_int_key'; this is incompatible with sql_mode=only_full_group_by
DROP VIEW v2;
# A variant: one column is an expression:
CREATE VIEW v2 AS SELECT
CONCAT(col_varchar_10_utf8,' ') AS col_varchar_10_utf8,
col_int_key,
pk
FROM t2;
SELECT COUNT(*), t1.col_int_key
FROM v2 LEFT OUTER JOIN t1
ON v2.col_varchar_10_utf8 = t1.col_varchar_10_utf8
WHERE v2.pk = 4;
ERROR 42000: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'test.t1.col_int_key'; this is incompatible with sql_mode=only_full_group_by
DROP VIEW v2;
# Query used in the commit comment: view column involving two tables
CREATE VIEW v2 AS
SELECT t1.pk, t2.col_int_key+1 as c, t1.pk+t2.col_int_key as p
FROM t1, t2;
# FDs will be discovered in this order: {}->v2.pk, v2.pk->v2.c,
# v2.c->v2.p
SELECT COUNT(*), v2.p
FROM v2
WHERE v2.c=v2.p and v2.c=v2.pk AND v2.pk = 4;
COUNT(*)	p
0	NULL
DROP VIEW v2;
# If in the query specification defining a view, a base table's pk is
# determined,
# and the view's column is a function of this base table's columns,
# then the view's column is also determined.
# So, in this view's result, {v2.pk}->{v2.coa}:
CREATE ALGORITHM=MERGE VIEW v2 AS
SELECT t2.pk, COALESCE(t2.pk, 3) AS coa
FROM t1 LEFT JOIN t2 ON 0;
# And thus {pk}->{coa} holds in the result of this query using the view;
# if there is NULL-complementing in the LEFT JOIN below then (pk,coa)
# will be (NULL,NULL) and if there is not it will be (non-NULL,3):
# v2.coa is determined by v2.pk. The key fact is that v2.pk is not
# NULLable so this is a NFFD.
SELECT v2.pk, v2.coa
FROM t1 LEFT JOIN v2 AS v2 ON 0
GROUP BY v2.pk;
pk	coa
DROP VIEW v2;
DROP TABLE t1,t2;
#
# Bug#21807579 FUNCTIONAL DEPENDENCIES ARE NOT RECOGNIZED IN GENERATED COLUMNS
#
CREATE TABLE t ( a INT, c INT GENERATED ALWAYS AS (a+2), d INT GENERATED ALWAYS AS (c+2) );
# {a}->{c}, {c}->{d} and {a}->{d} hold.
SELECT c FROM t GROUP BY a;
c
SELECT d FROM t GROUP BY c;
d
SELECT d FROM t GROUP BY a;
d
SELECT 1+c FROM t GROUP BY a;
1+c
SELECT 1+d FROM t GROUP BY c;
1+d
SELECT 1+d FROM t GROUP BY a;
1+d
# {t2.a}->{t2.d}->{t1.c}->{t1.d}
SELECT t1.d FROM t as t1, t as t2 WHERE t2.d=t1.c GROUP BY t2.a;
d
SELECT t1.d FROM t as t1, t as t2 WHERE t2.d>t1.c GROUP BY t2.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
DROP TABLE t;
# A non-NULL-friendly dependency.
CREATE TABLE t ( a INT,
c INT GENERATED ALWAYS AS (COALESCE(a,3)) );
INSERT INTO t (a) VALUES(NULL);
CREATE TABLE u ( a INT );
INSERT INTO u VALUES(0),(1);
# Even though {a}->{c} holds in 't', it doesn't propagate to the left
# join's result
SELECT t.a,t.c FROM u LEFT JOIN t ON u.a>0;
a	c
NULL	3
NULL	NULL
SELECT t.a,t.c FROM u LEFT JOIN t ON u.a>0 GROUP BY t.a;
ERROR 42000: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
DROP TABLE t,u;
#
# Bug #22924183 	GROUP BY ON DERIVED TABLE, EXPRESSION OF AGGREGATE AND GROUP COLUMN: ERROR
#
CREATE TABLE coll (
doc text
) ENGINE=InnoDB;
SELECT (je+1)+count(*) FROM (SELECT doc+1 AS je FROM coll) AS dt GROUP BY je;
(je+1)+count(*)
SELECT je+(1+count(*)) FROM (SELECT doc+1 AS je FROM coll) AS dt GROUP BY je;
je+(1+count(*))
DROP TABLE coll;
#
# Bug#27114719 FUNCTIONAL DEPENDENCY CHECK FAILS ON FIRST CALL, SUCCEEDS ON NEXT
#
CREATE TABLE t ( a INT, c INT GENERATED ALWAYS AS (a+2), d INT GENERATED
ALWAYS AS (c+2) );
SELECT t1.d FROM t as t1, t as t2 WHERE t2.d>t1.c GROUP BY t2.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT t1.d FROM t as t1, t as t2 WHERE t2.d>t1.c GROUP BY t2.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.d' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
FLUSH TABLES;
SELECT (SELECT t1.c FROM t as t1 GROUP BY -3) FROM t as t2;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT DISTINCT t1.a FROM t as t1 ORDER BY t1.d LIMIT 1;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.d' which is not in SELECT list; this is incompatible with DISTINCT
SELECT (SELECT DISTINCT t1.a FROM t as t1 ORDER BY t1.d LIMIT 1) FROM t as t2;
ERROR HY000: Expression #1 of ORDER BY clause is not in SELECT list, references column 'test.t1.d' which is not in SELECT list; this is incompatible with DISTINCT
DROP TABLE t;
#
# Bug#22279903 ISSUE WITH ONLY_FULL_GROUP_BY AND UNIQUE KEY WITH NULL VALUES
#
CREATE TABLE t(a INT NULL, b INT NOT NULL, c INT, UNIQUE(a,b));
INSERT INTO t VALUES (NULL, 1, 4);
INSERT INTO t VALUES (NULL, 1, 5);
SELECT a,b,c FROM t GROUP BY a,b;
ERROR 42000: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT a,b,c FROM t WHERE a IS NOT NULL GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE NOT (a IS NULL) GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a > 3 GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a = 3 GROUP BY b;
a	b	c
SELECT a,b,c FROM t WHERE a BETWEEN 3 AND 6 GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a <> 3 GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a IN (3,4) GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a IN (SELECT b FROM t) GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a IS TRUE GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE (a <> 3) IS TRUE GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a IS FALSE GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE (a <> 3) IS FALSE GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE NOT(a IN (3,4)) GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a NOT IN (3,4) GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a LIKE "%abc%" GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a NOT LIKE "%abc%" GROUP BY a,b;
a	b	c
SELECT a,b,c FROM t WHERE a IS TRUE GROUP BY b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT a,b,c FROM t WHERE a<=>NULL GROUP BY b;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT a,b,c FROM t WHERE a IS NOT TRUE GROUP BY a,b;
ERROR 42000: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT a,b,c FROM
(SELECT * FROM t WHERE a IS NOT NULL) AS dt GROUP BY a,b;
a	b	c
SELECT /*+ NO_MERGE() */ a,b,c FROM
(SELECT * FROM t WHERE a IS NOT NULL) AS dt GROUP BY a,b;
a	b	c
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "columns": [
                "dt.a",
                "dt.b",
                "dt.c"
              ] /* columns */,
              "searched_in_materialized_tables": [
                {
                  "table": " `dt`",
                  "all_columns_of_table_map_bits": [
                    0
                  ] /* all_columns_of_table_map_bits */,
                  "columns": [
                    "test.t.a",
                    "test.t.b"
                  ] /* columns */
                }
              ] /* searched_in_materialized_tables */
            } 
SELECT a,b,c FROM t WHERE a IS NULL GROUP BY a,b;
ERROR 42000: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT a,b,c FROM t WHERE NOT(a IS NOT NULL) GROUP BY a,b;
ERROR 42000: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT t.* FROM t LEFT JOIN t AS t1 ON t.a IS NOT NULL
GROUP BY t.a,t.b;
ERROR 42000: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT t.* FROM t RIGHT JOIN t AS t1 ON t.a IS NOT NULL
GROUP BY t.a,t.b;
a	b	c
NULL	NULL	NULL
DROP TABLE t;
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (b INT);
CREATE TABLE t3 (
b INT NULL,
c INT NULL,
d INT NULL,
e INT NULL,
UNIQUE KEY (b,d,e));
SELECT * FROM t1, t2, t3
WHERE t2.b = t1.b AND t2.b = t3.b
AND t3.d = 1 AND t3.e = 1 AND t3.d IS NOT NULL AND t1.a = 2
GROUP BY t1.b;
a	b	b	b	c	d	e
SELECT * FROM t1, t2, t3
WHERE t2.b = t1.b AND t2.b = t3.b
AND t3.d = 1 AND t3.e = 1 AND t1.a = 2
GROUP BY t1.b;
a	b	b	b	c	d	e
SELECT show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
show_json_object('"functional_dependencies_of_GROUP_columns": {', TRACE)
"functional_dependencies_of_GROUP_columns": {
              "all_columns_of_table_map_bits": [
                2
              ] /* all_columns_of_table_map_bits */,
              "columns": [
                "test.t1.b",
                "test.t2.b",
                "test.t3.b",
                "test.t3.d",
                "test.t3.e",
                "test.t1.a"
              ] /* columns */
            } 
SELECT * FROM t1, t2, t3
WHERE t2.b = t1.b AND t2.b = t3.b
AND t3.e = 1 AND t3.d IS NOT NULL AND t1.a = 2
GROUP BY t1.b;
ERROR 42000: Expression #5 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t3.c' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
# Verify that for semijoin and antijoin we catch the FD issues.
explain select (select 2 from t2 where 1=(select t1.b from t3)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
explain select (select 2 from t2 where exists(select t1.b from t3)) as col from t1 group by t1.a;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	1	100.00	Using temporary
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	1	100.00	NULL
2	DEPENDENT SUBQUERY	t3	NULL	index	NULL	b	15	NULL	1	100.00	Using index; FirstMatch(t2); Using join buffer (hash join)
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
Note	1003	/* select#1 */ select (/* select#2 */ select 2 from `test`.`t2` semi join (`test`.`t3`) where true) AS `col` from `test`.`t1` group by `test`.`t1`.`a`
explain select (select 2 from t2 where not exists(select t1.b from t3)) as col from t1 group by t1.a;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	t1	NULL	ALL	NULL	NULL	NULL	NULL	1	100.00	Using temporary
2	DEPENDENT SUBQUERY	t2	NULL	ALL	NULL	NULL	NULL	NULL	1	100.00	NULL
2	DEPENDENT SUBQUERY	t3	NULL	index	NULL	b	15	NULL	1	100.00	Using where; Not exists; Using index; Using join buffer (hash join)
Warnings:
Note	1276	Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
Note	1003	/* select#1 */ select (/* select#2 */ select 2 from `test`.`t2` anti join (`test`.`t3`) on(true) where true) AS `col` from `test`.`t1` group by `test`.`t1`.`a`
explain select (select 2 from t2 where exists(select 1 from t3 where t1.b)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
explain select (select 2 from t2 where not exists(select 1 from t3 where t1.b)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
explain select (select 2 from t2 where 1 in(select t1.b from t3)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
alter table t1 modify b int not null;
explain select (select 2 from t2 where 1 not in(select t1.b from t3)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
explain select (select 2 from t2 where 1 in(select 1 from t3 where t1.b)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
explain select (select 2 from t2 where 1 not in(select 1 from t3 where t1.b)) as col from t1 group by t1.a;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t1.b' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
DROP TABLE t1, t2, t3;
DROP FUNCTION show_json_object;
#
# Bug#29006668 UNEXPECTED ERROR: "ORDER BY CLAUSE IS NOT IN GROUP BY CLAUSE" WITH ROLLUP
#
CREATE TABLE t(i INT);
INSERT INTO t VALUES (-1),(2),(1);
SELECT ABS(i) AS a FROM t GROUP BY abs(i)             ORDER BY a + 1;
a
1
2
SELECT ABS(i) AS a FROM t GROUP BY abs(i) WITH ROLLUP ORDER BY a + 1;
a
NULL
1
2
SELECT ABS(i) AS a FROM t GROUP BY a                  ORDER BY a + 1;
a
1
2
SELECT ABS(i) AS a FROM t GROUP BY a      WITH ROLLUP ORDER BY a + 1;
a
NULL
1
2
DROP TABLE t;
#
# Bug#30753524 DBUG_ASSERT((MAP_OF_NEW_FDS & PSEUDO_TABLE_BITS) == 0)
#
CREATE TABLE t (a INT,b INT);
SELECT 1 FROM t AS ot
WHERE 1 <=
(
SELECT it2.a
FROM
(
SELECT ot.b FROM t AS it1
) AS dt
JOIN t AS it2 ON dt.b=it2.b
GROUP BY it2.b
)
;
ERROR 42000: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.it2.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT 1 FROM t AS ot
WHERE 1 <=
(
SELECT dt.b
FROM
(
SELECT ot.b FROM t AS it1
) AS dt
JOIN t AS it2 ON dt.b=it2.b
GROUP BY it2.b
)
;
1
DROP TABLE t;