summaryrefslogtreecommitdiffstats
path: root/mysql-test/suite/versioning/t/partition.test
blob: 5707746f96c3e2c63c2b3204641c8c97471c8c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
-- source include/have_partition.inc
-- source suite/versioning/common.inc
-- source suite/versioning/engines.inc
-- source include/have_sequence.inc

set @save_persistent=@@global.innodb_stats_persistent;
set global innodb_stats_persistent= 0;

call mtr.add_suppression("need more HISTORY partitions");

--enable_prepare_warnings

set system_versioning_alter_history=keep;
--let $datadir= `select @@datadir`
--echo # Check conventional partitioning on temporal tables

--replace_result $sys_datatype_expl SYS_DATATYPE
eval create or replace table t1 (
    x int,
    row_start $sys_datatype_expl as row start invisible,
    row_end $sys_datatype_expl as row end invisible,
    period for system_time(row_start, row_end))
with system versioning
partition by range columns (x) (
    partition p0 values less than (100),
    partition p1 values less than (1000));

insert into t1 values (3), (300);
select * from t1;
select * from t1 partition (p0);
select * from t1 partition (p1);

delete from t1;
select * from t1;
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 for system_time all;
select * from t1 partition (p0) for system_time all;
select * from t1 partition (p1) for system_time all;

--echo # Engine change native <-> non-native versioning prohibited
--replace_result $sys_datatype_expl SYS_DATATYPE $default_engine DEFAULT_ENGINE
eval create or replace table t1 (
    i int,
    row_start $sys_datatype_expl as row start invisible,
    row_end $sys_datatype_expl as row end invisible,
    period for system_time(row_start, row_end))
engine=$default_engine
with system versioning partition by hash(i);
--replace_result $non_default_engine NON_DEFAULT_ENGINE
--error ER_VERS_ALTER_ENGINE_PROHIBITED
eval alter table t1 engine=$non_default_engine;


--echo ## CREATE TABLE

--error ER_VERS_NOT_VERSIONED
create or replace table t1 (x int)
partition by system_time (
    partition p0 history,
    partition pn current);

create or replace table t1 (x int);
--error ER_VERS_NOT_VERSIONED
alter table t1
partition by system_time (
    partition p0 history,
    partition pn current);

--error ER_VERS_WRONG_PARTS
create or replace table t1 (x int)
with system versioning
partition by system_time (
    partition p0 current);

--error ER_VERS_WRONG_PARTS
create or replace table t1 (x int)
with system versioning
partition by system_time (
    partition p0 current,
    partition p1 current);

--error ER_VERS_WRONG_PARTS
create or replace table t1 (x int)
with system versioning
partition by system_time (
    partition p0 history,
    partition p1 history);

--error ER_VERS_WRONG_PARTS
create or replace table t1 (x int)
with system versioning
partition by system_time (
    partition pn current,
    partition p0 history);

--error ER_VERS_WRONG_PARTS
create or replace table t1 (x int)
with system versioning
partition by system_time (
    partition p0,
    partition pn current);

create or replace table t1 (x int)
with system versioning
partition by system_time (
    partition p0 history,
    partition pn current);

--error ER_PARTITION_WRONG_TYPE
create or replace table t1 (a int)
partition by range (a) (
  partition p0 history,
  partition p1 current);

--error ER_PARTITION_WRONG_TYPE
create or replace table t1 (b int)
partition by range (a) (
  partition p0 current,
  partition p1 history);


--echo ## ALTER TABLE

--error ER_VERS_WRONG_PARTS
alter table t1 add partition (
    partition p1 current);

alter table t1 add partition (
    partition p1 history);

--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

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

--error ER_VERS_WRONG_PARTS
alter table t1 drop partition pn;
alter table t1 drop partition p1;
--error ER_VERS_WRONG_PARTS
alter table t1 drop partition p0;

select x from t1;

--echo # rename works
create or replace table t1 (x int) with system versioning
partition by system_time;
alter table t1 reorganize partition p0 into
(partition custom_name history);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # merge and split doesn't (MDEV-19938)
create or replace table t1 (x int) with system versioning
partition by system_time limit 10 partitions 3;
--error ER_REORG_HASH_ONLY_ON_SAME_NO
alter table t1 reorganize partition p0, p1 into (partition p00 history);
--error ER_REORG_HASH_ONLY_ON_SAME_NO
alter table t1 reorganize partition p1 into (partition p1 history, partition p2 history);


--echo # Bug tempesta-tech/mariadb#260: incorrect IB partitioning warning
create or replace table t1 (x int)
with system versioning
partition by system_time limit 1;
alter table t1 change x big int;

create or replace table t1 (i int) engine myisam partition by hash(i) partitions 2;
--error ER_PARTITION_WRONG_TYPE
alter table t1 add partition (partition px history);


--echo ## INSERT, UPDATE, DELETE
create or replace table t1 (x int)
with system versioning
partition by system_time;

set @now= now(6);
insert into t1 values (1);
set @str= concat('select x, row_start < @now as A, row_end > @now as B from t1 partition (p0)');
prepare select_p0 from @str;
set @str= concat('select x, row_start > @now as C, row_end = timestamp\'2038-01-19 03:14:07.999999\' as D from t1 partition (pn)');
prepare select_pn from @str;

execute select_p0;
execute select_pn;

set @str= concat('select row_start from t1 partition (pn) into @ts0');
prepare stmt from @str; execute stmt; drop prepare stmt;

--source suite/versioning/wait_system_clock.inc

set @now= now(6);
delete from t1;
execute select_p0;
execute select_pn;

set @str= concat('select row_start from t1 partition (p0) into @ts1');
prepare stmt from @str; execute stmt; drop prepare stmt;

select @ts0 = @ts1;

set @now= now(6);
insert into t1 values (2);

--source suite/versioning/wait_system_clock.inc

execute select_p0;
execute select_pn;

set @str= concat('select row_start from t1 partition (pn) into @ts0');
prepare stmt from @str; execute stmt; drop prepare stmt;

set @now= now(6);
update t1 set x = x + 1;

--source suite/versioning/wait_system_clock.inc

execute select_p0;
execute select_pn;

drop prepare select_p0;
drop prepare select_pn;

set @str= concat('select row_start from t1 partition (p0) where x = 2 into @ts1');
prepare stmt from @str; execute stmt; drop prepare stmt;
set @str= concat('select row_end from t1 partition (p0) where x = 2 into @ts2');
prepare stmt from @str; execute stmt; drop prepare stmt;
set @str= concat('select row_start from t1 partition (pn) into @ts3');
prepare stmt from @str; execute stmt; drop prepare stmt;

select @ts0 = @ts1;
select @ts2 = @ts3;

--echo #
--echo # Rotation by LIMIT
--echo #
--error ER_PART_WRONG_VALUE
create or replace table t1 (x int)
with system versioning
partition by system_time limit 0 partitions 3;

create or replace table t1 (x int)
with system versioning
partition by system_time limit 2 partitions 3;

--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--error ER_PARTITION_DOES_NOT_EXIST
alter table t1 drop partition non_existent;

insert into t1 values (1), (2), (3), (4), (5), (6);
select * from t1 partition (pn);
delete from t1 where x < 4;
delete from t1;
--echo # You see warning above ^
select * from t1 partition (p0);
select * from t1 partition (p1);

insert into t1 values (7), (8);
--echo ### warn about full partition
delete from t1;
--echo # You see warning above ^
select * from t1 partition (p1) order by x;

--echo #
--echo # Rotation by INTERVAL
--echo #
--error ER_PART_WRONG_VALUE
create or replace table t1 (x int)
with system versioning
partition by system_time interval 0 second partitions 3;

--error ER_PARSE_ERROR
create table t1 (i int) with system versioning
partition by system_time interval 6 day limit 98;

--error ER_DATA_OUT_OF_RANGE
create or replace table t1 (pk int) with system versioning
partition by system_time interval 10 year partitions 3;

--echo # INTERVAL and ALTER TABLE
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 hour;

set @ts=(select partition_description from information_schema.partitions
  where table_schema='test' and table_name='t1' and partition_name='p0');

alter table t1 add column b int;
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
alter table t1 add partition (partition p1 history, partition p2 history);
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
alter table t1 drop partition p0;
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
--error ER_VERS_DROP_PARTITION_INTERVAL
alter table t1 drop partition p2;
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';

#
# partition rotation (moved from partition_rotation.test)
#
set timestamp=unix_timestamp('2001-02-03 10:20:30');
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day
    subpartition by key (i) subpartitions 2
    (partition p1 history, partition pn current);
set timestamp=unix_timestamp('2001-02-03 10:20:40');
insert t1 values (1); delete from t1;
set timestamp=unix_timestamp('2001-02-04 10:20:50');
insert t1 values (2); delete from t1;

