File: partition_locking.test

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 (3075 lines) | stat: -rw-r--r-- 76,157 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
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075

# Test have expected differences in count of commit and prepare handlers
# when run with and without binlog and/or with different binlog formats.
# Test skips when log_bin is disabled OR binlog_format != ROW.
--source include/have_binlog_format_row.inc

# Different page sizes gives different query plans
# results based on default 16k.

--echo # The results where created without innodb persistent stats.
SET @old_innodb_stats_persistent= @@global.innodb_stats_persistent;
SET @@global.innodb_stats_persistent= 0;

--echo # Original tests for WL#4443

# Helper statement

create table thread_to_monitor(thread_id int);

insert into thread_to_monitor(thread_id)
  SELECT THREAD_ID FROM performance_schema.threads
    WHERE PROCESSLIST_ID=CONNECTION_ID();

connect (monitor, localhost, root);

# This query needs to be in a separate monitoring session,
# so we do not polute the test session statistics.

let $get_handler_status_counts= SELECT VARIABLE_NAME, VARIABLE_VALUE
  FROM performance_schema.status_by_thread
  WHERE VARIABLE_NAME LIKE 'HANDLER_%' AND VARIABLE_VALUE > 0
    AND THREAD_ID IN (SELECT thread_id from test.thread_to_monitor);

connection default;

--let $MYSQLD_DATADIR= `SELECT @@datadir`


CREATE TABLE t1 (a int PRIMARY KEY, b varchar(128), KEY (b))
ENGINE = InnoDB
PARTITION BY HASH (a) PARTITIONS 13;

CREATE TABLE t2 (a int PRIMARY KEY AUTO_INCREMENT, b varchar(128))
ENGINE = InnoDB
PARTITION BY HASH (a) PARTITIONS 13;

SHOW CREATE TABLE t1;
SHOW CREATE TABLE t2;

--echo #
--echo #
--echo # Test how INSERT prune locks
--echo # First test, no defaults
--echo #
FLUSH STATUS;
INSERT INTO t1 VALUES (1, 'First row, p1');

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 commit
FLUSH STATUS;
--error ER_DUP_ENTRY
INSERT INTO t1 VALUES (1, 'First row, duplicate');

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 rollback
FLUSH STATUS;
INSERT INTO t1 VALUES (0, 'First row, p0'), (2, 'First row, p2'),
                      (3, 'First row, p3'), (4, 'First row, p4');

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (1 * 13, 'Second row, p0'), (2 * 13, 'Third row, p0'),
                      (3 * 13, 'Fourth row, p0'), (4 * 13, 'Fifth row, p0');

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 commit

--echo #
--echo # INSERT with auto increment, lock pruning
--echo #
FLUSH STATUS;
INSERT INTO t2 VALUES (NULL, 'First auto-inc row');

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Auto increment value is not known until write.
--echo # 1 commit

FLUSH STATUS;
INSERT INTO t2 (b) VALUES ('Second auto-inc row');

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Auto increment value is not known until write.
--echo # 1 commit

FLUSH STATUS;
INSERT INTO t2 VALUES (10, "First row, p10");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Insert pruning on tables with auto increment is not yet supported
--echo # 1 commit


--echo #
--echo # UPDATE with auto increment, lock pruning
--echo #
FLUSH STATUS;
UPDATE t2 SET b = CONCAT(b, ", UPDATED") WHERE a = 10;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_key + 1 update + 1 commit

--echo #
--echo # Test handling of INSERT INTO <table> VALUES (<all fields specified>)
--echo #
CREATE TABLE t3 (a INT, b CHAR(10)) PARTITION BY HASH (a) PARTITIONS 2;
SHOW CREATE TABLE t3;
FLUSH STATUS;
INSERT INTO t3 VALUES (1, "Test 1");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 VALUES (2, "Test 2"), (3, "Test 3"), (4, "Test 4");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 VALUES (6, "Test 6"), (8, "Test 8"), (10, "Test 10");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 VALUES (5, "Test 5"), (7, "Test 7"), (9, "Test 9");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 VALUES (0, "Test 0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (1, "Test 1");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (2, "Test 2"), (3, "Test 3"), (4, "Test 4");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (6, "Test 6"), (8, "Test 8"), (10, "Test 10");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (5, "Test 5"), (7, "Test 7"), (9, "Test 9");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (0, "Test 0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test handling of
--echo # INSERT INTO <table> VALUES (<not all fields specified>)
--echo #
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a) VALUES (2), (3), (4);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a) VALUES (6), (8), (10);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a) VALUES (5), (7), (9);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (b) VALUES ("Only b 1");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (b) VALUES ("Only b 2"), ("Only b 3");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t3 ORDER BY a, b;
DROP TABLE t3;

--echo #
--echo # Test of insert pruning with subpartitions
--echo #
--echo # I've placed the varchar column before the int column for better
--echo # distribution by LINEAR KEY.
CREATE TABLE t3
(a int DEFAULT 10,
 b varchar(64) DEFAULT "Default",
 c varchar(64) DEFAULT "Default",
 d int unsigned DEFAULT 9,
 e varchar(255) DEFAULT "Default-filler.filler.filler.",
 PRIMARY KEY (a,b,c,d))
charset latin1
PARTITION BY RANGE COLUMNS (a, b)
SUBPARTITION BY LINEAR KEY (d, c)
SUBPARTITIONS 4
(PARTITION pNeg VALUES LESS THAN (0, ""),
 PARTITION `p0-9` VALUES LESS THAN (9, MAXVALUE),
 PARTITION p10 VALUES LESS THAN (10, MAXVALUE),
 PARTITION `p11-100` VALUES LESS THAN (99, MAXVALUE));
