This file is indexed.

/usr/lib/mysql-testsuite/t/ps_ddl.test is in percona-server-test-5.6 5.6.22-rel71.0-0ubuntu4.

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
#
# Testing the behavior of 'PREPARE', 'DDL', 'EXECUTE' scenarios
#
# Background:
# In a statement like "select * from t1", t1 can be:
# - nothing (the table does not exist)
# - a real table
# - a temporary table
# - a view
#
# Changing the nature of "t1" between a PREPARE and an EXECUTE
# can invalidate the internal state of a prepared statement, so that,
# during the execute, the server should:
# - detect state changes and fail to execute a statement,
#   instead of crashing the server or returning wrong results
# - "RE-PREPARE" the statement to restore a valid internal state.
#
# Also, changing the physical structure of "t1", by:
# - changing the definition of t1 itself (DDL on tables, views)
# - changing TRIGGERs associated with a table
# - changing PROCEDURE, FUNCTION referenced by a TRIGGER body,
# - changing PROCEDURE, FUNCTION referenced by a VIEW body,
# impacts the internal structure of a prepared statement, and should
# cause the same verifications at execute time to be performed.
#
# This test provided in this file cover the different state transitions
# between a PREPARE and an EXECUTE, and are organized as follows:
# - Part  1: NOTHING -> TABLE
# - Part  2: NOTHING -> TEMPORARY TABLE
# - Part  3: NOTHING -> VIEW
# - Part  4: TABLE -> NOTHING
# - Part  5: TABLE -> TABLE (DDL)
# - Part  6: TABLE -> TABLE (TRIGGER)
# - Part  7: TABLE -> TABLE (TRIGGER dependencies)
# - Part  8: TABLE -> TEMPORARY TABLE
# - Part  9: TABLE -> VIEW
# - Part 10: TEMPORARY TABLE -> NOTHING
# - Part 11: TEMPORARY TABLE -> TABLE
# - Part 12: TEMPORARY TABLE -> TEMPORARY TABLE (DDL)
# - Part 13: TEMPORARY TABLE -> VIEW
# - Part 14: VIEW -> NOTHING
# - Part 15: VIEW -> TABLE
# - Part 16: VIEW -> TEMPORARY TABLE
# - Part 17: VIEW -> VIEW (DDL)
# - Part 18: VIEW -> VIEW (VIEW dependencies)
# - Part 19: Special tables (INFORMATION_SCHEMA)
# - Part 20: Special tables (log tables)
# - Part 21: Special tables (system tables)
# - Part 22: Special tables (views temp tables)
# - Part 23: Special statements
# - Part 24: Testing the strength of TABLE_SHARE version
--disable_warnings
drop temporary table if exists t1, t2, t3;
drop table if exists t1, t2, t3;
drop procedure if exists p_verify_reprepare_count;
drop procedure if exists p1;
drop function if exists f1;
drop view if exists v1, v2;
--enable_warnings

delimiter |;
create procedure p_verify_reprepare_count(expected int)
begin
  declare old_reprepare_count int default @reprepare_count;

  select variable_value from
  information_schema.session_status where
  variable_name='com_stmt_reprepare'
  into @reprepare_count;

  if old_reprepare_count + expected <> @reprepare_count then
    select concat("Expected: ", expected,
                   ", actual: ", @reprepare_count - old_reprepare_count)
    as "ERROR";
  else
    select '' as "SUCCESS";
  end if;
end|
delimiter ;|
set @reprepare_count= 0;
flush status;

--echo =====================================================================
--echo Part 1: NOTHING -> TABLE transitions
--echo =====================================================================

# can not be tested since prepare failed
--error ER_NO_SUCH_TABLE
prepare stmt from "select * from t1";

--echo =====================================================================
--echo Part 2: NOTHING -> TEMPORARY TABLE transitions
--echo =====================================================================

# can not be tested

--echo =====================================================================
--echo Part 3: NOTHING -> VIEW transitions
--echo =====================================================================

# can not be tested

--echo =====================================================================
--echo Part 4: TABLE -> NOTHING transitions
--echo =====================================================================

--echo # Test 4-a: select ... from <table>
create table t1 (a int);

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
deallocate prepare stmt;

--echo # Test 4-b: TABLE -> NOTHING by renaming the table
create table t1 (a int);
prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);

rename table t1 to t2;
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);

deallocate prepare stmt;
drop table t2;

--echo =====================================================================
--echo Part 5: TABLE -> TABLE (DDL) transitions
--echo =====================================================================

create table t1 (a int);

prepare stmt from "select a from t1";
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);

alter table t1 add column (b int);

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;
deallocate prepare stmt;


--echo =====================================================================
--echo Part 6: TABLE -> TABLE (TRIGGER) transitions
--echo =====================================================================

--echo # Test 6-a: adding a relevant trigger

create table t1 (a int);

prepare stmt from "insert into t1 (a) value (?)";
set @val=1;
execute stmt using @val;
call p_verify_reprepare_count(0);

# Relevant trigger: execute should reprepare
create trigger t1_bi before insert on t1 for each row
  set @message= new.a;

set @val=2;
execute stmt using @val;
call p_verify_reprepare_count(1);
select @message;
set @val=3;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

prepare stmt from "insert into t1 (a) value (?)";
set @val=4;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

--echo # Test 6-b: adding an irrelevant trigger

# Unrelated trigger: reprepare may or may not happen, implementation dependent
create trigger t1_bd before delete on t1 for each row
  set @message= old.a;

set @val=5;
execute stmt using @val;
call p_verify_reprepare_count(1);
select @message;
set @val=6;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