select subpartition_name, partition_description, table_rows from information_schema.partitions where table_schema='test' and table_name='t1';
select * from t1 partition (p1);

set timestamp=unix_timestamp('2001-02-04 10:20:55');
alter table t1 add partition (partition p0 history, partition p2 history);
set timestamp=unix_timestamp('2001-02-04 10:30:00');
insert t1 values (4),(5);
set timestamp=unix_timestamp('2001-02-04 10:30:10');
update t1 set i=6 where i=5;

select subpartition_name, partition_description, table_rows from information_schema.partitions where table_schema='test' and table_name='t1';
select * from t1 partition (p1);
select * from t1 partition (p0);
select * from t1 partition (p2);

alter table t1 rebuild partition p0, p1, p2;
select * from t1 partition (p1);
select * from t1 partition (p0);
select * from t1 partition (p2);

--echo ## pruning check
set @ts=(select partition_description from information_schema.partitions
  where table_schema='test' and table_name='t1' and partition_name='p0' limit 1);
--sorted_result
select * from t1;
--replace_column 10 #
explain partitions select * from t1;
--replace_column 10 #
explain partitions select * from t1 for system_time as of '2001-02-04 10:20:30';
set @ts=(select row_end from t1 for system_time all where i=1);
select * from t1 for system_time all where row_end = @ts;
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time all where row_end = @ts;

--echo #
--echo # MDEV-16023 Unfortunate error message WARN_VERS_PART_FULL
--echo #

set timestamp= unix_timestamp('2020-07-29 10:30:10');
create or replace table t1 (a int) with system versioning
  partition by system_time interval 1 second (
    partition p0 history,
    partition p1 history,
    partition pc current
  );

set timestamp= unix_timestamp('2020-07-29 10:30:14');
insert into t1 values (1),(2),(3);
show warnings;

--echo # Cleanup
set timestamp= default;

--echo ## INTERVAL ... STARTS
--error ER_PART_WRONG_VALUE
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts 'a';

--error ER_PART_WRONG_VALUE
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '00:00:00';

--error ER_PART_WRONG_VALUE
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2000-00-01 00:00:00';

--error ER_PART_WRONG_VALUE
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts 946684800;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2000-01-01 00:00:00';
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # Test STARTS warning
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2000-01-01 00:00:01';

--echo # Test default STARTS rounding
set timestamp= unix_timestamp('1999-12-15 13:33:33');
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 second;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 minute;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 hour;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 month;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (i int) with system versioning
partition by system_time interval 1 year;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # seconds equivalent of 1 day does not round:
create or replace table t1 (i int) with system versioning
partition by system_time interval 86400 second;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # STARTS value is in local time_zone:
set time_zone="+03:00";
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2000-01-01 00:00:00';

set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t2 (i int) with system versioning
partition by system_time interval 1 day;

--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t2;
set time_zone="+00:00";
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t2;

--echo # Test rotation
set timestamp= unix_timestamp('2001-01-01 00:00:00');
--echo # it's ok to add partitions for past:
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2000-01-01 00:00:00'
partitions 3;

insert into t1 values (0);
set timestamp= unix_timestamp('2001-01-01 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2001-01-01 00:00:02');
update t1 set i= i + 1;

select *, row_end from t1 partition (p0);
select *, row_end from t1 partition (p1);

set timestamp= unix_timestamp('2000-01-01 00:00:00');
--echo # now we "overflow" first partition a bit:
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2000-01-03 00:00:00'
partitions 3;

insert into t1 values (0);
set timestamp= unix_timestamp('2000-01-01 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2000-01-02 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2000-01-03 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2000-01-04 00:00:01');
update t1 set i= i + 1;

select *, row_end from t1 partition (p0);
select *, row_end from t1 partition (p1);

--echo # and this is how it usually goes:
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day
partitions 3;

insert into t1 values (0);
set timestamp= unix_timestamp('2000-01-01 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2000-01-02 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2000-01-03 00:00:01');
update t1 set i= i + 1;
set timestamp= unix_timestamp('2000-01-04 00:00:01');
update t1 set i= i + 1;

alter table t1 add partition (partition p2 history, partition p3 history);
select *, row_end from t1 partition (p0);
select *, row_end from t1 partition (p1);
select *, row_end from t1 partition (p2);
select *, row_end from t1 partition (p3);

drop tables t1, t2;

--echo ## Subpartitions
create or replace table t1 (x int)
with system versioning
partition by system_time limit 2 partitions 3
subpartition by key (x)
subpartitions 2;

insert into t1 (x) values (1), (2), (3), (4), (5);
select * from t1 partition (pnsp0);
select * from t1 partition (pnsp1);

delete from t1 where x < 3;
delete from t1;
--echo # You see warning above ^
delete from t1;
--echo # You see warning above ^ (no matter if nothing was deleted)
select * from t1 partition (p0sp0);
select * from t1 partition (p0sp1);
select * from t1 partition (p1sp0);
select * from t1 partition (p1sp1);

--echo # check implicit sys fields for implicit engine of partitioned table
create or replace table t1 (a bigint)
with system versioning
partition by range (a)
(partition p0 values less than (20) engine innodb,
 partition p1 values less than maxvalue engine innodb);
insert into t1 values (1);
select * from t1 partition (p0);

--echo # check for partition engine
create or replace table t1 (
  f_int1 integer default 0
) with system versioning
partition by range(f_int1)
subpartition by hash(f_int1)
( partition part1 values less than (1000)
(subpartition subpart11 storage engine = 'innodb',
subpartition subpart12 storage engine = 'innodb'));
insert into t1 values (1);
select * from t1 partition (part1);

--echo #
--echo # TRX_ID versioning (moved from partition_innodb.test)
--echo #
--echo # MDEV-15951 system versioning by trx id doesn't work with partitioning
--echo # currently trx_id does not support partitioning by system_time
--error ER_VERS_FIELD_WRONG_TYPE
create or replace table t1(
  i int,
  row_start bigint unsigned generated always as row start,
  row_end bigint unsigned generated always as row end,
  period for system_time(row_start, row_end)
) engine=InnoDB with system versioning partition by system_time (
  partition p0 history,
  partition pn current
);

create or replace table t1(
  i int,
  row_start bigint unsigned generated always as row start,
  row_end bigint unsigned generated always as row end,
  period for system_time(row_start, row_end)
) engine=InnoDB with system versioning;

--error ER_VERS_FIELD_WRONG_TYPE
alter table t1  partition by system_time (
  partition p0 history,
  partition pn current
);

drop table t1;

--error ER_VERS_TRX_PART_HISTORIC_ROW_NOT_SUPPORTED
create or replace table t (
  a int primary key,
  row_start bigint unsigned as row start invisible,
  row_end bigint unsigned as row end invisible,
  period for system_time(row_start, row_end)
) engine=innodb with system versioning
partition by key() (
  partition p1,
  partition p2
);

--error ER_VERS_TRX_PART_HISTORIC_ROW_NOT_SUPPORTED
create or replace table t (
  a int primary key,
  row_start bigint unsigned as row start invisible,
  row_end bigint unsigned as row end invisible,
  period for system_time(row_start, row_end)
) engine=innodb with system versioning
partition by key(a, row_start) (
  partition p1,
  partition p2
);

--error ER_VERS_TRX_PART_HISTORIC_ROW_NOT_SUPPORTED
create or replace table t (
  a int primary key,
  row_start bigint unsigned as row start invisible,
  row_end bigint unsigned as row end invisible,
  period for system_time(row_start, row_end)
) engine=innodb with system versioning
partition by hash(a + row_end * 2) (
  partition p1,
  partition p2
);

--error ER_VERS_TRX_PART_HISTORIC_ROW_NOT_SUPPORTED
create or replace table t (
  a int primary key,
  row_start bigint unsigned as row start invisible,
  row_end bigint unsigned as row end invisible,
  period for system_time(row_start, row_end)
) engine=innodb with system versioning
partition by range columns (a, row_start) (
  partition p1 values less than (100, 100)
);

--echo #
--echo # Assertion in ALTER on warning from partitioning LIMIT [#446]
--echo #
create or replace table t1 (x int) with system versioning;
insert into t1 values (1), (2);
delete from t1;
alter table t1 partition by system_time limit 1 (
  partition p1 history,
  partition pn current);

--echo #
--echo # MDEV-14649 Assertion `t->mysql_col_len == 8' failed in row_insert_for_mysql
--echo #
create or replace table t1 (i int) engine=innodb partition by key(i);
alter table t1 add system versioning;
insert into t1 values();

