1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880
|
/* Copyright (c) 2013, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/parse_tree_nodes.h"
#include <algorithm>
#include <cstring>
#include <limits>
#include <utility>
#include <vector>
#include "field_types.h"
#include "m_ctype.h"
#include "m_string.h"
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_dbug.h"
#include "mysql/mysql_lex_string.h"
#include "mysql/udf_registration_types.h"
#include "mysql_com.h"
#include "scope_guard.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/create_field.h"
#include "sql/dd/info_schema/show.h" // build_show_...
#include "sql/dd/types/abstract_table.h" // dd::enum_table_type::BASE_TABLE
#include "sql/dd/types/column.h"
#include "sql/derror.h" // ER_THD
#include "sql/field.h"
#include "sql/gis/srid.h"
#include "sql/intrusive_list_iterator.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_timefunc.h"
#include "sql/key_spec.h"
#include "sql/mdl.h"
#include "sql/mysqld.h" // global_system_variables
#include "sql/opt_explain_json.h" // Explain_format_JSON
#include "sql/opt_explain_traditional.h" // Explain_format_traditional
#include "sql/parse_location.h"
#include "sql/parse_tree_column_attrs.h" // PT_field_def_base
#include "sql/parse_tree_hints.h"
#include "sql/parse_tree_partitions.h" // PT_partition
#include "sql/parse_tree_window.h" // PT_window
#include "sql/parser_yystype.h"
#include "sql/query_options.h"
#include "sql/query_result.h"
#include "sql/query_term.h"
#include "sql/sp.h" // sp_add_used_routine
#include "sql/sp_head.h"
#include "sql/sp_instr.h" // sp_instr_set
#include "sql/sp_pcontext.h"
#include "sql/sql_base.h" // find_temporary_table
#include "sql/sql_call.h" // Sql_cmd_call...
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_cmd_ddl_table.h"
#include "sql/sql_component.h" // Sql_cmd_component
#include "sql/sql_const.h"
#include "sql/sql_data_change.h"
#include "sql/sql_delete.h" // Sql_cmd_delete...
#include "sql/sql_do.h" // Sql_cmd_do...
#include "sql/sql_error.h"
#include "sql/sql_insert.h" // Sql_cmd_insert...
#include "sql/sql_parse.h"
#include "sql/sql_select.h" // Sql_cmd_select...
#include "sql/sql_show.h" // Sql_cmd_show...
#include "sql/sql_show_processlist.h"
#include "sql/sql_show_status.h" // build_show_session_status, ...
#include "sql/sql_update.h" // Sql_cmd_update...
#include "sql/system_variables.h"
#include "sql/table_function.h"
#include "sql/thr_malloc.h"
#include "sql/trigger_def.h"
#include "sql/window.h" // Window
#include "sql_string.h"
#include "template_utils.h"
static constexpr const size_t MAX_SYS_VAR_LENGTH{32};
namespace {
template <typename Context, typename Node>
bool contextualize_safe(Context *pc, Node node) {
if (node == nullptr) return false;
return node->contextualize(pc);
}
template <typename Context>
bool contextualize_safe(Context *pc, mem_root_deque<Item *> *list) {
if (list == nullptr) return false;
for (Item *&item : *list) {
if (item->itemize(pc, &item)) return true;
}
return false;
}
/**
Convenience function that calls Parse_tree_node::contextualize() on each of
the nodes that are non-NULL, stopping when a call returns true.
*/
template <typename Context, typename Node, typename... Nodes>
bool contextualize_safe(Context *pc, Node node, Nodes... nodes) {
return contextualize_safe(pc, node) || contextualize_safe(pc, nodes...);
}
/**
Convenience function that calls Item::itemize() on the item if it's
non-NULL.
*/
bool itemize_safe(Parse_context *pc, Item **item) {
if (*item == nullptr) return false;
return (*item)->itemize(pc, item);
}
} // namespace
Table_ddl_parse_context::Table_ddl_parse_context(THD *thd_arg,
Query_block *select_arg,
Alter_info *alter_info)
: Parse_context(thd_arg, select_arg),
create_info(thd_arg->lex->create_info),
alter_info(alter_info),
key_create_info(&thd_arg->lex->key_create_info) {}
PT_joined_table *PT_table_reference::add_cross_join(PT_cross_join *cj) {
cj->add_rhs(this);
return cj;
}
bool PT_joined_table::contextualize_tabs(Parse_context *pc) {
if (m_left_table_ref != nullptr) return false; // already done
bool was_right_join = m_type & JTT_RIGHT;
// rewrite to LEFT JOIN
if (was_right_join) {
m_type =
static_cast<PT_joined_table_type>((m_type & ~JTT_RIGHT) | JTT_LEFT);
std::swap(m_left_pt_table, m_right_pt_table);
}
if (m_left_pt_table->contextualize(pc) || m_right_pt_table->contextualize(pc))
return true;
m_left_table_ref = m_left_pt_table->m_table_ref;
m_right_table_ref = m_right_pt_table->m_table_ref;
if (m_left_table_ref == nullptr || m_right_table_ref == nullptr) {
error(pc, m_join_pos);
return true;
}
if (m_type & JTT_LEFT) {
m_right_table_ref->outer_join = true;
if (was_right_join) {
m_right_table_ref->join_order_swapped = true;
m_right_table_ref->query_block->set_right_joins();
}
}
return false;
}
bool PT_option_value_no_option_type_charset::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
int flags = opt_charset ? 0 : set_var_collation_client::SET_CS_DEFAULT;
const CHARSET_INFO *cs2;
cs2 =
opt_charset ? opt_charset : global_system_variables.character_set_client;
set_var_collation_client *var;
var = new (thd->mem_root) set_var_collation_client(
flags, cs2, thd->variables.collation_database, cs2);
if (var == nullptr) return true;
lex->var_list.push_back(var);
return false;
}
bool PT_option_value_no_option_type_names::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
sp_pcontext *pctx = lex->get_sp_current_parsing_ctx();
LEX_CSTRING names = {STRING_WITH_LEN("names")};
if (pctx && pctx->find_variable(names.str, names.length, false))
my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
else
error(pc, pos);
return true; // always fails with an error
}
bool PT_set_names::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
const CHARSET_INFO *cs2;
const CHARSET_INFO *cs3;
int flags = set_var_collation_client::SET_CS_NAMES |
(opt_charset ? 0 : set_var_collation_client::SET_CS_DEFAULT) |
(opt_collation ? set_var_collation_client::SET_CS_COLLATE : 0);
cs2 =
opt_charset ? opt_charset : global_system_variables.character_set_client;
if (opt_collation != nullptr) {
if (!my_charset_same(cs2, opt_collation)) {
my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
opt_collation->m_coll_name, cs2->csname);
return true;
}
cs3 = opt_collation;
} else {
if (cs2 == &my_charset_utf8mb4_0900_ai_ci &&
cs2 != thd->variables.default_collation_for_utf8mb4)
cs3 = thd->variables.default_collation_for_utf8mb4;
else
cs3 = cs2;
}
set_var_collation_client *var;
var = new (thd->mem_root) set_var_collation_client(flags, cs3, cs3, cs3);
if (var == nullptr) return true;
lex->var_list.push_back(var);
return false;
}
bool PT_group::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
Query_block *select = pc->select;
select->parsing_place = CTX_GROUP_BY;
if (group_list->contextualize(pc)) return true;
assert(select == pc->select);
select->group_list = group_list->value;
// group by does not have to provide ordering
ORDER *group = select->group_list.first;
for (; group; group = group->next) group->direction = ORDER_NOT_RELEVANT;
// Ensure we're resetting parsing place of the right select
assert(select->parsing_place == CTX_GROUP_BY);
select->parsing_place = CTX_NONE;
switch (olap) {
case UNSPECIFIED_OLAP_TYPE:
break;
case ROLLUP_TYPE:
if (select->linkage == GLOBAL_OPTIONS_TYPE) {
my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
"global union parameters");
return true;
}
select->olap = ROLLUP_TYPE;
break;
default:
assert(!"unexpected OLAP type!");
}
return false;
}
bool PT_order::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->select->parsing_place = CTX_ORDER_BY;
pc->thd->where = "global ORDER clause";
if (order_list->contextualize(pc)) return true;
pc->select->order_list = order_list->value;
// Reset parsing place only for ORDER BY
if (pc->select->parsing_place == CTX_ORDER_BY)
pc->select->parsing_place = CTX_NONE;
pc->thd->where = THD::DEFAULT_WHERE;
return false;
}
bool PT_order_expr::contextualize(Parse_context *pc) {
return super::contextualize(pc) || item_initial->itemize(pc, &item_initial);
}
/**
Recognize the transition variable syntax: { OLD | NEW } "." ...
@param pc Parse context
@param opt_prefix Optional prefix to examine or empty LEX_CSTRING
@param prefix "OLD" or "NEW" to compare with
@returns true if the OLD transition variable syntax, otherwise false
*/
static bool is_transition_variable_prefix(const Parse_context &pc,
const LEX_CSTRING &opt_prefix,
const char *prefix) {
assert(strcmp(prefix, "OLD") == 0 || strcmp(prefix, "NEW") == 0);
sp_head *const sp = pc.thd->lex->sphead;
return opt_prefix.str != nullptr && sp != nullptr &&
sp->m_type == enum_sp_type::TRIGGER &&
my_strcasecmp(system_charset_info, opt_prefix.str, prefix) == 0;
}
static bool is_old_transition_variable_prefix(const Parse_context &pc,
const LEX_CSTRING &opt_prefix) {
return is_transition_variable_prefix(pc, opt_prefix, "OLD");
}
static bool is_new_transition_variable_prefix(const Parse_context &pc,
const LEX_CSTRING &opt_prefix) {
return is_transition_variable_prefix(pc, opt_prefix, "NEW");
}
/**
Recognize any of transition variable syntax: { NEW | OLD } "." ...
@param pc Parse context
@param opt_prefix Optional prefix or empty LEX_CSTRING
@returns true if the transition variable syntax, otherwise false
*/
static bool is_any_transition_variable_prefix(const Parse_context &pc,
const LEX_CSTRING &opt_prefix) {
return is_old_transition_variable_prefix(pc, opt_prefix) ||
is_new_transition_variable_prefix(pc, opt_prefix);
}
/**
Try to resolve a name as SP formal parameter or local variable
@note Doesn't output error messages on failure.
@param pc Parse context
@param name Name of probable SP variable
@returns pointer to SP formal parameter/local variable object on success,
otherwise nullptr
*/
static sp_variable *find_sp_variable(const Parse_context &pc,
const LEX_CSTRING &name) {
const sp_pcontext *pctx = pc.thd->lex->get_sp_current_parsing_ctx();
return pctx ? pctx->find_variable(name.str, name.length, false) : nullptr;
}
/**
Helper action for a SET statement.
Used to SET a field of NEW row.
@param pc the parse context
@param trigger_field_name the NEW-row field name
@param expr_item the value expression to be assigned
@param expr_pos the value expression query
@return error status (true if error, false otherwise).
*/
static bool set_trigger_new_row(Parse_context *pc,
LEX_CSTRING trigger_field_name, Item *expr_item,
const POS &expr_pos) {
THD *thd = pc->thd;
LEX *lex = thd->lex;
sp_head *sp = lex->sphead;
assert(expr_item != nullptr);
if (sp->m_trg_chistics.event == TRG_EVENT_DELETE) {
my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
return true;
}
if (sp->m_trg_chistics.action_time == TRG_ACTION_AFTER) {
my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
return true;
}
LEX_CSTRING expr_query{};
if (lex->is_metadata_used()) {
expr_query = make_string(thd, expr_pos.raw.start, expr_pos.raw.end);
if (expr_query.str == nullptr) return true; // OOM
}
assert(sp->m_trg_chistics.action_time == TRG_ACTION_BEFORE &&
(sp->m_trg_chistics.event == TRG_EVENT_INSERT ||
sp->m_trg_chistics.event == TRG_EVENT_UPDATE));
Item_trigger_field *trg_fld = new (pc->mem_root) Item_trigger_field(
POS(), TRG_NEW_ROW, trigger_field_name.str, UPDATE_ACL, false);
if (trg_fld == nullptr || trg_fld->itemize(pc, (Item **)&trg_fld))
return true;
assert(trg_fld->type() == Item::TRIGGER_FIELD_ITEM);
auto *sp_instr = new (pc->mem_root)
sp_instr_set_trigger_field(sp->instructions(), lex, trigger_field_name,
trg_fld, expr_item, expr_query);
if (sp_instr == nullptr) return true; // OM
/*
Add this item to list of all Item_trigger_field objects in trigger.
*/
sp->m_cur_instr_trig_field_items.link_in_list(trg_fld,
&trg_fld->next_trg_field);
return sp->add_instr(thd, sp_instr);
}
/**
Helper action for a SET statement.
Used to push a system variable into the assignment list.
In stored routines detects "SET AUTOCOMMIT" and reject "SET GTID..." stuff.
@param thd the current thread
@param prefix left hand side of a qualified name (if any)
or nullptr string
@param suffix the right hand side of a qualified name (if any)
or a whole unqualified name
@param var_type the scope of the variable
@param val the value being assigned to the variable
@return true if error, false otherwise.
*/
static bool add_system_variable_assignment(THD *thd, LEX_CSTRING prefix,
LEX_CSTRING suffix,
enum enum_var_type var_type,
Item *val) {
System_variable_tracker var_tracker = System_variable_tracker::make_tracker(
to_string_view(prefix), to_string_view(suffix));
if (var_tracker.access_system_variable(thd)) {
return true;
}
LEX *lex = thd->lex;
sp_head *sp = lex->sphead;
sp_pcontext *pctx = lex->get_sp_current_parsing_ctx();
/* No AUTOCOMMIT from a stored function or trigger. */
if (pctx != nullptr && var_tracker.eq_static_sys_var(Sys_autocommit_ptr))
sp->m_flags |= sp_head::HAS_SET_AUTOCOMMIT_STMT;
if (lex->uses_stored_routines() &&
(var_tracker.eq_static_sys_var(Sys_gtid_next_ptr) ||
#ifdef HAVE_GTID_NEXT_LIST
var_tracker.eq_static_sys_var(Sys_gtid_next_list_ptr) ||
#endif
var_tracker.eq_static_sys_var(Sys_gtid_purged_ptr))) {
my_error(ER_SET_STATEMENT_CANNOT_INVOKE_FUNCTION, MYF(0),
var_tracker.get_var_name());
return true;
}
/*
If the set value is a field, change it to a string to allow things like
SET table_type=MYISAM;
*/
if (val && val->type() == Item::FIELD_ITEM) {
Item_field *item_field = down_cast<Item_field *>(val);
if (item_field->table_name != nullptr) {
// Reject a dot-separated identifier as the RHS of:
// SET <variable_name> = <table_name>.<field_name>
my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var_tracker.get_var_name());
return true;
}
assert(item_field->field_name != nullptr);
val =
new Item_string(item_field->field_name, strlen(item_field->field_name),
system_charset_info); // names are utf8
if (val == nullptr) return true; // OOM
}
set_var *var = new (thd->mem_root) set_var(var_type, var_tracker, val);
if (var == nullptr) return true;
return lex->var_list.push_back(var);
}
bool PT_set_variable::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || itemize_safe(pc, &m_opt_expr)) {
return true;
}
const bool is_1d_name = m_opt_prefix.str == nullptr;
/*
1. Reject `SET OLD.<name>` in triggers:
*/
if (is_old_transition_variable_prefix(*pc, m_opt_prefix)) {
my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
return true;
}
/*
2. Process NEW.<name> in triggers:
*/
if (is_new_transition_variable_prefix(*pc, m_opt_prefix)) {
if (m_opt_expr == nullptr) {
error(pc, m_expr_pos); // Reject the syntax: SET NEW.<column> = DEFAULT
return true;
}
if (set_trigger_new_row(pc, m_name, m_opt_expr, m_expr_pos)) return true;
return false;
}
/*
3. Process SP local variable assignment:
*/
THD *const thd = pc->thd;
LEX *const lex = thd->lex;
if (is_1d_name) {
// Remove deprecation warning when FULL is used as keyword.
if (thd->get_stmt_da()->has_sql_condition(ER_WARN_DEPRECATED_IDENT)) {
thd->get_stmt_da()->reset_condition_info(thd);
}
sp_variable *spv = find_sp_variable(*pc, m_name);
if (spv != nullptr) {
if (m_opt_expr == nullptr) {
error(pc,
m_expr_pos); // Reject the syntax: SET <sp var name> = DEFAULT
return true;
}
LEX_CSTRING expr_query{};
if (lex->is_metadata_used()) {
expr_query = make_string(thd, m_expr_pos.raw.start, m_expr_pos.raw.end);
if (expr_query.str == nullptr) return true; // OOM
}
/*
NOTE: every SET-expression has its own LEX-object, even if it is
a multiple SET-statement, like:
SET spv1 = expr1, spv2 = expr2, ...
Every SET-expression has its own sp_instr_set. Thus, the
instruction owns the LEX-object, i.e. the instruction is
responsible for destruction of the LEX-object.
*/
sp_head *const sp = lex->sphead;
auto *sp_instr = new (thd->mem_root) sp_instr_set(
sp->instructions(), lex, spv->offset, m_opt_expr, expr_query,
true); // The instruction owns its lex.
return sp_instr == nullptr || sp->add_instr(thd, sp_instr);
}
}
/*
4. Process assignment of a system variable, including:
* Multiple Key Cache variables
* plugin variables
* component variables
*/
return add_system_variable_assignment(thd, m_opt_prefix, m_name,
lex->option_type, m_opt_expr);
}
bool PT_option_value_no_option_type_password_for::contextualize(
Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
set_var_password *var;
lex->contains_plaintext_password = true;
/*
In case of anonymous user, user->user is set to empty string with
length 0. But there might be case when user->user.str could be NULL.
For Ex: "set password for current_user() = password('xyz');".
In this case, set user information as of the current user.
*/
if (!user->user.str) {
LEX_CSTRING sctx_priv_user = thd->security_context()->priv_user();
assert(sctx_priv_user.str);
user->user.str = sctx_priv_user.str;
user->user.length = sctx_priv_user.length;
}
if (!user->host.str) {
LEX_CSTRING sctx_priv_host = thd->security_context()->priv_host();
assert(sctx_priv_host.str);
user->host.str = sctx_priv_host.str;
user->host.length = sctx_priv_host.length;
}
// Current password is specified through the REPLACE clause hence set the flag
if (current_password != nullptr) user->uses_replace_clause = true;
if (random_password_generator) password = nullptr;
var = new (thd->mem_root) set_var_password(
user, const_cast<char *>(password), const_cast<char *>(current_password),
retain_current_password, random_password_generator);
if (var == nullptr || lex->var_list.push_back(var)) {
return true; // Out of memory
}
lex->sql_command = SQLCOM_SET_PASSWORD;
if (lex->sphead) lex->sphead->m_flags |= sp_head::HAS_SET_AUTOCOMMIT_STMT;
if (sp_create_assignment_instr(pc->thd, expr_pos.raw.end)) return true;
return false;
}
bool PT_option_value_no_option_type_password::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
sp_head *sp = lex->sphead;
sp_pcontext *pctx = lex->get_sp_current_parsing_ctx();
LEX_CSTRING pw = {STRING_WITH_LEN("password")};
lex->contains_plaintext_password = true;
if (pctx && pctx->find_variable(pw.str, pw.length, false)) {
my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
return true;
}
LEX_CSTRING sctx_user = thd->security_context()->user();
LEX_CSTRING sctx_priv_host = thd->security_context()->priv_host();
assert(sctx_priv_host.str);
LEX_USER *user = LEX_USER::alloc(thd, (LEX_STRING *)&sctx_user,
(LEX_STRING *)&sctx_priv_host);
if (!user) return true;
if (random_password_generator) password = nullptr;
set_var_password *var = new (thd->mem_root) set_var_password(
user, const_cast<char *>(password), const_cast<char *>(current_password),
retain_current_password, random_password_generator);
if (var == nullptr || lex->var_list.push_back(var)) {
return true; // Out of Memory
}
lex->sql_command = SQLCOM_SET_PASSWORD;
if (sp) sp->m_flags |= sp_head::HAS_SET_AUTOCOMMIT_STMT;
if (sp_create_assignment_instr(pc->thd, expr_pos.raw.end)) return true;
return false;
}
PT_key_part_specification::PT_key_part_specification(Item *expression,
enum_order order)
: m_expression(expression), m_order(order) {}
PT_key_part_specification::PT_key_part_specification(
const LEX_CSTRING &column_name, enum_order order, int prefix_length)
: m_expression(nullptr),
m_order(order),
m_column_name(column_name),
m_prefix_length(prefix_length) {}
bool PT_key_part_specification::contextualize(Parse_context *pc) {
return super::contextualize(pc) || itemize_safe(pc, &m_expression);
}
bool PT_select_sp_var::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *lex = pc->thd->lex;
#ifndef NDEBUG
sp = lex->sphead;
#endif
sp_pcontext *pctx = lex->get_sp_current_parsing_ctx();
sp_variable *spv;
if (!pctx || !(spv = pctx->find_variable(name.str, name.length, false))) {
my_error(ER_SP_UNDECLARED_VAR, MYF(0), name.str);
return true;
}
offset = spv->offset;
return false;
}
Sql_cmd *PT_select_stmt::make_cmd(THD *thd) {
Parse_context pc(thd, thd->lex->current_query_block());
thd->lex->sql_command = m_sql_command;
if (m_qe->contextualize(&pc)) {
return nullptr;
}
const bool has_into_clause_inside_query_block = thd->lex->result != nullptr;
if (has_into_clause_inside_query_block && m_into != nullptr) {
my_error(ER_MULTIPLE_INTO_CLAUSES, MYF(0));
return nullptr;
}
if (contextualize_safe(&pc, m_into)) {
return nullptr;
}
if (pc.finalize_query_expression()) return nullptr;
if (m_into != nullptr && m_has_trailing_locking_clauses) {
// Example: ... INTO ... FOR UPDATE;
push_warning(thd, ER_WARN_DEPRECATED_INNER_INTO);
} else if (has_into_clause_inside_query_block &&
thd->lex->unit->is_set_operation()) {
// Example: ... UNION ... INTO ...;
if (!m_qe->has_trailing_into_clause()) {
// Example: ... UNION SELECT * INTO OUTFILE 'foo' FROM ...;
push_warning(thd, ER_WARN_DEPRECATED_INNER_INTO);
} else if (m_has_trailing_locking_clauses) {
// Example: ... UNION SELECT ... FROM ... INTO OUTFILE 'foo' FOR UPDATE;
push_warning(thd, ER_WARN_DEPRECATED_INNER_INTO);
}
}
DBUG_EXECUTE_IF("ast", Query_term *qn =
pc.select->master_query_expression()->query_term();
std::ostringstream buf; qn->debugPrint(0, buf);
DBUG_PRINT("ast", ("\n%s", buf.str().c_str())););
if (thd->lex->sql_command == SQLCOM_SELECT)
return new (thd->mem_root) Sql_cmd_select(thd->lex->result);
else // (thd->lex->sql_command == SQLCOM_DO)
return new (thd->mem_root) Sql_cmd_do(nullptr);
}
/*
Given a table in the source list, find a correspondent table in the
list of table references.
@param tbl Source table to match.
@param tables Table references list.
@remark The source table list (tables listed before the FROM clause
or tables listed in the FROM clause before the USING clause) may
contain table names or aliases that must match unambiguously one,
and only one, table in the target table list (table references list,
after FROM/USING clause).
@return Matching table, NULL if error.
*/
static Table_ref *multi_delete_table_match(Table_ref *tbl, Table_ref *tables) {
Table_ref *match = nullptr;
DBUG_TRACE;
for (Table_ref *elem = tables; elem; elem = elem->next_local) {
int cmp;
if (tbl->is_fqtn && elem->is_alias) continue; /* no match */
if (tbl->is_fqtn && elem->is_fqtn)
cmp = my_strcasecmp(table_alias_charset, tbl->table_name,
elem->table_name) ||
strcmp(tbl->db, elem->db);
else if (elem->is_alias)
cmp = my_strcasecmp(table_alias_charset, tbl->alias, elem->alias);
else
cmp = my_strcasecmp(table_alias_charset, tbl->table_name,
elem->table_name) ||
strcmp(tbl->db, elem->db);
if (cmp) continue;
if (match) {
my_error(ER_NONUNIQ_TABLE, MYF(0), elem->alias);
return nullptr;
}
match = elem;
}
if (!match)
my_error(ER_UNKNOWN_TABLE, MYF(0), tbl->table_name, "MULTI DELETE");
return match;
}
/**
Link tables in auxiliary table list of multi-delete with corresponding
elements in main table list, and set proper locks for them.
@param pc Parse context
@param delete_tables List of tables to delete from
@returns false if success, true if error
*/
static bool multi_delete_link_tables(Parse_context *pc,
SQL_I_List<Table_ref> *delete_tables) {
DBUG_TRACE;
Table_ref *tables = pc->select->get_table_list();
for (Table_ref *target_tbl = delete_tables->first; target_tbl;
target_tbl = target_tbl->next_local) {
/* All tables in aux_tables must be found in FROM PART */
Table_ref *walk = multi_delete_table_match(target_tbl, tables);
if (!walk) return true;
if (!walk->is_derived()) {
target_tbl->table_name = walk->table_name;
target_tbl->table_name_length = walk->table_name_length;
}
walk->updating = target_tbl->updating;
walk->set_lock(target_tbl->lock_descriptor());
/* We can assume that tables to be deleted from are locked for write. */
assert(walk->lock_descriptor().type >= TL_WRITE_ALLOW_WRITE);
walk->mdl_request.set_type(mdl_type_for_dml(walk->lock_descriptor().type));
target_tbl->correspondent_table = walk; // Remember corresponding table
}
return false;
}
bool PT_delete::add_table(Parse_context *pc, Table_ident *table) {
const ulong table_opts = is_multitable()
? TL_OPTION_UPDATING | TL_OPTION_ALIAS
: TL_OPTION_UPDATING;
const thr_lock_type lock_type = (opt_delete_options & DELETE_LOW_PRIORITY)
? TL_WRITE_LOW_PRIORITY
: TL_WRITE_DEFAULT;
const enum_mdl_type mdl_type = (opt_delete_options & DELETE_LOW_PRIORITY)
? MDL_SHARED_WRITE_LOW_PRIO
: MDL_SHARED_WRITE;
return !pc->select->add_table_to_list(
pc->thd, table, opt_table_alias, table_opts, lock_type, mdl_type, nullptr,
opt_use_partition, nullptr, pc);
}
Sql_cmd *PT_delete::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
Parse_context pc(thd, select);
assert(lex->query_block == select);
lex->sql_command = is_multitable() ? SQLCOM_DELETE_MULTI : SQLCOM_DELETE;
lex->set_ignore(opt_delete_options & DELETE_IGNORE);
select->init_order();
if (opt_delete_options & DELETE_QUICK) select->add_base_options(OPTION_QUICK);
if (contextualize_safe(&pc, m_with_clause))
return nullptr; /* purecov: inspected */
if (is_multitable()) {
for (Table_ident **i = table_list.begin(); i != table_list.end(); ++i) {
if (add_table(&pc, *i)) return nullptr;
}
} else if (add_table(&pc, table_ident))
return nullptr;
if (is_multitable()) {
select->m_table_list.save_and_clear(&delete_tables);
lex->query_tables = nullptr;
lex->query_tables_last = &lex->query_tables;
} else {
select->m_table_nest.push_back(select->get_table_list());
}
Yacc_state *const yyps = &pc.thd->m_parser_state->m_yacc;
yyps->m_lock_type = TL_READ_DEFAULT;
yyps->m_mdl_type = MDL_SHARED_READ;
if (is_multitable()) {
if (contextualize_array(&pc, &join_table_list)) return nullptr;
pc.select->context.table_list =
pc.select->context.first_name_resolution_table =
pc.select->get_table_list();
}
if (opt_where_clause != nullptr &&
opt_where_clause->itemize(&pc, &opt_where_clause))
return nullptr;
select->set_where_cond(opt_where_clause);
if (opt_order_clause != nullptr && opt_order_clause->contextualize(&pc))
return nullptr;
assert(select->select_limit == nullptr);
if (opt_delete_limit_clause != nullptr) {
if (opt_delete_limit_clause->itemize(&pc, &opt_delete_limit_clause))
return nullptr;
select->select_limit = opt_delete_limit_clause;
lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
}
if (is_multitable() && multi_delete_link_tables(&pc, &delete_tables))
return nullptr;
if (opt_hints != nullptr && opt_hints->contextualize(&pc)) return nullptr;
return new (thd->mem_root) Sql_cmd_delete(is_multitable(), &delete_tables);
}
Sql_cmd *PT_update::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
Parse_context pc(thd, select);
lex->duplicates = DUP_ERROR;
lex->set_ignore(opt_ignore);
if (contextualize_safe(&pc, m_with_clause))
return nullptr; /* purecov: inspected */
if (contextualize_array(&pc, &join_table_list)) return nullptr;
select->parsing_place = CTX_UPDATE_VALUE;
if (column_list->contextualize(&pc) || value_list->contextualize(&pc)) {
return nullptr;
}
select->fields = column_list->value;
// Ensure we're resetting parsing context of the right select
assert(select->parsing_place == CTX_UPDATE_VALUE);
select->parsing_place = CTX_NONE;
const bool is_multitable = select->m_table_list.elements > 1;
lex->sql_command = is_multitable ? SQLCOM_UPDATE_MULTI : SQLCOM_UPDATE;
/*
In case of multi-update setting write lock for all tables may
be too pessimistic. We will decrease lock level if possible in
Sql_cmd_update::prepare_inner().
*/
select->set_lock_for_tables(opt_low_priority);
if (opt_where_clause != nullptr &&
opt_where_clause->itemize(&pc, &opt_where_clause)) {
return nullptr;
}
select->set_where_cond(opt_where_clause);
if (opt_order_clause != nullptr && opt_order_clause->contextualize(&pc))
return nullptr;
assert(select->select_limit == nullptr);
if (opt_limit_clause != nullptr) {
if (opt_limit_clause->itemize(&pc, &opt_limit_clause)) return nullptr;
select->select_limit = opt_limit_clause;
lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
}
if (opt_hints != nullptr && opt_hints->contextualize(&pc)) return nullptr;
return new (thd->mem_root) Sql_cmd_update(is_multitable, &value_list->value);
}
bool PT_insert_values_list::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
for (List_item *item_list : many_values) {
for (auto it = item_list->begin(); it != item_list->end(); ++it) {
if ((*it)->itemize(pc, &*it)) return true;
}
}
return false;
}
Sql_cmd *PT_insert::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
Parse_context pc(thd, lex->current_query_block());
// Currently there are two syntaxes (old and new, respectively) for INSERT
// .. VALUES statements:
//
// - INSERT .. VALUES (), () ..
// - INSERT .. VALUES ROW(), ROW() ..
//
// The latter is a table value constructor, i.e. it has an subquery
// expression, while the former is the standard VALUES syntax. When the
// non-standard VALUES() function (primarily used in ON DUPLICATE KEY UPDATE
// update expressions) is deprecated in the future, the old syntax can be used
// as a table value constructor as well.
//
// However, until such a change is made, we convert INSERT statements with
// table value constructors into PT_insert objects that are equal to the old
// syntax, as to enforce consistency by making sure they both follow the same
// execution path.
//
// Note that this removes the constness of both row_value_list and
// insert_query_expression, which should both be restored when deprecating
// VALUES as mentioned above.
if (has_query_block() &&
insert_query_expression->is_table_value_constructor()) {
row_value_list = insert_query_expression->get_row_value_list();
assert(row_value_list != nullptr);
insert_query_expression = nullptr;
}
if (is_replace) {
lex->sql_command =
has_query_block() ? SQLCOM_REPLACE_SELECT : SQLCOM_REPLACE;
lex->duplicates = DUP_REPLACE;
} else {
lex->sql_command = has_query_block() ? SQLCOM_INSERT_SELECT : SQLCOM_INSERT;
lex->duplicates = DUP_ERROR;
lex->set_ignore(ignore);
}
Yacc_state *yyps = &pc.thd->m_parser_state->m_yacc;
if (!pc.select->add_table_to_list(
thd, table_ident, nullptr, TL_OPTION_UPDATING, yyps->m_lock_type,
yyps->m_mdl_type, nullptr, opt_use_partition)) {
return nullptr;
}
pc.select->set_lock_for_tables(lock_option);
assert(lex->current_query_block() == lex->query_block);
if (column_list->contextualize(&pc)) return nullptr;
if (has_query_block()) {
/*
In INSERT/REPLACE INTO t ... SELECT the table_list initially contains
here a table entry for the destination table `t'.
Backup it and clean the table list for the processing of
the query expression and push `t' back to the beginning of the
table_list finally.
@todo: Don't save the INSERT/REPLACE destination table in
Query_block::table_list and remove this backup & restore.
The following work only with the local list, the global list
is created correctly in this case
*/
SQL_I_List<Table_ref> save_list;
Query_block *const save_query_block = pc.select;
save_query_block->m_table_list.save_and_clear(&save_list);
if (insert_query_expression->contextualize(&pc)) return nullptr;
if (pc.finalize_query_expression()) return nullptr;
/*
The following work only with the local list, the global list
is created correctly in this case
*/
save_query_block->m_table_list.push_front(&save_list);
lex->bulk_insert_row_cnt = 0;
} else {
pc.select->parsing_place = CTX_INSERT_VALUES;
if (row_value_list->contextualize(&pc)) return nullptr;
// Ensure we're resetting parsing context of the right select
assert(pc.select->parsing_place == CTX_INSERT_VALUES);
pc.select->parsing_place = CTX_NONE;
lex->bulk_insert_row_cnt = row_value_list->get_many_values().size();
}
// Create a derived table to use as a table reference to the VALUES rows,
// which can be referred to from ON DUPLICATE KEY UPDATE. Naming the derived
// table columns is deferred to Sql_cmd_insert_base::prepare_inner, as this
// requires the insert table to be resolved.
Table_ref *values_table{nullptr};
if (opt_values_table_alias != nullptr && opt_values_column_list != nullptr) {
if (!strcmp(opt_values_table_alias, table_ident->table.str)) {
my_error(ER_NONUNIQ_TABLE, MYF(0), opt_values_table_alias);
return nullptr;
}
Table_ident *ti = new (pc.thd->mem_root)
Table_ident(lex->query_block->master_query_expression());
if (ti == nullptr) return nullptr;
values_table = pc.select->add_table_to_list(
pc.thd, ti, opt_values_table_alias, 0, TL_READ, MDL_SHARED_READ);
if (values_table == nullptr) return nullptr;
}
if (opt_on_duplicate_column_list != nullptr) {
assert(!is_replace);
assert(opt_on_duplicate_value_list != nullptr &&
opt_on_duplicate_value_list->elements() ==
opt_on_duplicate_column_list->elements());
lex->duplicates = DUP_UPDATE;
Table_ref *first_table = lex->query_block->get_table_list();
/* Fix lock for ON DUPLICATE KEY UPDATE */
if (first_table->lock_descriptor().type == TL_WRITE_CONCURRENT_DEFAULT)
first_table->set_lock({TL_WRITE_DEFAULT, THR_DEFAULT});
pc.select->parsing_place = CTX_INSERT_UPDATE;
if (opt_on_duplicate_column_list->contextualize(&pc) ||
opt_on_duplicate_value_list->contextualize(&pc))
return nullptr;
// Ensure we're resetting parsing context of the right select
assert(pc.select->parsing_place == CTX_INSERT_UPDATE);
pc.select->parsing_place = CTX_NONE;
}
if (opt_hints != nullptr && opt_hints->contextualize(&pc)) return nullptr;
Sql_cmd_insert_base *sql_cmd;
if (has_query_block())
sql_cmd =
new (thd->mem_root) Sql_cmd_insert_select(is_replace, lex->duplicates);
else
sql_cmd =
new (thd->mem_root) Sql_cmd_insert_values(is_replace, lex->duplicates);
if (sql_cmd == nullptr) return nullptr;
if (!has_query_block()) {
sql_cmd->insert_many_values = row_value_list->get_many_values();
sql_cmd->values_table = values_table;
sql_cmd->values_column_list = opt_values_column_list;
}
sql_cmd->insert_field_list = column_list->value;
if (opt_on_duplicate_column_list != nullptr) {
assert(!is_replace);
sql_cmd->update_field_list = opt_on_duplicate_column_list->value;
sql_cmd->update_value_list = opt_on_duplicate_value_list->value;
}
return sql_cmd;
}
Sql_cmd *PT_call::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
Parse_context pc(thd, lex->current_query_block());
if (opt_expr_list != nullptr && opt_expr_list->contextualize(&pc))
return nullptr; /* purecov: inspected */
lex->sql_command = SQLCOM_CALL;
sp_add_own_used_routine(lex, thd, Sroutine_hash_entry::PROCEDURE, proc_name);
mem_root_deque<Item *> *proc_args = nullptr;
if (opt_expr_list != nullptr) proc_args = &opt_expr_list->value;
return new (thd->mem_root) Sql_cmd_call(proc_name, proc_args);
}
bool PT_query_specification::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->m_stack.push_back(QueryLevel(pc->mem_root, SC_QUERY_SPECIFICATION));
pc->select->parsing_place = CTX_SELECT_LIST;
if (options.query_spec_options & SELECT_HIGH_PRIORITY) {
Yacc_state *yyps = &pc->thd->m_parser_state->m_yacc;
yyps->m_lock_type = TL_READ_HIGH_PRIORITY;
yyps->m_mdl_type = MDL_SHARED_READ;
}
if (options.save_to(pc)) return true;
if (item_list->contextualize(pc)) return true;
// Ensure we're resetting parsing place of the right select
assert(pc->select->parsing_place == CTX_SELECT_LIST);
pc->select->parsing_place = CTX_NONE;
if (contextualize_safe(pc, opt_into1)) return true;
if (!from_clause.empty()) {
if (contextualize_array(pc, &from_clause)) return true;
pc->select->context.table_list =
pc->select->context.first_name_resolution_table =
pc->select->get_table_list();
}
if (itemize_safe(pc, &opt_where_clause) ||
contextualize_safe(pc, opt_group_clause) ||
itemize_safe(pc, &opt_having_clause))
return true;
pc->select->set_where_cond(opt_where_clause);
pc->select->set_having_cond(opt_having_clause);
/*
Window clause is resolved under CTX_SELECT_LIST and not
under CTX_WINDOW. Reasons being:
1. Window functions are part of select list and the
resolution of window definition happens along with
window functions.
2. It is tricky to resolve window definition under CTX_WINDOW
and window functions under CTX_SELECT_LIST.
3. Unnamed window definitions are anyways naturally placed in
select list.
4. Named window definition are not placed in select list of
the query. But if this window definition is
used by any window functions, then we resolve under CTX_SELECT_LIST.
5. Because of all of the above, unused window definitions are
resolved under CTX_SELECT_LIST. (These unused window definitions
are removed after syntactic and semantic checks are done).
*/
pc->select->parsing_place = CTX_SELECT_LIST;
if (contextualize_safe(pc, opt_window_clause)) return true;
pc->select->parsing_place = CTX_NONE;
QueryLevel ql = pc->m_stack.back();
pc->m_stack.pop_back();
pc->m_stack.back().m_elts.push_back(pc->select);
return (opt_hints != nullptr ? opt_hints->contextualize(pc) : false);
}
bool PT_table_value_constructor::contextualize(Parse_context *pc) {
pc->m_stack.push_back(QueryLevel(pc->mem_root, SC_TABLE_VALUE_CONSTRUCTOR));
if (row_value_list->contextualize(pc)) return true;
pc->select->is_table_value_constructor = true;
pc->select->row_value_list = &row_value_list->get_many_values();
// Some queries, such as CREATE TABLE with SELECT, require item_list to
// contain items to call Query_block::prepare.
for (Item *item : *pc->select->row_value_list->front()) {
pc->select->fields.push_back(item);
}
QueryLevel ql = pc->m_stack.back();
pc->m_stack.pop_back();
pc->m_stack.back().m_elts.push_back(pc->select);
return false;
}
bool PT_query_expression::contextualize_order_and_limit(Parse_context *pc) {
/*
Quick reject test. We don't need to do anything if there are no limit
or order by clauses.
*/
if (m_order == nullptr && m_limit == nullptr) return false;
if (m_body->can_absorb_order_and_limit(m_order != nullptr,
m_limit != nullptr)) {
if (contextualize_safe(pc, m_order, m_limit)) return true;
} else {
LEX *lex = pc->thd->lex;
Query_block *orig_query_block = pc->select;
pc->select = nullptr;
lex->push_context(&pc->select->context);
assert(pc->select->parsing_place == CTX_NONE);
bool res = contextualize_safe(pc, m_order, m_limit);
lex->pop_context();
pc->select = orig_query_block;
if (res) return true;
}
return false;
}
bool PT_table_factor_function::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || m_expr->itemize(pc, &m_expr)) return true;
if (m_path->itemize(pc, &m_path)) return true;
auto nested_columns = new (pc->mem_root) List<Json_table_column>;
if (nested_columns == nullptr) return true; // OOM
for (auto col : *m_nested_columns) {
if (col->contextualize(pc) || nested_columns->push_back(col->get_column()))
return true;
}
auto root_el = new (pc->mem_root) Json_table_column(m_path, nested_columns);
auto *root_list = new (pc->mem_root) List<Json_table_column>;
if (root_el == nullptr || root_list == nullptr ||
root_list->push_front(root_el))
return true; // OOM
auto jtf = new (pc->mem_root)
Table_function_json(m_table_alias.str, m_expr, root_list);
if (jtf == nullptr) return true; // OOM
LEX_CSTRING alias;
alias.length = strlen(jtf->func_name());
alias.str = sql_strmake(jtf->func_name(), alias.length);
if (alias.str == nullptr) return true; // OOM
auto ti = new (pc->mem_root) Table_ident(alias, jtf);
if (ti == nullptr) return true;
m_table_ref = pc->select->add_table_to_list(pc->thd, ti, m_table_alias.str, 0,
TL_READ, MDL_SHARED_READ);
if (m_table_ref == nullptr || pc->select->add_joined_table(m_table_ref))
return true;
return false;
}
PT_derived_table::PT_derived_table(bool lateral, PT_subquery *subquery,
const LEX_CSTRING &table_alias,
Create_col_name_list *column_names)
: m_lateral(lateral),
m_subquery(subquery),
m_table_alias(table_alias.str),
column_names(*column_names) {
m_subquery->m_is_derived_table = true;
}
bool PT_derived_table::contextualize(Parse_context *pc) {
Query_block *outer_query_block = pc->select;
outer_query_block->parsing_place = CTX_DERIVED;
assert(outer_query_block->linkage != GLOBAL_OPTIONS_TYPE);
/*
Determine the immediate outer context for the derived table:
- if lateral: context of query which owns the FROM i.e. outer_query_block
- if not lateral: context of query outer to query which owns the FROM.
For a lateral derived table. this is just a preliminary decision.
Name resolution {Item_field,Item_ref}::fix_fields() may use or ignore this
outer context depending on where the derived table is placed in it.
*/
if (!m_lateral) {
pc->thd->lex->push_context(outer_query_block->context.outer_context);
}
if (m_subquery->contextualize(pc)) return true;
if (!m_lateral) pc->thd->lex->pop_context();
outer_query_block->parsing_place = CTX_NONE;
assert(pc->select->next_query_block() == nullptr);
Query_expression *unit = pc->select->first_inner_query_expression();
pc->select = outer_query_block;
Table_ident *ti = new (pc->mem_root) Table_ident(unit);
if (ti == nullptr) return true;
m_table_ref = pc->select->add_table_to_list(pc->thd, ti, m_table_alias, 0,
TL_READ, MDL_SHARED_READ);
if (m_table_ref == nullptr) return true;
if (column_names.size()) m_table_ref->set_derived_column_names(&column_names);
if (m_lateral) {
// Mark the unit as LATERAL, by turning on one bit in the map:
m_table_ref->derived_query_expression()->m_lateral_deps =
OUTER_REF_TABLE_BIT;
}
if (pc->select->add_joined_table(m_table_ref)) return true;
return false;
}
bool PT_table_factor_joined_table::contextualize(Parse_context *pc) {
if (Parse_tree_node::contextualize(pc)) return true;
Query_block *outer_query_block = pc->select;
if (outer_query_block->init_nested_join(pc->thd)) return true;
if (m_joined_table->contextualize(pc)) return true;
m_table_ref = m_joined_table->m_table_ref;
if (outer_query_block->end_nested_join() == nullptr) return true;
return false;
}
static Surrounding_context qt2sc(Query_term_type qtt) {
switch (qtt) {
case QT_UNION:
return SC_UNION_ALL;
case QT_EXCEPT:
return SC_EXCEPT_ALL;
case QT_INTERSECT:
return SC_INTERSECT_ALL;
default:
assert(false);
}
return SC_TOP;
}
/// Append the children of 'lower' to those of 'setop'. To avoid excessive
/// space usage of a large number of similar set ops: instead of the "obvious"
/// method of copying the operands of the child's array to the parent's array,
/// we may use the (possibly much) larger lower setop's child array as a basis,
/// inserting setop's childen at the front and then std::move'ing that (now
/// discarded) array to be setop's child array. This makes the space
/// allocation ~= linear instead of O(N*N/2) in the worst case (N: # of set
/// operations).
void PT_set_operation::merge_children(Query_term_set_op *setop,
Query_term_set_op *lower) {
if (lower->m_children.size() <= setop->m_children.size()) {
for (auto child : lower->m_children) {
setop->m_children.push_back(child);
}
} else {
const size_t lim = setop->m_children.size() - 1;
for (size_t i = 0; i < setop->m_children.size(); ++i) {
lower->m_children.push_front(setop->m_children[lim - i]);
}
setop->m_children = std::move(lower->m_children);
// lower->m_children is now empty.
}
}
/**
Possibly merge lower syntactic levels of set operations (UNION, INTERSECT and
EXCEPT) into setop, and set new last DISTINCT index for setop. We only ever
merge set operations of the same kind, but even that, not always: we prefer
streaming of UNIONs when possible, and merging UNIONs with a mix of DISTINCT
and ALL at the top level may lead to inefficient evaluation. Streaming only
makes sense when the result set is sent from the server, i.e. not further
materialized, e.g. for ORDER BY or windowing.
For example, the query:
EXPLAIN FORMAT=tree
SELECT * FROM t1 UNION DISTINCT
SELECT * FROM t2 UNION ALL
SELECT * FROM t3;
will yield the plan:
-> Append
-> Stream results
-> Table scan on \<union temporary\>
-> Union materialize with deduplication
-> Table scan on t1
-> Table scan on t2
-> Stream results
-> Table scan on t3
but
EXPLAIN FORMAT=tree
SELECT * FROM t1 UNION DISTINCT
SELECT * FROM t2 UNION DISTINCT
SELECT * FROM t3;
will yield the following plan, i.e. we merge the two syntactic levels. The
former case could also be merged, but the we'd need to write the rows of t3
to the temporary table, which gives a performance penalty, so we don't
merge.
-> Table scan on \<union temporary\>
-> Union materialize with deduplication
-> Table scan on t1
-> Table scan on t2
-> Table scan on t3
If have an outer ORDER BY, we will see how this would look (with merge):
EXPLAIN FORMAT=tree
SELECT * FROM t1 UNION DISTINCT
SELECT * FROM t2 UNION ALL
SELECT * FROM t3
ORDER BY a;
will yield
-> Sort: a
-> Table scan on \<union temporary\>
-> Union materialize with deduplication
-> Table scan on t1
-> Table scan on t2
-> Disable deduplication
-> Table scan on t3
since in this case, merging is advantageous, in that we need only one
temporary file. Another interesting case is when the upper level is
DISTINCT:
EXPLAIN FORMAT=tree
SELECT * FROM t1 UNION DISTINCT
( SELECT * FROM t2 UNION ALL
SELECT * FROM t3 );
will merge and remove the lower ALL level:
-> Table scan on <union temporary>
-> Union materialize with deduplication
-> Table scan on t1
-> Table scan on t2
-> Table scan on t3
For INTERSECT, the presence of one DISTINCT operator effectively
makes any INTERSECT ALL equivalent to DISTINCT, so we only retain ALL if
all operators are ALL, i.e. for a N-ary INTERSECT with at least one DISTINCT,
has_mixed_distinct_operators always returns false.
We aggressively merge up all sub-nests for INTERSECT.
INTERSECT ALL is only binary due to current implementation method, no merge
up.
EXCEPT [ALL] is not right associative, so be careful when merging: we only
merge up a left-most nested EXCEPT into an outer level EXCEPT, since we
evaluate from left to right. Note that for an N-ary EXCEPT operation, a mix
of ALL and DISTINCT is meaningful and supported. After the first operator
with DISTINCT, further ALL operators are moot since no duplicates are
left. Example explain:
SELECT * FROM r EXCEPT ALL SELECT * FROM s EXCEPT SELECT * FROM t
-> Table scan on \<except temporary\> (cost=..)
-> Except materialize with deduplication (cost=..)
-> Table scan on r (cost=..)
-> Disable deduplication
-> Table scan on s (cost=..)
-> Table scan on t (cost=..)
We can see that the first two operand tables are compared with ALL ("disable
de-duplication"), whereas the final one, t, is DISTINCT.
@param pc the parse context
@param setop the set operation query term to be filled in with children
@param ql parsing query level
*/
void PT_set_operation::merge_descendants(Parse_context *pc,
Query_term_set_op *setop,
QueryLevel &ql) {
// If a child is a Query_block, include it in this set op list as is. If a
// child is itself a set operation, include its members in this set
// operation's descendant list if allowed.
int count = 0; /// computes number for members if we collapse
// last_distinct is used by UNION and INTERSECT. For N-ary intersect, if
// DISTINCT is present at all, its final value will be N-1, since ALL is
// irrelevant once we have one DISTINCT for INTERSECT. Note that the
// computation of last_distinct may be wrong for EXCEPT, but it doesn't rely
// on it so we don't care.
int64_t last_distinct = 0;
// first_distinct is used by EXCEPT only, so its computation for UNION and
// DISTINCT may be wrong/misleading.
int64_t first_distinct = std::numeric_limits<int64_t>::max();
const Query_term_type op = setop->term_type();
Surrounding_context sc = qt2sc(op);
if (m_is_distinct) {
for (Query_term *elt : ql.m_elts) {
// We only merge same kind, and only if no LIMIT on it
if (elt->term_type() == QT_QUERY_BLOCK || /* 1 */
elt->term_type() != op || /* 2 */
(op == QT_EXCEPT && count > 0)) { /* 3 */
// (1) Nothing nested in this operand position, or (2) it's another
// kind of set operation or (3) EXCEPT in non-left position (EXCEPT is
// not right associative), hence no merge.
count++;
if (first_distinct == std::numeric_limits<int64_t>::max())
first_distinct = 1;
last_distinct = count - 1;
setop->m_children.push_back(elt);
} else if (Query_term_set_op *lower = down_cast<Query_term_set_op *>(elt);
elt->query_block()->select_limit == nullptr /* 4 */) {
// (4) we have no LIMIT in the nest, so we can merge, proceed.
if (lower->m_first_distinct < std::numeric_limits<int64_t>::max() &&
lower->m_first_distinct + count < first_distinct) {
assert(op != QT_EXCEPT || count == 0);
first_distinct = count + lower->m_first_distinct;
} else if (first_distinct == std::numeric_limits<int64_t>::max()) {
first_distinct = lower->m_children.size();
}
// upper DISTINCT trumps lower ALL or DISTINCT, so ignore nest's last
// distinct
last_distinct = count + lower->m_children.size() - 1;
count = count + lower->m_children.size();
merge_children(setop, lower);
} else {
// similar kind of set operation, but contains limit, so do not merge
count++;
last_distinct = count - 1;
setop->m_children.push_back(elt);
}
}
} else {
for (Query_term *elt : ql.m_elts) {
// We only collapse same kind, and only if no LIMIT on it. Also, we do
// not collapse INTERSECT ALL due to difficulty in computing it if we
// do, cf. logic in MaterializeIterator<Profiler>::MaterializeQueryBlock.
if (elt->term_type() == QT_QUERY_BLOCK || /* 1 */
elt->term_type() != op || /* 2 */
(op == QT_EXCEPT && count > 0) || /* 3 */
elt->query_block()->select_limit != nullptr) {
// (1) Nothing nested in this operand position, or (2) it's another kind
// of set operation, or non-left EXCEPT (3), hence no merge
count++;
setop->m_children.push_back(elt);
} else if (Query_term_set_op *lower = down_cast<Query_term_set_op *>(elt);
(count == 0 || // first operand can always be merged
// upper, lower are ALL, so ok:
lower->m_last_distinct == 0 ||
op == QT_INTERSECT /* maybe merge */)) {
if (lower->m_last_distinct > 0) {
if (pc->is_top_level_union_all(sc)) {
// If we merge we lose the chance to stream the right table,
// so don't.
setop->m_children.push_back(elt);
count++;
} else {
if (lower->m_first_distinct < std::numeric_limits<int64_t>::max() &&
first_distinct == std::numeric_limits<int64_t>::max()) {
first_distinct = count + lower->m_first_distinct;
}
last_distinct = count + lower->m_last_distinct;
count = count + lower->m_children.size();
merge_children(setop, lower);
}
} else {
// upper and lower level are both ALL, so ok to merge, unless we have
// INTERSECT
if (op != QT_INTERSECT) {
if (lower->m_first_distinct < std::numeric_limits<int64_t>::max() &&
first_distinct == std::numeric_limits<int64_t>::max()) {
first_distinct = count + lower->m_first_distinct;
}
count = count + lower->m_children.size();
merge_children(setop, lower);
} else {
// do not merge INTERSECT ALL: the execution time logic can only
// handle binary INTERSECT ALL.
count++;
setop->m_children.push_back(elt);
}
}
} else if (!pc->is_top_level_union_all(sc) &&
lower->m_last_distinct == 0) {
// We have a higher and lower level setop ALL, so ok to merge
// unless we have a top level UNION ALL in which case we prefer
// streaming.
if (count + lower->m_first_distinct <
std::numeric_limits<int64_t>::max() &&
first_distinct == std::numeric_limits<int64_t>::max()) {
first_distinct = count + lower->m_first_distinct;
}
count = count + lower->m_children.size();
merge_children(setop, lower);
} else {
// do not merge
count++;
setop->m_children.push_back(elt);
}
}
}
if (last_distinct > 0 && op == QT_INTERSECT) // distinct always wins
last_distinct = setop->m_children.size() - 1;
setop->m_last_distinct = last_distinct;
setop->m_first_distinct = first_distinct;
}
bool PT_set_operation::contextualize_setop(Parse_context *pc,
Query_term_type setop_type,
Surrounding_context context) {
pc->m_stack.push_back(QueryLevel(pc->mem_root, context));
if (super::contextualize(pc)) return true;
if (m_list[0]->contextualize(pc)) return true;
List_iterator<PT_query_expression_body> it(m_list);
PT_query_expression_body *elt;
it++; // skip first
while ((elt = it++)) {
pc->select = pc->thd->lex->new_set_operation_query(pc->select);
if (pc->select == nullptr || elt->contextualize(pc)) return true;
pc->thd->lex->pop_context();
}
QueryLevel ql = pc->m_stack.back();
pc->m_stack.pop_back();
Query_term_set_op *setop = nullptr;
switch (setop_type) {
case QT_UNION:
setop = new (pc->mem_root) Query_term_union(pc->mem_root);
break;
case QT_EXCEPT:
setop = new (pc->mem_root) Query_term_except(pc->mem_root);
break;
case QT_INTERSECT:
setop = new (pc->mem_root) Query_term_intersect(pc->mem_root);
break;
default:
assert(false);
}
if (setop == nullptr) return true;
merge_descendants(pc, setop, ql);
setop->label_children();
Query_expression *qe = pc->select->master_query_expression();
if (setop->set_block(qe->create_post_processing_block(setop))) return true;
pc->m_stack.back().m_elts.push_back(setop);
return false;
}
bool PT_union::contextualize(Parse_context *pc) {
return contextualize_setop(pc, QT_UNION,
m_is_distinct ? SC_UNION_DISTINCT : SC_UNION_ALL);
}
bool PT_except::contextualize(Parse_context *pc [[maybe_unused]]) {
return contextualize_setop(
pc, QT_EXCEPT, m_is_distinct ? SC_EXCEPT_DISTINCT : SC_EXCEPT_ALL);
}
bool PT_intersect::contextualize(Parse_context *pc [[maybe_unused]]) {
return contextualize_setop(
pc, QT_INTERSECT,
m_is_distinct ? SC_INTERSECT_DISTINCT : SC_INTERSECT_ALL);
}
static bool setup_index(keytype key_type, const LEX_STRING name,
PT_base_index_option *type,
List<PT_key_part_specification> *columns,
Index_options options, Table_ddl_parse_context *pc) {
*pc->key_create_info = default_key_create_info;
if (type != nullptr && type->contextualize(pc)) return true;
if (contextualize_nodes(options, pc)) return true;
List_iterator<PT_key_part_specification> li(*columns);
if ((key_type == KEYTYPE_FULLTEXT || key_type == KEYTYPE_SPATIAL ||
pc->key_create_info->algorithm == HA_KEY_ALG_HASH)) {
PT_key_part_specification *kp;
while ((kp = li++)) {
if (kp->is_explicit()) {
my_error(ER_WRONG_USAGE, MYF(0), "spatial/fulltext/hash index",
"explicit index order");
return true;
}
}
}
List<Key_part_spec> cols;
for (PT_key_part_specification &kp : *columns) {
if (kp.contextualize(pc)) return true;
Key_part_spec *spec;
if (kp.has_expression()) {
spec =
new (pc->mem_root) Key_part_spec(kp.get_expression(), kp.get_order());
} else {
spec = new (pc->mem_root) Key_part_spec(
kp.get_column_name(), kp.get_prefix_length(), kp.get_order());
}
if (spec == nullptr || cols.push_back(spec)) {
return true; /* purecov: deadcode */
}
}
Key_spec *key =
new (pc->mem_root) Key_spec(pc->mem_root, key_type, to_lex_cstring(name),
pc->key_create_info, false, true, cols);
if (key == nullptr || pc->alter_info->key_list.push_back(key)) return true;
return false;
}
Sql_cmd *PT_create_index_stmt::make_cmd(THD *thd) {
LEX *lex = thd->lex;
Query_block *query_block = lex->current_query_block();
thd->lex->sql_command = SQLCOM_CREATE_INDEX;
if (query_block->add_table_to_list(thd, m_table_ident, nullptr,
TL_OPTION_UPDATING, TL_READ_NO_INSERT,
MDL_SHARED_UPGRADABLE) == nullptr)
return nullptr;
Table_ddl_parse_context pc(thd, query_block, &m_alter_info);
m_alter_info.flags = Alter_info::ALTER_ADD_INDEX;
if (setup_index(m_keytype, m_name, m_type, m_columns, m_options, &pc))
return nullptr;
m_alter_info.requested_algorithm = m_algo;
m_alter_info.requested_lock = m_lock;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_create_index(&m_alter_info);
}
bool PT_inline_index_definition::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
if (setup_index(m_keytype, m_name, m_type, m_columns, m_options, pc))
return true;
if (m_keytype == KEYTYPE_PRIMARY && !pc->key_create_info->is_visible)
my_error(ER_PK_INDEX_CANT_BE_INVISIBLE, MYF(0));
return false;
}
bool PT_foreign_key_definition::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *const thd = pc->thd;
LEX *const lex = thd->lex;
LEX_CSTRING db;
LEX_CSTRING orig_db;
if (m_referenced_table->db.str) {
orig_db = m_referenced_table->db;
if (check_db_name(orig_db.str, orig_db.length) != Ident_name_check::OK)
return true;
if (lower_case_table_names) {
char *db_str = thd->strmake(orig_db.str, orig_db.length);
if (db_str == nullptr) return true; // OOM
db.length = my_casedn_str(files_charset_info, db_str);
db.str = db_str;
} else
db = orig_db;
} else {
/*
Before 8.0 foreign key metadata was handled by SEs and they
assumed that parent table belongs to the same database as
child table unless FQTN was used (and connection's current
database was ignored). We keep behavior compatible even
though this is inconsistent with interpretation of non-FQTN
table names in other contexts.
If this is ALTER TABLE with RENAME TO <db_name.table_name>
clause we need to use name of the target database.
*/
if (pc->alter_info->new_db_name.str) {
db = orig_db = pc->alter_info->new_db_name;
} else {
Table_ref *child_table = lex->query_block->get_table_list();
db = orig_db = LEX_CSTRING{child_table->db, child_table->db_length};
}
}
Ident_name_check ident_check_status = check_table_name(
m_referenced_table->table.str, m_referenced_table->table.length);
if (ident_check_status != Ident_name_check::OK) {
my_error(ER_WRONG_TABLE_NAME, MYF(0), m_referenced_table->table.str);
return true;
}
LEX_CSTRING table_name;
if (lower_case_table_names) {
char *table_name_str = thd->strmake(m_referenced_table->table.str,
m_referenced_table->table.length);
if (table_name_str == nullptr) return true; // OOM
table_name.length = my_casedn_str(files_charset_info, table_name_str);
table_name.str = table_name_str;
} else
table_name = m_referenced_table->table;
lex->key_create_info = default_key_create_info;
/*
If present name from the CONSTRAINT clause is used as name of generated
supporting index (which is created in cases when there is no explicitly
created supporting index). Otherwise, the FOREIGN KEY index_name value
is used. If both are missing name of generated supporting index is
automatically produced.
*/
const LEX_CSTRING key_name =
to_lex_cstring(m_constraint_name.str ? m_constraint_name
: m_key_name.str ? m_key_name
: NULL_STR);
if (key_name.str && check_string_char_length(key_name, "", NAME_CHAR_LEN,
system_charset_info, true)) {
my_error(ER_TOO_LONG_IDENT, MYF(0), key_name.str);
return true;
}
List<Key_part_spec> cols;
for (PT_key_part_specification &kp : *m_columns) {
if (kp.contextualize(pc)) return true;
Key_part_spec *spec = new (pc->mem_root) Key_part_spec(
kp.get_column_name(), kp.get_prefix_length(), kp.get_order());
if (spec == nullptr || cols.push_back(spec)) {
return true; /* purecov: deadcode */
}
}
/*
We always use value from CONSTRAINT clause as a foreign key name.
If it is not present we use generated name as a foreign key name
(i.e. we ignore value from FOREIGN KEY index_name part).
Validity of m_constraint_name has been already checked by the code
above that handles supporting index name.
*/
Key_spec *foreign_key = new (pc->mem_root) Foreign_key_spec(
pc->mem_root, to_lex_cstring(m_constraint_name), cols, db, orig_db,
table_name, m_referenced_table->table, m_ref_list, m_fk_delete_opt,
m_fk_update_opt, m_fk_match_option);
if (foreign_key == nullptr || pc->alter_info->key_list.push_back(foreign_key))
return true;
/* Only used for ALTER TABLE. Ignored otherwise. */
pc->alter_info->flags |= Alter_info::ADD_FOREIGN_KEY;
Key_spec *key =
new (pc->mem_root) Key_spec(thd->mem_root, KEYTYPE_MULTIPLE, key_name,
&default_key_create_info, true, true, cols);
if (key == nullptr || pc->alter_info->key_list.push_back(key)) return true;
return false;
}
bool PT_with_list::push_back(PT_common_table_expr *el) {
const LEX_STRING &n = el->name();
for (auto previous : m_elements) {
const LEX_STRING &pn = previous->name();
if (pn.length == n.length && !memcmp(pn.str, n.str, n.length)) {
my_error(ER_NONUNIQ_TABLE, MYF(0), n.str);
return true;
}
}
return m_elements.push_back(el);
}
PT_common_table_expr::PT_common_table_expr(
const LEX_STRING &name, const LEX_STRING &subq_text, uint subq_text_offs,
PT_subquery *subq_node, const Create_col_name_list *column_names,
MEM_ROOT *mem_root)
: m_name(name),
m_subq_text(subq_text),
m_subq_text_offset(subq_text_offs),
m_subq_node(subq_node),
m_column_names(*column_names),
m_postparse(mem_root) {
if (lower_case_table_names && m_name.length) {
// Lowercase name, as in Query_block::add_table_to_list()
m_name.length = my_casedn_str(files_charset_info, m_name.str);
}
m_postparse.name = m_name;
}
bool PT_with_clause::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true; /* purecov: inspected */
// WITH complements a query expression (a unit).
pc->select->master_query_expression()->m_with_clause = this;
return false;
}
void PT_with_clause::print(const THD *thd, String *str,
enum_query_type query_type) {
size_t len1 = str->length();
str->append("with ");
if (m_recursive) str->append("recursive ");
size_t len2 = str->length(), len3 = len2;
for (auto el : m_list->elements()) {
if (str->length() != len3) {
str->append(", ");
len3 = str->length();
}
el->print(thd, str, query_type);
}
if (str->length() == len2)
str->length(len1); // don't print an empty WITH clause
else
str->append(" ");
}
void PT_common_table_expr::print(const THD *thd, String *str,
enum_query_type query_type) {
size_t len = str->length();
append_identifier(thd, str, m_name.str, m_name.length);
if (m_column_names.size())
print_derived_column_names(thd, str, &m_column_names);
str->append(" as ");
/*
Printing the raw text (this->m_subq_text) would lack:
- expansion of '||' (which can mean CONCAT or OR, depending on
sql_mode's PIPES_AS_CONCAT (the effect would be that a view containing
a CTE containing '||' would change behaviour if sql_mode was
changed between its creation and its usage).
- quoting of table identifiers
- expansion of the default db.
So, we rather locate one resolved query expression for this CTE; for
it to be intact this query expression must be non-merged. And we print
it.
If query expression has been merged everywhere, its Query_expression is
gone and printing this CTE can be skipped. Note that when we print the
view's body to the data dictionary, no merging is done.
*/
bool found = false;
for (auto *tl : m_postparse.references) {
if (!tl->is_merged() &&
// If 2+ references exist, show the one which is shown in EXPLAIN
tl->query_block_id_for_explain() == tl->query_block_id()) {
str->append('(');
tl->derived_query_expression()->print(thd, str, query_type);
str->append(')');
found = true;
break;
}
}
if (!found) str->length(len); // don't print a useless CTE definition
}
bool PT_create_table_engine_option::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->create_info->used_fields |= HA_CREATE_USED_ENGINE;
const bool is_temp_table = pc->create_info->options & HA_LEX_CREATE_TMP_TABLE;
return resolve_engine(pc->thd, engine, is_temp_table, false,
&pc->create_info->db_type);
}
bool PT_create_table_secondary_engine_option::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->create_info->used_fields |= HA_CREATE_USED_SECONDARY_ENGINE;
pc->create_info->secondary_engine = m_secondary_engine;
return false;
}
bool PT_create_stats_auto_recalc_option::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
switch (value) {
case Ternary_option::ON:
pc->create_info->stats_auto_recalc = HA_STATS_AUTO_RECALC_ON;
break;
case Ternary_option::OFF:
pc->create_info->stats_auto_recalc = HA_STATS_AUTO_RECALC_OFF;
break;
case Ternary_option::DEFAULT:
pc->create_info->stats_auto_recalc = HA_STATS_AUTO_RECALC_DEFAULT;
break;
default:
assert(false);
}
pc->create_info->used_fields |= HA_CREATE_USED_STATS_AUTO_RECALC;
return false;
}
bool PT_create_stats_stable_pages::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->create_info->stats_sample_pages = value;
pc->create_info->used_fields |= HA_CREATE_USED_STATS_SAMPLE_PAGES;
return false;
}
bool PT_create_union_option::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *const thd = pc->thd;
LEX *const lex = thd->lex;
const Yacc_state *yyps = &thd->m_parser_state->m_yacc;
Table_ref **exclude_merge_engine_tables = lex->query_tables_last;
SQL_I_List<Table_ref> save_list;
lex->query_block->m_table_list.save_and_clear(&save_list);
if (pc->select->add_tables(thd, tables, TL_OPTION_UPDATING, yyps->m_lock_type,
yyps->m_mdl_type))
return true;
/*
Move the union list to the merge_list and exclude its tables
from the global list.
*/
pc->create_info->merge_list = lex->query_block->m_table_list;
lex->query_block->m_table_list = save_list;
/*
When excluding union list from the global list we assume that
elements of the former immediately follow elements which represent
table being created/altered and parent tables.
*/
assert(*exclude_merge_engine_tables == pc->create_info->merge_list.first);
*exclude_merge_engine_tables = nullptr;
lex->query_tables_last = exclude_merge_engine_tables;
pc->create_info->used_fields |= HA_CREATE_USED_UNION;
return false;
}
bool set_default_charset(HA_CREATE_INFO *create_info,
const CHARSET_INFO *value) {
assert(value != nullptr);
if ((create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
create_info->default_table_charset &&
!my_charset_same(create_info->default_table_charset, value)) {
my_error(ER_CONFLICTING_DECLARATIONS, MYF(0), "CHARACTER SET ",
create_info->default_table_charset->csname, "CHARACTER SET ",
value->csname);
return true;
}
if ((create_info->used_fields & HA_CREATE_USED_DEFAULT_COLLATE) == 0)
create_info->default_table_charset = value;
create_info->used_fields |= HA_CREATE_USED_DEFAULT_CHARSET;
return false;
}
bool PT_create_table_default_charset::contextualize(
Table_ddl_parse_context *pc) {
return (super::contextualize(pc) ||
set_default_charset(pc->create_info, value));
}
bool set_default_collation(HA_CREATE_INFO *create_info,
const CHARSET_INFO *collation) {
assert(collation != nullptr);
assert((create_info->default_table_charset == nullptr) ==
((create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) == 0));
if (merge_charset_and_collation(create_info->default_table_charset, collation,
&create_info->default_table_charset)) {
return true;
}
create_info->used_fields |= HA_CREATE_USED_DEFAULT_CHARSET;
create_info->used_fields |= HA_CREATE_USED_DEFAULT_COLLATE;
return false;
}
bool PT_create_table_default_collation::contextualize(
Table_ddl_parse_context *pc) {
return (super::contextualize(pc) ||
set_default_collation(pc->create_info, value));
}
bool PT_locking_clause::contextualize(Parse_context *pc) {
LEX *lex = pc->thd->lex;
if (lex->is_explain()) return false;
if (m_locked_row_action == Locked_row_action::SKIP)
lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SKIP_LOCKED);
if (m_locked_row_action == Locked_row_action::NOWAIT)
lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_NOWAIT);
lex->safe_to_cache_query = false;
return set_lock_for_tables(pc);
}
using Local_tables_iterator =
IntrusiveListIterator<Table_ref, &Table_ref::next_local>;
/// A list interface over the Table_ref::next_local pointer.
using Local_tables_list = IteratorContainer<Local_tables_iterator>;
bool PT_query_block_locking_clause::set_lock_for_tables(Parse_context *pc) {
Local_tables_list local_tables(pc->select->get_table_list());
for (Table_ref *table_list : local_tables)
if (!table_list->is_derived()) {
if (table_list->lock_descriptor().type != TL_READ_DEFAULT) {
my_error(ER_DUPLICATE_TABLE_LOCK, MYF(0), table_list->alias);
return true;
}
pc->select->set_lock_for_table(get_lock_descriptor(), table_list);
}
return false;
}
bool PT_column_def::contextualize(Table_ddl_parse_context *pc) {
// Since Alter_info objects are allocated on a mem_root and never
// destroyed we (move)-assign an empty vector to cf_appliers to
// ensure any dynamic memory is released. This must be done whenever
// leaving this scope since appliers may be added in
// field_def->contextualize(pc).
auto clr_appliers = create_scope_guard([&]() {
pc->alter_info->cf_appliers = decltype(pc->alter_info->cf_appliers)();
});
if (super::contextualize(pc) || field_def->contextualize(pc) ||
contextualize_safe(pc, opt_column_constraint))
return true;
pc->alter_info->flags |= field_def->alter_info_flags;
dd::Column::enum_hidden_type field_hidden_type =
(field_def->type_flags & FIELD_IS_INVISIBLE)
? dd::Column::enum_hidden_type::HT_HIDDEN_USER
: dd::Column::enum_hidden_type::HT_VISIBLE;
return pc->alter_info->add_field(
pc->thd, &field_ident, field_def->type, field_def->length, field_def->dec,
field_def->type_flags, field_def->default_value,
field_def->on_update_value, &field_def->comment, nullptr,
field_def->interval_list, field_def->charset,
field_def->has_explicit_collation, field_def->uint_geom_type,
field_def->gcol_info, field_def->default_val_info, opt_place,
field_def->m_srid, field_def->check_const_spec_list, field_hidden_type);
}
Sql_cmd *PT_create_table_stmt::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
lex->sql_command = SQLCOM_CREATE_TABLE;
Parse_context pc(thd, lex->current_query_block());
Table_ref *table = pc.select->add_table_to_list(
thd, table_name, nullptr, TL_OPTION_UPDATING, TL_WRITE, MDL_SHARED);
if (table == nullptr) return nullptr;
table->open_strategy = Table_ref::OPEN_FOR_CREATE;
lex->create_info = &m_create_info;
Table_ddl_parse_context pc2(thd, pc.select, &m_alter_info);
pc2.create_info->options = 0;
if (is_temporary) pc2.create_info->options |= HA_LEX_CREATE_TMP_TABLE;
if (only_if_not_exists)
pc2.create_info->options |= HA_LEX_CREATE_IF_NOT_EXISTS;
pc2.create_info->default_table_charset = nullptr;
lex->name.str = nullptr;
lex->name.length = 0;
Table_ref *qe_tables = nullptr;
if (opt_like_clause != nullptr) {
pc2.create_info->options |= HA_LEX_CREATE_TABLE_LIKE;
Table_ref **like_clause_table = &lex->query_tables->next_global;
Table_ref *src_table = pc.select->add_table_to_list(
thd, opt_like_clause, nullptr, 0, TL_READ, MDL_SHARED_READ);
if (!src_table) return nullptr;
/* CREATE TABLE ... LIKE is not allowed for views. */
src_table->required_type = dd::enum_table_type::BASE_TABLE;
qe_tables = *like_clause_table;
} else {
if (opt_table_element_list) {
for (auto element : *opt_table_element_list) {
if (element->contextualize(&pc2)) return nullptr;
}
}
if (opt_create_table_options) {
for (auto option : *opt_create_table_options)
if (option->contextualize(&pc2)) return nullptr;
}
if (opt_partitioning) {
Table_ref **exclude_part_tables = lex->query_tables_last;
if (opt_partitioning->contextualize(&pc)) return nullptr;
/*
Remove all tables used in PARTITION clause from the global table
list. Partitioning with subqueries is not allowed anyway.
*/
*exclude_part_tables = nullptr;
lex->query_tables_last = exclude_part_tables;
lex->part_info = &opt_partitioning->part_info;
}
switch (on_duplicate) {
case On_duplicate::IGNORE_DUP:
lex->set_ignore(true);
break;
case On_duplicate::REPLACE_DUP:
lex->duplicates = DUP_REPLACE;
break;
case On_duplicate::ERROR:
lex->duplicates = DUP_ERROR;
break;
}
if (opt_query_expression) {
Table_ref **query_expression_tables = &lex->query_tables->next_global;
/*
In CREATE TABLE t ... SELECT the table_list initially contains
here a table entry for the destination table `t'.
Backup it and clean the table list for the processing of
the query expression and push `t' back to the beginning of the
table_list finally.
@todo: Don't save the CREATE destination table in
Query_block::table_list and remove this backup & restore.
The following work only with the local list, the global list
is created correctly in this case
*/
SQL_I_List<Table_ref> save_list;
Query_block *const save_query_block = pc.select;
save_query_block->m_table_list.save_and_clear(&save_list);
if (opt_query_expression->contextualize(&pc)) return nullptr;
if (pc.finalize_query_expression()) return nullptr;
/*
The following work only with the local list, the global list
is created correctly in this case
*/
save_query_block->m_table_list.push_front(&save_list);
qe_tables = *query_expression_tables;
}
}
lex->set_current_query_block(pc.select);
if ((pc2.create_info->used_fields & HA_CREATE_USED_ENGINE) &&
!pc2.create_info->db_type) {
pc2.create_info->db_type =
pc2.create_info->options & HA_LEX_CREATE_TMP_TABLE
? ha_default_temp_handlerton(thd)
: ha_default_handlerton(thd);
push_warning_printf(
thd, Sql_condition::SL_WARNING, ER_WARN_USING_OTHER_HANDLER,
ER_THD(thd, ER_WARN_USING_OTHER_HANDLER),
ha_resolve_storage_engine_name(pc2.create_info->db_type),
table_name->table.str);
}
create_table_set_open_action_and_adjust_tables(lex);
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_create_table(&m_alter_info, qe_tables);
}
bool PT_table_locking_clause::set_lock_for_tables(Parse_context *pc) {
assert(!m_tables.empty());
for (Table_ident *table_ident : m_tables) {
Query_block *select = pc->select;
Table_ref *table_list = select->find_table_by_name(table_ident);
THD *thd = pc->thd;
if (table_list == nullptr)
return raise_error(thd, table_ident, ER_UNRESOLVED_TABLE_LOCK);
if (table_list->lock_descriptor().type != TL_READ_DEFAULT)
return raise_error(thd, table_ident, ER_DUPLICATE_TABLE_LOCK);
select->set_lock_for_table(get_lock_descriptor(), table_list);
}
return false;
}
bool PT_show_table_base::make_table_base_cmd(THD *thd, bool *temporary) {
LEX *const lex = thd->lex;
Parse_context pc(thd, lex->current_query_block());
lex->sql_command = m_sql_command;
// Create empty query block and add user specified table.
Table_ref **query_tables_last = lex->query_tables_last;
Query_block *schema_query_block = lex->new_empty_query_block();
if (schema_query_block == nullptr) return true;
Table_ref *tbl = schema_query_block->add_table_to_list(
thd, m_table_ident, nullptr, 0, TL_READ, MDL_SHARED_READ);
if (tbl == nullptr) return true;
lex->query_tables_last = query_tables_last;
if (m_wild.str && lex->set_wild(m_wild)) return true; // OOM
TABLE *show_table = find_temporary_table(thd, tbl);
*temporary = show_table != nullptr;
// If its a temporary table then use schema_table implementation,
// otherwise read I_S system view:
if (*temporary) {
Query_block *query_block = lex->current_query_block();
if (m_where != nullptr) {
if (m_where->itemize(&pc, &m_where)) return true;
query_block->set_where_cond(m_where);
}
enum enum_schema_tables schema_table = m_sql_command == SQLCOM_SHOW_FIELDS
? SCH_TMP_TABLE_COLUMNS
: SCH_TMP_TABLE_KEYS;
if (make_schema_query_block(thd, query_block, schema_table)) return true;
Table_ref *table_list = query_block->get_table_list();
table_list->schema_query_block = schema_query_block;
table_list->schema_table_reformed = true;
} else {
Query_block *sel = nullptr;
switch (m_sql_command) {
case SQLCOM_SHOW_FIELDS:
sel = dd::info_schema::build_show_columns_query(
m_pos, thd, m_table_ident, lex->wild, m_where);
break;
case SQLCOM_SHOW_KEYS:
sel = dd::info_schema::build_show_keys_query(m_pos, thd, m_table_ident,
m_where);
break;
default:
assert(false);
sel = nullptr;
break;
}
if (sel == nullptr) return true;
Table_ref *table_list = sel->get_table_list();
table_list->schema_query_block = schema_query_block;
}
return false;
}
static void setup_lex_show_cmd_type(THD *thd, Show_cmd_type show_cmd_type) {
thd->lex->verbose = false;
thd->lex->m_extended_show = false;
switch (show_cmd_type) {
case Show_cmd_type::STANDARD:
break;
case Show_cmd_type::FULL_SHOW:
thd->lex->verbose = true;
break;
case Show_cmd_type::EXTENDED_SHOW:
thd->lex->m_extended_show = true;
break;
case Show_cmd_type::EXTENDED_FULL_SHOW:
thd->lex->verbose = true;
thd->lex->m_extended_show = true;
break;
}
}
Sql_cmd *PT_show_binlog_events::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->mi.log_file_name = m_opt_log_file_name.str;
Parse_context pc(thd, thd->lex->current_query_block());
if (contextualize_safe(&pc, m_opt_limit_clause)) return nullptr; // OOM
return &m_sql_cmd;
}
Sql_cmd *PT_show_binlogs::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_charsets::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_character_set_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_collations::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_collation_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_count_base::make_cmd_generic(
THD *thd, LEX_CSTRING diagnostic_variable_name) {
LEX *const lex = thd->lex;
lex->sql_command = SQLCOM_SELECT;
// SHOW COUNT(*) { ERRORS | WARNINGS } doesn't clear them.
lex->keep_diagnostics = DA_KEEP_DIAGNOSTICS;
Parse_context pc(thd, lex->current_query_block());
Item *var =
get_system_variable(&pc, OPT_SESSION, diagnostic_variable_name, false);
if (var == nullptr) {
assert(false);
return nullptr; // should never happen
}
constexpr const char session_prefix[] = "@@session.";
assert(diagnostic_variable_name.length <= MAX_SYS_VAR_LENGTH);
char buff[sizeof(session_prefix) + MAX_SYS_VAR_LENGTH + 1];
/*
We set the name of Item to @@session.var_name because that then is used
as the column name in the output.
*/
char *end =
strxmov(buff, session_prefix, diagnostic_variable_name.str, nullptr);
var->item_name.copy(buff, end - buff);
add_item_to_list(thd, var);
return new (thd->mem_root) Sql_cmd_select(nullptr);
}
Sql_cmd *PT_show_create_database::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
assert(lex->create_info == nullptr);
lex->create_info = thd->alloc_typed<HA_CREATE_INFO>();
if (lex->create_info == nullptr) return nullptr; // OOM
lex->create_info->options = m_if_not_exists ? HA_LEX_CREATE_IF_NOT_EXISTS : 0;
lex->name = m_name;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_event::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->spname = m_spname;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_function::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->spname = m_spname;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_procedure::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->spname = m_spname;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_table::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
assert(lex->create_info == nullptr);
lex->create_info = thd->alloc_typed<HA_CREATE_INFO>();
if (lex->create_info == nullptr) return nullptr; // OOM
lex->create_info->storage_media = HA_SM_DEFAULT;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_view::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_trigger::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->spname = m_spname;
return &m_sql_cmd;
}
Sql_cmd *PT_show_create_user::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->grant_user = m_user;
return &m_sql_cmd;
}
Sql_cmd *PT_show_databases::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_databases_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_engine_logs::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
assert(lex->create_info == nullptr);
lex->create_info = thd->alloc_typed<HA_CREATE_INFO>();
if (lex->create_info == nullptr) return nullptr; // OOM
if (!m_all && resolve_engine(thd, to_lex_cstring(m_engine), false, true,
&lex->create_info->db_type))
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_engine_mutex::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
assert(lex->create_info == nullptr);
lex->create_info = thd->alloc_typed<HA_CREATE_INFO>();
if (lex->create_info == nullptr) return nullptr; // OOM
if (!m_all && resolve_engine(thd, to_lex_cstring(m_engine), false, true,
&lex->create_info->db_type))
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_engine_status::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
assert(lex->create_info == nullptr);
lex->create_info = thd->alloc_typed<HA_CREATE_INFO>();
if (lex->create_info == nullptr) return nullptr; // OOM
if (!m_all && resolve_engine(thd, to_lex_cstring(m_engine), false, true,
&lex->create_info->db_type))
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_engines::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (prepare_schema_table(thd, lex, nullptr, SCH_ENGINES)) return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_errors::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
// SHOW ERRORS will not clear diagnostics
lex->keep_diagnostics = DA_KEEP_DIAGNOSTICS;
Parse_context pc(thd, thd->lex->current_query_block());
if (contextualize_safe(&pc, m_opt_limit_clause)) return nullptr; // OOM
return &m_sql_cmd;
}
Sql_cmd *PT_show_fields::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
assert(lex->query_block->db == nullptr);
setup_lex_show_cmd_type(thd, m_show_cmd_type);
lex->current_query_block()->parsing_place = CTX_SELECT_LIST;
if (make_table_base_cmd(thd, &m_sql_cmd.m_temporary)) return nullptr;
// WL#6599 opt_describe_column is handled during prepare stage in
// prepare_schema_dd_view instead of execution stage
lex->current_query_block()->parsing_place = CTX_NONE;
return &m_sql_cmd;
}
Sql_cmd *PT_show_keys::make_cmd(THD *thd) {
thd->lex->m_extended_show = m_extended_show;
if (make_table_base_cmd(thd, &m_sql_cmd.m_temporary)) return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_events::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->query_block->db = m_opt_db;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_events_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_master_status::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_open_tables::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
Parse_context pc(thd, lex->query_block);
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (m_where != nullptr) {
if (m_where->itemize(&pc, &m_where)) return nullptr;
lex->query_block->set_where_cond(m_where);
}
lex->query_block->db = m_opt_db;
if (prepare_schema_table(thd, lex, nullptr, SCH_OPEN_TABLES)) return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_plugins::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (prepare_schema_table(thd, lex, nullptr, SCH_PLUGINS)) return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_privileges::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_processlist::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
// Read once, to avoid race conditions.
bool use_pfs = pfs_processlist_enabled;
m_sql_cmd.set_use_pfs(use_pfs);
if (use_pfs) {
if (build_processlist_query(m_pos, thd, m_sql_cmd.verbose()))
return nullptr;
}
return &m_sql_cmd;
}
Sql_cmd *PT_show_routine_code::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_profile::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->profile_options = m_opt_profile_options;
lex->show_profile_query_id = m_opt_query_id;
Parse_context pc(thd, thd->lex->current_query_block());
if (contextualize_safe(&pc, m_opt_limit_clause)) return nullptr; // OOM
if (prepare_schema_table(thd, lex, nullptr, SCH_PROFILES)) return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_profiles::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_relaylog_events::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->mi.log_file_name = m_opt_log_file_name.str;
if (lex->set_channel_name(m_opt_channel_name)) return nullptr; // OOM
Parse_context pc(thd, thd->lex->current_query_block());
if (contextualize_safe(&pc, m_opt_limit_clause)) return nullptr; // OOM
return &m_sql_cmd;
}
Sql_cmd *PT_show_replicas::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
return &m_sql_cmd;
}
Sql_cmd *PT_show_replica_status::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (lex->set_channel_name(m_opt_channel_name)) return nullptr; // OOM
return &m_sql_cmd;
}
Sql_cmd *PT_show_status::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (m_var_type == OPT_SESSION) {
if (build_show_session_status(m_pos, thd, lex->wild, m_where) == nullptr)
return nullptr;
} else if (m_var_type == OPT_GLOBAL) {
if (build_show_global_status(m_pos, thd, lex->wild, m_where) == nullptr)
return nullptr;
}
return &m_sql_cmd;
}
Sql_cmd *PT_show_status_func::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_procedures_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_status_proc::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_procedures_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_table_status::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->query_block->db = m_opt_db;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_tables_query(m_pos, thd, lex->wild, m_where,
true) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_tables::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
setup_lex_show_cmd_type(thd, m_show_cmd_type);
lex->query_block->db = m_opt_db;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_tables_query(m_pos, thd, lex->wild, m_where,
false) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_triggers::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
lex->verbose = m_full;
lex->query_block->db = m_opt_db;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (dd::info_schema::build_show_triggers_query(m_pos, thd, lex->wild,
m_where) == nullptr)
return nullptr;
return &m_sql_cmd;
}
Sql_cmd *PT_show_variables::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
if (m_wild.str && lex->set_wild(m_wild)) return nullptr; // OOM
if (m_var_type == OPT_SESSION) {
if (build_show_session_variables(m_pos, thd, lex->wild, m_where) == nullptr)
return nullptr;
} else if (m_var_type == OPT_GLOBAL) {
if (build_show_global_variables(m_pos, thd, lex->wild, m_where) == nullptr)
return nullptr;
}
return &m_sql_cmd;
}
Sql_cmd *PT_show_warnings::make_cmd(THD *thd) {
LEX *lex = thd->lex;
lex->sql_command = m_sql_command;
// SHOW WARNINGS will not clear diagnostics
lex->keep_diagnostics = DA_KEEP_DIAGNOSTICS;
Parse_context pc(thd, thd->lex->current_query_block());
if (contextualize_safe(&pc, m_opt_limit_clause)) return nullptr; // OOM
return &m_sql_cmd;
}
bool PT_alter_table_change_column::contextualize(Table_ddl_parse_context *pc) {
// Since Alter_info objects are allocated on a mem_root and never
// destroyed we (move)-assign an empty vector to cf_appliers to
// ensure any dynamic memory is released. This must be done whenever
// leaving this scope since appliers may be added in
// m_field_def->contextualize(pc).
auto clr_appliers = create_scope_guard([&]() {
pc->alter_info->cf_appliers = decltype(pc->alter_info->cf_appliers)();
});
if (super::contextualize(pc) || m_field_def->contextualize(pc)) return true;
pc->alter_info->flags |= m_field_def->alter_info_flags;
dd::Column::enum_hidden_type field_hidden_type =
(m_field_def->type_flags & FIELD_IS_INVISIBLE)
? dd::Column::enum_hidden_type::HT_HIDDEN_USER
: dd::Column::enum_hidden_type::HT_VISIBLE;
return pc->alter_info->add_field(
pc->thd, &m_new_name, m_field_def->type, m_field_def->length,
m_field_def->dec, m_field_def->type_flags, m_field_def->default_value,
m_field_def->on_update_value, &m_field_def->comment, m_old_name.str,
m_field_def->interval_list, m_field_def->charset,
m_field_def->has_explicit_collation, m_field_def->uint_geom_type,
m_field_def->gcol_info, m_field_def->default_val_info, m_opt_place,
m_field_def->m_srid, m_field_def->check_const_spec_list,
field_hidden_type);
}
bool PT_alter_table_rename::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true; // OOM
if (m_ident->db.str) {
LEX_STRING db_str = to_lex_string(m_ident->db);
if (check_and_convert_db_name(&db_str, false) != Ident_name_check::OK)
return true;
pc->alter_info->new_db_name = to_lex_cstring(db_str);
} else if (pc->thd->lex->copy_db_to(&pc->alter_info->new_db_name.str,
&pc->alter_info->new_db_name.length)) {
return true;
}
switch (check_table_name(m_ident->table.str, m_ident->table.length)) {
case Ident_name_check::WRONG:
my_error(ER_WRONG_TABLE_NAME, MYF(0), m_ident->table.str);
return true;
case Ident_name_check::TOO_LONG:
my_error(ER_TOO_LONG_IDENT, MYF(0), m_ident->table.str);
return true;
case Ident_name_check::OK:
break;
}
pc->alter_info->new_table_name = m_ident->table;
return false;
}
bool PT_alter_table_convert_to_charset::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true; // OOM
const CHARSET_INFO *const cs =
m_charset ? m_charset : pc->thd->variables.collation_database;
const CHARSET_INFO *const collation = m_collation ? m_collation : cs;
if (!my_charset_same(cs, collation)) {
my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0), collation->m_coll_name,
cs->csname);
return true;
}
if ((pc->create_info->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
pc->create_info->default_table_charset && collation &&
!my_charset_same(pc->create_info->default_table_charset, collation)) {
my_error(ER_CONFLICTING_DECLARATIONS, MYF(0), "CHARACTER SET ",
pc->create_info->default_table_charset->csname, "CHARACTER SET ",
collation->csname);
return true;
}
pc->create_info->table_charset = pc->create_info->default_table_charset =
collation;
pc->create_info->used_fields |=
HA_CREATE_USED_CHARSET | HA_CREATE_USED_DEFAULT_CHARSET;
if (m_collation != nullptr)
pc->create_info->used_fields |= HA_CREATE_USED_DEFAULT_COLLATE;
return false;
}
bool PT_alter_table_add_partition_def_list::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
Partition_parse_context part_pc(pc->thd, &m_part_info,
is_add_or_reorganize_partition());
for (auto *part_def : *m_def_list) {
if (part_def->contextualize(&part_pc)) return true;
}
m_part_info.num_parts = m_part_info.partitions.elements;
return false;
}
bool PT_alter_table_reorganize_partition_into::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *const lex = pc->thd->lex;
lex->no_write_to_binlog = m_no_write_to_binlog;
assert(pc->alter_info->partition_names.is_empty());
pc->alter_info->partition_names = m_partition_names;
Partition_parse_context ppc(pc->thd, &m_partition_info,
is_add_or_reorganize_partition());
for (auto *part_def : *m_into)
if (part_def->contextualize(&ppc)) return true;
m_partition_info.num_parts = m_partition_info.partitions.elements;
lex->part_info = &m_partition_info;
return false;
}
bool PT_alter_table_exchange_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->alter_info->with_validation = m_validation;
String *s = new (pc->mem_root) String(
m_partition_name.str, m_partition_name.length, system_charset_info);
if (s == nullptr || pc->alter_info->partition_names.push_back(s) ||
!pc->select->add_table_to_list(pc->thd, m_table_name, nullptr,
TL_OPTION_UPDATING, TL_READ_NO_INSERT,
MDL_SHARED_NO_WRITE)) {
return true;
}
return false;
}
/**
A common initialization part of ALTER TABLE statement variants.
@param pc The parse context.
@param table_name The name of a table to alter.
@param algo The ALGORITHM clause: inplace, copy etc.
@param lock The LOCK clause: none, shared, exclusive etc.
@param validation The WITH or WITHOUT VALIDATION clause.
@returns false on success, true on error.
*/
static bool init_alter_table_stmt(Table_ddl_parse_context *pc,
Table_ident *table_name,
Alter_info::enum_alter_table_algorithm algo,
Alter_info::enum_alter_table_lock lock,
Alter_info::enum_with_validation validation) {
LEX *lex = pc->thd->lex;
if (!lex->query_block->add_table_to_list(
pc->thd, table_name, nullptr, TL_OPTION_UPDATING, TL_READ_NO_INSERT,
MDL_SHARED_UPGRADABLE))
return true;
lex->query_block->init_order();
pc->create_info->db_type = nullptr;
pc->create_info->default_table_charset = nullptr;
pc->create_info->row_type = ROW_TYPE_NOT_USED;
pc->alter_info->new_db_name =
LEX_CSTRING{lex->query_block->get_table_list()->db,
lex->query_block->get_table_list()->db_length};
lex->no_write_to_binlog = false;
pc->create_info->storage_media = HA_SM_DEFAULT;
pc->alter_info->requested_algorithm = algo;
pc->alter_info->requested_lock = lock;
pc->alter_info->with_validation = validation;
return false;
}
Sql_cmd *PT_alter_table_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_ALTER_TABLE;
thd->lex->create_info = &m_create_info;
Table_ddl_parse_context pc(thd, thd->lex->current_query_block(),
&m_alter_info);
if (init_alter_table_stmt(&pc, m_table_name, m_algo, m_lock, m_validation))
return nullptr;
if (m_opt_actions) {
/*
Move RENAME TO <table_name> clauses to the head of array, so they are
processed before ADD FOREIGN KEY clauses. The latter need to know target
database name for proper contextualization.
Use stable sort to preserve order of other clauses which might be
sensitive to it.
*/
std::stable_sort(
m_opt_actions->begin(), m_opt_actions->end(),
[](const PT_ddl_table_option *lhs, const PT_ddl_table_option *rhs) {
return lhs->is_rename_table() && !rhs->is_rename_table();
});
for (auto *action : *m_opt_actions)
if (action->contextualize(&pc)) return nullptr;
}
if ((pc.create_info->used_fields & HA_CREATE_USED_ENGINE) &&
!pc.create_info->db_type) {
pc.create_info->used_fields &= ~HA_CREATE_USED_ENGINE;
}
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_alter_table(&m_alter_info);
}
Sql_cmd *PT_alter_table_standalone_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_ALTER_TABLE;
thd->lex->create_info = &m_create_info;
Table_ddl_parse_context pc(thd, thd->lex->current_query_block(),
&m_alter_info);
if (init_alter_table_stmt(&pc, m_table_name, m_algo, m_lock, m_validation) ||
m_action->contextualize(&pc))
return nullptr;
thd->lex->alter_info = &m_alter_info;
return m_action->make_cmd(&pc);
}
Sql_cmd *PT_repair_table_stmt::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
lex->sql_command = SQLCOM_REPAIR;
Query_block *const select = lex->current_query_block();
lex->no_write_to_binlog = m_no_write_to_binlog;
lex->check_opt.flags |= m_flags;
lex->check_opt.sql_flags |= m_sql_flags;
if (select->add_tables(thd, m_table_list, TL_OPTION_UPDATING, TL_UNLOCK,
MDL_SHARED_READ))
return nullptr;
lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_repair_table(&m_alter_info);
}
Sql_cmd *PT_analyze_table_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_ANALYZE;
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
lex->no_write_to_binlog = m_no_write_to_binlog;
if (select->add_tables(thd, m_table_list, TL_OPTION_UPDATING, TL_UNLOCK,
MDL_SHARED_READ))
return nullptr;
thd->lex->alter_info = &m_alter_info;
auto cmd = new (thd->mem_root) Sql_cmd_analyze_table(
thd, &m_alter_info, m_command, m_num_buckets, m_data);
if (cmd == nullptr) return nullptr;
if (m_command != Sql_cmd_analyze_table::Histogram_command::NONE) {
if (cmd->set_histogram_fields(m_columns)) return nullptr;
}
return cmd;
}
Sql_cmd *PT_check_table_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_CHECK;
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
if (lex->sphead) {
my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
return nullptr;
}
lex->check_opt.flags |= m_flags;
lex->check_opt.sql_flags |= m_sql_flags;
if (select->add_tables(thd, m_table_list, TL_OPTION_UPDATING, TL_UNLOCK,
MDL_SHARED_READ))
return nullptr;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_check_table(&m_alter_info);
}
Sql_cmd *PT_optimize_table_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_OPTIMIZE;
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
lex->no_write_to_binlog = m_no_write_to_binlog;
if (select->add_tables(thd, m_table_list, TL_OPTION_UPDATING, TL_UNLOCK,
MDL_SHARED_READ))
return nullptr;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_optimize_table(&m_alter_info);
}
Sql_cmd *PT_drop_index_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_DROP_INDEX;
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
m_alter_info.flags = Alter_info::ALTER_DROP_INDEX;
m_alter_info.drop_list.push_back(&m_alter_drop);
if (!select->add_table_to_list(thd, m_table, nullptr, TL_OPTION_UPDATING,
TL_READ_NO_INSERT, MDL_SHARED_UPGRADABLE))
return nullptr;
m_alter_info.requested_algorithm = m_algo;
m_alter_info.requested_lock = m_lock;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_drop_index(&m_alter_info);
}
Sql_cmd *PT_truncate_table_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_TRUNCATE;
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
if (!select->add_table_to_list(thd, m_table, nullptr, TL_OPTION_UPDATING,
TL_WRITE, MDL_EXCLUSIVE))
return nullptr;
return &m_cmd_truncate_table;
}
bool PT_assign_to_keycache::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
if (!pc->select->add_table_to_list(pc->thd, m_table, nullptr, 0, TL_READ,
MDL_SHARED_READ, m_index_hints))
return true;
return false;
}
bool PT_adm_partition::contextualize(Table_ddl_parse_context *pc) {
pc->alter_info->flags |= Alter_info::ALTER_ADMIN_PARTITION;
assert(pc->alter_info->partition_names.is_empty());
if (m_opt_partitions == nullptr)
pc->alter_info->flags |= Alter_info::ALTER_ALL_PARTITION;
else
pc->alter_info->partition_names = *m_opt_partitions;
return false;
}
Sql_cmd *PT_cache_index_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_ASSIGN_TO_KEYCACHE;
Table_ddl_parse_context pc(thd, thd->lex->current_query_block(),
&m_alter_info);
for (auto *tbl_index_list : *m_tbl_index_lists)
if (tbl_index_list->contextualize(&pc)) return nullptr;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root)
Sql_cmd_cache_index(&m_alter_info, m_key_cache_name);
}
Sql_cmd *PT_cache_index_partitions_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_ASSIGN_TO_KEYCACHE;
Query_block *const select = thd->lex->current_query_block();
Table_ddl_parse_context pc(thd, select, &m_alter_info);
if (m_partitions->contextualize(&pc)) return nullptr;
if (!select->add_table_to_list(thd, m_table, nullptr, 0, TL_READ,
MDL_SHARED_READ, m_opt_key_usage_list))
return nullptr;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root)
Sql_cmd_cache_index(&m_alter_info, m_key_cache_name);
}
Sql_cmd *PT_load_index_partitions_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_PRELOAD_KEYS;
Query_block *const select = thd->lex->current_query_block();
Table_ddl_parse_context pc(thd, select, &m_alter_info);
if (m_partitions->contextualize(&pc)) return nullptr;
if (!select->add_table_to_list(
thd, m_table, nullptr, m_ignore_leaves ? TL_OPTION_IGNORE_LEAVES : 0,
TL_READ, MDL_SHARED_READ, m_opt_cache_key_list))
return nullptr;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_load_index(&m_alter_info);
}
Sql_cmd *PT_load_index_stmt::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_PRELOAD_KEYS;
Table_ddl_parse_context pc(thd, thd->lex->current_query_block(),
&m_alter_info);
for (auto *preload_keys : *m_preload_list)
if (preload_keys->contextualize(&pc)) return nullptr;
thd->lex->alter_info = &m_alter_info;
return new (thd->mem_root) Sql_cmd_load_index(&m_alter_info);
}
/* Window functions */
Item *PT_border::build_addop(Item_cache *order_expr, bool prec, bool asc,
const Window *window) {
/*
Check according to SQL 2014 7.15 <window clause> SR 13.a.iii:
ORDER BY expression is temporal iff bound is temporal.
*/
if (order_expr->result_type() == STRING_RESULT && order_expr->is_temporal()) {
if (!m_date_time) {
my_error(ER_WINDOW_RANGE_FRAME_TEMPORAL_TYPE, MYF(0),
window->printable_name());
return nullptr;
}
} else {
if (m_date_time) {
my_error(ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE, MYF(0),
window->printable_name());
return nullptr;
}
}
Item *addop;
const bool substract = prec ? asc : !asc;
if (m_date_time) {
addop =
new Item_date_add_interval(order_expr, m_value, m_int_type, substract);
} else {
if (substract)
addop = new Item_func_minus(order_expr, m_value);
else
addop = new Item_func_plus(order_expr, m_value);
}
return addop;
}
PT_json_table_column_for_ordinality::PT_json_table_column_for_ordinality(
LEX_STRING name)
: m_name(name.str) {}
PT_json_table_column_for_ordinality::~PT_json_table_column_for_ordinality() =
default;
bool PT_json_table_column_for_ordinality::contextualize(Parse_context *pc) {
assert(m_column == nullptr);
m_column = make_unique_destroy_only<Json_table_column>(
pc->mem_root, enum_jt_column::JTC_ORDINALITY);
if (m_column == nullptr) return true;
m_column->init_for_tmp_table(MYSQL_TYPE_LONGLONG, 10, 0, true, true, 8,
m_name);
return super::contextualize(pc);
}
PT_json_table_column_with_path::PT_json_table_column_with_path(
unique_ptr_destroy_only<Json_table_column> column, LEX_STRING name,
PT_type *type, const CHARSET_INFO *collation)
: m_column(std::move(column)),
m_name(name.str),
m_type(type),
m_collation(collation) {}
PT_json_table_column_with_path::~PT_json_table_column_with_path() = default;
static bool check_unsupported_json_table_default(const Item *item) {
if (item == nullptr) return false;
// JSON_TABLE currently only supports string literals on JSON format in
// DEFAULT clauses. Other literals used to be rejected by the grammar, but the
// grammar was extended for JSON_VALUE and now accepts all types of literals.
// Until JSON_TABLE gets support for non-string defaults, reject them here.
if (item->data_type() != MYSQL_TYPE_VARCHAR) {
my_error(
ER_NOT_SUPPORTED_YET, MYF(0),
"non-string DEFAULT value for a column in a JSON_TABLE expression");
return true;
}
return false;
}
bool PT_json_table_column_with_path::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || m_type->contextualize(pc)) return true;
if (m_column->m_path_string->itemize(pc, &m_column->m_path_string))
return true;
if (check_unsupported_json_table_default(m_column->m_default_empty_string) ||
check_unsupported_json_table_default(m_column->m_default_error_string))
return true;
if (itemize_safe(pc, &m_column->m_default_empty_string)) return true;
if (itemize_safe(pc, &m_column->m_default_error_string)) return true;
const CHARSET_INFO *cs = nullptr;
if (merge_charset_and_collation(m_type->get_charset(), m_collation, &cs))
return true;
if (cs == nullptr) {
cs = pc->thd->variables.collation_connection;
}
m_column->init(pc->thd,
m_name, // Alias
m_type->type, // Type
m_type->get_length(), // Length
m_type->get_dec(), // Decimals
m_type->get_type_flags(), // Type modifier
nullptr, // Default value
nullptr, // On update value
&EMPTY_CSTR, // Comment
nullptr, // Change
m_type->get_interval_list(), // Interval list
cs, // Charset & collation
m_collation != nullptr, // Has "COLLATE" clause
m_type->get_uint_geom_type(), // Geom type
nullptr, // Gcol_info
nullptr, // Default gen expression
{}, // SRID
dd::Column::enum_hidden_type::HT_VISIBLE); // Hidden
return false;
}
bool PT_json_table_column_with_nested_path::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true; // OOM
if (m_path->itemize(pc, &m_path)) return true;
auto nested_columns = new (pc->mem_root) List<Json_table_column>;
if (nested_columns == nullptr) return true; // OOM
for (auto col : *m_nested_columns) {
if (col->contextualize(pc) || nested_columns->push_back(col->get_column()))
return true;
}
m_column = new (pc->mem_root) Json_table_column(m_path, nested_columns);
if (m_column == nullptr) return true; // OOM
return false;
}
Sql_cmd *PT_explain_for_connection::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_EXPLAIN_OTHER;
if (thd->lex->sphead) {
my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"non-standalone EXPLAIN FOR CONNECTION");
return nullptr;
}
if (thd->lex->is_explain_analyze) {
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "EXPLAIN ANALYZE FOR CONNECTION");
return nullptr;
}
return &m_cmd;
}
Sql_cmd *PT_explain::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
switch (m_format) {
case Explain_format_type::TRADITIONAL:
case Explain_format_type::TRADITIONAL_STRICT:
/*
With no format specified:
- With ANALYZE, convert TRADITIONAL[_STRICT] to TREE unconditionally.
- With hypergraph, convert TRADITIONAL to TREE. Don't convert
TRADITIONAL_STRICT, because it's purpose is to prevent exactly this
silent conversion with hypergraph. TRADITIONAL_STRICT will throw an
error, later.
*/
if (!m_explicit_format &&
(m_analyze ||
(thd->optimizer_switch_flag(OPTIMIZER_SWITCH_HYPERGRAPH_OPTIMIZER) &&
m_format == Explain_format_type::TRADITIONAL))) {
lex->explain_format = new (thd->mem_root) Explain_format_tree;
} else {
lex->explain_format = new (thd->mem_root) Explain_format_traditional;
}
break;
case Explain_format_type::JSON: {
lex->explain_format = new (thd->mem_root) Explain_format_JSON(
thd->optimizer_switch_flag(OPTIMIZER_SWITCH_HYPERGRAPH_OPTIMIZER)
? Explain_format_JSON::FormatVersion::kIteratorBased
: Explain_format_JSON::FormatVersion::kLinear);
break;
}
case Explain_format_type::TREE:
lex->explain_format = new (thd->mem_root) Explain_format_tree;
break;
default:
assert(false);
lex->explain_format = new (thd->mem_root) Explain_format_traditional;
}
if (lex->explain_format == nullptr) return nullptr; // OOM
lex->is_explain_analyze = m_analyze;
Sql_cmd *ret = m_explainable_stmt->make_cmd(thd);
if (ret == nullptr) return nullptr; // OOM
auto code = ret->sql_command_code();
if (!is_explainable_query(code) && code != SQLCOM_EXPLAIN_OTHER) {
assert(!"Should not happen!");
my_error(ER_WRONG_USAGE, MYF(0), "EXPLAIN", "non-explainable query");
return nullptr;
}
return ret;
}
Sql_cmd *PT_load_table::make_cmd(THD *thd) {
LEX *const lex = thd->lex;
Query_block *const select = lex->current_query_block();
if (lex->sphead) {
my_error(
ER_SP_BADSTATEMENT, MYF(0),
m_cmd.m_exchange.filetype == FILETYPE_CSV ? "LOAD DATA" : "LOAD XML");
return nullptr;
}
lex->sql_command = SQLCOM_LOAD;
switch (m_cmd.m_on_duplicate) {
case On_duplicate::ERROR:
lex->duplicates = DUP_ERROR;
break;
case On_duplicate::IGNORE_DUP:
lex->set_ignore(true);
break;
case On_duplicate::REPLACE_DUP:
lex->duplicates = DUP_REPLACE;
break;
}
/* Fix lock for LOAD DATA CONCURRENT REPLACE */
thr_lock_type lock_type = m_lock_type;
if (lex->duplicates == DUP_REPLACE && lock_type == TL_WRITE_CONCURRENT_INSERT)
lock_type = TL_WRITE_DEFAULT;
if (!select->add_table_to_list(
thd, m_cmd.m_table, nullptr, TL_OPTION_UPDATING, lock_type,
lock_type == TL_WRITE_LOW_PRIORITY ? MDL_SHARED_WRITE_LOW_PRIO
: MDL_SHARED_WRITE,
nullptr, m_cmd.m_opt_partitions))
return nullptr;
/* We can't give an error in the middle when using LOCAL files */
if (m_cmd.m_is_local_file && lex->duplicates == DUP_ERROR)
lex->set_ignore(true);
Parse_context pc(thd, select);
if (contextualize_safe(&pc, &m_cmd.m_opt_fields_or_vars)) {
return nullptr;
}
assert(select->parsing_place == CTX_NONE);
select->parsing_place = CTX_UPDATE_VALUE;
if (contextualize_safe(&pc, &m_cmd.m_opt_set_fields) ||
contextualize_safe(&pc, &m_cmd.m_opt_set_exprs)) {
return nullptr;
}
assert(select->parsing_place == CTX_UPDATE_VALUE);
select->parsing_place = CTX_NONE;
return &m_cmd;
}
bool PT_select_item_list::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->select->fields = value;
return false;
}
bool PT_limit_clause::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
if (limit_options.is_offset_first && limit_options.opt_offset != nullptr &&
limit_options.opt_offset->itemize(pc, &limit_options.opt_offset))
return true;
if (limit_options.limit->itemize(pc, &limit_options.limit)) return true;
if (!limit_options.is_offset_first && limit_options.opt_offset != nullptr &&
limit_options.opt_offset->itemize(pc, &limit_options.opt_offset))
return true;
pc->select->select_limit = limit_options.limit;
pc->select->offset_limit = limit_options.opt_offset;
pc->thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
return false;
}
bool PT_table_factor_table_ident::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
Yacc_state *yyps = &thd->m_parser_state->m_yacc;
m_table_ref = pc->select->add_table_to_list(
thd, table_ident, opt_table_alias, 0, yyps->m_lock_type, yyps->m_mdl_type,
opt_key_definition, opt_use_partition, nullptr, pc);
if (m_table_ref == nullptr) return true;
if (pc->select->add_joined_table(m_table_ref)) return true;
return false;
}
bool PT_table_reference_list_parens::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || contextualize_array(pc, &table_list))
return true;
assert(table_list.size() >= 2);
m_table_ref = pc->select->nest_last_join(pc->thd, table_list.size());
return m_table_ref == nullptr;
}
bool PT_joined_table::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || contextualize_tabs(pc)) return true;
if (m_type & JTT_NATURAL)
m_left_table_ref->add_join_natural(m_right_table_ref);
if (m_type & JTT_STRAIGHT) m_right_table_ref->straight = true;
return false;
}
bool PT_cross_join::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
m_table_ref = pc->select->nest_last_join(pc->thd);
return m_table_ref == nullptr;
}
bool PT_joined_table_on::contextualize(Parse_context *pc) {
if (this->contextualize_tabs(pc)) return true;
if (push_new_name_resolution_context(pc, this->m_left_table_ref,
this->m_right_table_ref)) {
this->error(pc, this->m_join_pos);
return true;
}
Query_block *sel = pc->select;
sel->parsing_place = CTX_ON;
if (super::contextualize(pc) || on->itemize(pc, &on)) return true;
if (!on->is_bool_func()) {
on = make_condition(pc, on);
if (on == nullptr) return true;
}
assert(sel == pc->select);
add_join_on(this->m_right_table_ref, on);
pc->thd->lex->pop_context();
assert(sel->parsing_place == CTX_ON);
sel->parsing_place = CTX_NONE;
m_table_ref = pc->select->nest_last_join(pc->thd);
return m_table_ref == nullptr;
}
bool PT_joined_table_using::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
m_left_table_ref->add_join_natural(m_right_table_ref);
m_table_ref = pc->select->nest_last_join(pc->thd);
if (m_table_ref == nullptr) return true;
m_table_ref->join_using_fields = using_fields;
return false;
}
void PT_table_locking_clause::print_table_ident(const THD *thd,
const Table_ident *ident,
String *s) {
if (ident->db.length > 0) {
append_identifier(thd, s, ident->db.str, ident->db.length);
s->append('.');
}
append_identifier(thd, s, ident->table.str, ident->table.length);
}
bool PT_table_locking_clause::raise_error(THD *thd, const Table_ident *name,
int error) {
String s;
print_table_ident(thd, name, &s);
my_error(error, MYF(0), s.ptr());
return true;
}
bool PT_table_locking_clause::raise_error(int error) {
my_error(error, MYF(0));
return true;
}
bool PT_set_scoped_system_variable::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || itemize_safe(pc, &m_opt_expr)) {
return true;
}
// Remove deprecation warning when FULL is used as keyword.
THD *thd = pc->thd;
if (thd->get_stmt_da()->has_sql_condition(ER_WARN_DEPRECATED_IDENT)) {
thd->get_stmt_da()->reset_condition_info(thd);
}
const bool is_1d_name = m_opt_prefix.str == nullptr;
/*
Reject transition variable names in the syntax:
{"GLOBAL" | "SESSION" | "PERSIST" | ...} {"NEW" | "OLD"} "." <name>
*/
if (is_any_transition_variable_prefix(*pc, m_opt_prefix)) {
error(pc, m_pos);
return true;
}
/*
Reject SP variable names in the syntax:
{"GLOBAL" | "SESSION" | ...} <SP variable name>
Note: we also reject overload system variable names here too.
TODO: most likely this reject is fine, since such a use case is confusing,
OTOH this is also probable that a rejection of overloaded system
variables is too strict, so a warning may be a better alternative.
*/
if (is_1d_name && find_sp_variable(*pc, m_name) != nullptr) {
error(pc, m_pos);
return true;
}
return add_system_variable_assignment(pc->thd, m_opt_prefix, m_name,
pc->thd->lex->option_type, m_opt_expr);
}
bool PT_option_value_no_option_type_user_var::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || expr->itemize(pc, &expr)) return true;
THD *thd = pc->thd;
Item_func_set_user_var *item =
new (pc->mem_root) Item_func_set_user_var(name, expr);
if (item == nullptr) return true;
set_var_user *var = new (thd->mem_root) set_var_user(item);
if (var == nullptr) return true;
return thd->lex->var_list.push_back(var);
}
bool PT_set_system_variable::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || itemize_safe(pc, &m_opt_expr)) {
return true;
}
// Remove deprecation warning when FULL is used as keyword.
THD *thd = pc->thd;
if (thd->get_stmt_da()->has_sql_condition(ER_WARN_DEPRECATED_IDENT)) {
thd->get_stmt_da()->reset_condition_info(thd);
}
/*
This is `@@[scope.][prefix.]name, so reject @@[scope].{NEW | OLD}.name:
*/
if (is_any_transition_variable_prefix(*pc, m_opt_prefix)) {
error(pc, m_name_pos);
return true;
}
return add_system_variable_assignment(thd, m_opt_prefix, m_name, m_scope,
m_opt_expr);
}
bool PT_option_value_type::contextualize(Parse_context *pc) {
pc->thd->lex->option_type = type;
return super::contextualize(pc) || value->contextualize(pc);
}
bool PT_option_value_list_head::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
#ifndef NDEBUG
LEX *old_lex = thd->lex;
#endif // NDEBUG
sp_create_assignment_lex(thd, delimiter_pos.raw.end);
assert(thd->lex->query_block == thd->lex->current_query_block());
Parse_context inner_pc(pc->thd, thd->lex->query_block);
if (value->contextualize(&inner_pc)) return true;
if (sp_create_assignment_instr(pc->thd, value_pos.raw.end)) return true;
assert(thd->lex == old_lex && thd->lex->current_query_block() == pc->select);
return false;
}
bool PT_start_option_value_list_no_type::contextualize(Parse_context *pc) {
if (super::contextualize(pc) || head->contextualize(pc)) return true;
if (sp_create_assignment_instr(pc->thd, head_pos.raw.end)) return true;
assert(pc->thd->lex->query_block == pc->thd->lex->current_query_block());
pc->select = pc->thd->lex->query_block;
if (tail != nullptr && tail->contextualize(pc)) return true;
return false;
}
bool PT_transaction_characteristic::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
Item *item = new (pc->mem_root) Item_int(value);
if (item == nullptr) return true;
System_variable_tracker var_tracker =
System_variable_tracker::make_tracker(name);
if (var_tracker.access_system_variable(thd)) {
assert(false);
return true; // should never happen / OOM
}
set_var *var =
new (thd->mem_root) set_var(lex->option_type, var_tracker, item);
if (var == nullptr) return true;
return lex->var_list.push_back(var);
}
bool PT_start_option_value_list_transaction::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
thd->lex->option_type = OPT_DEFAULT;
if (characteristics->contextualize(pc)) return true;
if (sp_create_assignment_instr(thd, end_pos.raw.end)) return true;
assert(pc->thd->lex->query_block == pc->thd->lex->current_query_block());
pc->select = pc->thd->lex->query_block;
return false;
}
bool PT_start_option_value_list_following_option_type_eq::contextualize(
Parse_context *pc) {
if (super::contextualize(pc) || head->contextualize(pc)) return true;
if (sp_create_assignment_instr(pc->thd, head_pos.raw.end)) return true;
assert(pc->thd->lex->query_block == pc->thd->lex->current_query_block());
pc->select = pc->thd->lex->query_block;
if (opt_tail != nullptr && opt_tail->contextualize(pc)) return true;
return false;
}
bool PT_start_option_value_list_following_option_type_transaction::
contextualize(Parse_context *pc) {
if (super::contextualize(pc) || characteristics->contextualize(pc))
return true;
if (sp_create_assignment_instr(pc->thd, characteristics_pos.raw.end))
return true;
assert(pc->thd->lex->query_block == pc->thd->lex->current_query_block());
pc->select = pc->thd->lex->query_block;
return false;
}
bool PT_start_option_value_list_type::contextualize(Parse_context *pc) {
pc->thd->lex->option_type = type;
return super::contextualize(pc) || list->contextualize(pc);
}
bool PT_set::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
THD *thd = pc->thd;
LEX *lex = thd->lex;
lex->sql_command = SQLCOM_SET_OPTION;
lex->option_type = OPT_SESSION;
lex->var_list.clear();
lex->autocommit = false;
sp_create_assignment_lex(thd, set_pos.raw.end);
assert(pc->thd->lex->query_block == pc->thd->lex->current_query_block());
pc->select = pc->thd->lex->query_block;
return list->contextualize(pc);
}
bool PT_into_destination::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *lex = pc->thd->lex;
if (!pc->thd->lex->parsing_options.allows_select_into) {
if (lex->sql_command == SQLCOM_SHOW_CREATE ||
lex->sql_command == SQLCOM_CREATE_VIEW)
my_error(ER_VIEW_SELECT_CLAUSE, MYF(0), "INTO");
else
error(pc, m_pos);
return true;
}
return false;
}
bool PT_into_destination_outfile::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *lex = pc->thd->lex;
lex->set_uncacheable(pc->select, UNCACHEABLE_SIDEEFFECT);
lex->result = new (pc->mem_root) Query_result_export(&m_exchange);
return lex->result == nullptr;
}
bool PT_into_destination_dumpfile::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *lex = pc->thd->lex;
lex->set_uncacheable(pc->select, UNCACHEABLE_SIDEEFFECT);
lex->result = new (pc->mem_root) Query_result_dump(&m_exchange);
return lex->result == nullptr;
}
bool PT_select_var_list::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
List_iterator<PT_select_var> it(value);
PT_select_var *var;
while ((var = it++)) {
if (var->contextualize(pc)) return true;
}
LEX *const lex = pc->thd->lex;
Query_dumpvar *dumpvar = new (pc->mem_root) Query_dumpvar();
if (dumpvar == nullptr) return true;
dumpvar->var_list = value;
lex->result = dumpvar;
lex->set_uncacheable(pc->select, UNCACHEABLE_SIDEEFFECT);
return false;
}
bool PT_query_expression::contextualize(Parse_context *pc) {
if (!pc->thd->lex->opt_hints_global)
pc->thd->lex->opt_hints_global =
new (pc->thd->mem_root) Opt_hints_global(pc->thd->mem_root);
pc->thd->lex->opt_hints_global->deferred_hints =
new (pc->thd->mem_root) PT_hint_list(pc->thd->mem_root);
pc->m_stack.push_back(
QueryLevel(pc->mem_root, SC_QUERY_EXPRESSION, m_order != nullptr));
if (contextualize_safe(pc, m_with_clause))
return true; /* purecov: inspected */
if (Parse_tree_node::contextualize(pc) || m_body->contextualize(pc))
return true;
QueryLevel ql = pc->m_stack.back();
Query_term *expr = ql.m_elts.back();
pc->m_stack.pop_back();
switch (expr->term_type()) {
case QT_UNARY: {
Query_term_unary *ex = down_cast<Query_term_unary *>(expr);
Query_expression *qe = pc->select->master_query_expression();
// The setting of no_table_names_allowed in the created post processing
// block below to false foreshadows our removing the parentheses in
// Query_term::pushdown_limit_order_by. We need to duplicate the logic
// here in order to allow a construction like
//
// ( SELECT a FROM t ) ORDER BY t.a
//
// for which we remove the parentheses because the inner query expression
// has no LIMIT or ORDER BY of its own.
if (ex->set_block(qe->create_post_processing_block(ex))) return true;
if (m_order != nullptr)
ex->query_block()->order_list = m_order->order_list->value;
if (m_limit != nullptr) {
ex->query_block()->select_limit = m_limit->limit_options.limit;
ex->query_block()->offset_limit = m_limit->limit_options.opt_offset;
}
Query_block *orig_query_block = pc->select;
LEX *lex = pc->thd->lex;
pc->select = ex->query_block();
lex->push_context(&pc->select->context);
assert(pc->select->parsing_place == CTX_NONE);
if (contextualize_safe(pc, m_order, m_limit)) return true;
lex->pop_context();
pc->select = orig_query_block;
if (pc->m_stack.back().m_type == SC_QUERY_EXPRESSION) { // 3 deep or more
expr = new (pc->mem_root) Query_term_unary(pc->mem_root, expr);
if (expr == nullptr) return true;
}
QueryLevel upper = pc->m_stack.back();
pc->m_stack.pop_back();
ql.m_elts.pop_back();
if (upper.m_type == SC_UNION_DISTINCT || upper.m_type == SC_UNION_ALL ||
upper.m_type == SC_EXCEPT_DISTINCT || upper.m_type == SC_EXCEPT_ALL ||
upper.m_type == SC_INTERSECT_DISTINCT ||
upper.m_type == SC_INTERSECT_ALL)
ql = upper;
ql.m_elts.push_back(expr);
pc->m_stack.push_back(ql);
} break;
case QT_QUERY_BLOCK: {
if (contextualize_order_and_limit(pc)) return true;
if (pc->m_stack.back().m_type == SC_QUERY_EXPRESSION) { // 2 deep or more
expr = new (pc->mem_root) Query_term_unary(pc->mem_root, expr);
if (expr == nullptr) return true;
}
QueryLevel upper = pc->m_stack.back();
pc->m_stack.pop_back();
ql.m_elts.pop_back();
if (upper.m_type == SC_UNION_DISTINCT || upper.m_type == SC_UNION_ALL ||
upper.m_type == SC_EXCEPT_DISTINCT || upper.m_type == SC_EXCEPT_ALL ||
upper.m_type == SC_INTERSECT_DISTINCT ||
upper.m_type == SC_INTERSECT_ALL)
ql = upper;
ql.m_elts.push_back(expr);
pc->m_stack.push_back(ql);
} break;
case QT_UNION:
case QT_EXCEPT:
case QT_INTERSECT: {
auto ex = down_cast<Query_term_set_op *>(expr);
const bool already_decorated =
ex->query_block()->order_list.elements > 0 ||
ex->query_block()->select_limit != nullptr;
if (already_decorated) {
ex = new (pc->mem_root) Query_term_unary(pc->mem_root, ex);
if (ex == nullptr) return true;
Query_expression *qe = pc->select->master_query_expression();
if (ex->set_block(qe->create_post_processing_block(ex))) return true;
}
if (m_order != nullptr) {
ex->query_block()->order_list = m_order->order_list->value;
}
if (m_limit != nullptr) {
ex->query_block()->select_limit = m_limit->limit_options.limit;
ex->query_block()->offset_limit = m_limit->limit_options.opt_offset;
}
Query_block *orig_query_block = pc->select;
LEX *lex = pc->thd->lex;
pc->select = ex->query_block();
lex->push_context(&pc->select->context);
assert(pc->select->parsing_place == CTX_NONE);
if (contextualize_safe(pc, m_order, m_limit)) return true;
lex->pop_context();
pc->select = orig_query_block;
if (pc->m_stack.back().m_type == SC_QUERY_EXPRESSION) { // 3 deep or more
ex = new (pc->mem_root) Query_term_unary(pc->mem_root, ex);
if (ex == nullptr) return true;
}
pc->m_stack.back().m_elts.push_back(ex);
} break;
}
contextualize_deferred_hints(pc);
return false;
}
bool PT_subquery::contextualize(Parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *lex = pc->thd->lex;
if (!lex->expr_allows_subquery || lex->sql_command == SQLCOM_PURGE) {
error(pc, pos);
return true;
}
// Create a Query_expression and Query_block for the subquery's query
// expression.
Query_block *child = lex->new_query(pc->select);
if (child == nullptr) return true;
Parse_context inner_pc(pc->thd, child);
inner_pc.m_stack.push_back(QueryLevel(pc->mem_root, SC_SUBQUERY));
if (m_is_derived_table) child->linkage = DERIVED_TABLE_TYPE;
if (qe->contextualize(&inner_pc)) return true;
if (qe->has_into_clause()) {
my_error(ER_MISPLACED_INTO, MYF(0));
return true;
}
query_block = inner_pc.select->master_query_expression()->first_query_block();
if (inner_pc.finalize_query_expression()) return true;
inner_pc.m_stack.pop_back();
assert(inner_pc.m_stack.size() == 0);
lex->pop_context();
pc->select->n_child_sum_items += child->n_sum_items;
/*
A subquery (and all the subsequent query blocks in a UNION) can add
columns to an outer query block. Reserve space for them.
*/
for (Query_block *temp = child; temp != nullptr;
temp = temp->next_query_block()) {
pc->select->select_n_where_fields += temp->select_n_where_fields;
pc->select->select_n_having_items += temp->select_n_having_items;
}
return false;
}
Sql_cmd *PT_create_srs::make_cmd(THD *thd) {
// Note: This function hard-codes the maximum length of various
// strings. These lengths must match those in
// sql/dd/impl/tables/spatial_reference_systems.cc.
thd->lex->sql_command = SQLCOM_CREATE_SRS;
if (m_srid > std::numeric_limits<gis::srid_t>::max()) {
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "SRID",
m_or_replace ? "CREATE OR REPLACE SPATIAL REFERENCE SYSTEM"
: "CREATE SPATIAL REFERENCE SYSTEM");
return nullptr;
}
if (m_srid == 0) {
my_error(ER_CANT_MODIFY_SRID_0, MYF(0));
return nullptr;
}
if (m_attributes.srs_name.str == nullptr) {
my_error(ER_SRS_MISSING_MANDATORY_ATTRIBUTE, MYF(0), "NAME");
return nullptr;
}
MYSQL_LEX_STRING srs_name_utf8 = {nullptr, 0};
if (thd->convert_string(&srs_name_utf8, &my_charset_utf8mb3_bin,
m_attributes.srs_name.str,
m_attributes.srs_name.length, thd->charset())) {
/* purecov: begin inspected */
my_error(ER_DA_OOM, MYF(0));
return nullptr;
/* purecov: end */
}
if (srs_name_utf8.length == 0 || std::isspace(srs_name_utf8.str[0]) ||
std::isspace(srs_name_utf8.str[srs_name_utf8.length - 1])) {
my_error(ER_SRS_NAME_CANT_BE_EMPTY_OR_WHITESPACE, MYF(0));
return nullptr;
}
if (contains_control_char(srs_name_utf8.str, srs_name_utf8.length)) {
my_error(ER_SRS_INVALID_CHARACTER_IN_ATTRIBUTE, MYF(0), "NAME");
return nullptr;
}
String srs_name_str(srs_name_utf8.str, srs_name_utf8.length,
&my_charset_utf8mb3_bin);
if (srs_name_str.numchars() > 80) {
my_error(ER_SRS_ATTRIBUTE_STRING_TOO_LONG, MYF(0), "NAME", 80);
return nullptr;
}
if (m_attributes.definition.str == nullptr) {
my_error(ER_SRS_MISSING_MANDATORY_ATTRIBUTE, MYF(0), "DEFINITION");
return nullptr;
}
MYSQL_LEX_STRING definition_utf8 = {nullptr, 0};
if (thd->convert_string(&definition_utf8, &my_charset_utf8mb3_bin,
m_attributes.definition.str,
m_attributes.definition.length, thd->charset())) {
/* purecov: begin inspected */
my_error(ER_DA_OOM, MYF(0));
return nullptr;
/* purecov: end */
}
String definition_str(definition_utf8.str, definition_utf8.length,
&my_charset_utf8mb3_bin);
if (contains_control_char(definition_utf8.str, definition_utf8.length)) {
my_error(ER_SRS_INVALID_CHARACTER_IN_ATTRIBUTE, MYF(0), "DEFINITION");
return nullptr;
}
if (definition_str.numchars() > 4096) {
my_error(ER_SRS_ATTRIBUTE_STRING_TOO_LONG, MYF(0), "DEFINITION", 4096);
return nullptr;
}
MYSQL_LEX_STRING organization_utf8 = {nullptr, 0};
if (m_attributes.organization.str != nullptr) {
if (thd->convert_string(&organization_utf8, &my_charset_utf8mb3_bin,
m_attributes.organization.str,
m_attributes.organization.length, thd->charset())) {
/* purecov: begin inspected */
my_error(ER_DA_OOM, MYF(0));
return nullptr;
/* purecov: end */
}
if (organization_utf8.length == 0 ||
std::isspace(organization_utf8.str[0]) ||
std::isspace(organization_utf8.str[organization_utf8.length - 1])) {
my_error(ER_SRS_ORGANIZATION_CANT_BE_EMPTY_OR_WHITESPACE, MYF(0));
return nullptr;
}
String organization_str(organization_utf8.str, organization_utf8.length,
&my_charset_utf8mb3_bin);
if (contains_control_char(organization_utf8.str,
organization_utf8.length)) {
my_error(ER_SRS_INVALID_CHARACTER_IN_ATTRIBUTE, MYF(0), "ORGANIZATION");
return nullptr;
}
if (organization_str.numchars() > 256) {
my_error(ER_SRS_ATTRIBUTE_STRING_TOO_LONG, MYF(0), "ORGANIZATION", 256);
return nullptr;
}
if (m_attributes.organization_coordsys_id >
std::numeric_limits<gis::srid_t>::max()) {
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "IDENTIFIED BY",
m_or_replace ? "CREATE OR REPLACE SPATIAL REFERENCE SYSTEM"
: "CREATE SPATIAL REFERENCE SYSTEM");
return nullptr;
}
}
MYSQL_LEX_STRING description_utf8 = {nullptr, 0};
if (m_attributes.description.str != nullptr) {
if (thd->convert_string(&description_utf8, &my_charset_utf8mb3_bin,
m_attributes.description.str,
m_attributes.description.length, thd->charset())) {
/* purecov: begin inspected */
my_error(ER_DA_OOM, MYF(0));
return nullptr;
/* purecov: end */
}
String description_str(description_utf8.str, description_utf8.length,
&my_charset_utf8mb3_bin);
if (contains_control_char(description_utf8.str, description_utf8.length)) {
my_error(ER_SRS_INVALID_CHARACTER_IN_ATTRIBUTE, MYF(0), "DESCRIPTION");
return nullptr;
}
if (description_str.numchars() > 2048) {
my_error(ER_SRS_ATTRIBUTE_STRING_TOO_LONG, MYF(0), "DESCRIPTION", 2048);
return nullptr;
}
}
sql_cmd.init(m_or_replace, m_if_not_exists, m_srid, srs_name_utf8,
definition_utf8, organization_utf8,
m_attributes.organization_coordsys_id, description_utf8);
return &sql_cmd;
}
Sql_cmd *PT_drop_srs::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_DROP_SRS;
if (m_srid > std::numeric_limits<gis::srid_t>::max()) {
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "SRID",
"DROP SPATIAL REFERENCE SYSTEM");
return nullptr;
}
if (m_srid == 0) {
my_error(ER_CANT_MODIFY_SRID_0, MYF(0));
return nullptr;
}
return &sql_cmd;
}
Sql_cmd *PT_alter_instance::make_cmd(THD *thd) {
thd->lex->no_write_to_binlog = false;
return &sql_cmd;
}
bool PT_check_constraint::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc) ||
cc_spec.check_expr->itemize(pc, &cc_spec.check_expr))
return true;
if (pc->alter_info->check_constraint_spec_list.push_back(&cc_spec))
return true;
pc->alter_info->flags |= Alter_info::ADD_CHECK_CONSTRAINT;
return false;
}
Sql_cmd *PT_create_role::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_CREATE_ROLE;
return &sql_cmd;
}
Sql_cmd *PT_drop_role::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_DROP_ROLE;
return &sql_cmd;
}
Sql_cmd *PT_set_role::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_SET_ROLE;
return &sql_cmd;
}
LEX_USER *PT_role_or_privilege::get_user(THD *thd) {
thd->syntax_error_at(pos, "Illegal authorization identifier");
return nullptr;
}
Privilege *PT_role_or_privilege::get_privilege(THD *thd) {
thd->syntax_error_at(pos, "Illegal privilege identifier");
return nullptr;
}
LEX_USER *PT_role_at_host::get_user(THD *thd) {
return LEX_USER::alloc(thd, &role, &host);
}
LEX_USER *PT_role_or_dynamic_privilege::get_user(THD *thd) {
return LEX_USER::alloc(thd, &ident, nullptr);
}
Privilege *PT_role_or_dynamic_privilege::get_privilege(THD *thd) {
return new (thd->mem_root) Dynamic_privilege(ident, nullptr);
}
Privilege *PT_static_privilege::get_privilege(THD *thd) {
return new (thd->mem_root) Static_privilege(grant, columns);
}
Privilege *PT_dynamic_privilege::get_privilege(THD *thd) {
return new (thd->mem_root) Dynamic_privilege(ident, nullptr);
}
Sql_cmd *PT_grant_roles::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_GRANT_ROLE;
List<LEX_USER> *role_objects = new (thd->mem_root) List<LEX_USER>;
if (role_objects == nullptr) return nullptr; // OOM
for (PT_role_or_privilege *r : *roles) {
LEX_USER *user = r->get_user(thd);
if (r == nullptr || role_objects->push_back(user)) return nullptr;
}
return new (thd->mem_root)
Sql_cmd_grant_roles(role_objects, users, with_admin_option);
}
Sql_cmd *PT_revoke_roles::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_REVOKE_ROLE;
List<LEX_USER> *role_objects = new (thd->mem_root) List<LEX_USER>;
if (role_objects == nullptr) return nullptr; // OOM
for (PT_role_or_privilege *r : *roles) {
LEX_USER *user = r->get_user(thd);
if (r == nullptr || role_objects->push_back(user)) return nullptr;
}
return new (thd->mem_root) Sql_cmd_revoke_roles(role_objects, users);
}
Sql_cmd *PT_alter_user_default_role::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_ALTER_USER_DEFAULT_ROLE;
return &sql_cmd;
}
Sql_cmd *PT_show_grants::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_SHOW_GRANTS;
return &sql_cmd;
}
bool PT_alter_table_action::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->alter_info->flags |= flag;
return false;
}
bool PT_alter_table_set_default::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc) || itemize_safe(pc, &m_expr)) return true;
Alter_column *alter_column;
if (m_expr == nullptr || m_expr->basic_const_item()) {
Item *actual_expr = m_expr;
if (m_expr && m_expr->type() == Item::FUNC_ITEM) {
/*
Default value should be literal => basic constants =>
no need fix_fields()
*/
Item_func *func = down_cast<Item_func *>(m_expr);
if (func->result_type() != INT_RESULT) {
my_error(ER_INVALID_DEFAULT, MYF(0), m_name);
return true;
}
assert(dynamic_cast<Item_func_true *>(func) ||
dynamic_cast<Item_func_false *>(func));
actual_expr = new Item_int(func->val_int());
}
alter_column = new (pc->mem_root) Alter_column(m_name, actual_expr);
} else {
auto vg = new (pc->mem_root) Value_generator;
if (vg == nullptr) return true; // OOM
vg->expr_item = m_expr;
vg->set_field_stored(true);
alter_column = new (pc->mem_root) Alter_column(m_name, vg);
}
if (alter_column == nullptr ||
pc->alter_info->alter_list.push_back(alter_column)) {
return true; // OOM
}
return false;
}
bool PT_alter_table_order::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc) || m_order->contextualize(pc)) return true;
pc->select->order_list = m_order->value;
return false;
}
bool PT_alter_table_partition_by::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc) || m_partition->contextualize(pc)) return true;
pc->thd->lex->part_info = &m_partition->part_info;
return false;
}
bool PT_alter_table_add_partition::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *const lex = pc->thd->lex;
lex->no_write_to_binlog = m_no_write_to_binlog;
assert(lex->part_info == nullptr);
lex->part_info = &m_part_info;
return false;
}
bool PT_alter_table_drop_partition::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
assert(pc->alter_info->partition_names.is_empty());
pc->alter_info->partition_names = m_partitions;
return false;
}
bool PT_alter_table_rebuild_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->thd->lex->no_write_to_binlog = m_no_write_to_binlog;
return false;
}
bool PT_alter_table_optimize_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->thd->lex->no_write_to_binlog = m_no_write_to_binlog;
return false;
}
bool PT_alter_table_analyze_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->thd->lex->no_write_to_binlog = m_no_write_to_binlog;
return false;
}
bool PT_alter_table_check_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *const lex = pc->thd->lex;
lex->check_opt.flags |= m_flags;
lex->check_opt.sql_flags |= m_sql_flags;
return false;
}
bool PT_alter_table_repair_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
LEX *const lex = pc->thd->lex;
lex->no_write_to_binlog = m_no_write_to_binlog;
lex->check_opt.flags |= m_flags;
lex->check_opt.sql_flags |= m_sql_flags;
return false;
}
bool PT_alter_table_coalesce_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->thd->lex->no_write_to_binlog = m_no_write_to_binlog;
pc->alter_info->num_parts = m_num_parts;
return false;
}
bool PT_alter_table_truncate_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
return false;
}
bool PT_alter_table_reorganize_partition::contextualize(
Table_ddl_parse_context *pc) {
if (super::contextualize(pc)) return true;
pc->thd->lex->part_info = &m_partition_info;
pc->thd->lex->no_write_to_binlog = m_no_write_to_binlog;
return false;
}
bool PT_preload_keys::contextualize(Table_ddl_parse_context *pc) {
if (super::contextualize(pc) ||
!pc->select->add_table_to_list(
pc->thd, m_table, nullptr,
m_ignore_leaves ? TL_OPTION_IGNORE_LEAVES : 0, TL_READ,
MDL_SHARED_READ, m_opt_cache_key_list))
return true;
return false;
}
Alter_tablespace_parse_context::Alter_tablespace_parse_context(THD *thd)
: thd(thd), mem_root(thd->mem_root) {}
bool PT_alter_tablespace_option_nodegroup::contextualize(
Alter_tablespace_parse_context *pc) {
if (super::contextualize(pc)) return true; /* purecov: inspected */ // OOM
if (pc->nodegroup_id != UNDEF_NODEGROUP) {
my_error(ER_FILEGROUP_OPTION_ONLY_ONCE, MYF(0), "NODEGROUP");
return true;
}
pc->nodegroup_id = m_nodegroup_id;
return false;
}
Sql_cmd *PT_create_resource_group::make_cmd(THD *thd) {
if (check_resource_group_support()) return nullptr;
if (check_resource_group_name_len(sql_cmd.m_name, Sql_condition::SL_ERROR))
return nullptr;
if (has_priority &&
validate_resource_group_priority(thd, &sql_cmd.m_priority, sql_cmd.m_name,
sql_cmd.m_type))
return nullptr;
for (auto &range : *sql_cmd.m_cpu_list) {
if (validate_vcpu_range(range)) return nullptr;
}
thd->lex->sql_command = SQLCOM_CREATE_RESOURCE_GROUP;
return &sql_cmd;
}
Sql_cmd *PT_alter_resource_group::make_cmd(THD *thd) {
if (check_resource_group_support()) return nullptr;
if (check_resource_group_name_len(sql_cmd.m_name, Sql_condition::SL_ERROR))
return nullptr;
for (auto &range : *sql_cmd.m_cpu_list) {
if (validate_vcpu_range(range)) return nullptr;
}
thd->lex->sql_command = SQLCOM_ALTER_RESOURCE_GROUP;
return &sql_cmd;
}
Sql_cmd *PT_drop_resource_group::make_cmd(THD *thd) {
if (check_resource_group_support()) return nullptr;
if (check_resource_group_name_len(sql_cmd.m_name, Sql_condition::SL_ERROR))
return nullptr;
thd->lex->sql_command = SQLCOM_DROP_RESOURCE_GROUP;
return &sql_cmd;
}
Sql_cmd *PT_set_resource_group::make_cmd(THD *thd) {
if (check_resource_group_support()) return nullptr;
if (check_resource_group_name_len(sql_cmd.m_name, Sql_condition::SL_ERROR))
return nullptr;
thd->lex->sql_command = SQLCOM_SET_RESOURCE_GROUP;
return &sql_cmd;
}
Sql_cmd *PT_restart_server::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_RESTART_SERVER;
return &sql_cmd;
}
/**
Generic attribute node that can be used with different base types
and corresponding parse contexts. CFP (Contextualizer Function
Pointer) argument implements a suitable contextualize action in the
given context. Value is typically a decayed captureless lambda.
*/
template <class ATTRIBUTE, class BASE>
class PT_attribute : public BASE {
ATTRIBUTE m_attr;
using CFP = bool (*)(ATTRIBUTE, typename BASE::context_t *);
CFP m_cfp;
public:
PT_attribute(ATTRIBUTE a, CFP cfp) : m_attr{a}, m_cfp{cfp} {}
bool contextualize(typename BASE::context_t *pc) override {
return BASE::contextualize(pc) || m_cfp(m_attr, pc);
}
};
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_alter_tablespace_option_base* to PT_attribute object.
*/
PT_alter_tablespace_option_base *make_tablespace_engine_attribute(
MEM_ROOT *mem_root, LEX_CSTRING attr) {
return new (mem_root)
PT_attribute<LEX_CSTRING, PT_alter_tablespace_option_base>(
attr, +[](LEX_CSTRING a, Alter_tablespace_parse_context *pc) {
pc->engine_attribute = a;
return false;
});
}
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_alter_tablespace_option_base* to PT_attribute object.
*/
PT_create_table_option *make_table_engine_attribute(MEM_ROOT *mem_root,
LEX_CSTRING attr) {
return new (mem_root) PT_attribute<LEX_CSTRING, PT_create_table_option>(
attr, +[](LEX_CSTRING a, Table_ddl_parse_context *pc) {
pc->create_info->engine_attribute = a;
pc->create_info->used_fields |= HA_CREATE_USED_ENGINE_ATTRIBUTE;
pc->alter_info->flags |=
(Alter_info::ALTER_OPTIONS | Alter_info::ANY_ENGINE_ATTRIBUTE);
return false;
});
}
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_create_table_option* to PT_attribute object.
*/
PT_create_table_option *make_table_secondary_engine_attribute(
MEM_ROOT *mem_root, LEX_CSTRING attr) {
return new (mem_root) PT_attribute<LEX_CSTRING, PT_create_table_option>(
attr, +[](LEX_CSTRING a, Table_ddl_parse_context *pc) {
pc->create_info->secondary_engine_attribute = a;
pc->create_info->used_fields |=
HA_CREATE_USED_SECONDARY_ENGINE_ATTRIBUTE;
pc->alter_info->flags |= Alter_info::ALTER_OPTIONS;
return false;
});
}
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_create_table_option* to PT_attribute object.
*/
PT_column_attr_base *make_column_engine_attribute(MEM_ROOT *mem_root,
LEX_CSTRING attr) {
return new (mem_root) PT_attribute<LEX_CSTRING, PT_column_attr_base>(
attr, +[](LEX_CSTRING a, Column_parse_context *pc) {
// Note that a std::function is created from the lambda and constructed
// directly in the vector.
// This means it is necessary to ensure that the elements of the vector
// are destroyed. This will not happen automatically when the vector is
// moved to the Alter_info struct which is allocated on the mem_root
// and not destroyed.
pc->cf_appliers.emplace_back([=](Create_field *cf, Alter_info *ai) {
cf->m_engine_attribute = a;
ai->flags |= Alter_info::ANY_ENGINE_ATTRIBUTE;
return false;
});
return false;
});
}
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_column_attr_base* to PT_attribute object.
*/
PT_column_attr_base *make_column_secondary_engine_attribute(MEM_ROOT *mem_root,
LEX_CSTRING attr) {
return new (mem_root) PT_attribute<LEX_CSTRING, PT_column_attr_base>(
attr, +[](LEX_CSTRING a, Column_parse_context *pc) {
// Note that a std::function is created from the lambda and constructed
// directly in the vector.
// This means it is necessary to ensure that the elements of the vector
// are destroyed. This will not happen automatically when the vector is
// moved to the Alter_info struct which is allocated on the mem_root
// and not destroyed.
pc->cf_appliers.emplace_back([=](Create_field *cf, Alter_info *) {
cf->m_secondary_engine_attribute = a;
return false;
});
return false;
});
}
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_base_index_option* to PT_attribute object.
*/
PT_base_index_option *make_index_engine_attribute(MEM_ROOT *mem_root,
LEX_CSTRING attr) {
return new (mem_root) PT_attribute<LEX_CSTRING, PT_base_index_option>(
attr, +[](LEX_CSTRING a, Table_ddl_parse_context *pc) {
pc->key_create_info->m_engine_attribute = a;
pc->alter_info->flags |= Alter_info::ANY_ENGINE_ATTRIBUTE;
return false;
});
}
/**
Factory function which instantiates PT_attribute with suitable
parameters, allocates on the provided mem_root, and returns the
appropriate base pointer.
@param mem_root Memory arena.
@param attr Attribute value from parser.
@return PT_base_index_option* to PT_attribute object.
*/
PT_base_index_option *make_index_secondary_engine_attribute(MEM_ROOT *mem_root,
LEX_CSTRING attr) {
return new (mem_root) PT_attribute<LEX_CSTRING, PT_base_index_option>(
attr, +[](LEX_CSTRING a, Table_ddl_parse_context *pc) {
pc->key_create_info->m_secondary_engine_attribute = a;
return false;
});
}
PT_install_component::PT_install_component(
THD *thd, const Mem_root_array_YY<LEX_STRING> urns,
List<PT_install_component_set_element> *set_elements)
: m_urns(urns), m_set_elements(set_elements) {
const char *prefix = "file://component_";
const auto prefix_len = sizeof("file://component_") - 1;
if (m_urns.size() == 1 &&
!thd->charset()->coll->strnncoll(
thd->charset(), pointer_cast<const uchar *>(m_urns[0].str),
m_urns[0].length, pointer_cast<const uchar *>(prefix), prefix_len,
true)) {
for (auto &elt : *m_set_elements) {
if (elt.name.prefix.length == 0) {
elt.name.prefix.str = m_urns[0].str + prefix_len;
elt.name.prefix.length = m_urns[0].length - prefix_len;
}
}
}
}
Sql_cmd *PT_install_component::make_cmd(THD *thd) {
thd->lex->sql_command = SQLCOM_INSTALL_COMPONENT;
thd->lex->expr_allows_subquery = false;
auto f =
create_scope_guard([thd]() { thd->lex->expr_allows_subquery = true; });
if (!m_set_elements->is_empty()) {
Parse_context pc(thd, thd->lex->current_query_block());
for (auto &elt : *m_set_elements) {
if (elt.expr->itemize(&pc, &elt.expr)) return nullptr;
/*
If the SET value is a field, change it to a string to allow things
SET variable = OFF
*/
if (elt.expr->type() == Item::FIELD_ITEM) {
Item_field *item_field = down_cast<Item_field *>(elt.expr);
if (item_field->table_name != nullptr) {
// Reject a dot-separated identified at the RHS of:
// SET <variable_name> = <table_name>.<field_name>
my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), elt.name.name.str);
return nullptr;
}
assert(item_field->field_name != nullptr);
elt.expr = new (thd->mem_root)
Item_string(item_field->field_name, strlen(item_field->field_name),
system_charset_info); // names are utf8
if (elt.expr == nullptr) return nullptr; // OOM
}
if (elt.expr->has_subquery() || elt.expr->has_stored_program() ||
elt.expr->has_aggregation()) {
my_error(ER_SET_CONSTANTS_ONLY, MYF(0));
return nullptr;
}
}
}
return new (thd->mem_root) Sql_cmd_install_component(m_urns, m_set_elements);
}
|