This file is indexed.

/usr/lib/mysql-testsuite/t/partition_locking.test is in mysql-testsuite-5.6 5.6.16-1~exp1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
--source include/have_innodb.inc
--source include/have_partition.inc
# Different page sizes gives different query plans
# results based on default 16k.
--source include/have_innodb_16k.inc

--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

--echo # Must have InnoDB as engine to get the same statistics results.
--echo # embedded uses MyISAM as default. CREATE SELECT uses the default engine.
SET @old_default_storage_engine = @@default_storage_engine;
SET @@default_storage_engine = 'InnoDB';

let $get_handler_status_counts= SELECT * FROM INFORMATION_SCHEMA.SESSION_STATUS
WHERE VARIABLE_NAME LIKE 'HANDLER_%' AND VARIABLE_VALUE > 0;

--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;


--echo #
--echo #
--echo # Test how INSERT prune locks
--echo # First test, no defaults
--echo #
FLUSH STATUS;
INSERT INTO t1 VALUES (1, 'First row, p1');
eval $get_handler_status_counts;
--echo # 4 locks (table + partition lock/unlock)
--echo # 1 commit
FLUSH STATUS;
--error ER_DUP_ENTRY
INSERT INTO t1 VALUES (1, 'First row, duplicate');
eval $get_handler_status_counts;
--echo # 4 locks (table + partition lock/unlock)
--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');
eval $get_handler_status_counts;
--echo # 10 locks (table + 4 partition * lock/unlock)
--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');
eval $get_handler_status_counts;
--echo # 4 locks (table + 1 partition lock/unlock)
--echo # 1 commit

--echo #
--echo # INSERT with auto increment, lock pruning
--echo #
FLUSH STATUS;
INSERT INTO t2 VALUES (NULL, 'First auto-inc row');
eval $get_handler_status_counts;
--echo # Auto increment value is not known until write.
--echo # 28 locks (table + 13 partition lock/unlock)
--echo # 1 commit

FLUSH STATUS;
INSERT INTO t2 (b) VALUES ('Second auto-inc row');
eval $get_handler_status_counts;
--echo # Auto increment value is not known until write.
--echo # 28 locks (table + 13 partition lock/unlock)
--echo # 1 commit

FLUSH STATUS;
INSERT INTO t2 VALUES (10, "First row, p10");
eval $get_handler_status_counts;
--echo # Insert pruning on tables with auto increment is not yet supported
--echo # 28 locks (table + 13 partition lock/unlock)
--echo # 1 commit


--echo #
--echo # UPDATE with auto increment, lock pruning
--echo #
FLUSH STATUS;
UPDATE t2 SET b = CONCAT(b, ", UPDATED") WHERE a = 10;
eval $get_handler_status_counts;
--echo # 4 locks (table + 1 partition lock/unlock)
--echo # 1 read_key + 1 update + 1 commit

--echo #
--echo # Test of pruning with secondary column auto_inc INSERT
--echo #
CREATE TABLE t3 (a INT, b INT AUTO_INCREMENT, PRIMARY KEY (a, b))
ENGINE = MyISAM
PARTITION BY HASH (a) PARTITIONS 5;
FLUSH STATUS;
INSERT INTO t3 VALUES (1, 1);
eval $get_handler_status_counts;
--echo # 4 locks (table + partition lock/unlock)
--echo # No commits, since MyISAM!
FLUSH STATUS;
INSERT INTO t3 VALUES (1, NULL);
eval $get_handler_status_counts;
--echo # 4 locks (table + partition lock/unlock)
SELECT @@sql_mode;
SET @old_sql_mode = @@sql_mode;
SET @@sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
SELECT @@sql_mode;
FLUSH STATUS;
INSERT INTO t3 VALUES (1, 0);
eval $get_handler_status_counts;
--echo # 4 locks (table + partition lock/unlock)
SET @@sql_mode = @old_sql_mode;
SELECT @@sql_mode;
FLUSH STATUS;
INSERT INTO t3 VALUES (1, 0);
eval $get_handler_status_counts;
--echo # 4 locks (table + partition lock/unlock)
SELECT * FROM t3;
DROP TABLE t3;

--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;
FLUSH STATUS;
INSERT INTO t3 VALUES (1, "Test 1");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 VALUES (2, "Test 2"), (3, "Test 3"), (4, "Test 4");
eval $get_handler_status_counts;
--echo # 6 locks (1 table, 2 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 VALUES (6, "Test 6"), (8, "Test 8"), (10, "Test 10");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 VALUES (5, "Test 5"), (7, "Test 7"), (9, "Test 9");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 VALUES (0, "Test 0");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (1, "Test 1");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (2, "Test 2"), (3, "Test 3"), (4, "Test 4");
eval $get_handler_status_counts;
--echo # 6 locks (1 table, 2 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (6, "Test 6"), (8, "Test 8"), (10, "Test 10");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (5, "Test 5"), (7, "Test 7"), (9, "Test 9");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (0, "Test 0");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