--echo #
--echo # MDEV-14722 Assertion in ha_commit_trans for sub-statement
--echo #
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day;
create or replace table t2 (f int);
create or replace trigger tr before insert on t2
for each row select table_rows from information_schema.tables
where table_name = 't1' into @a;
insert into t2 values (1);

--echo #
--echo # MDEV-14740 Locking assertion for system_time partitioning
--echo #
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 week;
create or replace table t2 (f int);
create or replace trigger tr before insert on t2
for each row select count(*) from t1 into @a;
insert into t2 values (1);

--echo #
--echo # MDEV-14747 ALTER PARTITION BY SYSTEM_TIME after LOCK TABLES
--echo #
create or replace table t1 (x int) with system versioning;
lock table t1 write;
alter table t1 partition by system_time interval 1 week (
  partition p1 history,
  partition pn current);
unlock tables;

--echo #
--echo # MDEV-14748 Assertion in ha_myisammrg::attach_children()
--echo #
create or replace table t1 (x int) engine=myisam with system versioning
  partition by system_time interval 1 month (partition p1 history, partition pn current);
create or replace table t2 (x int) engine=myisam;
create or replace table t3 (x int) engine=merge union=(t2);
create or replace table t4 (x int) engine=myisam;
create or replace trigger tr after insert on t4 for each row insert into t2
  ( select x from t3 ) union ( select x from t1 );
insert into t4 values (1);

--echo #
--echo # MDEV-14821 Assertion failure
--echo #
create or replace table t1 (x int) with system versioning;
insert into t1 values (0), (1);
update t1 set x= x + 1;
alter table t1 partition by system_time limit 1 (
    partition p1 history,
    partition p2 history,
    partition pn current);
delete from t1 where x = 1;
--echo # You see warning above ^
delete from t1 where x = 2;
--echo # You see warning above ^

--echo #
--echo # MDEV-14923 Assertion upon INSERT into locked versioned partitioned table
--echo #
create or replace table t1 (x int) with system versioning
partition by system_time;
lock table t1 write;
--error ER_SAME_NAME_PARTITION
alter table t1 add partition (partition p0 history);
insert into t1 values (1);
unlock tables;

--echo #
--echo # MDEV-15103 Assertion in ha_partition::part_records() for updating VIEW
--echo #
create or replace table t1 (pk int primary key, f int) with system versioning
partition by system_time limit 100;
insert into t1 values (1,10), (2,20);
create or replace view v1 as select * from t1;
update v1 set f= 30;

--echo #
--echo # MDEV-15168 Unexpected ER_VERS_ENGINE_UNSUPPORTED upon dropping versioning on a partitioned table
--echo #
create or replace table t (a int) with system versioning
partition by system_time;
--error ER_DROP_VERSIONING_SYSTEM_TIME_PARTITION
alter table t drop system versioning;

--echo #
--echo # MDEV-15191 Assertion `bit < (map)->n_bits' failed in bitmap_is_set upon INSERT
--echo #
create or replace table t1 (i int) with system versioning;
insert into t1 values (1), (2);
update t1 set i= 3;
alter table t1 partition by system_time interval 1 month (partition p1 history, partition pn current);
lock table t1 write;
alter table t1 add partition (partition p2 history);
insert into t1 values (4);
unlock tables;

--echo #
--echo # MDEV-15036 Assertion `!is_set() || (m_status == DA_OK_BULK && is_bulk_op())' in Diagnostics_area::set_ok_status or unexpected ER_RANGE_NOT_INCREASING_ERROR
--echo #
create or replace table t1 (a int) with system versioning
partition by system_time limit 2 partitions 4;
insert into t1 values (1),(2),(3);
update t1 set a = 4;
delete from t1;
delete from t1 where a is not null;

--echo #
--echo # MDEV-14823 Wrong error message upon selecting from a system_time partition
--echo #
create or replace table t1 (i int) with system versioning partition by system_time limit 10;
--error ER_VERS_QUERY_IN_PARTITION
select * from t1 partition (p0) for system_time all;
--echo # MDEV-18929 2nd execution of SP does not detect ER_VERS_NOT_VERSIONED
create or replace procedure sp()
select * from t1 partition (p0) for system_time all;
--error ER_VERS_QUERY_IN_PARTITION
call sp;
--error ER_VERS_QUERY_IN_PARTITION
call sp;
drop procedure sp;

--echo #
--echo # MDEV-15380 Index for versioned table gets corrupt after partitioning and DELETE
--echo #
create or replace table t1 (pk int primary key)
  engine=myisam
  with system versioning
  partition by key() partitions 3;
set timestamp=1523466002.799571;
insert into t1 values (11),(12);
set timestamp=1523466004.169435;
delete from t1 where pk in (11, 12);
--echo Same test but for Aria storage engine
create or replace table t1 (pk int primary key)
  engine=aria
  with system versioning
  partition by key() partitions 3;
set timestamp=1523466002.799571;
insert into t1 values (11),(12);
set timestamp=1523466004.169435;
delete from t1 where pk in (11, 12);