prepare stmt from "insert into t1 (a) value (?)";
set @val=7;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

--echo # Test 6-c: changing a relevant trigger

# Relevant trigger: execute should reprepare
drop trigger t1_bi;
create trigger t1_bi before insert on t1 for each row
  set @message= concat("new trigger: ", new.a);

set @val=8;
execute stmt using @val;
call p_verify_reprepare_count(1);
select @message;
set @val=9;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

prepare stmt from "insert into t1 (a) value (?)";
set @val=10;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

--echo # Test 6-d: changing an irrelevant trigger

# Unrelated trigger: reprepare may or may not happen, implementation dependent
drop trigger t1_bd;

set @val=11;
execute stmt using @val;
call p_verify_reprepare_count(1);
select @message;

--echo Test 6-e: removing a relevant trigger

drop trigger t1_bi;

set @val=12;
execute stmt using @val;
call p_verify_reprepare_count(1);
select @message;
set @val=13;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

prepare stmt from "insert into t1 (a) value (?)";
set @val=14;
execute stmt using @val;
call p_verify_reprepare_count(0);
select @message;

select * from t1 order by a;
drop table t1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 7: TABLE -> TABLE (TRIGGER dependencies) transitions
--echo =====================================================================

--echo # Test 7-a: dependent PROCEDURE has changed
--echo #

create table t1 (a int);
create trigger t1_ai after insert on t1 for each row
  call p1(new.a);
create procedure p1(a int) begin end;
prepare stmt from "insert into t1 (a) values (?)";
set @var= 1;
execute stmt using @var;
drop procedure p1;
create procedure p1 (a int) begin end;
set @var= 2;
execute stmt using @var;
--echo # Cleanup
drop procedure p1;
call p_verify_reprepare_count(1);

--echo # Test 7-b: dependent FUNCTION has changed
--echo #
--echo # Note, this scenario is supported, subject of Bug#12093
--echo #
drop trigger t1_ai;
create trigger t1_ai after insert on t1 for  each row
  select f1(new.a+1) into @var;
create function f1 (a int) returns int return a;
prepare stmt from "insert into t1(a) values (?)";
set @var=3;
execute stmt using @var;
select @var;
drop function f1;
create function f1 (a int) returns int return 0;
execute stmt using @var;
call p_verify_reprepare_count(1);
drop function f1;
deallocate prepare stmt;

--echo # Test 7-c: dependent VIEW has changed
--echo #
--echo #
drop trigger t1_ai;
create table t2 (a int unique);
create table t3 (a int unique);
create view v1 as select a from t2;
create trigger t1_ai after insert on t1 for each row
  insert into v1 (a) values (new.a);

--echo # Demonstrate that the same bug is present
--echo # without prepared statements
insert into t1 (a) values (5);
select * from t2;
select * from t3;
drop view v1;
create view v1 as select a from t3;
insert into t1 (a) values (6);
select * from t2;
select * from t3;

prepare stmt from "insert into t1 (a) values (?)";
set @var=7;
execute stmt using @var;
call p_verify_reprepare_count(0);
select * from t3;
select * from t2;
drop view v1;
create view v1 as select a from t2;
set @var=8;
execute stmt using @var;
call p_verify_reprepare_count(1);
select * from t2;
select * from t3;
drop view v1;
drop table t1,t2,t3;

--echo # Test 7-d: dependent TABLE has changed
create table t1 (a int);
create trigger t1_ai after insert on t1 for each row
  insert into t2 (a) values (new.a);
create table t2 (a int);

prepare stmt from "insert into t1 (a) values (?)";
set @var=1;
execute stmt using @var;
alter table t2 add column comment varchar(255);
set @var=2;
--echo # Since the dependent table is tracked in the prelocked
--echo # list of the prepared statement, invalidation happens
--echo # and the statement is re-prepared. This is an unnecessary
--echo # side effect, since the statement that *is* dependent
--echo # on t2 definition is inside the trigger, and it is currently
--echo # not reprepared (see the previous test case).
execute stmt using @var;
call p_verify_reprepare_count(1);
select * from t1;
select * from t2;
drop table t1,t2;

--echo # Test 7-e: dependent TABLE TRIGGER has changed
create table t1 (a int);
create trigger t1_ai after insert on t1 for each row
  insert into t2 (a) values (new.a);
create table t2 (a int unique);
create trigger t2_ai after insert on t2 for each row
  insert into t3 (a) values (new.a);
create table t3 (a int unique);
create table t4 (a int unique);

insert into t1 (a) values (1);
select * from t1 join t2 on (t1.a=t2.a) join t3 on (t2.a=t3.a);
drop trigger t2_ai;
create trigger t2_ai after insert on t2 for each row
  insert into t4 (a) values (new.a);
insert into t1 (a) values (2);
select * from t1 join t2 on (t1.a=t2.a) join t4 on (t2.a=t4.a);

prepare stmt from "insert into t1 (a) values (?)";
set @var=3;
execute stmt using @var;
select * from t1 join t2 on (t1.a=t2.a) join t4 on (t2.a=t4.a);
drop trigger t2_ai;
create trigger t2_ai after insert on t2 for each row
  insert into t3 (a) values (new.a);
set @var=4;
execute stmt using @var;
call p_verify_reprepare_count(1);
select * from t1 join t2 on (t1.a=t2.a) join t3 on (t2.a=t3.a);
select * from t1 join t2 on (t1.a=t2.a) join t4 on (t2.a=t4.a);

drop table t1, t2, t3, t4;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 8: TABLE -> TEMPORARY TABLE transitions
--echo =====================================================================