--echo #
--echo # Test handling of
--echo # INSERT INTO <table> VALUES (<not all fields specified>)
--echo #
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (1);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (2), (3), (4);
eval $get_handler_status_counts;
--echo # 6 locks (1 table, 2 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (6), (8), (10);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (5), (7), (9);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (b) VALUES ("Only b 1");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (b) VALUES ("Only b 2"), ("Only b 3");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
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))
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));
--echo #
--echo # Test INSERT with
--echo # empty field specifier list and empty value list
--echo #
FLUSH STATUS;
INSERT INTO t3 () VALUES ();
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)

--echo #
--echo # Test INSERT with
--echo # no field specifier list and full value list, including DEFAULT
--echo # specifier
--echo #
FLUSH STATUS;
INSERT INTO t3 VALUES (-1, "ZZZzzzz", "yyyYYY", -1, DEFAULT);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)

--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);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)

--echo #
--echo # Test INSERT with field specifier list for only some fields
--echo #
FLUSH STATUS;
INSERT INTO t3 (a) VALUES (1);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b) VALUES (1, "Part expr fulfilled"),
                             (10, "Part expr fulfilled");
eval $get_handler_status_counts;
--echo # 6 locks (1 table, 2 subpartition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (d) VALUES (1), (2);
eval $get_handler_status_counts;
--echo # 6 locks (1 table, 2 subpartition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (c, d) VALUES ("Subpart expr fulfilled", 1);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)
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);
eval $get_handler_status_counts;
--echo # d = 0 and d = 4 goes to the same subpart!
--echo # 12 locks (1 table, 5 subpartition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (a, b, c) VALUES (1, "Full part", "Half subpart");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)
--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);
eval $get_handler_status_counts;
--echo # First and last row goes to the same subpartition.
--echo # 6 locks (1 table, 2 subpartition lock/unlock)
FLUSH STATUS;
INSERT INTO t3 (b, c, d) VALUES ("Half part", "Full subpart", 1);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)
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);
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)

--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 ();
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 subpartition lock/unlock)

--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;
eval $get_handler_status_counts;
--echo # 17 locks (1 table, 16 partitions lock)
--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 ();
eval $get_handler_status_counts;
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;
eval $get_handler_status_counts;
FLUSH STATUS;
INSERT INTO t3 (b, d, e) VALUES (DEFAULT, DEFAULT, "All default!");
eval $get_handler_status_counts;
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;
eval $get_handler_status_counts;
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;
eval $get_handler_status_counts;
FLUSH STATUS;

--echo #
--echo # Test REPLACE of default row (INSERT, since not duplicate)
--echo #
REPLACE INTO t3 (e) VALUES ("New default row");
eval $get_handler_status_counts;
FLUSH STATUS;
SELECT * FROM t3
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;
eval $get_handler_status_counts;
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t3
WHERE a = 10 AND b = "Default" AND c = "Default" AND d = 9;
eval $get_handler_status_counts;
FLUSH STATUS;

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

--echo #
--echo # Test SELECT with explicit partition selection
--echo #
FLUSH STATUS;
--sorted_result
SELECT * FROM t3 PARTITION (p10);
eval $get_handler_status_counts;
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t3 PARTITION (p10);
eval $get_handler_status_counts;

FLUSH STATUS;
UNLOCK TABLES;
eval $get_handler_status_counts;
--echo # 17 locks (1 table, 16 partitions unlock)
DROP TABLE t3;
--echo #
--echo # End of LOCK TABLE test.
--echo #

# Only TIMESTAMP auto converts NULL to NOW().
--let $null_as_now= 1;
--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;
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;

# Only TIMESTAMP auto converts NULL to NOW().
--let $null_as_now= 0;
--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;

--echo #
--echo # Test INSERT SELECT
--echo #
FLUSH STATUS;
TRUNCATE TABLE t2;
eval $get_handler_status_counts;
--echo # All partitions needs to be locked
--echo # 28 locks (table + 13 partition lock/unlock)
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t2 SELECT a, b FROM t1 WHERE a IN (1,4);
eval $get_handler_status_counts;
--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 # 34 locks (2 table + 15 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
INSERT INTO t2 SELECT a, b FROM t1 WHERE a = 1;
eval $get_handler_status_counts;
--echo # 32 locks (2 table + 13 + 1 partitions)