SHOW CREATE TABLE t3;
--echo #
--echo # Test INSERT with
--echo # empty field specifier list and empty value list
--echo #
FLUSH STATUS;
INSERT INTO t3 () VALUES ();

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test INSERT with
--echo # no field specifier list and full value list, including DEFAULT
--echo # specifier
--echo #
FLUSH STATUS;
INSERT IGNORE INTO t3 VALUES (-1, "ZZZzzzz", "yyyYYY", -1, DEFAULT);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test INSERT with
--echo # empty field specifier list and full value list, including NULL
--echo #
FLUSH STATUS;
INSERT INTO t3 () VALUES (0, "", "", 0, NULL);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test INSERT with field specifier list for only some fields
--echo #
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (1, "Part expr fulfilled"),
                             (10, "Part expr fulfilled");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (d) VALUES (1), (2);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (c, d) VALUES ("Subpart expr fulfilled", 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (a, b, d) VALUES (10, "Full part, half subpart", 1),
                                (12, "Full part, half subpart", 1),
                                (12, "Full part, half subpart", 2),
                                (12, "Full part, half subpart", 3),
                                (12, "Full part, half subpart", 4),
                                (12, "Full part, half subpart", 0);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # d = 0 and d = 4 goes to the same subpart!
FLUSH STATUS;
INSERT INTO t3 (a, b, c) VALUES (1, "Full part", "Half subpart");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Adding 'Default' as padding to see if LINEAR KEY uses different parts.
FLUSH STATUS;
INSERT INTO t3 (a, c, d) VALUES (12, "Half part, full subpart", 1),
                                (12, "Half part, full subpartDefault", 1),
                                (12, "Half part, full subpart Default", 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # First and last row goes to the same subpartition.
FLUSH STATUS;
INSERT INTO t3 (b, c, d) VALUES ("Half part", "Full subpart", 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;

--echo #
--echo # Test INSERT with full field specifier list and full value list
--echo #
INSERT INTO t3 (a, b, c, d) VALUES (1, "Full part", "Full subpart", 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test INSERT with no field specifier list and empty value list
--echo # (need to delete previous inserted default row first...)
--echo #
DELETE FROM t3 WHERE a = 10 AND b = 'Default' AND c = 'Default' AND D = 9;
FLUSH STATUS;
INSERT INTO t3 VALUES ();

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Verifing result
--echo #
--sorted_result
SELECT * FROM t3;
--sorted_result
SELECT d, c FROM t3 PARTITION(`p11-100sp0`);
--sorted_result
SELECT d, c FROM t3 PARTITION(`p11-100sp1`);
--sorted_result
SELECT d, c FROM t3 PARTITION(`p11-100sp2`);
--sorted_result
SELECT d, c FROM t3 PARTITION(`p11-100sp3`);

--echo #
--echo # Test with LOCK TABLES
--echo #
--error ER_PARSE_ERROR
LOCK TABLES t3 PARTITION (`p11-100sp0`) WRITE;
FLUSH STATUS;
LOCK TABLES t3 WRITE;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # No further locks/unlocks until UNLOCK TABLES.

--echo #
--echo # Test INSERT with no field specifier list and empty value list
--echo # (need to delete previous inserted default row first...)
--echo #
DELETE FROM t3 WHERE a = 10 AND b = 'Default' AND c = 'Default' AND D = 9;
FLUSH STATUS;
INSERT INTO t3 VALUES ();

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;

--echo #
--echo # Test INSERT with field specifier list for only some fields
--echo # (need to delete previous inserted default row first...)
--echo #
DELETE FROM t3
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
INSERT INTO t3 (b, d, e) VALUES (DEFAULT, DEFAULT, "All default!");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;

--echo #
--echo # Test UPDATE of non PK field in default row
--echo #
UPDATE t3
SET e = CONCAT(e, ", updated")
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;

--echo #
--echo # Test UPDATE of PK field + non PK field in default row
--echo #
UPDATE t3
SET a = DEFAULT, b = "Not DEFAULT!", e = CONCAT(e, ", updated2")
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;

--echo #
--echo # Test REPLACE of default row (INSERT, since not duplicate)
--echo #
REPLACE INTO t3 (e) VALUES ("New default row");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
SELECT * FROM t3
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t3
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;

--echo #
--echo # Test REPLACE of default row (REPLACE, since duplicate exists)
--echo #
REPLACE INTO t3 (e) VALUES ("Newest default row");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test SELECT with explicit partition selection
--echo #
FLUSH STATUS;
--sorted_result
SELECT * FROM t3 PARTITION (p10);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t3 PARTITION (p10);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UNLOCK TABLES;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

DROP TABLE t3;
--echo #
--echo # End of LOCK TABLE test.
--echo #

--let $default_update= 0;
--echo #
--echo # Test INSERT with timestamp column NO default function
--echo #
SELECT UNIX_TIMESTAMP('2011-01-01 00:00:00') as time_t,
       UNIX_TIMESTAMP('2011-01-01 00:00:00') % 3 as part,
       1234567890 % 3 as part2;
SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
CREATE TABLE t3
(a timestamp DEFAULT 0,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY HASH (UNIX_TIMESTAMP(a)) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--echo #
--echo # Test INSERT with timestamp column DEFAULT INSERT + UPDATE
--echo #
--let $default_update= 1;
CREATE TABLE t3
(a timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY HASH (UNIX_TIMESTAMP(a)) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--echo #
--echo # Test INSERT with timestamp column DEFAULT UPDATE
--echo #
--let $default_update= 1;
CREATE TABLE t3
(a timestamp DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY HASH (UNIX_TIMESTAMP(a)) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--echo #
--echo # Test INSERT with timestamp column DEFAULT INSERT
--echo #
--let $default_update= 0;
CREATE TABLE t3
(a timestamp DEFAULT CURRENT_TIMESTAMP,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY HASH (UNIX_TIMESTAMP(a)) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--let $default_update= 0;
--echo #
--echo # Test INSERT with DATETIME column NO default function
--echo #
CREATE TABLE t3
(a DATETIME DEFAULT 0,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY KEY (a) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--echo #
--echo # Test INSERT with DATETIME column DEFAULT INSERT + UPDATE
--echo #
--let $default_update= 1;
CREATE TABLE t3
(a DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY KEY (a) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--echo #
--echo # Test INSERT with DATETIME column DEFAULT UPDATE
--echo #
--let $default_update= 1;
CREATE TABLE t3
(a DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY KEY (a) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;

--echo #
--echo # Test INSERT with DATETIME column DEFAULT INSERT
--echo #
--let $default_update= 0;
CREATE TABLE t3
(a DATETIME DEFAULT CURRENT_TIMESTAMP,
 b char(10),
 PRIMARY KEY (a))
PARTITION BY KEY (a) PARTITIONS 3;
SHOW CREATE TABLE t3;
--source include/partition_default_functions.inc
DROP TABLE t3;
SET sql_mode = default;
--echo #
--echo # Test INSERT SELECT
--echo #
FLUSH STATUS;
TRUNCATE TABLE t2;

connection monitor;
--replace_regex /[0-9]+//
--source include/get_handler_status_counts.inc
connection default;

--echo # All partitions needs to be locked
--echo # 1 commit
SHOW CREATE TABLE t2;
FLUSH STATUS;
INSERT INTO t2 SELECT a, b FROM t1 WHERE a IN (1,4);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # All partitions in t2 needs to be locked (no propagation from t1 yet).
--echo # 2 partitions in t1 needs to be locked (for 1 and 4)
--echo # 2 read_first, read_key and read_next.
--echo # 1 commit

--echo #
--echo # Test TRUNCATE PARTITION
--echo #
FLUSH STATUS;
ALTER TABLE t2 TRUNCATE PARTITION p1;

connection monitor;
--replace_regex /[0-9]+//
--source include/get_handler_status_counts.inc
connection default;

--echo # Lots of lock acquisitions, reads and updated due to data-dictionary
--echo # being updated.
--echo # Warm-up data-dictionary cache.
--disable_query_log
--disable_result_log
SHOW CREATE TABLE t2;
--enable_result_log
--enable_query_log
FLUSH STATUS;
INSERT INTO t2 SELECT a, b FROM t1 WHERE a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test insert on duplicated key update
--echo #
FLUSH STATUS;
INSERT INTO t1 VALUES (65, "No duplicate")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", INSERT_DUP_KEY_UPDATE");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 write (insert)
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (65, "No duplicate")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", INSERT_DUP_KEY_UPDATE");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_key
--echo # 1 update
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (78, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 13, b = CONCAT(b, ", INSERT_DUP_KEY_UPDATE");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If a partitioning column is updated, no pruning
--echo # 1 write (insert)
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (78, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 13, b = CONCAT(b, ", INSERT_DUP_KEY_UPDATE");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 read_key
--echo # 1 update
--echo # 1 commit

--echo #
--echo # Test of insert on duplicate key with failed update
--echo #
FLUSH STATUS;
INSERT INTO t1 VALUES (78, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 13,
                        b = CONCAT(b, ", INSERT_DUP_KEY_UPDATE third");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 commit
FLUSH STATUS;
--error ER_DUP_ENTRY
INSERT INTO t1 VALUES (78, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 13,
                        b = CONCAT(b, ", INSERT_DUP_KEY_UPDATE fail?");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 read_key
--echo # 1 update
--echo # 1 rollback

--echo #
--echo # Test of insert on duplicate key with update to different partition
--echo #
FLUSH STATUS;
INSERT INTO t1 VALUES (104, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 write
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (104, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 delete
--echo # 1 write
--echo # 1 read_key
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (104, "No duplicate 104")
ON DUPLICATE KEY UPDATE a = a + 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 write
--echo # 1 commit

--echo #
--echo # Test of insert on duplicate key with failed update to different
--echo # partition
--echo #
FLUSH STATUS;
--error ER_DUP_ENTRY
INSERT INTO t1 VALUES (104, "No duplicate 104 + 1")
ON DUPLICATE KEY UPDATE a = a + 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # If partitioning column is updated, no pruning
--echo # 1 write
--echo # 1 read_key
--echo # 1 rollback

--echo #
--echo # Test replace
--echo #
FLUSH STATUS;
REPLACE INTO t1 VALUES (5, "REPLACE first");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 write
--echo # 1 commit
FLUSH STATUS;
REPLACE INTO t1 VALUES (5, "REPLACE second");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 write
--echo # 1 read_key
--echo # 1 update (NOTE: write_record() may cheat instead of delete/insert!)
--echo # 1 rollback

--echo #
--echo # Test SELECT
--echo #
FLUSH STATUS;
SELECT * FROM t1 ORDER BY a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 13 read_first
--echo # 13 read_key
--echo # 15 read_next
FLUSH STATUS;
SELECT * FROM t1 WHERE a IN (0, 1, 4, 13, 26) ORDER BY a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 3 read_first, read_key
--echo # 12 read_next
FLUSH STATUS;
--sorted_result
SELECT * FROM t1 WHERE a IN (13, 26, 39, 52);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_first, read_key
--echo # 9 read_next
FLUSH STATUS;
--sorted_result
SELECT * FROM t1 WHERE a = 3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_key
FLUSH STATUS;
SELECT * FROM t1 WHERE b LIKE 'First%' ORDER BY a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 13 read_key
--echo # 5 read_next

--echo #
--echo # Test EXPLAIN SELECT
--echo #
FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE a IN (0, 1, 4, 13, 26) ORDER BY a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE a IN (13, 26, 39, 52);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE a = 3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE b LIKE 'First%' ORDER BY a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test pruning of non static values
--echo # They will need to lock all partitions, but will allow scan pruning
--echo # due to a second pruning call in optimize.
--echo #
CREATE TABLE t3 (a INT);
INSERT INTO t3 VALUES (1);
FLUSH STATUS;
SELECT * FROM t1 WHERE a = (SELECT a FROM t3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_first (NOTE only reads from one partition!)
--echo # 2 read_key
--echo # 2 read_rnd_next
FLUSH STATUS;
SELECT t1.a FROM t1 INNER JOIN t3 ON t1.a = t3.a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_first (NOTE only reads from one partition!)
--echo # 2 read_key
FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT t1.a, t1.b FROM t1 INNER JOIN t3 ON t1.a = t3.a;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE a = (SELECT a FROM t3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
SELECT * FROM t1 WHERE a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_key
FLUSH STATUS;
SELECT * FROM t1 WHERE a = (SELECT COUNT(*) FROM t3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_first
--echo # 2 read_key, read_rnd_next
--echo #
--echo # Test of non indexed partition column
--echo #
CREATE TABLE t4 SELECT a, b FROM t1;
ALTER TABLE t4 PARTITION BY HASH (a) PARTITIONS 5;
SHOW CREATE TABLE t4;
FLUSH STATUS;
SELECT * FROM t4 WHERE a = (SELECT a FROM t3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 2 read_first, read_key
FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t4 WHERE a = (SELECT a FROM t3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

INSERT INTO t3 VALUES (3);
--error ER_SUBQUERY_NO_1_ROW
SELECT * FROM t4 WHERE a = (SELECT a FROM t3);
--error ER_SUBQUERY_NO_1_ROW
EXPLAIN SELECT * FROM t4 WHERE a = (SELECT a FROM t3);
--replace_column 10 #
EXPLAIN SELECT * FROM t4 WHERE a = (SELECT a FROM t3 LIMIT 1);
--replace_column 10 #
EXPLAIN SELECT * FROM t4 WHERE a = (SELECT MAX(a) FROM t3);
DROP TABLE t3;
DROP TABLE t4;


--echo #
--echo # Test derived tables like SELECT * FROM (SELECT * FROM ...)
--echo #
set @optimizer_switch_saved=@@optimizer_switch;
set optimizer_switch='derived_merge=off';

FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 3 read_first, read_key
--echo # 11 read_next
--echo # 6 read_rnd_next (tmp table)
FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 3 read_first, read_key
--echo # 11 read_next
--echo # 12 read_rnd_next (tmp table)


--echo #
--echo # Test EXPLAIN SELECT * FROM (SELECT * FROM ...)
--echo #
FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;



--echo #
--echo # Test SELECT ... UNION SELECT ...
--echo #
FLUSH STATUS;
--sorted_result
SELECT * FROM t1 UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26) UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3 UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26) UNION SELECT * FROM t2) t3) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3 UNION SELECT * FROM t2) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3) t4 UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3 UNION SELECT * FROM t2 WHERE a = 1) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;



--echo #
--echo # Test EXPLAIN SELECT ... UNION SELECT ...
--echo #
FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26) UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3 UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26) UNION SELECT * FROM t2) t3) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3 UNION SELECT * FROM t2) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3) t4 UNION SELECT * FROM t2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
--replace_column 10 #
EXPLAIN SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4, 26)) t3 UNION SELECT * FROM t2 WHERE a = 1) t4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


set @@optimizer_switch=@optimizer_switch_saved;

--echo #
--echo # Test UPDATE
--echo #
SELECT * FROM t1 ORDER BY a;
--echo # This should be prunable (does not change the partitioning key)
FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", updated 1") WHERE a IN (13, 26, 39, 52);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 4 read_key
--echo # 4 update
--echo #
--echo # This should not be prunable (only after implementing 'update pruning')
--echo # i.e if all changed partitioning field is set to constant values,
--echo # set lock_partitions to be a union of read_partition and the matching
--echo # partition for the constants. Easy if all partitioning fields are set,
--echo # probably needs a second round of prune_partitions() with these fields
--echo # set to see if possible to prune locks.
FLUSH STATUS;
UPDATE t1 SET a = 99, b = CONCAT(b, ", updated 2 -> p8") WHERE a = 13;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 2 read_key
--echo # 1 read_rnd
--echo # 1 delete (due to moved to another partition)
--echo # 1 write
--echo #
--echo # This should use ha_update_row instead of ha_write_row + ha_delete_row
FLUSH STATUS;
UPDATE t1 SET a = 13 + 99, b = CONCAT(b, ", updated 3") WHERE a = 99;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 2 read_key
--echo # 1 read_rnd
--echo # 1 update
--echo #
--echo # This should not be prunable (only after implementing
--echo # 'optimized update pruning', which will probably never happen, since
--echo # it depends on which partitioning type is used (for this only hash is
--echo # simple, but range and list is possible, key is very hard)
FLUSH STATUS;
UPDATE t1 SET a = a + 1, b = CONCAT(b, ", updated 4 -> p9") WHERE a = 112;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 2 read_key
--echo # 1 read_rnd
--echo # 1 delete (due to moved to another partition)
--echo # 1 write
FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", same as min(a) + 2 in t2") WHERE a = (SELECT MIN(a) + 2 FROM t2);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", max(a) in t2: ", (SELECT MAX(a) FROM t2)) WHERE a = 5;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test multi table UPDATE
--echo #
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ", t1.b:", t1.b)
WHERE t2.b = t1.b and t2.a = 4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ", t1.b:", t1.b)
WHERE t2.b = t1.b and t2.a = 4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ", t1.b:", t1.b)
WHERE t2.b = t1.b and t2.a = 4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 15 read_key
--echo # 1 read_next, read_rnd
--echo # 2 read_rnd_next
--echo # 2 update