--echo # Test 8-a: base table used recreated as temporary table
create table t1 (a int);

prepare stmt from "select * from t1";
execute stmt;

drop table t1;
create temporary table t1 (a int);

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;
deallocate prepare stmt;

--echo # Test 8-b: temporary table has precedence over base table with same name
create table t1 (a int);
prepare stmt from 'select count(*) from t1';
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);

create temporary table t1 AS SELECT 1;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

deallocate prepare stmt;
drop temporary table t1;
drop table t1;


--echo =====================================================================
--echo Part 9: TABLE -> VIEW transitions
--echo =====================================================================

create table t1 (a int);

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;
create table t2 (a int);
create view t1 as select * from t2;

execute stmt;
call p_verify_reprepare_count(1);

drop view t1;
drop table t2;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 10: TEMPORARY TABLE -> NOTHING transitions
--echo =====================================================================

create temporary table t1 (a int);

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

drop temporary table t1;
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
deallocate prepare stmt;

--echo =====================================================================
--echo Part 11: TEMPORARY TABLE -> TABLE transitions
--echo =====================================================================

--echo # Test 11-a: temporary table replaced by base table
create table t1 (a int);
insert into t1 (a) value (1);
create temporary table t1 (a int);

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

drop temporary table t1;

execute stmt;
call p_verify_reprepare_count(1);

select * from t1;
drop table t1;
deallocate prepare stmt;


--echo # Test 11-b: temporary table has precedence over base table with same name
--echo #            temporary table disappears
create table t1 (a int);
create temporary table t1 as select 1 as a;
prepare stmt from "select count(*) from t1";
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);

drop temporary table t1;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

deallocate prepare stmt;
drop table t1;


--echo =====================================================================
--echo Part 12: TEMPORARY TABLE -> TEMPORARY TABLE (DDL) transitions
--echo =====================================================================

create temporary table t1 (a int);

prepare stmt from "select a from t1";
execute stmt;
call p_verify_reprepare_count(0);

drop temporary table t1;
create temporary table t1 (a int, b int);

execute stmt;
call p_verify_reprepare_count(1);

select * from t1;
drop temporary table t1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 13: TEMPORARY TABLE -> VIEW transitions
--echo =====================================================================

create temporary table t1 (a int);
create table t2 (a int);

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

drop temporary table t1;
create view t1 as select * from t2;

execute stmt;
call p_verify_reprepare_count(1);

drop view t1;
drop table t2;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 14: VIEW -> NOTHING transitions
--echo =====================================================================

create table t2 (a int);
create view t1 as select * from t2;

prepare stmt from "select * from t1";
execute stmt;
drop view t1;

--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);

drop table t2;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 15: VIEW -> TABLE transitions
--echo =====================================================================

create table t2 (a int);
create view t1 as select * from t2;

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

drop view t1;
create table t1 (a int);

execute stmt;
call p_verify_reprepare_count(1);

drop table t2;
drop table t1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 16: VIEW -> TEMPORARY TABLE transitions
--echo =====================================================================

--echo #
--echo # Test 1: Merged view
--echo #
create table t2 (a int);
insert into t2 (a) values (1);
create view t1 as select * from t2;

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

create temporary table t1 (a int);
# t1 still refers to the view - no reprepare has been done.
execute stmt;
call p_verify_reprepare_count(0);

drop view t1;
# t1 still refers to the, now deleted, view - no reprepare has been done.
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);

drop table t2;
drop temporary table t1;
deallocate prepare stmt;

--echo #
--echo # Test 2: Materialized view
--echo #
create table t2 (a int);
insert into t2 (a) values (1);
create algorithm = temptable view t1 as select * from t2;

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

create temporary table t1 (a int);
# t1 still refers to the view - no reprepare has been done.
execute stmt;
call p_verify_reprepare_count(0);

drop view t1;
# t1 still refers to the, now deleted, view - no reprepare has been done.
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);

drop table t2;
drop temporary table t1;
deallocate prepare stmt;

--echo #
--echo # Test 3: View referencing an Information schema table
--echo #
create view t1 as select table_name from information_schema.views;

prepare stmt from "select * from t1";
execute stmt;
call p_verify_reprepare_count(0);

create temporary table t1 (a int);
# t1 has been substituted with a reference to the IS table
execute stmt;
call p_verify_reprepare_count(0);

drop view t1;
# Since the IS table has been substituted in, the statement still works
execute stmt;
call p_verify_reprepare_count(0);

drop temporary table t1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 17: VIEW -> VIEW (DDL) transitions
--echo =====================================================================

create table t2 (a int);
insert into t2 values (10), (20), (30);

create view t1 as select a, 2*a as b, 3*a as c from t2;
select * from t1;

prepare stmt from "select * from t1";
execute stmt;

drop view t1;
create view t1 as select a, 2*a as b, 5*a as c from t2;
select * from t1;

--echo # This is actually a test case for Bug#11748352 (36002 Prepared
--echo # statements: if a view used in a statement is replaced, bad data).
execute stmt;
call p_verify_reprepare_count(1);

flush table t2;

execute stmt;
call p_verify_reprepare_count(1);

--echo # Check that we properly handle ALTER VIEW statements.
execute stmt;
call p_verify_reprepare_count(0);
alter view t1 as select a, 3*a as b, 4*a as c from t2;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);
select * from t1;