--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");
eval $get_handler_status_counts;
--echo # 4 lock (1 table + 1 partition lock/unlock)
--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");
eval $get_handler_status_counts;
--echo # 4 lock (1 table + 1 partition lock/unlock)
--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");
eval $get_handler_status_counts;
--echo # If a partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--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");
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--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");
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--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?");
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--echo # 1 write
--echo # 1 commit
FLUSH STATUS;
INSERT INTO t1 VALUES (104, "No duplicate")
ON DUPLICATE KEY UPDATE a = a + 1;
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # If partitioning column is updated, no pruning
--echo # 28 lock (1 table + 13 partition lock/unlock)
--echo # 1 write
--echo # 1 read_key
--echo # 1 rollback

--echo #
--echo # Test replace
--echo #
FLUSH STATUS;
REPLACE INTO t1 VALUES (5, "REPLACE first");
eval $get_handler_status_counts;
--echo # 4 lock (1 table + 1 partition lock/unlock)
--echo # 1 write
--echo # 1 commit
FLUSH STATUS;
REPLACE INTO t1 VALUES (5, "REPLACE second");
eval $get_handler_status_counts;
--echo # 4 lock (1 table + 1 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # 28 locks
--echo # 13 read_first
--echo # 13 read_key
--echo # 15 read_next
FLUSH STATUS;
SELECT * FROM t1 WHERE a IN (0, 1, 4) ORDER BY a;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
--echo # 3 read_first, read_key
--echo # 12 read_next
FLUSH STATUS;
--sorted_result
SELECT * FROM t1 WHERE a IN (13, 26, 39, 52);
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partitions lock/unlock)
--echo # 1 read_first, read_key
--echo # 9 read_next
FLUSH STATUS;
--sorted_result
SELECT * FROM t1 WHERE a = 3;
eval $get_handler_status_counts;
--echo # 4 locks
--echo # 1 read_key
FLUSH STATUS;
SELECT * FROM t1 WHERE b LIKE 'First%' ORDER BY a;
eval $get_handler_status_counts;
--echo # 28 locks
--echo # 13 read_key
--echo # 5 read_next

--echo #
--echo # Test EXPLAIN SELECT
--echo #
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1;
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a IN (0, 1, 4) ORDER BY a;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a IN (13, 26, 39, 52);
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = 3;
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE b LIKE 'First%' ORDER BY a;
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)

--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);
eval $get_handler_status_counts;
--echo # 30 locks (2 table + 13 partitions lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # 30 locks (2 table + 13 partitions lock/unlock)
--echo # 1 read_first (NOTE only reads from one partition!)
--echo # 2 read_key
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT t1.a, t1.b FROM t1 INNER JOIN t3 ON t1.a = t3.a;
eval $get_handler_status_counts;
--echo # 30 locks (2 table + 13 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = (SELECT a FROM t3);
eval $get_handler_status_counts;
--echo # 30 locks (2 table + 13 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM t1 WHERE a = 1;
eval $get_handler_status_counts;
--echo # 4 locks
--echo # 1 read_key
FLUSH STATUS;
SELECT * FROM t1 WHERE a = (SELECT COUNT(*) FROM t3);
eval $get_handler_status_counts;
--echo # 30 locks (2 table + 13 partitions lock/unlock)
--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;
FLUSH STATUS;
SELECT * FROM t4 WHERE a = (SELECT a FROM t3);
eval $get_handler_status_counts;
--echo # 14 locks (2 tables + 5 partitions lock/unlock)
--echo # 2 read_first, read_key
--echo # 6 read_rnd_next
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t4 WHERE a = (SELECT a FROM t3);
eval $get_handler_status_counts;
--echo # 14 locks (2 tables + 5 partitions lock/unlock)
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 PARTITIONS SELECT * FROM t4 WHERE a = (SELECT a FROM t3);
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t4 WHERE a = (SELECT a FROM t3 LIMIT 1);
--replace_column 10 #
EXPLAIN PARTITIONS 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 #
FLUSH STATUS;
SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
--echo # 3 read_first, read_key
--echo # 11 read_next
--echo # 6 read_rnd_next (tmp table)
FLUSH STATUS;
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3) t4;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
--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 PARTITIONS SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0,2,3,13,26)) t3) t4;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)


--echo #
--echo # Test SELECT ... UNION SELECT ...
--echo #
FLUSH STATUS;
--sorted_result
SELECT * FROM t1 UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 56 locks (2 tables + 26 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM t1 WHERE a IN (0, 1, 13, 4) UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3 UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4) UNION SELECT * FROM t2) t3) t4;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3 UNION SELECT * FROM t2) t4;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3) t4 UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3 UNION SELECT * FROM t2 WHERE a = 1) t4;
eval $get_handler_status_counts;
--echo # 12 locks (2 tables, 3 + 1 partitions lock/unlock)