--echo #
--echo # Test of views
--echo #
FLUSH STATUS;
CREATE VIEW v1_25 AS SELECT a, b FROM t1 PARTITION (p2, p5);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 34 locks (dictionary related)
FLUSH STATUS;
CREATE VIEW v1_25_check AS SELECT a, b FROM t1 PARTITION (p2, p5) t1_alias WITH CHECK OPTION;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 34 locks (dictionary related)
FLUSH STATUS;
CREATE VIEW v1_9 AS SELECT a, b FROM t1 WHERE a = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 34 locks (dictionary related)
FLUSH STATUS;
CREATE VIEW v1_9_check AS SELECT a, b FROM t1 WHERE a = 9 WITH CHECK OPTION;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 34 locks (dictionary related)
FLUSH STATUS;
CREATE VIEW v1_all AS SELECT a, b FROM t1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 34 locks (dictionary related)
--sorted_result
SELECT TABLE_NAME, CHECK_OPTION, IS_UPDATABLE, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME LIKE 'v1_%';

SHOW CREATE VIEW v1_all;
FLUSH STATUS;
INSERT INTO v1_all VALUES (23, "Insert in v1_all");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SHOW CREATE VIEW v1_25;
FLUSH STATUS;
INSERT INTO v1_25 VALUES (18, "Insert in v1_25");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
--error ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
INSERT INTO v1_25 VALUES (17, "Insert in v1_25 fail");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT IGNORE INTO v1_25 VALUES (17, "Insert ignore in v1_25");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SHOW CREATE VIEW v1_25_check;
FLUSH STATUS;
INSERT INTO v1_25_check VALUES (31, "Insert in v1_25_check");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
--error ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
INSERT INTO v1_25_check VALUES (30, "Insert in v1_25_check fail");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT IGNORE INTO v1_25_check VALUES (30, "Insert ignore in v1_25_check");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SHOW CREATE VIEW v1_9;
FLUSH STATUS;
INSERT INTO v1_9 VALUES (9, "Insert in v1_9");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO v1_9 VALUES (8, "Insert in v1_9 NO CHECK!");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SELECT * FROM t1 WHERE a = 8;
--echo # DELETE will not find row not in view
SHOW CREATE VIEW v1_9_check;
FLUSH STATUS;
DELETE FROM v1_9_check WHERE a = 8;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 0 locks, impossible where!
--replace_column 10 #
EXPLAIN DELETE FROM v1_9_check WHERE a = 8;
--replace_column 10 #
EXPLAIN SELECT * FROM v1_9_check WHERE a = 8;
SELECT * FROM t1 WHERE a = 8;