--echo # Check that DROP & CREATE is properly handled under LOCK TABLES.
drop view t1;
flush tables; # empty TDC
create view t1 as select a, 5*a as b, 6*a as c from t2;
lock tables t1 read, t2 read;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);
unlock tables;
--echo #   ... and once again...
drop view t1;
create view t1 as select a, 6*a as b, 7*a as c from t2;
lock tables t1 read, t2 read;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);
unlock tables;

--echo # Check that ALTER VIEW is properly handled under LOCK TABLES.
alter view t1 as select a, 7*a as b, 8*a as c from t2;
lock tables t1 read, t2 read;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(0);
unlock tables;

drop table t2;
drop view t1;
deallocate prepare stmt;

--echo # Check that DROP & CREATE is properly handled under LOCK TABLES when
--echo # LOCK TABLES does not contain the complete set of views.

create table t1(a int);
insert into t1 values (1), (2), (3);

create view v1 as select a from t1;

lock tables t1 read, v1 read;

prepare stmt from 'select * from v1';

execute stmt;
call p_verify_reprepare_count(0);

execute stmt;
call p_verify_reprepare_count(0);

unlock tables;

drop view v1;
create view v1 as select 2*a from t1;

# Miss v1.
lock tables t1 read;

--error ER_TABLE_NOT_LOCKED
execute stmt;

unlock tables;

drop table t1;
drop view v1;
deallocate prepare stmt;

--echo # Check that ALTER VIEW is properly handled under LOCK TABLES when
--echo # LOCK TABLES does not contain the complete set of views.

create table t1(a int);
insert into t1 values (1), (2), (3);

create view v1 as select a from t1;

lock tables t1 read, v1 read;

prepare stmt from 'select * from v1';

execute stmt;
call p_verify_reprepare_count(0);

execute stmt;
call p_verify_reprepare_count(0);

unlock tables;

alter view v1 as select 2*a from t1;

# Miss v1.
lock tables t1 read;

--error ER_TABLE_NOT_LOCKED
execute stmt;

unlock tables;

drop table t1;
drop view v1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 18: VIEW -> VIEW (VIEW dependencies) transitions
--echo =====================================================================

--echo # Part 18a: dependent function has changed
create table t1 (a int);
insert into t1 (a) values (1), (2), (3);
create function f1() returns int return (select max(a) from t1);
create view v1 as select f1();
prepare stmt from "select * from v1";
execute stmt;
execute stmt;
call p_verify_reprepare_count(0);
drop function f1;
create function f1() returns int return 2;
--echo # XXX: Used to be another manifestation of Bug#12093.
--echo # We only used to get a different error
--echo # message because the non-existing procedure error is masked
--echo # by the view.
execute stmt;
execute stmt;
call p_verify_reprepare_count(1);

--echo # Part 18b: dependent procedure has changed (referred to via a function)

create table t2 (a int);
insert into t2 (a) values (4), (5), (6);

drop function f1;
delimiter |;
create function f1() returns int
begin
  declare x int;
  call p1(x);
  return x;
end|
delimiter ;|
create procedure p1(out x int) select max(a) from t1 into x;

prepare stmt from "select * from v1";
execute stmt;
execute stmt;
call p_verify_reprepare_count(0);
drop procedure p1;
create procedure p1(out x int) select max(a) from t2 into x;
--echo # XXX: used to be a bug. The prelocked list was not invalidated
--echo # and we kept opening table t1, whereas the procedure
--echo # is now referring to table t2
execute stmt;
call p_verify_reprepare_count(1);
flush table t1;
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;

--echo # Test 18-c: dependent VIEW has changed

drop view v1;
create view v2 as select a from t1;
create view v1 as select * from v2;
prepare stmt from "select * from v1";
execute stmt;
execute stmt;
call p_verify_reprepare_count(0);
drop view v2;
create view v2 as select a from t2;
execute stmt;
execute stmt;
call p_verify_reprepare_count(1);
flush table t1;
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
--echo # Test 18-d: dependent TABLE has changed
drop view v2;
create table v2 as select * from t1;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);
drop table v2;
create table v2 (a int unique) as select * from t2;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 18-e: dependent TABLE trigger has changed

prepare stmt from "insert into v1 (a) values (?)";
set @var= 7;
execute stmt using @var;
call p_verify_reprepare_count(0);
create trigger v2_bi before insert on v2 for each row set @message="v2_bi";
set @var=8;
execute stmt using @var;
call p_verify_reprepare_count(1);
select @message;
drop trigger v2_bi;
set @message=null;
set @var=9;
execute stmt using @var;
call p_verify_reprepare_count(1);
select @message;
create trigger v2_bi after insert on v2 for each row set @message="v2_ai";
set @var= 10;
execute stmt using @var;
call p_verify_reprepare_count(1);
select @message;
select * from v1;

--echo # Cleanup

--disable_warnings
drop table if exists t1, t2, v1, v2;
drop view if exists v1, v2;
drop function f1;
drop procedure p1;
--enable_warnings
deallocate prepare stmt;

--echo =====================================================================
--echo Part 19: Special tables (INFORMATION_SCHEMA)
--echo =====================================================================

# Using a temporary table internally should not confuse the prepared
# statement code, and should not raise ER_PS_INVALIDATED errors
prepare stmt from
 "select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE
 from INFORMATION_SCHEMA.ROUTINES where
 routine_name='p1'";

create procedure p1() select "hi there";

execute stmt;
execute stmt;

drop procedure p1;
create procedure p1() select "hi there, again";

execute stmt;
execute stmt;
call p_verify_reprepare_count(0);

drop procedure p1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 20: Special tables (log tables)
--echo =====================================================================

prepare stmt from
 "select * from mysql.general_log where argument='IMPOSSIBLE QUERY STRING'";