--echo #
--echo # Test EXPLAIN SELECT ... UNION SELECT ...
--echo #
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 56 locks
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a IN (0, 1, 13, 4) UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3 UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4) UNION SELECT * FROM t2) t3) t4;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3 UNION SELECT * FROM t2) t4;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3) t4 UNION SELECT * FROM t2;
eval $get_handler_status_counts;
--echo # 36 locks (2 tables, 13 + 3 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE a IN (0, 1, 13, 4)) t3 UNION SELECT * FROM t2 WHERE a = 1) t4;
eval $get_handler_status_counts;
--echo # 12 locks (2 tables, 3 + 1 partitions lock/unlock)


--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);
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
--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;
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
--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);
eval $get_handler_status_counts;
--echo # 56 locks (2 tables, 13 + 13 partitions lock/unlock)
FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", max(a) in t2: ", (SELECT MAX(a) FROM t2)) WHERE a = 5;
eval $get_handler_status_counts;
--echo # 32 locks (2 tables, 1 + 13 partitions lock/unlock)

--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 PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions lock/unlock)
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;
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions lock/unlock)
--echo # 15 read_key
--echo # 1 read_next, read_rnd
--echo # 2 rean_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);
eval $get_handler_status_counts;
--echo # No locks!
FLUSH STATUS;
CREATE VIEW v1_25_check AS SELECT a, b FROM t1 PARTITION (p2, p5) t1_alias WITH CHECK OPTION;
eval $get_handler_status_counts;
--echo # No locks!
FLUSH STATUS;
CREATE VIEW v1_9 AS SELECT a, b FROM t1 WHERE a = 9;
eval $get_handler_status_counts;
--echo # No locks!
FLUSH STATUS;
CREATE VIEW v1_9_check AS SELECT a, b FROM t1 WHERE a = 9 WITH CHECK OPTION;
eval $get_handler_status_counts;
--echo # No locks!
FLUSH STATUS;
CREATE VIEW v1_all AS SELECT a, b FROM t1;
eval $get_handler_status_counts;
--echo # No locks!
SELECT TABLE_NAME, CHECK_OPTION, IS_UPDATABLE, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME LIKE 'v1_%';

FLUSH STATUS;
INSERT INTO v1_all VALUES (23, "Insert in v1_all");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

FLUSH STATUS;
INSERT INTO v1_25 VALUES (18, "Insert in v1_25");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

FLUSH STATUS;
--error ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
INSERT INTO v1_25 VALUES (17, "Insert in v1_25 fail");
eval $get_handler_status_counts;
--echo # 2 locks (1 table, 0 partition lock/unlock)

FLUSH STATUS;
INSERT IGNORE INTO v1_25 VALUES (17, "Insert ignore in v1_25");
eval $get_handler_status_counts;
--echo # 2 locks (1 table, 0 partition lock/unlock)

FLUSH STATUS;
INSERT INTO v1_25_check VALUES (31, "Insert in v1_25_check");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

FLUSH STATUS;
--error ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
INSERT INTO v1_25_check VALUES (30, "Insert in v1_25_check fail");
eval $get_handler_status_counts;
--echo # 2 locks (1 table, 0 partition lock/unlock)

FLUSH STATUS;
INSERT IGNORE INTO v1_25_check VALUES (30, "Insert ignore in v1_25_check");
eval $get_handler_status_counts;
--echo # 2 locks (1 table, 0 partition lock/unlock)

FLUSH STATUS;
INSERT INTO v1_9 VALUES (9, "Insert in v1_9");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

FLUSH STATUS;
INSERT INTO v1_9 VALUES (8, "Insert in v1_9 NO CHECK!");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

SELECT * FROM t1 WHERE a = 8;
--echo # DELETE will not find row not in view
FLUSH STATUS;
DELETE FROM v1_9_check WHERE a = 8;
eval $get_handler_status_counts;
--echo # 0 locks, impossible where!
--replace_column 10 #
EXPLAIN PARTITIONS DELETE FROM v1_9_check WHERE a = 8;
--replace_column 10 #
EXPLAIN PARTITIONS 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");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

SELECT * FROM t1 WHERE a = 9;
FLUSH STATUS;
DELETE FROM v1_9_check WHERE a = 9;
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

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

FLUSH STATUS;
--sorted_result
SELECT * FROM v1_9;
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)

FLUSH STATUS;
--sorted_result
SELECT * FROM v1_25;
eval $get_handler_status_counts;
--echo # 6 locks (1 table, 2 partition lock/unlock)

FLUSH STATUS;
--sorted_result
SELECT * FROM v1_all;
eval $get_handler_status_counts;
--echo # 28 locks (1 table, 13 partition lock/unlock)
DROP VIEW v1_all;
DROP VIEW v1_9, v1_9_check;
DROP VIEW v1_25, v1_25_check;