--echo #
--echo # MDEV-18136 Server crashes in Item_func_dyncol_create::prepare_arguments
--echo #
create or replace table t1 (pk int) with system versioning
partition by system_time interval 7 second;
alter table t1
partition by system_time interval column_get(column_create(7,7), 7 as int) second (
  partition ver_p1 history,
  partition ver_pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= default;

--echo #
--echo # MDEV-18794 Assertion `!m_innodb' failed in ha_partition::cmp_ref upon SELECT from partitioned table
--echo #
create or replace table t1 (pk int auto_increment, i int, c char(1), primary key (pk), key(i))
engine=innodb with system versioning partition by key() partitions 2;
insert into t1 (i, c) values (1, 'a'), (2, 'b'), (null, 'c'), (null, 'b');
alter table t1 drop system versioning;
replace into t1 select * from t1;
select * from t1 where i > 0 or pk = 1000 limit 1;
drop table t1;

--echo #
--echo # MDEV-19175 Server crashes in ha_partition::vers_can_native upon INSERT DELAYED into versioned partitioned table
--echo #
create or replace table t1 (f int) with system versioning partition by hash(f);
# delayed works differently in embedded server
--error 0,ER_DELAYED_NOT_SUPPORTED
insert delayed into t1 values (1);

--echo #
--echo # MDEV-20068 History partition rotation is not done under LOCK TABLES
--echo #
create or replace table t1 (x int) with system versioning partition by system_time limit 1
(partition p1 history, partition pn current);
lock tables t1 write;
insert into t1 values (0), (1), (2), (3);
delete from t1 where x < 3;
--echo # You see warning above ^
delete from t1;
--echo # You see warning above ^
unlock tables;

--echo #
--echo # MDEV-20336 Assertion bitmap_is_set(read_partitions) upon SELECT FOR UPDATE from versioned table
--echo #
create or replace table t1 (pk int primary key) with system versioning partition by system_time limit 100 (partition p1 history, partition pn current);
execute immediate 'select * from t1 for update';

--echo #
--echo # MDEV-19903 Setup default partitions for system versioning
--echo #
create or replace table t1 (x int) with system versioning partition by system_time;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--echo # 2 partitions are created: p0 and pn
select PARTITION_NAME, PARTITION_METHOD, PARTITION_DESCRIPTION from information_schema.partitions where table_name = 't1' order by PARTITION_NAME;

create or replace table t1 (x int) with system versioning partition by system_time limit 10 partitions 4;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--echo # 4 partitions are created: p0, p1, p2 and pn
select PARTITION_NAME, PARTITION_METHOD, PARTITION_DESCRIPTION from information_schema.partitions where table_name = 't1' order by PARTITION_NAME;

--echo # Test cleanup
drop view v1;
drop tables t, t1, t2, t3, t4;

--echo #
--echo # MDEV-18957 UPDATE with LIMIT clause is wrong for versioned partitioned tables
--echo #
create or replace table t1 (
  x int,
  a varchar(255)
) with system versioning partition by system_time (partition p1 history, partition pn current);

insert into t1 (x) values (1), (2), (3), (4);
update t1 set a= 'foo' limit 3;
update t1 set a= 'bar' limit 4;
select * from t1;
drop table t1;

--echo #
--echo # MDEV-21011 Table corruption reported for versioned partitioned table after DELETE: "Found a misplaced row"
--echo #
create table t1 (a int) with system versioning
partition by system_time limit 3
(partition p1 history, partition p2 history, partition pn current);
insert into t1 values (1),(2),(3),(4);
delete from t1;
delete from t1;
check table t1;

# cleanup
drop table t1;

--echo #
--echo # MDEV-21233 Assertion `m_extra_cache' failed in ha_partition::late_extra_cache
--echo #
create table t1 (id int, a varchar(8)) with system versioning partition by key (id) partitions 2;
insert into t1 values  (1,'foo'),(2,'bar');

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

update t1, t2 set a = 1;

# cleanup
drop table t1, t2;

--echo #
--echo # MDEV-20515 multi-update tries to position updated table by null reference
--echo #
create or replace table t1 (a int);
insert into t1 values (0), (1);

create or replace table t2 (b int) with system versioning
partition by system_time
(partition p1 history, partition pn current);

insert into t2 values (0), (2);
update t1 left join t2 on a > b set b= 2 order by b;

# cleanup
drop table t1, t2;

--echo #
--echo # MDEV-17091 Assertion `old_part_id == m_last_part' failed in
--echo # ha_partition::update_row or `part_id == m_last_part' in
--echo # ha_partition::delete_row upon UPDATE/DELETE after dropping versioning
--echo #
create or replace table t1 (pk int primary key, f int) engine=innodb
        with system versioning
        partition by key() partitions 2;
insert into t1 values (1,10),(2,20);
--echo # expected to hit same partition
select * from t1 partition (p0);
alter table t1 drop system versioning;

--echo # 1 and 2 are expected to be in different partitions
select * from t1 partition(p0);
select * from t1 partition(p1);

update t1 set f=pk;
delete from t1;
drop table t1;

--echo #
--echo # MDEV-22413 Server hangs upon UPDATE/DELETE on a view reading from versioned partitioned table
--echo #
create or replace table t1 (f char(6)) engine innodb with system versioning;

insert into t1 values (null);
update t1 set f= 'foo';
update t1 set f= 'bar';
--echo # You see warning above ^

create or replace view v1 as select * from t1 for system_time all;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
update v1 set f = '';

create or replace table t1 (f char(6)) engine innodb with system versioning
partition by system_time limit 1
(partition p1 history, partition p2 history, partition pn current);

insert into t1 values (null);
update t1 set f= 'foo';
update t1 set f= 'bar';

create or replace view v1 as select * from t1 for system_time all;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
update v1 set f= '';
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
delete from v1;

# cleanup
drop view v1;
drop table t1;

--echo #
--echo # MDEV-22112 Assertion `tab_part_info->part_type == RANGE_PARTITION || tab_part_info->part_type == LIST_PARTITION' failed in prep_alter_part_table
--echo #

create table t1 (a int) with system versioning partition by system_time;
drop table t1;

create table t1 (a int) with system versioning partition by system_time
(partition p1 history, partition pn current);
--error ER_PARTITION_WRONG_TYPE
alter table t1 add partition (partition p2);

--echo # MDEV-17891 Assertion failures in select_insert::abort_result_set and
--echo #            mysql_load upon attempt to replace into a full table

--let $max_heap_table_size_orig= `select @@max_heap_table_size;`
set @@max_heap_table_size= 1024*1024;
create or replace table t1 (
  pk integer auto_increment,
  primary key (pk),
  f varchar(45000)
) with system versioning engine=memory
  partition by system_time interval 1 year (partition p1 history,
                                            partition pn current);

--echo # fill the table until full
insert into t1 () values (),(),(),(),(),(),(),(),(),(),(),(),(),(),(),();
--error ER_RECORD_FILE_FULL
insert into t1 (f) select f from t1;
--echo # leave space for exactly one record in current partition
delete from t1 where pk = 1;
--echo # copy all data into history partition
replace into t1 select * from t1;
--error ER_RECORD_FILE_FULL
replace into t1 select * from t1;

create or replace table t1 (
  pk integer auto_increment,
  primary key (pk),
  f varchar(45000)
) with system versioning engine=memory
  partition by system_time interval 1 year (partition p1 history,
                                            partition pn current);

insert into t1 () values (),(),(),(),(),(),(),(),(),(),(),(),(),(),(),();

--disable_ps2_protocol
select * into outfile 'MDEV-17891.data' from t1;
--enable_ps2_protocol
load data infile 'MDEV-17891.data' replace into table t1;
--error ER_RECORD_FILE_FULL
load data infile 'MDEV-17891.data' replace into table t1;
--error ER_RECORD_FILE_FULL
load data infile 'MDEV-17891.data' replace into table t1;

# Cleanup
--remove_file $datadir/test/MDEV-17891.data
eval set @@max_heap_table_size= $max_heap_table_size_orig;
drop table t1;

--echo #
--echo # MDEV-22178 Assertion `info->alias.str' failed in partition_info::check_partition_info instead of ER_VERS_WRONG_PARTS
--echo #
create or replace table t1 (a int) with system versioning;
--error ER_VERS_WRONG_PARTS
alter table t1 partition by system_time (partition pn current);
# Cleanup
drop table t1;

--echo #
--echo # MDEV-22247 History partition overflow leads to wrong SELECT result
--echo #
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour
(partition p0 history, partition p1 history, partition pn current);

insert into t1 values (0);
update t1 set x= x + 1;

set timestamp= unix_timestamp('2000-01-01 02:00:01');
update t1 set x= x + 1;

select *, row_start, row_end from t1 for system_time as of '2000-01-01 02:00:00';
--replace_column 10 #
explain partitions select * from t1 for system_time as of '2000-01-01 02:00:00';
--replace_column 5 # 10 # 11 #
explain partitions select * from t1;
drop table t1;

--echo #
--echo # MDEV-27244 Table corruption upon adding serial data type
--echo #
create table t1 (f int, key(f)) with system versioning
partition by system_time limit 10 (partition p0 history, partition pn current);
alter table t1 add x serial;
alter table t1 add partition (partition p1 history);
alter table t1 add partition (partition p2 history);
drop table t1;

--echo #
--echo # MDEV-27217 DELETE partition selection doesn't work for history partitions
--echo #
create table t1 (f char) with system versioning
partition by system_time limit 10 (
  partition p0 history,
  partition p1 history,
  partition p2 history,
  partition pn current);

--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p1);
--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p0, pn);
--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p0, p1);
--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p0, p1, pn);
drop table t1;

set timestamp=unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day (
  partition p0 history,
  partition p1 history,
  partition pn current);
set timestamp=unix_timestamp('2000-01-02 00:00:00');
insert t1 values (1);
--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p0, pn);
--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p0, p1, pn);
lock tables t1 write;
--error ER_VERS_NOT_ALLOWED
delete from t1 partition (p0, pn);
delete from t1;
unlock tables;
drop table t1;
set timestamp= default;

--echo #
--echo # MDEV-25546 LIMIT partitioning does not respect ROLLBACK
--echo #
create or replace table t1 (pk int primary key)
with system versioning engine innodb
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);
insert into t1 select seq from seq_1_to_90;

start transaction;
# Puts 80 rows into p0
replace into t1 select seq from seq_1_to_80;
# Puts another 70 rows into p0
replace into t1 select seq from seq_1_to_70;
# Puts 60 rows into p1
replace into t1 select seq from seq_1_to_60;

select partition_name, table_rows
from information_schema.partitions
where table_name = 't1';
rollback;

select partition_name, table_rows
from information_schema.partitions
where table_name = 't1';

# Should put 10 rows into the empty partition p0
replace into t1 select seq from seq_1_to_10;
select partition_name, table_rows
from information_schema.partitions
where table_name = 't1';
 # Cleanup
drop table t1;

--echo #
--echo # MDEV-28271 Assertion on TRUNCATE PARTITION for PARTITION BY SYSTEM_TIME
--echo #
create table t1 (x int) with system versioning
partition by system_time limit 1 (
  partition p0 history,
  partition p1 history,
  partition p2 history, # p2 just disables warning about p1 partition full
  partition pn current);

insert into t1 values (0);
update t1 set x= x + 1;
update t1 set x= x + 1;

select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (pn);

delete from t1;
delete history from t1;
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (pn);

insert into t1 values (0);
update t1 set x= x + 1;
update t1 set x= x + 1;

--echo # TRUNCATE PARTITION ALL does the same
alter table t1 truncate partition all;
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (pn);

insert into t1 values (0);
update t1 set x= x + 1;
update t1 set x= x + 1;

--echo # TRUNCATE PARTITION deletes data from HISTORY partition
alter table t1 truncate partition p1;
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (pn);

--echo # or from CURRENT partition
alter table t1 truncate partition pn;
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (pn);

drop table t1;

--echo #
--echo # MDEV-20077 Warning on full history partition is delayed until next DML statement
--echo #
--echo # DELETE
create table t1 (x int) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_200;

--echo # p0 is filled with 100 records (no warnings):
delete from t1 where x <= 99;
--echo # p1 is filled with 1 + 100 records (warning is printed):
delete from t1 where x <= 100;
delete from t1;
--echo # You see warning above ^

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;

--echo # DELETE under LOCK TABLES
create table t1 (x int) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_200;