--disable_result_log
execute stmt;
execute stmt;
execute stmt;
execute stmt;
--enable_result_log
call p_verify_reprepare_count(0);
deallocate prepare stmt;

--echo =====================================================================
--echo Part 21: Special tables (system tables)
--echo =====================================================================

prepare stmt from
 "select type, db, name from mysql.proc where name='p1'";

create procedure p1() select "hi there";

execute stmt;
execute stmt;

drop procedure p1;
create procedure p1() select "hi there, again";

execute stmt;
execute stmt;
call p_verify_reprepare_count(0);

drop procedure p1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 22: Special tables (views temp tables)
--echo =====================================================================

create table t1 (a int);

create algorithm=temptable view v1 as select a*a as a2 from t1;

--echo # Using a temporary table internally should not confuse the prepared
--echo # statement code, and should not raise ER_PS_INVALIDATED errors
show create view v1;

prepare stmt from "select * from v1";

insert into t1 values (1), (2), (3);
execute stmt;
execute stmt;

insert into t1 values (4), (5), (6);
execute stmt;
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;
drop view v1;

--echo =====================================================================
--echo Part 23: Special statements
--echo =====================================================================

--echo # SQLCOM_ALTER_TABLE:


create table t1 (a int);

prepare stmt from "alter table t1 add column b int";
execute stmt;

drop table t1;
create table t1 (a1 int, a2 int);

--echo # t1 has changed, and it's does not lead to reprepare
execute stmt;

alter table t1 drop column b;
execute stmt;

alter table t1 drop column b;
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;

--echo # SQLCOM_REPAIR:

create table t1 (a int);

insert into t1 values (1), (2), (3);

prepare stmt from "repair table t1";

execute stmt;
execute stmt;

drop table t1;
create table t1 (a1 int, a2 int);
insert into t1 values (1, 10), (2, 20), (3, 30);

--echo # t1 has changed, and it's does not lead to reprepare
execute stmt;

alter table t1 add column b varchar(50) default NULL;
execute stmt;
call p_verify_reprepare_count(0);

alter table t1 drop column b;
execute stmt;
call p_verify_reprepare_count(0);

--echo # SQLCOM_ANALYZE:

prepare stmt from "analyze table t1";
execute stmt;

drop table t1;
create table t1 (a1 int, a2 int);
insert into t1 values (1, 10), (2, 20), (3, 30);
--echo # t1 has changed, and it's not a problem
execute stmt;

alter table t1 add column b varchar(50) default NULL;
execute stmt;

alter table t1 drop column b;
execute stmt;

call p_verify_reprepare_count(0);

--echo # SQLCOM_OPTIMIZE:

prepare stmt from "optimize table t1";
execute stmt;

drop table t1;
create table t1 (a1 int, a2 int);
insert into t1 values (1, 10), (2, 20), (3, 30);

--echo # t1 has changed, and it's not a problem
execute stmt;

alter table t1 add column b varchar(50) default NULL;
execute stmt;

alter table t1 drop column b;
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;

--echo # SQLCOM_SHOW_CREATE_PROC:

prepare stmt from "show create procedure p1";
--error ER_SP_DOES_NOT_EXIST
execute stmt;
--error ER_SP_DOES_NOT_EXIST
execute stmt;

create procedure p1() begin end;

--disable_result_log
execute stmt;
execute stmt;
--enable_result_log

drop procedure p1;
create procedure p1(x int, y int) begin end;

--disable_result_log
execute stmt;
execute stmt;
--enable_result_log

drop procedure p1;

--error ER_SP_DOES_NOT_EXIST
execute stmt;
--error ER_SP_DOES_NOT_EXIST
execute stmt;
call p_verify_reprepare_count(0);

--echo # SQLCOM_SHOW_CREATE_FUNC:

prepare stmt from "show create function f1";
--error ER_SP_DOES_NOT_EXIST
execute stmt;
--error ER_SP_DOES_NOT_EXIST
execute stmt;

create function f1() returns int return 0;

--disable_result_log
execute stmt;
execute stmt;
--enable_result_log

drop function f1;
create function f1(x int, y int) returns int return x+y;

--disable_result_log
execute stmt;
execute stmt;
--enable_result_log

drop function f1;

--error ER_SP_DOES_NOT_EXIST
execute stmt;
--error ER_SP_DOES_NOT_EXIST
execute stmt;
call p_verify_reprepare_count(0);

--echo # SQLCOM_SHOW_CREATE_TRIGGER:

create table t1 (a int);

prepare stmt from "show create trigger t1_bi";
--error ER_TRG_DOES_NOT_EXIST
execute stmt;
--error ER_TRG_DOES_NOT_EXIST
execute stmt;

create trigger t1_bi before insert on t1 for each row set @message= "t1_bi";

--disable_result_log
execute stmt;
execute stmt;
--enable_result_log

drop trigger t1_bi;

create trigger t1_bi before insert on t1 for each row set @message= "t1_bi (2)";

--disable_result_log
execute stmt;
execute stmt;
--enable_result_log

drop trigger t1_bi;

--error ER_TRG_DOES_NOT_EXIST
execute stmt;
--error ER_TRG_DOES_NOT_EXIST
execute stmt;
call p_verify_reprepare_count(0);

drop table t1;
deallocate prepare stmt;

--echo =====================================================================
--echo Part 24: Testing the strength of TABLE_SHARE version
--echo =====================================================================

--echo # Test 24-a: number of columns

create table t1 (a int);

prepare stmt from "select a from t1";
execute stmt;
call p_verify_reprepare_count(0);