--echo #
--echo # Test CREATE SELECT
--echo #
FLUSH STATUS;
CREATE TABLE t3 SELECT a, b FROM t1 WHERE a IN (0, 1, 13, 113);
eval $get_handler_status_counts;
--echo # 10 locks (2 tables, 3 partitions lock/unlock)
SELECT * FROM t3 ORDER BY a;
DROP TABLE t3;
FLUSH STATUS;
CREATE TABLE t3 SELECT a, b FROM t1 WHERE b LIKE 'First%';
eval $get_handler_status_counts;
--echo # 30 locks (2 tables, 13 partitions lock/unlock)
SELECT * FROM t3 ORDER BY a;
DROP TABLE t3;

--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;

FLUSH STATUS;
CALL sp_insert(313,"Test313");
eval $get_handler_status_counts;
--echo # 6 locks (proc table, t1 + 1 partition lock/unlock)
FLUSH STATUS;
CALL sp_insert_partition("p7", 98, "Test98");
eval $get_handler_status_counts;
--echo # 6 locks (proc table, t1 + 1 partition lock/unlock)
FLUSH STATUS;
--error ER_ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET
CALL sp_insert_partition("p8", 111, "Test111");
eval $get_handler_status_counts;
--echo # no proc locking since already in proc cache.
--echo # 2 locks (1 table, no partitions lock/unlock)
FLUSH STATUS;
CALL sp_insert_partition("p7,p8", 111, "Test111");
eval $get_handler_status_counts;
--echo # 4 locks (1 table, 1 partition lock/unlock)
FLUSH STATUS;
--sorted_result
CALL sp_select_all();
eval $get_handler_status_counts;
--echo # 30 locks (procs table, t1 + 13 partitions lock/unlock)
FLUSH STATUS;
--sorted_result
CALL sp_select_exact(98);
eval $get_handler_status_counts;
--echo # 6 locks (proc table, t1 + 1 partition lock/unlock)
FLUSH STATUS;
--sorted_result
CALL sp_select_partition("p7");
eval $get_handler_status_counts;
--echo # 6 locks (proc table, t1 + 1 partition lock/unlock)
FLUSH STATUS;
--sorted_result
CALL sp_select_partition("p8");
eval $get_handler_status_counts;
--echo # no proc locking since already in proc cache.
--echo # 4 locks (t1 + 1 partition lock/unlock)
FLUSH STATUS;
--sorted_result
CALL sp_select_partition("p7,p8");
eval $get_handler_status_counts;
--echo # 6 locks (t1 + 2 partitions lock/unlock)
FLUSH STATUS;
--sorted_result
CALL sp_select_range(1,5);
eval $get_handler_status_counts;
--echo # 14 locks (proc table, t1 + 5 partitions lock/unlock)
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 PARTITIONS DELETE FROM t1 WHERE a = 105;
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS DELETE FROM t1 WHERE b = "No duplicate";
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS DELETE FROM t1 WHERE a = 105;
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS DELETE FROM t1 WHERE b = "No duplicate";
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
--echo #
--echo # Test DELETE
--echo #
FLUSH STATUS;
DELETE FROM t1 WHERE a = 105;
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
--echo # 1 read_key
--echo # 1 delete
FLUSH STATUS;
DELETE FROM t1 WHERE b = "No duplicate";
eval $get_handler_status_counts;
--echo # 28 locks (1 table + 13 partition lock/unlock)
--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);
eval $get_handler_status_counts;
--echo # 32 locks (2 table + 13 + 1 partition lock/unlock)
--echo # 2 read_key
--echo # 2 read_next (if more matches after the first match)
--echo # 1 delete
--replace_column 10 #
EXPLAIN PARTITIONS 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);
eval $get_handler_status_counts;
--echo # Impossible delete, all partitions pruned away after locking!
--echo # 8 locks (2 table + 1 PARTITION(p0) + 1 (a = 1) partition lock/unlock)
--echo # 1 read_key
--replace_column 10 #
EXPLAIN PARTITIONS 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 PARTITIONS DELETE t1, t2 FROM t1, t2
WHERE t1.a = t2.a AND t1.b = 'First row, p1';
eval $get_handler_status_counts;
--echo # 56 locks (2 tables + 26 partitions lock/unlock)
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS DELETE FROM t2, t1 USING t2, t1
WHERE t1.b = t2.b AND t2.a = 4;
eval $get_handler_status_counts;
--echo # 32 locks (2 tables , 13 + 1 partition lock/unlock)