FLUSH STATUS;
--error ER_VIEW_CHECK_FAILED
INSERT INTO v1_9_check VALUES (10, "Insert in v1_9_check fail");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SELECT * FROM t1 WHERE a = 9;
FLUSH STATUS;
DELETE FROM v1_9_check WHERE a = 9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO v1_9_check VALUES (9, "Insert in v1_9_check");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
--sorted_result
SELECT * FROM v1_9;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
--sorted_result
SELECT * FROM v1_25;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
--sorted_result
SELECT * FROM v1_all;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

DROP VIEW v1_all;
DROP VIEW v1_9, v1_9_check;
DROP VIEW v1_25, v1_25_check;

# CREATE SELECT result in different values for HANDLER_COMMIT,
# HANDLER_READ_KEY and HANDLER_EXTERNAL_LOCK when run --ps option.
--disable_ps_protocol
--echo #
--echo # Test CREATE SELECT
--echo #
FLUSH STATUS;
CREATE TABLE t3 SELECT a, b FROM t1 WHERE a IN (0, 1, 13, 113, 26);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t3 ORDER BY a;
DROP TABLE t3;
FLUSH STATUS;
CREATE TABLE t3 SELECT a, b FROM t1 WHERE b LIKE 'First%';

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t3 ORDER BY a;
DROP TABLE t3;
--enable_ps_protocol

--echo #
--echo # Test Stored procedures
--echo #
CREATE PROCEDURE sp_insert(a INT, b CHAR(16))
  INSERT INTO test.t1 VALUES (a, b);

delimiter |;
CREATE PROCEDURE sp_insert_partition(p CHAR(16), a INT, b CHAR(16))
BEGIN
  SET @str = CONCAT("INSERT INTO test.t1 PARTITION(", p, ") VALUES (?, ?)");
  SET @x = a, @y = b;
  PREPARE stmt FROM @str;
  EXECUTE stmt USING @x, @y;
  DEALLOCATE PREPARE stmt;
END|
delimiter ;|

CREATE PROCEDURE sp_select_all()
  SELECT * FROM test.t1;

CREATE PROCEDURE sp_select_exact(x INT)
  SELECT * FROM test.t1 WHERE a = x;

delimiter |;
CREATE PROCEDURE sp_select_partition(p CHAR(16))
BEGIN
  SET @str = CONCAT("SELECT * FROM test.t1 PARTITION(", p, ")");
  PREPARE stmt FROM @str;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END|
delimiter ;|

CREATE PROCEDURE sp_select_range(x INT, y INT)
  SELECT * FROM test.t1 WHERE a between x and y;

--echo # Warm-up data-dictionary cache.
--disable_result_log
--disable_query_log
SHOW CREATE PROCEDURE sp_insert;
SHOW CREATE PROCEDURE sp_insert_partition;
SHOW CREATE PROCEDURE sp_select_all;
SHOW CREATE PROCEDURE sp_select_exact;
SHOW CREATE PROCEDURE sp_select_partition;
SHOW CREATE PROCEDURE sp_select_range;
--enable_query_log
--enable_result_log

FLUSH STATUS;
CALL sp_insert(313,"Test313");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
CALL sp_insert_partition("p7", 98, "Test98");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--error ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
CALL sp_insert_partition("p8", 111, "Test111");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # no proc locking since already in proc cache.
FLUSH STATUS;
CALL sp_insert_partition("p7,p8", 111, "Test111");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
CALL sp_select_all();

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
CALL sp_select_exact(98);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
CALL sp_select_partition("p7");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
CALL sp_select_partition("p8");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # no proc locking since already in proc cache.
FLUSH STATUS;
--sorted_result
CALL sp_select_partition("p7,p8");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--sorted_result
CALL sp_select_range(1,5);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

DROP PROCEDURE sp_insert;
DROP PROCEDURE sp_insert_partition;
DROP PROCEDURE sp_select_all;
DROP PROCEDURE sp_select_partition;
DROP PROCEDURE sp_select_range;
DROP PROCEDURE sp_select_exact;