alter table t1 add column b varchar(50) default NULL;

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-b: column name

alter table t1 change b c int;
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-c: column type

alter table t1 change a a varchar(10);

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-d: column type length

alter table t1 change a a varchar(20);

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-e: column NULL property

alter table t1 change a a varchar(20) NOT NULL;

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-f: column DEFAULT

alter table t1 change c c int DEFAULT 20;

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-g: number of keys
create unique index t1_a_idx on t1 (a);

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Test 24-h: changing index uniqueness

drop index t1_a_idx on t1;
create index t1_a_idx on t1 (a);

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

--echo # Cleanup
drop table t1;

deallocate prepare stmt;

--echo =====================================================================
--echo Testing reported bugs
--echo =====================================================================

--echo #
--echo # Bug#27420 A combination of PS and view operations cause
--echo # error + assertion on shutdown
--echo #

--disable_warnings
drop table if exists t_27420_100;
drop table if exists t_27420_101;
drop view if exists v_27420;
--enable_warnings

create table t_27420_100(a int);
insert into t_27420_100 values (1), (2);

create table t_27420_101(a int);
insert into t_27420_101 values (1), (2);

create view v_27420 as select t_27420_100.a X, t_27420_101.a Y
  from t_27420_100, t_27420_101
  where t_27420_100.a=t_27420_101.a;

prepare stmt from "select * from v_27420";

execute stmt;
call p_verify_reprepare_count(0);

drop view v_27420;
create table v_27420(X int, Y int);

execute stmt;
call p_verify_reprepare_count(1);

drop table v_27420;
# passes in 5.0, fails in 5.1, should pass
create table v_27420 (a int, b int, filler char(200));

execute stmt;
call p_verify_reprepare_count(1);

drop table t_27420_100;
drop table t_27420_101;
drop table v_27420;
deallocate prepare stmt;

--echo #
--echo # Bug#27430 Crash in subquery code when in PS and table DDL changed
--echo # after PREPARE
--echo #

--disable_warnings
drop table if exists t_27430_1;
drop table if exists t_27430_2;
--enable_warnings

create table t_27430_1 (a int not null, oref int not null, key(a));
insert into t_27430_1 values
  (1, 1),
  (1, 1234),
  (2, 3),
  (2, 1234),
  (3, 1234);

create table t_27430_2 (a int not null, oref int not null);
insert into t_27430_2 values
  (1, 1),
  (2, 2),
  (1234, 3),
  (1234, 4);

prepare stmt from
 "select oref, a, a in (select a from t_27430_1 where oref=t_27430_2.oref) Z from t_27430_2";

execute stmt;
call p_verify_reprepare_count(0);

drop table t_27430_1, t_27430_2;

create table t_27430_1 (a int, oref int, key(a));
insert into t_27430_1 values
  (1, 1),
  (1, NULL),
  (2, 3),
  (2, NULL),
  (3, NULL);

create table t_27430_2 (a int, oref int);
insert into t_27430_2 values
  (1, 1),
  (2,2),
  (NULL, 3),
  (NULL, 4);

execute stmt;
call p_verify_reprepare_count(1);

drop table t_27430_1;
drop table t_27430_2;
deallocate prepare stmt;

--echo #
--echo # Bug#27690 Re-execution of prepared statement after table
--echo # was replaced with a view crashes
--echo #

--disable_warnings
drop table if exists t_27690_1;
drop view if exists v_27690_1;
drop table if exists v_27690_2;
--enable_warnings

create table t_27690_1 (a int, b int);
insert into t_27690_1 values (1,1),(2,2);

create table v_27690_1 as select * from t_27690_1;
create table v_27690_2 as select * from t_27690_1;

prepare stmt from "select * from v_27690_1, v_27690_2";

execute stmt;
execute stmt;

drop table v_27690_1;

--error ER_NO_SUCH_TABLE
execute stmt;

--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);

create view v_27690_1 as select A.a, A.b from t_27690_1 A, t_27690_1 B;

execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);

drop table t_27690_1;
drop view v_27690_1;
drop table v_27690_2;
deallocate prepare stmt;

--echo #=====================================================================
--echo #
--echo # Bug#21294 Executing a prepared statement that executes
--echo # a stored function which was recreat
--echo #

create function f1() returns int return 10;

prepare stmt from "select f1()";
execute stmt;

drop function f1;
create function f1() returns int return 10;

# might pass or fail, implementation dependent
execute stmt;

drop function f1;
create function f1() returns int return 20;

execute stmt;
call p_verify_reprepare_count(2);

drop function f1;
deallocate prepare stmt;

--echo #
--echo # Bug#12093 SP not found on second PS execution if another thread drops
--echo # other SP in between
--echo #
--disable_warnings
drop table if exists t_12093;
drop function if exists f_12093;
drop function if exists f_12093_unrelated;
drop procedure if exists p_12093;
drop view if exists v_12093_unrelated;
--enable_warnings

create table t_12093 (a int);
create function f_12093() returns int return (select count(*) from t_12093);
create procedure p_12093(a int) select * from t_12093;

create function f_12093_unrelated() returns int return 2;
create procedure p_12093_unrelated() begin end;
create view v_12093_unrelated as select * from t_12093;

connect (con1,localhost,root,,);
connection default;

let $my_drop = drop function f_12093_unrelated;
--source include/ps_ddl_1.inc
#
let $my_drop = drop procedure p_12093_unrelated;
--source include/ps_ddl_1.inc
#
# A reprepare of stmt_sf and stmt_sp is necessary because there is no
# information about views within the table definition cache.
let $my_drop = drop view v_12093_unrelated;
--source include/ps_ddl_1.inc