FLUSH STATUS;
DELETE t1, t2 FROM t1, t2
WHERE t1.a = t2.a AND t1.b = 'First row, p1';
eval $get_handler_status_counts;
--echo # 56 locks (2 tables + 26 partitions lock/unlock)
--echo # 15 read_key
--echo # 1 read_next, read_rnd
--echo # 2 delete
FLUSH STATUS;
DELETE FROM t2, t1 USING t2, t1
WHERE t1.b = t2.b AND t2.a = 4;
eval $get_handler_status_counts;
--echo # 32 locks (2 tables , 13 + 1 partition lock/unlock)
--echo # 15 read_key
--echo # 1 read_next, read_rnd
--echo # 2 delete

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

--echo #
--echo # Test subquery IN expression
--echo #
FLUSH STATUS;
EXPLAIN PARTITIONS SELECT count(*) FROM t1 p
WHERE a IN (1, 2, 9);
eval $get_handler_status_counts;
--echo # 8 locks (1 table, 3 partitions, lock/unlock)

FLUSH STATUS;
EXPLAIN PARTITIONS SELECT count(*) FROM t1 p
WHERE a IN
(SELECT a + 1 FROM t2 WHERE a = 4);
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions, lock/unlock)

--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));

FLUSH STATUS;
INSERT INTO t1 VALUES (2, "First row, p2")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key 2");
eval $get_handler_status_counts;
--echo # 28 locks (3 tables, 1 + 5 + 5 partition lock/unlock)
--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");
eval $get_handler_status_counts;
--echo # 16 locks (2 tables, 1 + 5 partition lock/unlock)
--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 PARTITIONS UPDATE t1 SET b = CONCAT(b, ", UPDATED2") WHERE a = 3;
eval $get_handler_status_counts;
--echo # 16 locks (2 tables, 1 + 5 partition lock/unlock)
--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);

FLUSH STATUS;
REPLACE INTO t1 VALUES (0, "First row, p0 REPLACED2");
eval $get_handler_status_counts;
--echo # 28 locks (3 tables, 1 + 5 + 5 partition lock/unlock)
--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);

FLUSH STATUS;
REPLACE INTO t1 VALUES (0, "First row, p0 REPLACED3");
eval $get_handler_status_counts;
--echo # 40 locks (4 tables, 1 + 5 + 5 + 5 partition lock/unlock)
--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));

FLUSH STATUS;
INSERT INTO t1 VALUES (2, "First row, p2")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key 2");
eval $get_handler_status_counts;
--echo # No pruning possible, due to BEFORE UPDATE trigger
--echo # 64 locks (4 tables, 13 + 5 + 5 + 5 partitions lock/unlock)
--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");
eval $get_handler_status_counts;
--echo # 40 locks (4 tables, 1 + 5 + 5 + 5 partition lock/unlock)
--echo # t1, t3 after insert, t3 before delete, t3 after delete
SELECT * FROM t1 WHERE a = 0;

FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS UPDATE t1 SET b = CONCAT(b, ", UPDATED2") WHERE a = 3;
eval $get_handler_status_counts;
--echo # No pruning possible, due to BEFORE UPDATE trigger
--echo # 52 locks (3 tables, 13 + 5 + 5 partition lock/unlock)
--echo # t1, before update, after update
SELECT * FROM t1 WHERE a = 3;

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", UPDATED2") WHERE a = 3;
eval $get_handler_status_counts;
--echo # 52 locks (3 tables, 13 + 5 + 5 partition lock/unlock)
--echo # t1, before update, after update
SELECT * FROM t1 WHERE a = 3;

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

FLUSH STATUS;
INSERT INTO t1 VALUES (12, "First row, p12");
eval $get_handler_status_counts;
--echo # 16 locks (2 table, 1 + 5 partition lock/unlock)
--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));

FLUSH STATUS;
INSERT INTO t1 VALUES (11, "First row, p11");
eval $get_handler_status_counts;
--echo # Nothing can be pruned, due to triggers.
--echo # 52 locks (3 tables, 13 + 5 + 5 partition lock/unlock)
--echo # t1, t3 before insert, t3 after insert.

FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS DELETE FROM t1 WHERE a = 98;
eval $get_handler_status_counts;
--echo # 28 locks (3 tables, 1 + 5 + 5 partitions)
--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;
eval $get_handler_status_counts;
--echo # 28 locks (3 tables, 1 + 5 + 5 partitions)
--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);

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "first row, p0");
eval $get_handler_status_counts;
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "Second row, p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key");
eval $get_handler_status_counts;
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET a = 1, b = CONCAT(b, ", a was 0") WHERE a = 0;
eval $get_handler_status_counts;
--echo # Updating partitioning column, no lock pruning
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