lock tables t1 write;
--echo # (LOCK TABLES) p0 is filled with 100 records (no warnings):
delete from t1 where x <= 99;
--echo # (LOCK TABLES) p1 is filled with 1 + 100 records (warning is printed):
delete from t1 where x <= 100;
delete from t1;
--echo # You see warning above ^
unlock tables;

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;

--echo # DELETE multitable
create table t1 (x int) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

create table t2 (y int);

insert into t1 select seq from seq_0_to_200;
insert into t2 select seq from seq_0_to_3;
delete t1, t2 from t1 join t2 where x < 50 and y = 0;
delete t1, t2 from t1 join t2 where x < 100 and y = 1;
delete t1, t2 from t1 join t2 where x < 150 and y = 2;
delete t1, t2 from t1 join t2;
--echo # You see warning above ^

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;

--echo # UDPATE
create table t1 (x int) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_49;

update t1 set x= x + 1;
update t1 set x= x + 1;
update t1 set x= x + 1;
update t1 set x= x + 1;
--echo # You see warning above ^

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop tables t1, t2;

--echo # UPDATE multitable
create table t1 (x int) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

create table t2 (y int);

insert into t1 select seq from seq_0_to_49;
insert into t2 values (5);

update t1, t2 set x= x + 1;
update t1, t2 set x= x + 1;
update t1, t2 set x= x + 1;
update t1, t2 set x= x + 1;
--echo # You see warning above ^

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop tables t1, t2;

--echo # INSERT .. ON DUPLICATE KEY UPDATE (ODKU)
create table t1 (x int primary key) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_100;

delete from t1 where x <= 99;
insert into t1 values (100) on duplicate key update x= 400;

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;

--echo # INSERT .. SELECT .. ON DUPLICATE KEY UPDATE (ODKU)
create table t1 (x int primary key) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

create table t2 (y int);
insert into t2 values (100);
insert into t1 select seq from seq_0_to_100;

delete from t1 where x <= 99;
insert into t1 select * from t2 on duplicate key update x= 500;

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop tables t1, t2;

--echo # REPLACE
create table t1 (x int primary key) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_100;

delete from t1 where x < 99;
replace t1 values (100);
replace t1 values (100);

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;

--echo # LOAD DATA .. REPLACE
create table t1 (x int primary key) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_49;
--disable_ps2_protocol
select x into outfile 'MDEV-20077.data' from t1;
--enable_ps2_protocol

load data infile 'MDEV-20077.data' replace into table t1 (x);
load data infile 'MDEV-20077.data' replace into table t1 (x);
load data infile 'MDEV-20077.data' replace into table t1 (x);
load data infile 'MDEV-20077.data' replace into table t1 (x);
--echo # You see warning above ^

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;
--remove_file $datadir/test/MDEV-20077.data

--echo # REPLACE .. SELECT
create table t1 (x int primary key) with system versioning
partition by system_time limit 100 (
  partition p0 history,
  partition p1 history,
  partition pn current);

insert into t1 select seq from seq_0_to_49;
replace t1 select * from t1;
replace t1 select * from t1;
replace t1 select * from t1;
replace t1 select * from t1;
--echo # You see warning above ^

select count(*) from t1 partition (p0);
select count(*) from t1 partition (p1);
drop table t1;

--echo #
--echo # MDEV-28552 Assertion `inited==RND' failed in handler::ha_rnd_end
--echo #
create table tcount (c int unsigned);
insert into tcount values (0);

create table t (f int) with system versioning
partition by system_time limit 1000
(partition p1 history, partition pn current);
insert into t values (1),(2);
create trigger tr before insert on t for each row update tcount set c = c + 1;

insert into t select * from t;

# cleanup
drop table tcount, t;

--echo #
--echo # MDEV-19569 Assertion `table_list->table' failed in find_field_in_table_ref and Assertion `table_ref->table || table_ref->view' in Field_iterator_table_ref::set_field_iterator
--echo #
set timestamp=unix_timestamp('2000-01-01 00:00:00');
create table t1 (i int); 
create table t2 (i int); 
 
--error ER_SUBQUERIES_NOT_SUPPORTED
alter table t1 partition by system_time 
  interval (select i from t2) day (partition p1 history, partition pn current);

drop table t1;

--error ER_SUBQUERIES_NOT_SUPPORTED
create table t1 (id int) with system versioning
  partition by system_time 
  interval (select i from t2) day (partition p1 history, partition pn current);

--error ER_PART_WRONG_VALUE
create table t1 (id int) with system versioning
  partition by system_time
  interval "hello" day (partition p1 history, partition pn current);

create table t1 (id int) with system versioning
  partition by system_time
  interval 3.893 day (partition p1 history, partition pn current);

drop table t1, t2;