call p_verify_reprepare_count(6);

disconnect con1;
drop table t_12093;
drop function f_12093;
drop procedure p_12093;
deallocate prepare stmt_sf;
deallocate prepare stmt_sp;


--echo =====================================================================
--echo Ensure that metadata validation is performed for every type of
--echo SQL statement where it is needed.
--echo =====================================================================

--echo #
--echo # SQLCOM_SELECT
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "select 1 as res from dual where (1) in (select * from t1)";
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;
call p_verify_reprepare_count(1);

--echo #
--echo # SQLCOM_CREATE_TABLE
--echo #

--disable_warnings
drop table if exists t1;
drop table if exists t2;
--enable_warnings
create table t1 (a int);
prepare stmt from 'create table t2 as select * from t1';
execute stmt;
drop table t2;
execute stmt;
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
# Base table with name of table to be created exists
--error ER_TABLE_EXISTS_ERROR
execute stmt;
call p_verify_reprepare_count(1);
--error ER_TABLE_EXISTS_ERROR
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
# Temporary table with name of table to be created exists
create temporary table t2 (a int);
# Temporary table and base table are not in the same name space.
execute stmt;
call p_verify_reprepare_count(0);
--error ER_TABLE_EXISTS_ERROR
execute stmt;
call p_verify_reprepare_count(1);
drop temporary table t2;
--error ER_TABLE_EXISTS_ERROR
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
# View with name of table to be created exists
# Attention:
#    We cannot print the error message because it contains a random filename.
#    Example: 1050: Table '<some_path>/var/tmp/#sql_6979_0' already exists
#    Therefore we mangle it via
#    "--error ER_TABLE_EXISTS_ERROR,9999" (9999 is currently not used)
#    to "Got one of the listed errors".
create view t2 as select 1;
--error ER_TABLE_EXISTS_ERROR,9999
execute stmt;
call p_verify_reprepare_count(1);
--error ER_TABLE_EXISTS_ERROR,9999
execute stmt;
call p_verify_reprepare_count(0);
drop view t2;
drop table t1;
# Table to be used recreated (drop,create) with different layout
create table t1 (x varchar(20));
execute stmt;
call p_verify_reprepare_count(1);
select * from t2;
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
# Table to be used has a modified (alter table) layout
alter table t1 add column y decimal(10,3);
execute stmt;
call p_verify_reprepare_count(1);
select * from t2;
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
drop table t1;
deallocate prepare stmt;
create table t1 (a int);
insert into t1 (a) values (1);
prepare stmt from "create temporary table if not exists t2 as select * from t1";
execute stmt;
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
execute stmt;
call p_verify_reprepare_count(1);
select * from t2;
execute stmt;
call p_verify_reprepare_count(0);
select * from t2;
drop table t2;
create temporary table t2 (a varchar(10));
execute stmt;
select * from t2;
call p_verify_reprepare_count(1);
drop table t1;
create table t1 (x int);
execute stmt;
call p_verify_reprepare_count(1);
execute stmt;
call p_verify_reprepare_count(0);
drop table t1;
drop temporary table t2;
drop table t2;
deallocate prepare stmt;

create table t1 (a int);
prepare stmt from "create table t2 like t1";
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
# Table to be used does not exist
drop table t1;
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
--error ER_NO_SUCH_TABLE
execute stmt;
call p_verify_reprepare_count(0);
# Table to be used recreated (drop,create) with different layout
create table t1 (x char(17));
execute stmt;
call p_verify_reprepare_count(1);
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
drop table t2;
# Table to be used has a modified (alter table) layout
alter table t1 add column y time;
execute stmt;
call p_verify_reprepare_count(1);
select * from t2;
drop table t2;
execute stmt;
call p_verify_reprepare_count(0);
drop table t1;
drop table t2;
deallocate prepare stmt;


--echo #
--echo # SQLCOM_UPDATE
--echo #

--disable_warnings ONCE
drop table if exists t1, t2;
create table t1 (a int);
create table t2 (a int);
prepare stmt from "update t2 set a=a+1 where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_INSERT
--echo #

--disable_warnings ONCE
drop table if exists t1, t2;
create table t1 (a int);
create table t2 (a int);
prepare stmt from "insert into t2 set a=((1) in (select * from t1))";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;

drop table t1, t2;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_INSERT_SELECT
--echo #

--disable_warnings ONCE
drop table if exists t1, t2;
create table t1 (a int);
create table t2 (a int);
prepare stmt from "insert into t2 select * from t1";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_REPLACE
--echo #

--disable_warnings ONCE
drop table if exists t1, t2;
create table t1 (a int);
create table t2 (a int);
prepare stmt from "replace t2 set a=((1) in (select * from t1))";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_REPLACE_SELECT
--echo #

--disable_warnings ONCE
drop table if exists t1, t2;
create table t1 (a int);
create table t2 (a int);
prepare stmt from "replace t2 select * from t1";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_DELETE
--echo #

--disable_warnings ONCE
drop table if exists t1, t2;
create table t1 (a int);
create table t2 (a int);
prepare stmt from "delete from t2 where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_DELETE_MULTI
--echo #

--disable_warnings ONCE
drop table if exists t1, t2, t3;
create table t1 (a int);
create table t2 (a int);
create table t3 (a int);
prepare stmt from "delete t2, t3 from t2, t3 where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2, t3;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_UPDATE_MULTI
--echo #