--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);

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "first row, p0");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "Second row, p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", duplicate key");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET a = 2, b = CONCAT(b, ", a was 0") WHERE a = 0;
eval $get_handler_status_counts;
--echo # Updating partitioning column, no lock pruning
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

--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);

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "1st p0");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "2nd p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", dup key");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;
eval $get_handler_status_counts;
--echo # Lock pruning possible!
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET a = 3, b = CONCAT(b, ", a was 0") WHERE a = 0;
eval $get_handler_status_counts;
--echo # Updating partitioning column, no lock pruning
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

--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);

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "1st p0");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "2nd p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", dup key");
eval $get_handler_status_counts;
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;
eval $get_handler_status_counts;
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET a = 4, b = CONCAT(b, ", a was 0") WHERE a = 0;
eval $get_handler_status_counts;
--echo # Updating partitioning column, no lock pruning
--echo # 28 locks (1 tables, 13 partitions lock/unlock)


--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);

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "1st p0");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
INSERT INTO t1 VALUES (0, "2nd p0")
ON DUPLICATE KEY UPDATE b = CONCAT(b, ", dup key");
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET b = CONCAT(b, ", Updated") WHERE a = 0;
eval $get_handler_status_counts;
--echo # 4 locks (1 tables, 1 partitions lock/unlock)

FLUSH STATUS;
UPDATE t1 SET a = 5, b = CONCAT(b, ", a was 0") WHERE a = 0;
eval $get_handler_status_counts;
--echo # Updating partitioning column, no lock pruning
--echo # 28 locks (1 tables, 13 partitions lock/unlock)

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;

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);

FLUSH STATUS;
--replace_column 9 #
EXPLAIN EXTENDED 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;
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions lock/unlock)

FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions lock/unlock)

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;
eval $get_handler_status_counts;
--echo # 32 locks (2 table, 13 + 1 partitions lock/unlock)
--echo # 14 read_first
--echo # 16 read_key
--echo # 2 read_rnd
--echo # 21 rean_rnd_next
--echo # 2 update

FLUSH STATUS;
--replace_column 9 #
EXPLAIN EXTENDED 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;
eval $get_handler_status_counts;
--echo # Trigger touches partitioning column, unable to prune locks
--echo # 56 locks (2 tables + 2 * 13 partitions lock/unlock)

FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # Trigger touches partitioning column, unable to prune locks
--echo # 56 locks (2 tables + 2 * 13 partitions lock/unlock)

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;
eval $get_handler_status_counts;
--echo # Due to the BEFORE UPDATE trigger on t2 that looks at 'a',
--echo # no locks can be pruned.
--echo # 56 locks (2 table, 2 * 13 partitions lock/unlock)
--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);


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;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 6 + 1 partitions lock/unlock)
#--echo # 8 locks (2 tables + 2 partitions lock/unlock)

FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND t1.a = 1;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 1 + 6 partitions lock/unlock)
#--echo # 8 locks (2 tables + 2 partitions lock/unlock)

--echo # Test OR propagation
FLUSH STATUS;
SELECT * FROM t1, t2
WHERE t1.a = t2.a AND (t2.a = 1 OR t2.a = 2);
eval $get_handler_status_counts;
--echo # 20 locks (2 tables, 6 + 2 partitions lock/unlock)
--echo # But it will be scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 22 locks (2 tables, 6 + 3 partitions lock/unlock)

--echo # But it will be scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 24 locks (2 tables, 6 + 4 partitions lock/unlock)
--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 22 locks (2 tables, 6 + 3 partitions lock/unlock)
--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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);
eval $get_handler_status_counts;
--echo # 20 locks (2 tables, 6 + 2 partitions lock/unlock)
--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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);
eval $get_handler_status_counts;
--echo # 20 locks (2 tables, 6 + 2 partitions lock/unlock)
--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 6 + 1 partitions lock/unlock)
#--echo # 8 locks (2 tables, 2 partitions lock/unlock)

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;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 1 + 6 partitions lock/unlock)
#--echo # 8 locks (2 tables, 2 partitions lock/unlock)

SELECT * FROM t1 ORDER BY a;
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);
eval $get_handler_status_counts;
--echo # 20 locks (2 tables, 6 + 2 partitions lock/unlock)
--echo # But is scanned pruned!
--replace_column 10 #
EXPLAIN PARTITIONS 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;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 6 + 1 partitions lock/unlock)
#--echo # 8 locks (2 tables, 2 partitions lock/unlock)
FLUSH STATUS;
DELETE t1 FROM t1, t2
WHERE t1.a = t2.a and t1.a = 1;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 1 + 6 partitions lock/unlock)
#--echo # 8 locks (2 tables, 2 partitions lock/unlock)

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