--echo #
--echo # Test EXPLAIN DELETE
--echo #
SELECT * FROM t1 ORDER BY a;
FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE FROM t1 WHERE a = 105;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE FROM t1 WHERE b = "No duplicate";

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE FROM t1 WHERE a = 105;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE FROM t1 WHERE b = "No duplicate";

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo #
--echo # Test DELETE
--echo #
FLUSH STATUS;
DELETE FROM t1 WHERE a = 105;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 1 read_key
--echo # 1 delete
FLUSH STATUS;
DELETE FROM t1 WHERE b = "No duplicate";

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 13 read_key
--echo # 1 read_next (if more matches after the first match)
--echo # 1 delete
FLUSH STATUS;
DELETE FROM t1 WHERE a = (SELECT a + 90 FROM t2 WHERE a = 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 2 read_key
--echo # 2 read_next (if more matches after the first match)
--echo # 1 delete
--replace_column 10 #
EXPLAIN DELETE FROM t1 WHERE a = (SELECT a + 90 FROM t2 WHERE a = 1);
FLUSH STATUS;
DELETE FROM t1 PARTITION (p0)
WHERE a = (SELECT a + 2 FROM t2 WHERE a = 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Impossible delete, all partitions pruned away after locking!
--echo # 1 read_key
--replace_column 10 #
EXPLAIN DELETE FROM t1 PARTITION (p0)
WHERE a = (SELECT a + 2 FROM t2 WHERE a = 1);

--echo #
--echo # Test multi table DELETE
--echo #
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE t1, t2 FROM t1, t2
WHERE t1.a = t2.a AND t1.b = 'First row, p1';

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE FROM t2, t1 USING t2, t1
WHERE t1.b = t2.b AND t2.a = 4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
DELETE t1, t2 FROM t1, t2
WHERE t1.a = t2.a AND t1.b = 'First row, p1';

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 15 read_key
--echo # 2 delete
FLUSH STATUS;
DELETE FROM t2, t1 USING t2, t1
WHERE t1.b = t2.b AND t2.a = 4;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;

--echo #
--echo # Test subquery IN expression
--echo #
FLUSH STATUS;
EXPLAIN SELECT count(*) FROM t1 p
WHERE a IN (1, 2, 9);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
EXPLAIN SELECT count(*) FROM t1 p
WHERE a IN
(SELECT a + 1 FROM t2 WHERE a = 4);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo #
--echo # Test triggers
--echo # Tables used in triggers cannot be pruned for locks.
--echo # Tables with triggers cannot be pruned for locks if
--echo # BEFORE INSERT/UPDATE trigger uses any partitioning columns.
--echo #
CREATE TABLE t3
(old_a int,
 new_a int,
 old_b varchar(255),
 new_b varchar(255),
 key (new_a, new_b),
 key(new_b))
PARTITION BY HASH (new_a) PARTITIONS 5;

CREATE TRIGGER t1_after_insert AFTER INSERT
ON t1 FOR EACH ROW
INSERT INTO t3 VALUES (2, NEW.a, NULL, CONCAT("AI: ", NEW.b));

CREATE TRIGGER t1_after_update AFTER UPDATE
ON t1 FOR EACH ROW
INSERT INTO t3 VALUES (OLD.a, NEW.a, CONCAT("AU: ", OLD.b), CONCAT("AU: ", NEW.b));

SHOW CREATE TABLE t1;
SHOW CREATE TABLE t2;
SHOW CREATE TABLE t3;
FLUSH STATUS;
INSERT INTO t1 VALUES (2, "First row, p2")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key 2");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # (t1 to insert, t3 after insert trigger, t3 after update trigger)
SELECT * FROM t1 WHERE a = 2;

FLUSH STATUS;
REPLACE INTO t1 VALUES (0, "First row, p0 REPLACED");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # (t1 to replace, t3 after insert trigger)
--echo # Note that since there is no delete trigger, REPLACE cheats by
--echo # doing update instead of delete+insert!
SELECT * FROM t1 WHERE a = 0;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1 SET b = CONCAT(b, ", UPDATED2") WHERE a = 3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # (t1 to insert, t3 after update trigger)

CREATE TRIGGER t1_after_delete AFTER DELETE
ON t1 FOR EACH ROW
INSERT INTO t3 VALUES (OLD.a, NULL, CONCAT("AD: ", OLD.b), NULL);

SHOW CREATE TABLE t1;
FLUSH STATUS;
REPLACE INTO t1 VALUES (0, "First row, p0 REPLACED2");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # (t1 to replace, t3 after insert trigger, t3 after delete trigger)
--echo # Note that now it does delete+insert instead, due to delete trigger!
SELECT * FROM t1 WHERE a = 0;

CREATE TRIGGER t1_before_delete BEFORE DELETE
ON t1 FOR EACH ROW
INSERT INTO t3 VALUES (OLD.a, NULL, CONCAT("BD: ", OLD.b), NULL);

SHOW CREATE TABLE t1;
FLUSH STATUS;
REPLACE INTO t1 VALUES (0, "First row, p0 REPLACED3");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # (t1 to replace, t3 after insert trigger, t3 before delete trigger,
--echo #  t3 after delete trigger)
SELECT * FROM t1 WHERE a = 0;

CREATE TRIGGER t1_before_update BEFORE UPDATE
ON t1 FOR EACH ROW
INSERT INTO t3 VALUES (OLD.a, NEW.a, CONCAT("BU: ", OLD.b), CONCAT("BU: ", NEW.b));

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (2, "First row, p2")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key 2");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # No pruning possible, due to BEFORE UPDATE trigger
--echo # t1, t3 after insert, t3 before update, t3 after update
SELECT * FROM t1 WHERE a = 2;

FLUSH STATUS;
REPLACE INTO t1 VALUES (0, "First row, p0 REPLACED4");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # t1, t3 after insert, t3 before delete, t3 after delete
SELECT * FROM t1 WHERE a = 0;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1 SET b = CONCAT(b, ", UPDATED2") WHERE a = 3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # No pruning possible, due to BEFORE UPDATE trigger
--echo # t1, before update, after update
SELECT * FROM t1 WHERE a = 3;

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", UPDATED2") WHERE a = 3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # t1, before update, after update
SELECT * FROM t1 WHERE a = 3;

EXPLAIN INSERT INTO t1 VALUES (12, "First row, p12");

FLUSH STATUS;
INSERT INTO t1 VALUES (12, "First row, p12");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # t1, t3 after insert trigger

CREATE TRIGGER t1_before_insert BEFORE INSERT
ON t1 FOR EACH ROW
INSERT INTO t3 VALUES (1, NEW.a, NULL, CONCAT("BI: ", NEW.b));

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (11, "First row, p11");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Nothing can be pruned, due to triggers.
--echo # t1, t3 before insert, t3 after insert.

FLUSH STATUS;
--replace_column 10 #
EXPLAIN DELETE FROM t1 WHERE a = 98;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # t1, t3 before delete trigger, t3 after delete trigger
--echo # part 7, part 0-4, part 0-4.
SELECT * FROM t1 ORDER BY a;
FLUSH STATUS;
DELETE FROM t1 WHERE a = 98;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # t1, t3 before delete trigger, t3 after delete trigger

SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
--sorted_result
SELECT * FROM t3 ORDER BY new_a;
TRUNCATE TABLE t1;

DROP TRIGGER t1_before_insert;
DROP TRIGGER t1_before_update;
DROP TRIGGER t1_before_delete;
DROP TRIGGER t1_after_insert;
DROP TRIGGER t1_after_update;
DROP TRIGGER t1_after_delete;

--echo #
--echo # Test BEFORE INSERT TRIGGER depending on partitioning column
--echo #
CREATE TRIGGER t1_before_insert BEFORE INSERT
ON t1 FOR EACH ROW
SET NEW.b = CONCAT("b: ", NEW.b, " a: ", NEW.a);

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (0, "first row, p0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO t1 VALUES (0, "Second row, p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET a = 1, b = CONCAT(b, ", a was 0") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Updating partitioning column, no lock pruning

--echo #
--echo # Test BEFORE INSERT TRIGGER not depending on partitioning column
--echo #
DROP TRIGGER t1_before_insert;

CREATE TRIGGER t1_before_insert BEFORE INSERT
ON t1 FOR EACH ROW
SET NEW.b = CONCAT("b: ", NEW.b);

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (0, "first row, p0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO t1 VALUES (0, "Second row, p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET a = 2, b = CONCAT(b, ", a was 0") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Updating partitioning column, no lock pruning

--echo #
--echo # Test BEFORE UPDATE TRIGGER OLD depending on partitioning column.
--echo # Note that it does not update any partitioning column.
--echo #
CREATE TRIGGER t1_before_update BEFORE UPDATE
ON t1 FOR EACH ROW
SET NEW.b = CONCAT("old a: ", OLD.a, " new b: ", NEW.b);

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (0, "1st p0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO t1 VALUES (0, "2nd p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", dup key");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Lock pruning possible!

FLUSH STATUS;
UPDATE t1 SET a = 3, b = CONCAT(b, ", a was 0") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Updating partitioning column, no lock pruning

--echo #
--echo # Test BEFORE UPDATE TRIGGER NEW depending on partitioning column.
--echo # Note that it does not update any partitioning column.
--echo #
DROP TRIGGER t1_before_update;

CREATE TRIGGER t1_before_update BEFORE UPDATE
ON t1 FOR EACH ROW
SET NEW.b = CONCAT("new a: ", NEW.a, " new b: ", NEW.b);

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (0, "1st p0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO t1 VALUES (0, "2nd p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", dup key");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET a = 4, b = CONCAT(b, ", a was 0") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Updating partitioning column, no lock pruning


--echo #
--echo # Test BEFORE UPDATE TRIGGER not depending on partitioning column
--echo #
DROP TRIGGER t1_before_update;

CREATE TRIGGER t1_before_update BEFORE UPDATE
ON t1 FOR EACH ROW
SET NEW.b = CONCAT("new b: ", NEW.b);

SHOW CREATE TABLE t1;
FLUSH STATUS;
INSERT INTO t1 VALUES (0, "1st p0");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
INSERT INTO t1 VALUES (0, "2nd p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", dup key");

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1 SET a = 5, b = CONCAT(b, ", a was 0") WHERE a = 0;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Updating partitioning column, no lock pruning

SELECT * FROM t1 ORDER BY a;

DROP TABLE t1, t2, t3;

--echo #
--echo # Test of BEFORE UPDATE triggers and multi UPDATE
--echo #
CREATE TABLE t1 (a int, b varchar(128), KEY (b))
ENGINE = InnoDB
PARTITION BY HASH (a) PARTITIONS 13;

CREATE TABLE t2 (a int PRIMARY KEY, b varchar(128))
ENGINE = InnoDB
PARTITION BY HASH (a) PARTITIONS 13;

SHOW CREATE TABLE t1;
SHOW CREATE TABLE t2;

INSERT INTO t1 VALUES (1, "MultiUpdate1");
INSERT INTO t1 VALUES (2, "MultiUpdate2");
INSERT INTO t2 VALUES (1, "MultiUpdate1");
INSERT INTO t2 VALUES (2, "MultiUpdate2");

CREATE TRIGGER t1_before_update BEFORE UPDATE
ON t1 FOR EACH ROW
SET NEW.b = CONCAT("new1 b: ", NEW.b);

CREATE TRIGGER t2_before_update BEFORE UPDATE
ON t2 FOR EACH ROW
SET NEW.b = CONCAT("new2 a: ", NEW.a, " new2 b: ", NEW.b);

SHOW CREATE TABLE t1;
SHOW CREATE TABLE t2;
FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ",(1) t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ",(1) t1.b:", t1.b)
WHERE t2.b = t1.b and t1.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ",(1) t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ",(1) t1.b:", t1.b)
WHERE t2.b = t1.b and t1.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ",(1) t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ",(1) t1.b:", t1.b)
WHERE t2.b = t1.b and t1.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # 14 read_first
--echo # 16 read_key
--echo # 2 read_rnd
--echo # 2 update

FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ",(2) t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ",(2) t1.b:", t1.b)
WHERE t1.b = t2.b and t2.a = 2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Trigger touches partitioning column, unable to prune locks

FLUSH STATUS;
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ",(2) t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ",(2) t1.b:", t1.b)
WHERE t1.b = t2.b and t2.a = 2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Trigger touches partitioning column, unable to prune locks

FLUSH STATUS;
UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ",(2) t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ",(2) t1.b:", t1.b)
WHERE t1.b = t2.b and t2.a = 2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # Due to the BEFORE UPDATE trigger on t2 that looks at 'a',
--echo # no locks can be pruned.
--echo # 15 read_key
--echo # 1 read_next, read_rnd
--echo # 2 read_rnd_next
--echo # 2 update

SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
DROP TABLE t1, t2;

--echo #
--echo # Test constant propagation in WHERE clause
--echo # (Currently no propagation is done before locking).
CREATE TABLE t1 (a int, b varchar(128), KEY (b))
ENGINE = InnoDB
PARTITION BY RANGE (a)
(PARTITION pNeg VALUES LESS THAN (0),
 PARTITION p0 VALUES LESS THAN (1),
 PARTITION p1 VALUES LESS THAN (2),
 PARTITION p2 VALUES LESS THAN (3),
 PARTITION p3 VALUES LESS THAN (4),
 PARTITION pMax VALUES LESS THAN MAXVALUE);

CREATE TABLE t2 (a int PRIMARY KEY, b varchar(128))
ENGINE = InnoDB
PARTITION BY RANGE (a)
(PARTITION pNeg VALUES LESS THAN (0),
 PARTITION p0 VALUES LESS THAN (1),
 PARTITION p1 VALUES LESS THAN (2),
 PARTITION p2 VALUES LESS THAN (3),
 PARTITION p3 VALUES LESS THAN (4),
 PARTITION pMax VALUES LESS THAN MAXVALUE);

SHOW CREATE TABLE t1;
SHOW CREATE TABLE t2;

INSERT INTO t1 VALUES (1, "Const1");
INSERT INTO t2 VALUES (1, "Const1");
INSERT INTO t1 VALUES (2, "Const2");
INSERT INTO t2 VALUES (2, "Const2");
INSERT INTO t1 VALUES (3, "Const3");
INSERT INTO t2 VALUES (3, "Const3");

--echo # Test simple '=' propagation
FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t2.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t1.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo # Test OR propagation
FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND (t2.a = 1 OR t2.a = 2);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # But it will be scanned pruned!
--replace_column 10 #
EXPLAIN SELECT * FROM t1, t2
WHERE t1.a = t2.a AND (t1.a = 1 OR t1.a = 2);

--echo # Test closed range propagation
FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t1.a >= 1 AND t1.a <=3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


--echo # But it will be scanned pruned!
--replace_column 10 #
EXPLAIN SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t1.a >= 1 AND t1.a <=3;

--echo # Test open range propagation
FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t2.a >= 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t2.a >= 1;

FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t2.a <= 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t2.a <= 1;

--echo # Test IN propagation
FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a and t2.a IN (1, 3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t1.a IN (1, 3);

--echo # Same for UPDATE
FLUSH STATUS;
UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b)
WHERE t1.a = t2.a and t2.a IN (2, 3);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b)
WHERE t1.a = t2.a and t2.a IN (2, 3);

FLUSH STATUS;
UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ", t1.b:", t1.b)
WHERE t1.a = t2.a and t2.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


FLUSH STATUS;
UPDATE t1, t2
SET t1.b = CONCAT(t1.b, ", t2.b:", t2.b),
    t2.b = CONCAT(t2.b, ", t1.b:", t1.b)
WHERE t1.a = t2.a and t1.a = 2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SELECT * FROM t1 ORDER BY a;
# It is unspecified whether the assignment of t2.b in the UPDATE
# statement above sees the modification made to t1.b on the same row.
# It depends on the plan, and the hypergraph optimizer chooses a
# different plan than the old optimizer.
--skip_if_hypergraph
SELECT * FROM t2 ORDER BY a;

--echo # Same for DELETE
FLUSH STATUS;
DELETE t1 FROM t1, t2
WHERE t1.a = t2.a AND t2.a IN (1, 9);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN DELETE t1 FROM t1, t2
WHERE t1.a = t2.a AND t2.a IN (1, 9);

SELECT * FROM t1 ORDER BY a;

FLUSH STATUS;
DELETE t1 FROM t1, t2
WHERE t1.a = t2.a and t2.a = 2;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
DELETE t1 FROM t1, t2
WHERE t1.a = t2.a and t1.a = 1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SELECT * FROM t1 ORDER BY a;
--skip_if_hypergraph  # Difference caused by non-deterministic UPDATE.
SELECT * FROM t2 ORDER BY a;

FLUSH STATUS;
DELETE t1, t2 FROM t1, t2
WHERE t1.a = t2.a and t2.a = 3;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;


SELECT * FROM t1 ORDER BY a;
--skip_if_hypergraph  # Difference caused by non-deterministic UPDATE.
SELECT * FROM t2 ORDER BY a;
DROP TABLE t1, t2;

--echo #
--echo # DO is not supported by WL#4443 !!!
--echo # Test of DO (eg. SELECT without returning values)
--echo #
CREATE TABLE t1 (a INT, b VARCHAR(66))
PARTITION BY HASH (a) PARTITIONS 3;
INSERT INTO t1 VALUES (1, "One"), (2, "Two"), (3, "Three"), (4, "Four"), (5, "Five"), (6, "Six"), (0, "Zero");
--error ER_SUBQUERY_NO_1_ROW
DO (SELECT a FROM t1);
FLUSH STATUS;
DO (SELECT @x:= b FROM t1 WHERE a = 5);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT @x;
FLUSH STATUS;
DO (SELECT @x:= b FROM t1 WHERE a = 5 or a = 1 ORDER BY b LIMIT 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT @x;

--echo #
--echo # SET is not supported by WL#4443 !!!
--echo # Test of SET (eg. SELECT only setting an internal variable from
--echo # the returning value)
--echo #
FLUSH STATUS;
SET @x = (SELECT a FROM t1 WHERE a = 5);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT @x;
FLUSH STATUS;
SET @y = (SELECT @x:= b FROM t1 WHERE a = 5);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT @x, @y;
FLUSH STATUS;
SET @y = (SELECT @x:= b FROM t1 WHERE a = 5 or a = 1 ORDER BY b LIMIT 1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT @x, @y;

--echo #
--echo # LOAD DATA is not supported by WL#4443 !!!
--echo #
FLUSH STATUS;
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
eval SELECT * FROM t1 WHERE a IN (1, 4)
INTO OUTFILE '$MYSQLTEST_VARDIR/tmp/t1.part1';

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

DELETE FROM t1 WHERE a IN (1, 4);
SELECT * FROM t1 ORDER BY a, b;
FLUSH STATUS;
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
eval LOAD DATA INFILE '$MYSQLTEST_VARDIR/tmp/t1.part1' INTO TABLE t1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t1 ORDER BY a, b;
DELETE FROM t1 WHERE a IN (1, 4);
SELECT * FROM t1 ORDER BY a, b;
--echo # It is possible to avoid locking with explicit partitioning selection!
FLUSH STATUS;
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
eval LOAD DATA INFILE '$MYSQLTEST_VARDIR/tmp/t1.part1' INTO TABLE t1 PARTITION(p1);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t1 ORDER BY a, b;
--remove_file $MYSQLTEST_VARDIR/tmp/t1.part1

DROP TABLE t1;

--echo #
--echo # Test EXCHANGE PARTITION to only lock exchanged partition
--echo #
CREATE TABLE t1 (a INT, b VARCHAR(44));
CREATE TABLE t2 (a INT, b VARCHAR(44))
PARTITION BY HASH (a) PARTITIONS 3;
INSERT INTO t1 VALUES (10, "Ten"), (13, "Thirteen"), (16, "Sixteen");
INSERT INTO t2 VALUES (0, "Zero"), (1, "One"), (2, "Two"),
                      (3, "Three"), (4, "Four"), (5, "Five"),
                      (6, "Six"), (7, "Seven"), (8, "Eight");
FLUSH STATUS;
ALTER TABLE t2 EXCHANGE PARTITION p1 WITH TABLE t1;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
DROP TABLE t1, t2;

--echo #
--echo # Prepared statement
--echo #
CREATE TABLE t1 (N int, M tinyint)
PARTITION BY HASH (N) PARTITIONS 3; 
INSERT INTO t1 VALUES (1,0),(1,0),(2,0),(2,0),(3,0);
PREPARE stmt FROM 'UPDATE t1 AS P1 INNER JOIN (SELECT N FROM t1 GROUP BY N HAVING COUNT(M) > 1) AS P2 ON P1.N = P2.N SET P1.M = 2';
FLUSH STATUS;
EXECUTE stmt;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

SELECT * FROM t1 ORDER BY N, M;
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'SELECT * FROM t1 WHERE N = 2';
FLUSH STATUS;
EXECUTE stmt;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

DROP TABLE t1;

--echo # Check if we can infer from condition on partition fields that 
--echo # no records will match.
CREATE TABLE t1 ( a int NOT NULL) PARTITION BY HASH(a) PARTITIONS 2;
INSERT INTO t1 VALUES (1),(2),(3);
FLUSH STATUS;
--replace_column 10 #
EXPLAIN SELECT * FROM t1 WHERE a=5 AND a=6;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

FLUSH STATUS;
SELECT * FROM t1 WHERE a=5 AND a=6;

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

DROP TABLE t1;

--echo #
--echo # Test of subqueries in INSERT
--echo #
CREATE TABLE t1 (a INT, b VARCHAR(64));
CREATE TABLE t2 (a INT, b VARCHAR(64)) PARTITION BY HASH (a) PARTITIONS 3;
INSERT INTO t1 VALUES (1, "test 1");
--error ER_PARSE_ERROR
INSERT INTO t2 VALUES (SELECT * FROM t1);
SHOW CREATE TABLE t2;
FLUSH STATUS;
INSERT INTO t2 VALUES ((SELECT a FROM t1), (SELECT b FROM t1));

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # I.e. No lock pruning possible
FLUSH STATUS;
INSERT INTO t2 VALUES (1 + (SELECT a FROM t1),
                       CONCAT("subq: ", (SELECT b FROM t1)));

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # I.e. No lock pruning possible
--sorted_result
SELECT * FROM t2;
DROP TABLE t1, t2;
CREATE TABLE t1 (a INT, b INT) PARTITION BY HASH (a) PARTITIONS 3;
CREATE TABLE t2 (a INT, b INT) PARTITION BY HASH (a) PARTITIONS 3;
INSERT INTO t1 VALUES (1, 1), (2, 0), (4, -1), (5, 2), (7, -3), (8, -9),
                      (10, 5), (11, 9);
SHOW CREATE TABLE t2;
FLUSH STATUS;
INSERT INTO t2 VALUES ((SELECT max(a) FROM t1), (SELECT min(a) FROM t1));

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # I.e. No lock pruning possible
FLUSH STATUS;
--replace_column 10 #
EXPLAIN INSERT INTO t2 VALUES ((SELECT max(a) FROM t1),
                                          (SELECT min(a) FROM t1));

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # I.e. No lock pruning possible
FLUSH STATUS;
INSERT INTO t2 VALUES ((SELECT a FROM t1 WHERE a = 1),
                       (SELECT b FROM t1 WHERE a = 2));

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # I.e. No lock pruning possible on insert table
FLUSH STATUS;
--replace_column 10 #
EXPLAIN INSERT INTO t2 VALUES ((SELECT a FROM t1 WHERE a = 1),
                                          (SELECT b FROM t1 WHERE a = 2));

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--echo # I.e. No lock pruning possible on insert table
SELECT * FROM t2 ORDER BY a, b;
DROP TABLE t1;
DROP TABLE t2;

--echo #
--echo # Test of InnoDB INSERT TABLE with non existing table in trigger
--echo #
CREATE TABLE t1 (a INT)
ENGINE = InnoDB;

--echo # Create a table to be used in a trigger on t1
CREATE TABLE t2 (a INT)
ENGINE = InnoDB;
--echo # Create a trigger on t1 which uses t2
delimiter //;
CREATE TRIGGER tr1_1_N  BEFORE INSERT ON t1
FOR EACH ROW BEGIN
  UPDATE t2 SET a = 8 WHERE a > 3 LIMIT 0;
END//
delimiter ;//

--echo # Drop t2 to cause a failure when inserting into t1
DROP TABLE t2;
--error ER_NO_SUCH_TABLE
INSERT INTO t1 VALUES (1);
DROP TABLE t1;

# CREATE SELECT result in different values for HANDLER_COMMIT,
# HANDLER_READ_KEY and HANDLER_EXTERNAL_LOCK when run --ps option.
--disable_ps_protocol
CREATE TABLE t1 (a INT) PARTITION BY HASH (a) PARTITIONS 3;
INSERT INTO t1 VALUES (1), (3), (9), (2), (8), (7);

FLUSH STATUS;
CREATE TABLE t2 SELECT * FROM t1 PARTITION (p1, p2);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--sorted_result
SELECT * FROM t2;
DROP TABLE t2;

FLUSH STATUS;
CREATE TABLE t2 SELECT * FROM t1 WHERE a IN (1, 3, 9);

connection monitor;
--source include/get_handler_status_counts.inc
connection default;

--sorted_result
SELECT * FROM t2;

DROP TABLE t1, t2;
--disable_ps_protocol

--echo #
--echo # Test subqueries/stored functions with UPDATE/DELETE/SELECT
--echo #
# Bug#37560280:
# The EXPLAIN plan for queries with the sf_a_from_t1b_d() function call
# differs from the actual execution plan because constant folding
# is performed in the actual plan but not in EXPLAIN.

CREATE TABLE tq (id int PRIMARY KEY auto_increment, query varchar(255), not_select tinyint);
CREATE TABLE tsq (id int PRIMARY KEY auto_increment, subquery varchar(255), can_be_locked tinyint);
CREATE TABLE t1 (a int, b varchar(255), PRIMARY KEY (a), KEY (b))
ENGINE = InnoDB
PARTITION BY HASH (a) PARTITIONS 3;
CREATE TABLE t2 (a int, b varchar(255), PRIMARY KEY (a), KEY (b))
ENGINE = InnoDB
PARTITION BY HASH (a) PARTITIONS 3;
START TRANSACTION;
INSERT INTO t1 VALUES (1, "1");
INSERT INTO t1 VALUES (2, "2");
INSERT INTO t1 VALUES (8, "8");
INSERT INTO t2 VALUES (1, "1");
INSERT INTO t2 VALUES (2, "2");
INSERT INTO t2 VALUES (8, "8");

CREATE FUNCTION sf_add_hello(s VARCHAR(240))
  RETURNS VARCHAR(246) DETERMINISTIC
  RETURN CONCAT('hello ', s);
CREATE FUNCTION sf_add_1(i INT)
  RETURNS INT DETERMINISTIC
  RETURN i + 1;
delimiter |;
CREATE FUNCTION sf_a_from_t1b_d(s varchar(128))
  RETURNS INT DETERMINISTIC
  BEGIN
  DECLARE i INT;
    SELECT a INTO i FROM t1 where b = s;
    RETURN i;
  END|
CREATE FUNCTION sf_a_from_t1b(s varchar(128))
  RETURNS INT
  BEGIN
  DECLARE i INT;
    SELECT a INTO i FROM t1 where b = s;
    RETURN i;
  END|
delimiter ;|

INSERT INTO tq (query, not_select) VALUES
  ("SELECT * FROM t2", 0),
  ("SELECT sf_add_1(a) - 1, sf_add_hello(b) FROM t2", 0),
  ("UPDATE t2 SET b = CONCAT('+', b)", 1),
  ("UPDATE t2 SET b = sf_add_hello(b)", 1),
  ("UPDATE t2 SET a = sf_add_1(a) + 4", 1),
  ("DELETE FROM t2", 1);
INSERT INTO tsq (subquery, can_be_locked) VALUES
  ("(SELECT a FROM t1 WHERE b = '1')", 1),
  ("7 + (SELECT a FROM t1 WHERE b = '1')", 1),
  ("sf_a_from_t1b('1')", 1),
  ("sf_a_from_t1b_d('1')", 1),
  ("7 + sf_a_from_t1b('1')", 1),
  ("7 + sf_a_from_t1b_d('1')", 1),
  ("sf_a_from_t1b('1') AND a = 2", 1),
  ("sf_a_from_t1b_d('1') AND a = 2", 1),
  ("(SELECT a FROM t1 WHERE b = '1') AND a = 2", 1),
  ("(SELECT a FROM t1 WHERE b = '1') OR a = 2", 1),
  ("(SELECT a FROM t1 WHERE b = '1') AND a = 2 OR a = 8 AND sf_a_from_t1b('2')", 0);

set @old_autocommit= @@autocommit;
let $subnr= 1;
let $qnr= 1;

let $query = `SELECT query FROM tq WHERE id = $qnr`;
let $not_select = `SELECT not_select FROM tq WHERE id = $qnr`;
let $subq = `SELECT subquery FROM tsq WHERE id = $subnr`;
let $can_be_locked = `SELECT can_be_locked FROM tsq WHERE id = $subnr`;
while ($query != '')
{
  while ($subq != '')
  {
    --replace_column 10 #
    eval EXPLAIN $query WHERE a = $subq;
    FLUSH STATUS;
    START TRANSACTION;
    eval $query WHERE a = $subq;

connection monitor;
    --source include/get_handler_status_counts.inc
connection default;

    if ($not_select)
    {
      --sorted_result
      SELECT * FROM t2;
      ROLLBACK;
    }
    if ($can_be_locked)
    {
      FLUSH STATUS;
      # Cannot use START TRANSACTION with LOCK TABLES
      SET autocommit = 0;
      LOCK TABLES t1 read, t2 write;
      eval $query WHERE a = $subq;

connection monitor;
      --source include/get_handler_status_counts.inc
connection default;

      if ($not_select)
      {
        --sorted_result
        SELECT * FROM t2;
        ROLLBACK;
      }
      UNLOCK TABLES;
    }
    inc $subnr;
    let $subq = `SELECT subquery FROM tsq WHERE id = $subnr`;
    let $can_be_locked = `SELECT can_be_locked FROM tsq WHERE id = $subnr`;
  }
  let $subnr= 1;
  let $subq = `SELECT subquery FROM tsq WHERE id = $subnr`;
  let $can_be_locked = `SELECT can_be_locked FROM tsq WHERE id = $subnr`;
  inc $qnr;
  let $query = `SELECT query FROM tq WHERE id = $qnr`;
  let $not_select = `SELECT not_select FROM tq WHERE id = $qnr`;
}

set @@autocommit= @old_autocommit;
DROP FUNCTION sf_add_hello;
DROP FUNCTION sf_add_1;
DROP FUNCTION sf_a_from_t1b_d;
DROP FUNCTION sf_a_from_t1b;

DROP TABLE tq, tsq, t1, t2;

DROP TABLE test.thread_to_monitor;

SET @@global.innodb_stats_persistent= @old_innodb_stats_persistent;