--disable_warnings ONCE
drop table if exists t1, t2, t3;
create table t1 (a int);
create table t2 (a int);
create table t3 (a int);
prepare stmt from "update t2, t3 set t3.a=t2.a, t2.a=null where (1) in (select * from t1)";
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1, t2, t3;
deallocate prepare stmt;
--echo # Intermediate results: 8 SQLCOMs tested, 8 automatic reprepares
call p_verify_reprepare_count(8);

--echo #
--echo # SQLCOM_LOAD
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a varchar(20));
--error ER_UNSUPPORTED_PS
prepare stmt from "load data infile '../std_data_ln/words.dat' into table t1";
drop table t1;

--echo #
--echo # SQLCOM_SHOW_DATABASES
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show databases where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_TABLES
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show tables where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_FIELDS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show fields from t1 where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_KEYS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show keys from t1 where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_VARIABLES
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show variables where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_STATUS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show status where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_LOGS,
--echo # SQLCOM_SHOW_ENGINE_MUTEX, SQLCOM_SHOW_PROCESSLIST
--echo #

--echo # Currently can not have a where clause, need to be covered
--echo # with tests

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
--error ER_PARSE_ERROR
prepare stmt from "show engine all status where (1) in (select * from t1)";
--error ER_PARSE_ERROR
prepare stmt from "show engine all logs where (1) in (select * from t1)";
--error ER_PARSE_ERROR
prepare stmt from "show engine all mutex where (1) in (select * from t1)";
--error ER_PARSE_ERROR
prepare stmt from "show processlist where (1) in (select * from t1)";
drop table t1;

--echo #
--echo # SQLCOM_SHOW_CHARSETS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show charset where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_COLLATIONS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show collation where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_TABLE_STATUS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show table status where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_TRIGGERS
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show triggers where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_OPEN_TABLES
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show open tables where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_STATUS_PROC
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show procedure status where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_STATUS_FUNC
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "show function status where (1) in (select * from t1)";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_SHOW_EVENTS
--echo #
--echo #
--echo # Please see this test in ps.test, it requires not_embedded.inc
--echo #

--echo #
--echo # SQLCOM_SET_OPTION
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "set @a=((1) in (select * from t1))";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_DO
--echo #

--disable_warnings ONCE
drop table if exists t1;
create table t1 (a int);
prepare stmt from "do ((1) in (select * from t1))";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_CALL
--echo #

--disable_warnings
drop table if exists t1;
drop procedure if exists p1;
--enable_warnings
create procedure p1(a int) begin end;
create table t1 (a int);
prepare stmt from "call p1((1) in (select * from t1))";
execute stmt;
drop table t1;
create table t1 (x int);
execute stmt;
drop table t1;
drop procedure p1;
deallocate prepare stmt;

--echo #
--echo # SQLCOM_CREATE_VIEW
--echo #

--disable_warnings
drop table if exists t1;
drop view if exists v1;
--enable_warnings
create table t1 (a int);
prepare stmt from "create view v1 as select * from t1";
execute stmt;
drop view v1;
drop table t1;
create table t1 (x int);
execute stmt;
drop view v1;
drop table t1;
deallocate prepare stmt;
--echo # Intermediate result: number of reprepares matches the number
--echo # of tests
call p_verify_reprepare_count(17);

--echo #
--echo # SQLCOM_ALTER_VIEW
--echo #

--disable_warnings ONCE
drop view if exists v1;
create view v1 as select 1;
--error ER_UNSUPPORTED_PS
prepare stmt from "alter view v1 as select 2";
drop view v1;

--echo # Cleanup
--echo #
--disable_warnings
drop temporary table if exists t1, t2, t3;
drop table if exists t1, t2, t3, v1, v2;
drop procedure if exists p_verify_reprepare_count;
drop procedure if exists p1;
drop function if exists f1;
drop view if exists v1, v2;
--enable_warnings


--echo #
--echo # Additional coverage for refactoring which was made as part of work
--echo # on bug '27480: Extend CREATE TEMPORARY TABLES privilege to allow
--echo # temp table operations'. 
--echo #
--echo # Check that we don't try to pre-open temporary tables for the elements
--echo # from prelocking list, as this can lead to unwarranted ER_CANT_REOPEN
--echo # errors.
--disable_warnings ONCE
DROP TABLE IF EXISTS t1, tm;
CREATE TABLE t1 (a INT);
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
  SET @a:= (SELECT COUNT(*) FROM t1);
--echo # Prelocking list for the below statement should
--echo # contain t1 twice - once for the INSERT and once
--echo # SELECT from the trigger.
PREPARE stmt1 FROM 'INSERT INTO t1 VALUES (1)';
EXECUTE stmt1;
--echo # Create temporary table which will shadow t1.
CREATE TEMPORARY TABLE t1 (b int);
--echo # The below execution of statement should not fail with ER_CANT_REOPEN
--echo # error. Instead stmt1 should be auto-matically reprepared and succeed.
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
DROP TEMPORARY TABLE t1;
DROP TABLE t1;
--echo #
--echo # Also check that we properly reset table list elements from UNION
--echo # clause of CREATE TABLE and ALTER TABLE statements.
--echo #
CREATE TEMPORARY TABLE t1 (i INT);
PREPARE stmt2 FROM 'CREATE TEMPORARY TABLE tm (i INT) ENGINE=MERGE UNION=(t1)';
EXECUTE stmt2;
DROP TEMPORARY TABLE tm;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
PREPARE stmt3 FROM 'ALTER TABLE tm UNION=(t1)';
EXECUTE stmt3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
DROP TEMPORARY TABLES tm, t1;