FLUSH STATUS;
DELETE t1, t2 FROM t1, t2
WHERE t1.a = t2.a and t2.a = 3;
eval $get_handler_status_counts;
--echo # 18 locks (2 tables, 6 + 1 partitions lock/unlock)
#--echo # 8 locks (2 tables, 2 partitions lock/unlock)

SELECT * FROM t1 ORDER BY a;
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");
DO (SELECT a FROM t1);
FLUSH STATUS;
DO (SELECT @x:= b FROM t1 WHERE a = 5);
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
SELECT @x;
FLUSH STATUS;
DO (SELECT @x:= b FROM t1 WHERE a = 5 or a = 1 ORDER BY b LIMIT 1);
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
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);
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
SELECT @x;
FLUSH STATUS;
SET @y = (SELECT @x:= b FROM t1 WHERE a = 5);
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
SELECT @x, @y;
FLUSH STATUS;
SET @y = (SELECT @x:= b FROM t1 WHERE a = 5 or a = 1 ORDER BY b LIMIT 1);
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
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';
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partitions lock/unlock)
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;
eval $get_handler_status_counts;
--echo # 8 locks (1 table + 3 partitions lock/unlock)
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);
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partitions lock/unlock)
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;
eval $get_handler_status_counts;
--echo # 6 locks (2 table + 1 partition lock/unlock)
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;
eval $get_handler_status_counts;
--echo # 16 locks (2 table + 6 partition lock/unlock)
SELECT * FROM t1 ORDER BY N, M;
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'SELECT * FROM t1 WHERE N = 2';
FLUSH STATUS;
EXECUTE stmt;
eval $get_handler_status_counts;
--echo # 4 locks (1 table + 1 partition lock/unlock)
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 PARTITIONS SELECT * FROM t1 WHERE a=5 AND a=6;
eval $get_handler_status_counts;
--echo # 2 locks (1 table + 0 partition lock/unlock)
FLUSH STATUS;
SELECT * FROM t1 WHERE a=5 AND a=6;
eval $get_handler_status_counts;
--echo # 2 locks (1 table + 0 partition lock/unlock)
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);
FLUSH STATUS;
INSERT INTO t2 VALUES ((SELECT a FROM t1), (SELECT b FROM t1));
eval $get_handler_status_counts;
--echo # 12 locks (3 tables + 3 partitions) x lock/unlock
--echo # I.e. No lock pruning possible
FLUSH STATUS;
INSERT INTO t2 VALUES (1 + (SELECT a FROM t1),
                       CONCAT("subq: ", (SELECT b FROM t1)));
eval $get_handler_status_counts;
--echo # 12 locks (3 tables + 3 partitions) x lock/unlock
--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);
FLUSH STATUS;
INSERT INTO t2 VALUES ((SELECT max(a) FROM t1), (SELECT min(a) FROM t1));
eval $get_handler_status_counts;
--echo # 24 locks (3 tables + 3 x 3 partitions) x lock/unlock
--echo # I.e. No lock pruning possible
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS INSERT INTO t2 VALUES ((SELECT max(a) FROM t1),
                                          (SELECT min(a) FROM t1));
eval $get_handler_status_counts;
--echo # 24 locks (3 tables + 3 x 3 partitions) x lock/unlock
--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));
eval $get_handler_status_counts;
--echo # 16 locks (3 tables + 3 + 1 + 1 partitions) x lock/unlock
--echo # I.e. No lock pruning possible on insert table
FLUSH STATUS;
--replace_column 10 #
EXPLAIN PARTITIONS INSERT INTO t2 VALUES ((SELECT a FROM t1 WHERE a = 1),
                                          (SELECT b FROM t1 WHERE a = 2));
eval $get_handler_status_counts;
--echo # 16 locks (3 tables + 3 + 1 + 1 partitions) x lock/unlock
--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 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);
eval $get_handler_status_counts;
--echo # 8 locks (2 tables, 2 partitions, lock/unlock)
--sorted_result
SELECT * FROM t2;
DROP TABLE t2;

FLUSH STATUS;
CREATE TABLE t2 SELECT * FROM t1 WHERE a IN (1, 3, 9);
eval $get_handler_status_counts;
--echo # 8 locks (2 tables, 2 partitions, lock/unlock)
--sorted_result
SELECT * FROM t2;

DROP TABLE t1, t2;

--echo #
--echo # Test subqueries/stored functions with UPDATE/DELETE/SELECT
--echo #
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 PARTITIONS $query WHERE a = $subq;
    FLUSH STATUS;
    START TRANSACTION;
    eval $query WHERE a = $subq;
    eval $get_handler_status_counts;
    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;
      eval $get_handler_status_counts;
      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;

SET @@global.innodb_stats_persistent= @old_innodb_stats_persistent;