create table t1 (id int) with system versioning
  partition by system_time interval "3-11" year_month (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

create table t1 (id int) with system versioning
  partition by system_time interval "3 11" day_hour (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

create table t1 (id int) with system versioning
  partition by system_time interval "3 11:12" day_minute (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

create table t1 (id int) with system versioning
  partition by system_time interval "3 11:12:13" day_second (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

create table t1 (id int) with system versioning
  partition by system_time interval "11:12" hour_minute (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

create table t1 (id int) with system versioning
  partition by system_time interval "11:12:13" hour_second (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

create table t1 (id int) with system versioning
  partition by system_time interval "12:13" minute_second (partition p1 history, partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop table t1;

--error ER_PART_WRONG_VALUE
create table t1 (id int) with system versioning
  partition by system_time interval "12:13.123" minute_microsecond (partition p1 history, partition pn current);

--echo #
--echo # End of 10.3 tests
--echo #

--echo #
--echo # MDEV-22283 Server crashes in key_copy or unexpected error 156: The table already existed in the storage engine
--echo #
create table t1 (a int primary key) engine=aria page_checksum=0
with system versioning
partition by system_time (partition p1 history, partition pn current);

alter table t1 add partition (partition p2 history);
show table status;
drop table t1;

create table t1 (b int) engine=aria row_format=dynamic with system versioning
partition by system_time (partition p1 history, partition pn current);
insert into t1 values (1);
replace into t1 values (1);

# cleanup
drop table t1;

--echo #
--echo # MDEV-18794 Assertion `!m_innodb' failed in ha_partition::cmp_ref upon SELECT from partitioned table
--echo #
create or replace table t1 (pk int auto_increment, i int, c char(1), primary key (pk), key(i))
engine=innodb with system versioning partition by key() partitions 2;
insert into t1 (i, c) values (1, 'a'), (2, 'b'), (null, 'c'), (null, 'b');
alter table t1 drop system versioning;
replace into t1 select * from t1;
select * from t1 where i > 0 or pk = 1000 limit 1;
drop table t1;

--echo #
--echo # End of 10.4 tests
--echo #

--echo #
--echo # MDEV-22153 ALTER add default history partitions makes table inaccessible
--echo #
create or replace table t1 (x int) with system versioning partition by system_time;
alter table t1 add partition partitions 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
alter table t1 add partition partitions 2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
alter table t1 add partition partitions 3;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop tables t1;

--echo #
--echo # MDEV-22207 Drop default history partitions renders table inaccessible
--echo #
create or replace table t1 (i int) with system versioning
partition by system_time limit 1 partitions 5;

alter table t1 drop partition p0, p2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
alter table t1 add partition partitions 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

drop tables t1;

--echo #
--echo # MDEV-22155 ALTER add default history partitions name clash on non-default partitions
--echo #
set timestamp= default;
create or replace table t1 (x int) with system versioning
partition by system_time limit 1
(partition p2 history, partition p8 history, partition pn current);
alter table t1 add partition partitions 1;
alter table t1 add partition partitions 2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
alter table t1 add partition partitions 8;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

drop tables t1;

--echo #
--echo # MDEV-27328 Change of SYSTEM_TIME partitioning options is not possible without data copy
--echo #
create or replace table t1 (f int) with system versioning
partition by hash(f);
alter table t1 partition by system_time;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (f int) with system versioning
partition by system_time;
alter table t1 partition by hash(f);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (x int) with system versioning;
alter table t1 partition by system_time;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

create or replace table t1 (x int) with system versioning
partition by system_time limit 100 partitions 4;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
alter table t1 add partition partitions 2;
alter table t1 partition by system_time;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
alter table t1 partition by system_time limit 33;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 00:00:00');
insert t1 values (0);
set timestamp= unix_timestamp('2000-01-01 00:10:00');
update t1 set x= x + 1;
set timestamp= unix_timestamp('2000-01-01 01:00:00');
update t1 set x= x + 1;
set timestamp= unix_timestamp('2000-01-01 01:30:00');
update t1 set x= x + 1;
set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x + 1;

# When we switch to INTERVAL we must reorganize partitions.
# Otherwise pruning won't work correctly.
alter table t1 partition by system_time interval 1 hour
starts '2000-01-01 00:00:00';
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (p2);
select * from t1 partition (pn);
set timestamp= default;

# When we switch to LIMIT we probably don't want to reorganize old partitions.
# Note: reorganize for LIMIT is broken, it pushes all history into first partition.
# TODO: MDEV-27337
alter table t1 partition by system_time limit 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
update t1 set x= x + 1;
update t1 set x= x + 1;
--echo # You see warning above ^
select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (p2);
select * from t1 partition (p3);
select * from t1 partition (p4);
select * from t1 partition (pn);

drop table t1;

--echo # End of 10.6 tests

--echo #
--echo # MDEV-22166 MIGRATE PARTITION: move out partition into a table
--echo #
create or replace table t1 (x int)
with system versioning
partition by range(x) (
  partition p1 values less than (10),
  partition p2 values less than (20),
  partition p3 values less than (30),
  partition p4 values less than (40),
  partition p5 values less than (50),
  partition pn values less than maxvalue);

insert into t1 values (2), (12), (22), (32), (42), (52);
update t1 set x= x + 1;

alter table t1 convert partition p2 to table tp2;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp2;
select * from tp2;
select * from tp2 for system_time all order by x;

--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;
select * from t1 order by x;
select * from t1 for system_time all order by x;

--echo # SP
create or replace procedure sp()
alter table t1 convert partition p3 to table tp3;
call sp;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp3;
select * from tp3;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;
select * from t1 order by x;
drop table tp3;
--error ER_PARTITION_DOES_NOT_EXIST
call sp;
--error ER_PARTITION_DOES_NOT_EXIST
call sp;
drop procedure sp;

--echo # LOCK TABLES, PS, SP
create or replace procedure sp()
alter table t1 convert partition p4 to table tp4;
lock tables t1 write;
prepare stmt from 'call sp';
execute stmt;

# TODO: don't unlock here (see above TODO)
unlock tables;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp4;
select * from tp4;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;
select * from t1 order by x;
drop table tp4;
lock tables t1 write;
--error ER_PARTITION_DOES_NOT_EXIST
execute stmt;
--error ER_PARTITION_DOES_NOT_EXIST
call sp;
drop prepare stmt;
unlock tables;
drop procedure sp;
unlock tables;

drop tables t1, tp2;

--echo # System-versioned tables (SYSTEM_TIME LIMIT)

create or replace table t1 (
  x int,
  row_start timestamp(6) as row start invisible,
  row_end timestamp(6) as row end invisible,
  period for system_time(row_start, row_end)
) with system versioning
partition by system_time limit 1 partitions 4;

insert into t1 values (2), (12), (22);
update t1 set x= x + 1 where x = 2;
update t1 set x= x + 1 where x = 12;
update t1 set x= x + 1 where x = 22;

select * from t1 partition (p1);
--error ER_VERS_WRONG_PARTS
alter table t1 convert partition pn to table tp1;
alter table t1 convert partition p1 to table tp1;

--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp1;
select * from tp1;
select * from tp1 for system_time all;

--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;
select * from t1 order by x;
select * from t1 for system_time all order by x;

drop tables t1, tp1;

--echo # System-versioned tables (SYSTEM_TIME INTERVAL)

set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (
  x int,
  row_start timestamp(6) as row start invisible,
  row_end timestamp(6) as row end invisible,
  period for system_time(row_start, row_end)
) with system versioning
partition by system_time interval 1 hour partitions 4;

insert into t1 values (2), (12), (22);
set timestamp= unix_timestamp('2000-01-01 00:00:01');
update t1 set x= x + 1 where x = 2;
set timestamp= unix_timestamp('2000-01-01 01:00:00');
update t1 set x= x + 1 where x = 12;
set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x + 1 where x = 22;

select * from t1 partition (p0);
select * from t1 partition (p1);
select * from t1 partition (p2);
--error ER_VERS_DROP_PARTITION_INTERVAL
alter table t1 convert partition p1 to table tp1;
alter table t1 convert partition p0 to table tp0;
alter table t1 convert partition p1 to table tp1;
--error ER_VERS_WRONG_PARTS
alter table t1 convert partition p2 to table tp2;

--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp0;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp1;
select * from tp0;
select * from tp1;
select * from tp0 for system_time all;
select * from tp1 for system_time all;

--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;
select * from t1;
select * from t1 for system_time all order by x;

drop tables t1, tp0, tp1;

--echo # System-versioned tables (implicit)

create or replace table t1(x int) with system versioning
partition by system_time limit 1 partitions 3;

alter table t1 convert partition p1 to table tp1;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp1;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;

drop tables t1, tp1;

if (!$MTR_COMBINATION_HEAP)
{
--echo # Complex table
create or replace table t1 (
  x int primary key auto_increment,
  t timestamp(6) default '2001-11-11 11:11:11',
  b blob(4096) compressed null,
  c varchar(1033) character set utf8 not null,
  u int,
  unique key (x, u),
  m enum('a', 'b', 'c') not null default 'a' comment 'absolute',
  i1 tinyint, i2 smallint, i3 bigint,
  index three(i1, i2, i3),
  v1 timestamp(6) generated always as (t + interval 1 day),
  v2 timestamp(6) generated always as (t + interval 1 month) stored,
  s timestamp(6) as row start,
  e timestamp(6) as row end,
  period for system_time (s, e),
  ps date, pe date,
  period for app_time (ps, pe),
  constraint check_constr check (u > -1))
with system versioning default charset=ucs2
partition by range(x) (
  partition p0 values less than (10),
  partition p1 values less than (20),
  partition pn values less than maxvalue);

alter table t1 convert partition p1 to table tp1;

--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table tp1;
--replace_result $default_engine X ' PAGE_CHECKSUM=1' ''
show create table t1;

drop tables t1, tp1;
}

--echo #
--echo # MDEV-29841 Partition by system_time can be converted into table but not back
--echo #
create or replace table t (a int) with system versioning
partition by system_time limit 10 partitions 3;
alter table t convert partition p0 to table tp;
--error ER_ONLY_ON_RANGE_LIST_PARTITION
alter table t convert table tp to partition p0;
drop tables t, tp;

--echo #
--echo # End of 10.7 tests
--echo #

--echo #
--echo # MDEV-17554 Auto-create new partition for system versioned tables
--echo #            with history partitioned by INTERVAL/LIMIT
--echo #
create or replace table t1 (x int) with system versioning
partition by system_time limit 1 auto;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--echo # Turn off AUTO
alter table t1 partition by system_time limit 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--echo # Get AUTO back
alter table t1 partition by system_time limit 1 auto;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

insert into t1 values (1);

create or replace table t2 (y int);
insert into t2 values (2);

insert into t1 select * from t2;
insert into t2 select * from t1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # Too many partitions error
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto;
set timestamp= unix_timestamp('2001-01-01 00:01:00');
--error ER_VERS_HIST_PART_FAILED
update t1 set x= x + 1;
show warnings;

--echo # Auto-create failed error
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning engine innodb
partition by system_time interval 1 hour auto;

insert into t1 values (1);

call mtr.add_suppression("rror number .*(File exists|file operation)");
call mtr.add_suppression("InnoDB: Cannot create file");

--let $datadir= `select @@datadir`
--let $dummy= $datadir/test/t1#P#p1.ibd
--write_file $dummy
EOF

set timestamp= unix_timestamp('2000-01-01 01:00:00');
--error ER_GET_ERRNO
update t1 set x= x + 2;
show warnings;
--remove_file $dummy

--echo # Partition overflow error and manual fix
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour;

insert into t1 values (440);
set timestamp= unix_timestamp('2000-01-01 00:10:00');
update t1 set x= x + 1;

--echo # Check how pruning boundaries work
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time as of '2000-01-01 00:59:58';
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time as of '2000-01-01 00:59:59';
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time as of '2000-01-01 01:00:00';
select * from t1 for system_time as of '2000-01-01 00:09:59';

set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x + 1;

select * from t1 for system_time as of '2000-01-01 01:00:00';
select * from t1 partition (p0) order by x;

--echo # Here is how manual fix works: just add new partitions there
alter table t1 add partition partitions 3;
select * from t1 for system_time as of '2000-01-01 01:00:00';
select * from t1 partition (p0) order by x;

--echo # Check pruning after ALTER
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time as of '2000-01-01 00:59:58';
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time as of '2000-01-01 00:59:59';
--replace_column 5 # 10 # 11 #
explain partitions select * from t1 for system_time as of '2000-01-01 01:00:00';

drop table t1;

--enable_info
create or replace table t1 (x int) with system versioning
partition by system_time interval 3600 second
starts '2000-01-01 00:00:00' auto partitions 3;

insert into t1 values (1);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x + 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 03:00:00');
update t1 set x= x + 2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto (
    partition p1 history,
    partition p3 history,
    partition pn current);

insert into t1 values (1);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x + 3;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 03:00:00');
update t1 set x= x + 4;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 04:00:00');
lock tables t1 write;
update t1 set x= x + 5;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
unlock tables;
set timestamp= default;

--echo # Couple of more LOCK TABLES cases
create or replace table t1 (x int) with system versioning
partition by system_time limit 1 auto;
lock tables t1 write;
insert into t1 values (1);
update t1 set x= x + 1;
update t1 set x= x + 2;
update t1 set x= x + 3;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
unlock tables;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # Overflow prevention under LOCK TABLES
create or replace table t1 (x int)
with system versioning partition by system_time
limit 10 auto;

insert into t1 values (1), (2), (3), (4), (5), (6), (7), (8), (9);
update t1 set x= x + 10;

lock tables t1 write;
update t1 set x= 1 where x = 11;
update t1 set x= 2 where x = 12;
update t1 set x= 3 where x = 13;
unlock tables;

select count(x) from t1 partition (p0);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
drop tables t1;

--echo # Test VIEW, LOCK TABLES
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto;
create or replace view v1 as select * from t1;

insert into t1 values (1);
set timestamp= unix_timestamp('2000-01-01 01:00:00');
update v1 set x= x + 2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 02:00:00');
lock tables v1 write;
update v1 set x= x + 3;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
unlock tables;

drop view v1;
drop tables t1;

set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto partitions 3;

create or replace table t2 (y int) with system versioning
partition by system_time interval 1 hour auto;

insert into t1 values (1);
insert into t2 values (2);

set timestamp= unix_timestamp('2000-01-01 01:00:00');
update t1, t2 set x= x + 1, y= y + 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t2;

set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1, t2 set x= x + 1, y= y + 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t2;

set timestamp= unix_timestamp('2000-01-01 03:00:00');
update t1, t2 set t1.x= 0 where t1.x< t2.y;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
# Multiupdate_prelocking_strategy::handle_end() is processed after table open.
# For PS it is possible to skip unneeded auto-creation because the above happens at
# prepare stage and auto-creation is done at execute stage.
--replace_result $default_engine DEFAULT_ENGINE 'PARTITIONS 4' 'PARTITIONS ok' 'PARTITIONS 5' 'PARTITIONS ok'
show create table t2;

drop tables t1, t2;

--echo # PS, SP, LOCK TABLES
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto;

insert into t1 values (1);

set timestamp= unix_timestamp('2000-01-01 01:00:00');
execute immediate 'update t1 set x= x + 5';
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

prepare s from 'update t1 set x= x + 6';
set timestamp= unix_timestamp('2000-01-01 02:00:00');
execute s; execute s;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 03:00:00');
lock tables t1 write;
execute s; execute s;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
unlock tables;
drop prepare s;

create procedure sp() update t1 set x= x + 7;
set timestamp= unix_timestamp('2000-01-01 04:00:00');
call sp; call sp;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 05:00:00');
lock tables t1 write;
call sp; call sp;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
unlock tables;
drop procedure sp;

set timestamp= unix_timestamp('2001-01-01 00:00:00');
create or replace table t1 (i int) with system versioning
partition by system_time interval 1 day starts '2001-01-01 00:00:00';
insert into t1 values (0);
set timestamp= unix_timestamp('2001-01-01 00:00:01');
prepare s from 'update t1 set i= i + 1';
execute s;
set timestamp= unix_timestamp('2001-01-02 00:00:01');
execute s;
drop prepare s;

# Because of blobs:
if (!$MTR_COMBINATION_HEAP)
{
--echo # Complex table
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (
  x int primary key auto_increment,
  t timestamp(6) default '2001-11-11 11:11:11',
  b blob(4096) compressed null,
  c varchar(1033) character set utf8 not null,
  u int unique,
  m enum('a', 'b', 'c') not null default 'a' comment 'absolute',
  i1 tinyint, i2 smallint, i3 bigint,
  index three(i1, i2, i3),
  v1 timestamp(6) generated always as (t + interval 1 day),
  v2 timestamp(6) generated always as (t + interval 1 month) stored,
  s timestamp(6) as row start,
  e timestamp(6) as row end,
  period for system_time (s, e),
  ps date, pe date,
  period for app_time (ps, pe),
  constraint check_constr check (u > -1))
with system versioning default charset=ucs2
partition by system_time interval 1 hour auto (
    partition p2 history,
    partition pn current);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

insert into t1 (x, c, u, i1, i2, i3, ps, pe)
values (1, 'cc', 0, 1, 2, 3, '1999-01-01', '2000-01-01');
set timestamp= unix_timestamp('2000-01-01 01:00:00');
update t1 set x= x + 8;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x - 8;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
}
--disable_info

--echo # INSERT .. ON DUPLICATE KEY UPDATE (ODKU)
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int primary key) with system versioning
partition by system_time interval 1 hour auto;
insert into t1 values (1);
set timestamp= unix_timestamp('2000-01-01 01:00:00');
insert into t1 values (1) on duplicate key update x= x + 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--echo # LOAD DATA .. REPLACE
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int primary key) with system versioning
partition by system_time interval 1 hour auto;

insert t1 values (1), (2), (3);
--disable_ps2_protocol
select x into outfile 'MDEV-17554.data' from t1;
--enable_ps2_protocol

set timestamp= unix_timestamp('2000-01-01 01:00:00');
load data infile 'MDEV-17554.data' replace into table t1 (x);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--remove_file $datadir/test/MDEV-17554.data

--echo # Concurrent DML
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto partitions 3;

insert into t1 values (1);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

--connect con8, localhost, root
--connect con7, localhost, root
--connect con6, localhost, root
--connect con5, localhost, root
--connect con4, localhost, root
--connect con3, localhost, root
--connect con2, localhost, root
--connect con1, localhost, root
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 10;
--connection con2
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 20;
--connection con3
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 30;
--connection con4
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 40;
--connection con5
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 50;
--connection con6
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 60;
--connection con7
set timestamp= unix_timestamp('2000-01-01 02:00:00');
send update t1 set x= x + 70;
--connection con8
set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set x= x + 80;
--connection con1
reap;
--disconnect con1
--connection con2
reap;
--disconnect con2
--connection con3
reap;
--disconnect con3
--connection con4
reap;
--disconnect con4
--connection con5
reap;
--disconnect con5
--connection con6
reap;
--disconnect con6
--connection con7
reap;
--disconnect con7
--disconnect con8
--connection default
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

drop tables t1;
set timestamp= default;

--echo # Concurrent DML (LIMIT)
create or replace table t1 (x int) with system versioning engine heap
partition by system_time limit 1 auto partitions 3;

insert into t1 values (1);

--let $max_loop= 3
# For more intensity use
# --let $max_loop= 30
--echo update t1 set x= x + N; # (running multithreaded for $max_loop times)
--disable_query_log
--disable_result_log
--connect con9, localhost, root
--connect con10, localhost, root
--connect con11, localhost, root
--connect con12, localhost, root
--connect con13, localhost, root
--connect con14, localhost, root
--connect con15, localhost, root
--connect con16, localhost, root
--connect con17, localhost, root
--connect con18, localhost, root
--connect con19, localhost, root
--connect con20, localhost, root
--connect con8, localhost, root
--connect con7, localhost, root
--connect con6, localhost, root
--connect con5, localhost, root
--connect con4, localhost, root
--connect con3, localhost, root
--connect con2, localhost, root
--connect con1, localhost, root
--let $i= 0
while ($i < $max_loop)
{
  --connection con1
  send update t1 set x= x + 100;
  --connection con2
  send update t1 set x= x + 200;
  --connection con3
  send update t1 set x= x + 300;
  --connection con4
  send update t1 set x= x + 400;
  --connection con5
  send update t1 set x= x + 500;
  --connection con6
  send update t1 set x= x + 600;
  --connection con7
  send update t1 set x= x + 700;
  --connection con8
  send update t1 set x= x + 800;
  --connection con9
  send update t1 set x= x + 900;
  --connection con10
  send update t1 set x= x + 1000;
  --connection con11
  send update t1 set x= x + 1100;
  --connection con12
  send update t1 set x= x + 1200;
  --connection con13
  send update t1 set x= x + 1300;
  --connection con14
  send update t1 set x= x + 1400;
  --connection con15
  send update t1 set x= x + 1500;
  --connection con16
  send update t1 set x= x + 1600;
  --connection con17
  send update t1 set x= x + 1700;
  --connection con18
  send update t1 set x= x + 1800;
  --connection con19
  send update t1 set x= x + 1900;
  --connection con20
  send update t1 set x= x + 2000;
  --connection con1
  reap;
  --connection con2
  reap;
  --connection con3
  reap;
  --connection con4
  reap;
  --connection con5
  reap;
  --connection con6
  reap;
  --connection con7
  reap;
  --connection con8
  reap;
  --connection con9
  reap;
  --connection con10
  reap;
  --connection con11
  reap;
  --connection con12
  reap;
  --connection con13
  reap;
  --connection con14
  reap;
  --connection con15
  reap;
  --connection con16
  reap;
  --connection con17
  reap;
  --connection con18
  reap;
  --connection con19
  reap;
  --connection con20
  reap;
  --inc $i
}

--disconnect con1
--disconnect con2
--disconnect con3
--disconnect con4
--disconnect con5
--disconnect con6
--disconnect con7
--disconnect con8
--disconnect con9
--disconnect con10
--disconnect con11
--disconnect con12
--disconnect con13
--disconnect con14
--disconnect con15
--disconnect con16
--disconnect con17
--disconnect con18
--disconnect con19
--disconnect con20

--connection default
# Result is undeterministic under LIMIT concurrency (MDEV-28459)
# show create table t1;

--enable_query_log
--enable_result_log

drop tables t1;

--echo # Transaction
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning engine innodb
partition by system_time interval 1 hour auto;

insert into t1 values (1);
set timestamp= unix_timestamp('2000-01-01 01:00:00');
start transaction;
update t1 set x= 0;
--connect con1, localhost, root
select * from t1;
show create table t1;
--connection default
commit;
show create table t1;

set timestamp= unix_timestamp('2000-01-01 02:00:00');
start transaction;
update t1 set x= 1;
--connection con1
select * from t1;
--connection default
rollback;
show create table t1;
--disconnect con1
--connection default
drop table t1;

--echo #
--echo # MDEV-25479 Auto-create: 2nd and further executions of PS or SP fail to create partition
--echo #
create table t (a int) with system versioning
  partition by system_time interval 1 hour auto;

insert into t values (1), (2);
prepare stmt from "update t set a= a + 1";
set @@timestamp= @@timestamp + 3601;
execute stmt;
set @@timestamp= @@timestamp + 3601;
execute stmt;
drop prepare stmt;
--replace_result $default_engine DEFAULT_ENGINE
show create table t;

create procedure sp() update t set a= a + 1;
set @@timestamp= @@timestamp + 3601;
call sp();
set @@timestamp= @@timestamp + 3601;
call sp();
drop procedure sp;
--replace_result $default_engine DEFAULT_ENGINE
show create table t;

# Cleanup
drop table t;

--echo #
--echo # MDEV-23639 Auto-create does not work under LOCK TABLES or inside triggers
--echo #
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create or replace table t1 (x int) with system versioning
partition by system_time interval 1 hour auto
partitions 3;

create table t2 (x int);
create table t3 (x int);
insert into t3 values (3);

create trigger tr after insert on t2 for each row update t1 set x= x + 11;
create or replace procedure sp() update t1 set x= x + 5;
create or replace procedure sp2() insert into t2 values (5);
prepare ps from 'update t1 set x= x + 6';
prepare ps2 from 'insert into t2 values (6)';

insert into t1 values (1);
set timestamp= unix_timestamp('2000-01-01 02:00:00');
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
insert into t2 values (2);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 03:00:00');
call sp; call sp;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 04:00:00');
call sp2; call sp2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 05:00:00');
execute ps; execute ps;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 06:00:00');
execute ps2; execute ps2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 08:00:00');
lock tables t1 write, t2 write;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 09:00:00');
update t1 set x= x + 1;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 10:00:00');
update t1 set x= x + 2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
update t2 set x= x + 1;
set timestamp= unix_timestamp('2000-01-01 11:00:00');
insert into t2 values (4);
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
--error ER_TABLE_NOT_LOCKED
update t3 set x= x + 1;

set timestamp= unix_timestamp('2000-01-01 12:00:00');
call sp; call sp;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 13:00:00');
call sp2; call sp2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

set timestamp= unix_timestamp('2000-01-01 14:00:00');
execute ps; execute ps;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;
set timestamp= unix_timestamp('2000-01-01 15:00:00');
execute ps2; execute ps2;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

unlock tables;
--replace_result $default_engine DEFAULT_ENGINE
show create table t1;

# Cleanup
drop tables t1, t2, t3;
drop procedure sp;
drop procedure sp2;
drop prepare ps;
drop prepare ps2;

--echo #
--echo # MDEV-27456 Assertion `!thd->is_error()' fails in vers_create_partitions upon DML with ER_UNKNOWN_PARTITION
--echo #
create table t (a int) with system versioning
partition by system_time interval 1 minute auto;
set @@timestamp= @@timestamp + 61;
select * from t;
--error ER_UNKNOWN_PARTITION
delete from t partition (px);
lock tables t write;
--error ER_UNKNOWN_PARTITION
delete from t partition (px);
unlock tables;
drop table t;
set timestamp= default;

--echo #
--echo # MDEV-28978 Assertion failure in THD::binlog_query or unexpected
--echo #            ER_ERROR_ON_WRITE with auto-partitioning
--echo #
create table t (a int) with system versioning partition by system_time limit 6 auto;
insert into t () values (),(),(),(),(),();
update t set a = 1;
update t set a = 2 limit 0;
# cleanup
drop table t;

--echo #
--echo # MDEV-31244 Assertion "not SELECT" in vers_set_hist_part()
--echo #
create table t (a int) with system versioning partition by system_time;
--delimiter $
create function f() returns int
begin
  update t set a = 1;
  return 1;
end $
--delimiter ;
create procedure p() select f();
call p();
call p();
# cleanup
drop procedure p;
drop function f;
drop table t;

--echo #
--echo # MDEV-29873 MSAN uninitialized value errors in bcmp /
--echo #            prep_alter_part_table upon re-partitioning by system time
--echo #
create table t (a int) with system versioning partition by system_time interval 5 week;
alter table t partition by system_time interval 10 week;
# cleanup
drop table t;

--echo #
--echo # MDEV-16546 System versioning setting to allow history modification
--echo #
create table t1 (a varchar(100)) with system versioning
partition by system_time interval 1 day
starts '2021-09-30 00:00:00' partitions 3;
set system_versioning_insert_history=1;
insert into t1 (a,row_start,row_end) values
('p0',                   '2021-09-30', '2021-09-30 10:00:00'),
('p1',                   '2021-09-30', '2021-10-01 10:00:00'),
('overflows, so also p1','2021-09-30', '2021-10-10 10:00:00'),
('pn, current',          '2021-09-30', '2038-01-19 03:14:07.999999');
select table_name,partition_name,partition_ordinal_position,partition_method,partition_description,table_rows
from information_schema.partitions where table_schema='test';
drop table t1;
set system_versioning_insert_history=0;

--disable_prepare_warnings

--echo #
--echo # MDEV-29727 ALTER and CREATE with default partitioning
--echo #            differently react to SQL_MODE => unusable SHOW CREATE
--echo #
create table t (a int) with system versioning;
--error WARN_VERS_PARAMETERS
alter table t partition by system_time partitions 3;
drop table t;
--error WARN_VERS_PARAMETERS
create table t (a int) with system versioning partition by system_time partitions 3;

--echo #
--echo # End of 10.5 tests
--echo #

--echo #
--echo # MDEV-31903 Server crashes in _ma_reset_history upon UNLOCK table with auto-create history partitions
--echo #
set timestamp= unix_timestamp('2000-01-01 00:00:00');
create table t1 (x int) engine=aria with system versioning partition by system_time interval 1 hour auto partitions 3;
insert into t1 values (1);

create table t2 (x int) engine=aria;
create trigger tr after insert on t2 for each row update t1 set x= x + 11;
lock tables t1 write, t2 write;
update t1 set x= x + 1;
set timestamp= unix_timestamp('2000-01-01 13:00:00');
insert into t2 values (5);
unlock tables;
drop table t1, t2;
set timestamp= default;

--echo #
--echo # MDEV-29872 MSAN/Valgrind uninitialised value errors in TABLE::vers_switch_partition
--echo #
create table t (a int) with system versioning partition by system_time limit 100 partitions 3;
let $emb= `select if(version() like '%embedded%', 1, 0)`;
if (!$emb)
{
  --disable_result_log
  --disable_query_log
  --error ER_DELAYED_NOT_SUPPORTED
  insert delayed into t () values ();
  --enable_query_log
  --enable_result_log
}
# cleanup
drop table t;

--echo #
--echo # End of 10.9 tests
--echo #

set global innodb_stats_persistent= @save_persistent;
--source suite/versioning/common_finish.inc