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
|
/* Copyright (c) 2002, 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 */
/**
@file
@brief
Implements the subselect Item, used when there is a subselect in a
SELECT list, WHERE, etc.
*/
#include "sql/item_subselect.h"
#include <climits>
#include <cstdio>
#include <cstring>
#include <initializer_list>
#include <string>
#include <utility>
#include "decimal.h"
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_pointer_arithmetic.h"
#include "my_sys.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "scope_guard.h"
#include "sql/check_stack.h"
#include "sql/current_thd.h" // current_thd
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/derror.h" // ER_THD
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_sum.h" // Item_sum_max
#include "sql/iterators/basic_row_iterators.h" // ZeroRowsIterator
#include "sql/iterators/composite_iterators.h" // FilterIterator
#include "sql/iterators/ref_row_iterators.h"
#include "sql/iterators/row_iterator.h" // RowIterator
#include "sql/iterators/timing_iterator.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/cost_model.h"
#include "sql/join_optimizer/join_optimizer.h"
#include "sql/key.h"
#include "sql/my_decimal.h"
#include "sql/mysqld.h" // in_left_expr_name
#include "sql/opt_explain_format.h"
#include "sql/opt_trace.h" // OPT_TRACE_TRANSFORM
#include "sql/opt_trace_context.h"
#include "sql/parse_tree_nodes.h" // PT_subquery
#include "sql/query_options.h"
#include "sql/query_result.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h" // Query_block
#include "sql/sql_list.h"
#include "sql/sql_opt_exec_shared.h"
#include "sql/sql_optimizer.h" // JOIN
#include "sql/sql_select.h"
#include "sql/sql_test.h" // print_where
#include "sql/sql_tmp_table.h" // free_tmp_table
#include "sql/sql_union.h" // Query_result_union
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/temp_table_param.h"
#include "sql/thd_raii.h"
#include "sql/window.h"
#include "sql_string.h"
#include "template_utils.h"
class Json_wrapper;
Item_subselect::Item_subselect()
: Item_result_field(),
value_assigned(false),
traced_before(false),
substitution(nullptr),
in_cond_of_tab(NO_PLAN_IDX),
used_tables_cache(0),
have_to_be_excluded(false),
changed(false) {
set_subquery();
reset();
/*
Item value is NULL if Query_result_interceptor didn't change this value
(i.e. some rows will be found returned)
*/
null_value = true;
}
Item_subselect::Item_subselect(const POS &pos)
: super(pos),
value_assigned(false),
traced_before(false),
substitution(nullptr),
in_cond_of_tab(NO_PLAN_IDX),
used_tables_cache(0),
have_to_be_excluded(false),
changed(false) {
set_subquery();
reset();
/*
Item value is NULL if Query_result_interceptor didn't change this value
(i.e. some rows will be found returned)
*/
null_value = true;
}
void Item_subselect::init(Query_block *query_block,
Query_result_subquery *result) {
/*
Please see Item_singlerow_subselect::invalidate_and_restore_query_block(),
which depends on alterations to the parse tree implemented here.
*/
DBUG_TRACE;
DBUG_PRINT("enter", ("query_block: %p", query_block));
unit = query_block->master_query_expression();
if (unit->item) {
subquery = std::move(unit->item->subquery);
parsing_place = unit->item->parsing_place;
unit->item = this;
subquery->change_query_result(current_thd, this, result);
} else {
Query_block *outer_query_block = unit->outer_query_block();
/*
do not take into account expression inside aggregate functions because
they can access original table fields
*/
parsing_place =
(outer_query_block->in_sum_expr ? CTX_NONE
: outer_query_block->parsing_place);
subquery.reset(new (*THR_MALLOC) SubqueryWithResult(unit, result, this));
}
{
Query_block *upper = unit->outer_query_block();
if (upper->parsing_place == CTX_HAVING) upper->subquery_in_having = true;
}
}
/**
Accumulate missing used_tables information from embedded query expression
into the subquery.
This function relies on a few other functions to accumulate information:
accumulate_expression(), accumulate_condition().
Currently, the only property that is accumulated is INNER_TABLE_BIT.
Information about local tables and outer references are accumulated in
mark_as_dependent() (@see item.cc).
RAND_TABLE_BIT is currently not accumulated (but uncacheable is used instead).
@todo - maybe_null is not set properly for all types of subqueries and
expressions. Use this sketch as a guideline for further handling:
- When constructing an Item_subselect, maybe_null is false and null_value
is true. This is obviously wrong.
- When constructing an Item_in_subselect (subclass of Item_subselect),
maybe_null is set true and null_value is set false.
We should probably keep both maybe_null and null_value as false in
the constructor. Then, set maybe_null during preparation, according to
type of subquery:
- Scalar subquery is nullable when query block may have an empty result (not
DUAL or implicitly grouped).
- Scalar subquery is nullable when one of the selected expressions
are nullable.
- Scalar subquery is nullable when WHERE clause or HAVING clause is non-empty
and not always true.
- EXISTS subquery is never nullable!
- IN subquery nullability ignores subquery cardinality.
- IN subquery is nullable when one of the selected expressions are nullable.
- UNIONed query blocks may cancel out nullability.
*/
void Item_subselect::accumulate_properties() {
for (Query_block *select = unit->first_query_block(); select != nullptr;
select = select->next_query_block())
accumulate_properties(select);
for (auto qt : unit->query_terms<QTC_POST_ORDER, VL_SKIP_LEAVES>()) {
/*
qt->query_block() may only contain components with special table
dependencies in the ORDER BY clause, so inspect these expressions only.
(The SELECT list may contain table references that are valid only in
a local scope - references to the UNION temporary table - and should
not be propagated to the subquery level.)
*/
for (ORDER *order = qt->query_block()->order_list.first; order != nullptr;
order = order->next)
accumulate_condition(*order->item);
}
// Save used tables information for the subquery only
used_tables_cache |= m_subquery_used_tables;
}
/**
Accumulate missing used_tables information for a query block.
@param select Reference to query block
*/
void Item_subselect::accumulate_properties(Query_block *select) {
for (Item *item : select->visible_fields()) {
accumulate_expression(item);
}
if (select->where_cond()) accumulate_condition(select->where_cond());
if (select->m_current_table_nest)
walk_join_list(*select->m_current_table_nest,
[this](Table_ref *tr) -> bool {
if (tr->join_cond()) accumulate_condition(tr->join_cond());
return false;
});
for (ORDER *group = select->group_list.first; group; group = group->next)
accumulate_condition(*group->item);
if (select->having_cond()) accumulate_condition(select->having_cond());
for (ORDER *order = select->order_list.first; order; order = order->next)
accumulate_expression(*order->item);
if (select->has_tables()) m_subquery_used_tables |= INNER_TABLE_BIT;
List_iterator<Window> wi(select->m_windows);
Window *w;
while ((w = wi++)) {
for (ORDER *wp = w->first_partition_by(); wp != nullptr; wp = wp->next)
accumulate_expression(*wp->item);
for (ORDER *wo = w->first_order_by(); wo != nullptr; wo = wo->next)
accumulate_expression(*wo->item);
}
}
/**
Accumulate used_tables information for an expression from a query block.
@param item Reference to expression.
*/
void Item_subselect::accumulate_expression(Item *item) {
if (item->used_tables() & ~OUTER_REF_TABLE_BIT)
m_subquery_used_tables |= INNER_TABLE_BIT;
set_nullable(is_nullable() || item->is_nullable());
}
/**
Accumulate used_tables information for a condition from a query block.
@param item Reference to condition.
*/
void Item_subselect::accumulate_condition(Item *item) {
if (item->used_tables() & ~OUTER_REF_TABLE_BIT)
m_subquery_used_tables |= INNER_TABLE_BIT;
}
void Item_subselect::create_iterators(THD *thd) {
if (indexsubquery_engine != nullptr) {
indexsubquery_engine->create_iterators(thd);
}
}
Item_subselect::enum_engine_type Item_subselect::engine_type() const {
if (indexsubquery_engine != nullptr) {
switch (indexsubquery_engine->engine_type()) {
case subselect_indexsubquery_engine::INDEXSUBQUERY_ENGINE:
return Item_subselect::INDEXSUBQUERY_ENGINE;
case subselect_indexsubquery_engine::HASH_SJ_ENGINE:
return Item_subselect::HASH_SJ_ENGINE;
default:
assert(false);
}
}
return Item_subselect::OTHER_ENGINE;
}
const TABLE *Item_subselect::get_table() const {
return down_cast<subselect_hash_sj_engine *>(indexsubquery_engine)
->get_table();
}
const Index_lookup &Item_subselect::index_lookup() const {
return down_cast<subselect_hash_sj_engine *>(indexsubquery_engine)
->index_lookup();
}
join_type Item_subselect::get_join_type() const {
return down_cast<subselect_hash_sj_engine *>(indexsubquery_engine)
->get_join_type();
}
void Item_subselect::cleanup() {
Item_result_field::cleanup();
if (indexsubquery_engine) {
indexsubquery_engine->cleanup();
destroy(indexsubquery_engine);
indexsubquery_engine = nullptr;
}
if (subquery) subquery->cleanup();
reset();
value_assigned = false;
traced_before = false;
in_cond_of_tab = NO_PLAN_IDX;
}
void Item_singlerow_subselect::cleanup() {
DBUG_TRACE;
Item_subselect::cleanup();
}
/**
Decide whether to mark the injected left expression "outer" relative to
the subquery. It should be marked as outer in the following cases:
1) If the left expression is not constant.
2) If the left expression could be a constant NULL and we care about the
difference between UNKNOWN and FALSE. In this case, JOIN::optimize() for
the subquery must be prevented from evaluating any triggered condition, as
the triggers for such conditions have not yet been properly set by
Item_in_optimizer::val_int(). By marking the left expression as outer, a
triggered condition using it will not be considered constant, will not be
evaluated by JOIN::optimize(); it will only be evaluated by JOIN::exec()
which is called from Item_in_optimizer::val_int()
3) If the left expression comes from a subquery and is not a basic
constant. In this case, the value cannot be read until after the subquery
has been evaluated. By marking it as outer, we prevent it from being read
when JOIN::optimize() attempts to evaluate constant conditions.
@param[in] left_row The item that represents the left operand of the IN
operator
@param[in] col The column number of the expression in the left operand
to possibly mark as dependent of the outer select
@returns true if we should mark the injected left expression "outer"
relative to the subquery
*/
bool Item_in_subselect::mark_as_outer(Item *left_row, size_t col) {
const Item *left_col = left_row->element_index(col);
return !left_col->const_item() ||
(!abort_on_null && left_col->is_nullable()) ||
(left_row->type() == SUBSELECT_ITEM && !left_col->basic_const_item());
}
bool Item_in_subselect::finalize_exists_transform(THD *thd,
Query_block *query_block) {
assert(strategy == Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT ||
strategy == Subquery_strategy::SUBQ_EXISTS);
/*
Note that if the subquery is "SELECT1 UNION SELECT2" then this is not
working optimally (Bug#14215895).
*/
if (!(unit->global_parameters()->select_limit = new Item_int(1))) return true;
unit->global_parameters()->m_internal_limit = true;
if (unit->set_limit(thd, unit->global_parameters()))
return true; /* purecov: inspected */
if (unit->finalize(thd)) {
return true;
}
query_block->join->allow_outer_refs = true; // for JOIN::set_prefix_tables()
strategy = Subquery_strategy::SUBQ_EXISTS;
return false;
}
Item *remove_in2exists_conds(Item *conds) {
bool modified = false;
List<Item> new_conds;
if (WalkConjunction(conds, [&modified, &new_conds](Item *cond) {
if (cond->created_by_in2exists()) {
modified = true;
return false;
} else {
return new_conds.push_back(cond);
}
})) {
return nullptr;
}
return modified ? CreateConjunction(&new_conds) : conds;
}
bool Item_in_subselect::finalize_materialization_transform(THD *thd,
JOIN *join) {
assert(strategy == Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT);
assert(join == subquery->single_query_block()->join);
// No UNION in materialized subquery so this holds:
assert(join->query_block == unit->first_query_block());
assert(join->query_expression() == unit);
assert(unit->global_parameters()->select_limit == nullptr);
strategy = Subquery_strategy::SUBQ_MATERIALIZATION;
// We need to undo several changes which IN->EXISTS had done:
/*
The conditions added by in2exists depend on the concrete value from the
outer query block, so they need to be removed before we materialize.
*/
// This part is not relevant for the hypergraph optimizer.
if (join->where_cond)
join->where_cond = remove_in2exists_conds(join->where_cond);
if (join->having_cond)
join->having_cond = remove_in2exists_conds(join->having_cond);
// This part is only relevant for the hypergraph optimizer.
unit->change_to_access_path_without_in2exists(thd);
assert(!in2exists_info->dependent_before);
if (unit->finalize(thd)) {
return true;
}
join->query_block->uncacheable &= ~UNCACHEABLE_DEPENDENT;
unit->uncacheable &= ~UNCACHEABLE_DEPENDENT;
OPT_TRACE_TRANSFORM(&thd->opt_trace, oto0, oto1,
subquery->single_query_block()->select_number,
"IN (SELECT)", "materialization");
oto1.add("chosen", true);
subselect_hash_sj_engine *const new_engine =
new (thd->mem_root) subselect_hash_sj_engine(this, unit);
if (!new_engine) return true;
if (new_engine->setup(thd, *unit->get_unit_column_types())) {
/*
For some reason we cannot use materialization for this IN predicate.
Delete all materialization-related objects, and return error.
*/
new_engine->cleanup();
destroy(new_engine);
return true;
}
indexsubquery_engine = new_engine;
join->allow_outer_refs = false; // for JOIN::set_prefix_tables()
return false;
}
void Item_in_subselect::cleanup() {
DBUG_TRACE;
if (left_expr_cache) {
left_expr_cache->destroy_elements();
destroy(left_expr_cache);
left_expr_cache = nullptr;
}
left_expr_cache_filled = false;
need_expr_cache = true;
switch (strategy) {
case Subquery_strategy::SUBQ_MATERIALIZATION:
if (in2exists_info->dependent_after) {
unit->first_query_block()->uncacheable |= UNCACHEABLE_DEPENDENT;
unit->uncacheable |= UNCACHEABLE_DEPENDENT;
}
[[fallthrough]];
case Subquery_strategy::SUBQ_EXISTS:
/*
Back to EXISTS_OR_MAT, so that next execution of this statement can
choose between the two.
*/
unit->global_parameters()->select_limit = nullptr;
strategy = Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT;
break;
default:
break;
}
Item_subselect::cleanup();
}
AccessPath *Item_in_subselect::root_access_path() const {
if (strategy == Subquery_strategy::SUBQ_MATERIALIZATION &&
indexsubquery_engine->engine_type() ==
subselect_indexsubquery_engine::HASH_SJ_ENGINE) {
return down_cast<subselect_hash_sj_engine *>(indexsubquery_engine)
->root_access_path();
} else {
// Only subselect_hash_sj_engine owns its own iterator;
// for subselect_indexsubquery_engine, the unit still has it, since it's a
// normally executed query block. Thus, we should never get called
// otherwise.
//
// However, in some situations where the hypergraph optimizer prints out
// the query to the log for debugging, it isn't fully optimized
// yet and might not yet have an iterator. Thus, return nullptr instead of
// assert-failing.
assert(current_thd->lex->using_hypergraph_optimizer());
return nullptr;
}
}
bool Item_subselect::fix_fields(THD *thd, Item **ref) {
char const *save_where = thd->where;
uint8 uncacheable;
bool res;
assert(!fixed);
assert(indexsubquery_engine == nullptr);
#ifndef NDEBUG
// Engine accesses THD via its 'item' pointer, check it:
assert(subquery->get_item() == this);
#endif
if (check_stack_overrun(thd, STACK_MIN_SIZE, (uchar *)&res)) return true;
if (!(res = subquery->prepare(thd))) {
// all transformation is done (used by prepared statements)
changed = true;
// Accumulate properties referring to "inner tables"
accumulate_properties();
/*
Substitute the current item with an Item_in_optimizer that was
created by Item_in_subselect::select_in_like_transformer and
call fix_fields for the substituted item which in turn calls
subquery->prepare for the subquery predicate.
*/
if (substitution) {
int ret = 0;
(*ref) = substitution;
if (item_name.is_set()) {
substitution->item_name = item_name;
}
if (have_to_be_excluded) {
unit->exclude_level();
}
substitution = nullptr;
thd->where = "checking transformed subquery";
if (!(*ref)->fixed) ret = (*ref)->fix_fields(thd, ref);
thd->where = save_where;
return ret;
}
// Is it one field subselect?
if (unit_cols() > max_columns) {
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
return true;
}
if (resolve_type(thd)) goto err;
} else
goto err;
if ((uncacheable = unit->uncacheable)) {
if (uncacheable & UNCACHEABLE_RAND) {
m_subquery_used_tables |= RAND_TABLE_BIT;
used_tables_cache |= RAND_TABLE_BIT;
}
}
/*
If this subquery references window functions, per the SQL standard they
are aggregated in the subquery's query block, and never outside of it, so:
*/
assert(!has_wf());
fixed = true;
err:
thd->where = save_where;
return res;
}
bool Item_subselect::walk(Item_processor processor, enum_walk walk,
uchar *arg) {
if ((walk & enum_walk::PREFIX) && (this->*processor)(arg)) return true;
if ((walk & enum_walk::SUBQUERY) && unit->walk(processor, walk, arg))
return true;
return (walk & enum_walk::POSTFIX) && (this->*processor)(arg);
}
/**
Register subquery to the table where it is used within a condition.
@param arg qep_row to which the subquery belongs
@retval false
@note We always return "false" as far as we don't want to dive deeper because
we explain inner subqueries in their joins contexts.
*/
bool Item_subselect::explain_subquery_checker(uchar **arg) {
qep_row *qr = reinterpret_cast<qep_row *>(*arg);
qr->register_where_subquery(unit);
return false;
}
bool Item_subselect::exec(THD *thd) {
DBUG_TRACE;
/*
Do not execute subselect in case of a fatal error
or if the query has been killed.
*/
if (thd->is_error() || thd->killed) return true;
// No subqueries should be evaluated when analysing a view
assert(!thd->lex->is_view_context_analysis());
/*
Simulate a failure in sub-query execution. Used to test e.g.
out of memory or query being killed conditions.
*/
DBUG_EXECUTE_IF("subselect_exec_fail", return true;);
/*
Disable tracing of subquery execution if
1) this is not the first time the subselect is executed, and
2) REPEATED_SUBSELECT is disabled
*/
Opt_trace_context *const trace = &thd->opt_trace;
const bool disable_trace =
traced_before &&
!trace->feature_enabled(Opt_trace_context::REPEATED_SUBSELECT);
Opt_trace_disable_I_S disable_trace_wrapper(trace, disable_trace);
traced_before = true;
Opt_trace_object trace_wrapper(trace);
Opt_trace_object trace_exec(trace, "subselect_execution");
trace_exec.add_select_number(unit->first_query_block()->select_number);
Opt_trace_array trace_steps(trace, "steps");
// subselect_hash_sj_engine creates its own iterators; it does not call
// exec().
bool should_create_iterators =
!(indexsubquery_engine != nullptr &&
indexsubquery_engine->engine_type() ==
subselect_indexsubquery_engine::HASH_SJ_ENGINE);
// Normally, the unit would be optimized here, but statements like DO and SET
// may still rely on lazy optimization. Also, we might not have iterators,
// so make sure to create them if they're missing.
if (!unit->is_optimized()) {
if (unit->optimize(thd, /*materialize_destination=*/nullptr,
/*create_iterators=*/false,
/*finalize_access_paths=*/false))
return true;
// NOTE: We defer finalization and creating iterators to the statements
// below; asking optimize() to finalize a plan with two choices
// (materialization and exists) rightfully causes a failed assertion. We
// should probably have made a cost-based choice between the two here, but
// as it stands, we will always implicitly choose in2exists in these cases
// (DO/SET).
}
if (should_create_iterators && unit->root_access_path() != nullptr) {
if (unit->finalize(thd)) {
return true;
}
if (unit->force_create_iterators(thd)) return true;
}
if (indexsubquery_engine != nullptr) {
return indexsubquery_engine->exec(thd);
} else {
return subquery->exec(thd);
}
}
/// @see Query_expression::fix_after_pullout()
void Item_subselect::fix_after_pullout(Query_block *parent_query_block,
Query_block *removed_query_block)
{
/* Clear usage information for this subquery predicate object */
used_tables_cache = 0;
m_subquery_used_tables = 0;
unit->fix_after_pullout(parent_query_block, removed_query_block);
// Accumulate properties like INNER_TABLE_BIT
accumulate_properties();
if (unit->uncacheable & UNCACHEABLE_RAND) {
m_subquery_used_tables |= RAND_TABLE_BIT;
}
used_tables_cache = m_subquery_used_tables;
}
bool Item_in_subselect::walk(Item_processor processor, enum_walk walk,
uchar *arg) {
if (left_expr->walk(processor, walk, arg)) return true;
return Item_subselect::walk(processor, walk, arg);
}
Item *Item_in_subselect::transform(Item_transformer transformer, uchar *arg) {
left_expr = left_expr->transform(transformer, arg);
if (left_expr == nullptr) return nullptr;
return (this->*transformer)(arg);
}
Item *Item_in_subselect::compile(Item_analyzer analyzer, uchar **arg_p,
Item_transformer transformer, uchar *arg_t) {
if (!(this->*analyzer)(arg_p)) return this;
// Compile the left expression of the IN subquery
Item *item = left_expr->compile(analyzer, arg_p, transformer, arg_t);
if (item == nullptr) return nullptr; /* purecov: inspected */
if (item != left_expr) current_thd->change_item_tree(&left_expr, item);
return (this->*transformer)(arg_t);
}
/*
Compute the IN predicate if the left operand's cache changed.
*/
bool Item_in_subselect::exec(THD *thd) {
DBUG_TRACE;
assert(strategy != Subquery_strategy::SUBQ_MATERIALIZATION ||
indexsubquery_engine->engine_type() ==
subselect_indexsubquery_engine::HASH_SJ_ENGINE);
/*
Initialize the cache of the left predicate operand. This has to be done as
late as now, because Cached_item directly contains a resolved field (not
an item, and in some cases (when temp tables are created), these fields
end up pointing to the wrong field. One solution is to change Cached_item
to not resolve its field upon creation, but to resolve it dynamically
from a given Item_ref object.
Do not init the cache if a previous execution decided that it is not needed.
TODO: the cache should be applied conditionally based on:
- rules - e.g. only if the left operand is known to be ordered, and/or
- on a cost-based basis, that takes into account the cost of a cache
lookup, the cache hit rate, and the savings per cache hit.
*/
if (need_expr_cache && !left_expr_cache &&
strategy == Subquery_strategy::SUBQ_MATERIALIZATION &&
init_left_expr_cache(thd))
return true;
if (left_expr_cache != nullptr) {
const int result = update_item_cache_if_changed(*left_expr_cache);
if (left_expr_cache_filled && // cache was previously filled
result < 0) // new value is identical to previous cached value
{
/*
We needn't do a full execution, can just reuse "value", "was_null",
"null_value" of the previous execution.
*/
return false;
}
left_expr_cache_filled = true;
}
const bool uncacheable = unit->uncacheable || indexsubquery_engine != nullptr;
if (unit->is_executed() && uncacheable) {
// Second or later execution (and it's actually going to be executed,
// not just the return the cached value from the first run), so clear out
// state from the previous run(s).
//
// Note that subselect_hash_sj_engine and subselect_indexsubquery_engine
// are both uncacheable (due to dependency on the left side), even if the
// underlying unit is not marked as such.
null_value = false;
was_null = false;
}
return Item_subselect::exec(thd);
}
Item::Type Item_subselect::type() const { return SUBSELECT_ITEM; }
bool Item_subselect::resolve_type(THD *) {
subquery->fix_length_and_dec(nullptr);
return false;
}
Item *Item_subselect::get_tmp_table_item(THD *thd_arg) {
DBUG_TRACE;
if (!has_aggregation() && !(const_for_execution() &&
evaluate_during_optimization(
this, thd_arg->lex->current_query_block()))) {
Item *result = new Item_field(result_field);
return result;
}
Item *result = copy_or_same(thd_arg);
return result;
}
void Item_subselect::update_used_tables() {
if (!unit->uncacheable) {
// There is no expression with outer reference, randomness or side-effect,
// so the subquery's content depends only on its inner tables:
m_subquery_used_tables &= INNER_TABLE_BIT;
}
used_tables_cache = m_subquery_used_tables;
}
void Item_subselect::print(const THD *thd, String *str,
enum_query_type query_type) const {
if (subquery) {
str->append('(');
if (query_type & QT_SUBSELECT_AS_ONLY_SELECT_NUMBER) {
str->append("select #");
uint select_number = unit->first_query_block()->select_number;
str->append_ulonglong(select_number);
} else if (indexsubquery_engine != nullptr) {
indexsubquery_engine->print(thd, str, query_type);
} else {
subquery->print(thd, str, query_type);
}
str->append(')');
} else
str->append("(...)");
}
/* Single value subselect interface class */
class Query_result_scalar_subquery : public Query_result_subquery {
public:
explicit Query_result_scalar_subquery(Item_subselect *item_arg)
: Query_result_subquery(item_arg) {}
bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
};
bool Query_result_scalar_subquery::send_data(
THD *thd, const mem_root_deque<Item *> &items) {
DBUG_TRACE;
Item_singlerow_subselect *it = down_cast<Item_singlerow_subselect *>(item);
if (it->assigned()) {
my_error(ER_SUBQUERY_NO_1_ROW, MYF(0));
return true;
}
uint i = 0;
for (Item *val_item : VisibleFields(items)) {
it->store(i++, val_item);
}
if (thd->is_error()) return true;
it->assigned(true);
return false;
}
Item_singlerow_subselect::Item_singlerow_subselect(Query_block *query_block)
: Item_subselect(), value(nullptr), no_rows(false) {
DBUG_TRACE;
init(query_block, new (*THR_MALLOC) Query_result_scalar_subquery(this));
set_nullable(true); // if the subquery is empty, value is NULL
max_columns = UINT_MAX;
}
Query_block *Item_singlerow_subselect::invalidate_and_restore_query_block() {
DBUG_TRACE;
Query_block *result = unit->first_query_block();
assert(result);
/*
This code restore the parse tree in it's state before the execution of
Item_singlerow_subselect::Item_singlerow_subselect(),
and in particular decouples this object from the Query_block,
so that the Query_block can be used with a different flavor
or Item_subselect instead, as part of query rewriting.
*/
unit->item = nullptr;
return result;
}
/* used in independent ALL/ANY optimisation */
class Query_result_max_min_subquery final : public Query_result_subquery {
Item_cache *cache;
bool (Query_result_max_min_subquery::*op)();
bool fmax;
/**
If ignoring NULLs, comparisons will skip NULL values. If not
ignoring NULLs, the first (if any) NULL value discovered will be
returned as the maximum/minimum value.
*/
bool ignore_nulls;
public:
Query_result_max_min_subquery(Item_subselect *item_arg, bool mx,
bool ignore_nulls)
: Query_result_subquery(item_arg),
cache(nullptr),
fmax(mx),
ignore_nulls(ignore_nulls) {}
void cleanup() override;
bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
private:
bool cmp_real();
bool cmp_int();
bool cmp_decimal();
bool cmp_str();
};
void Query_result_max_min_subquery::cleanup() {
DBUG_TRACE;
cache = nullptr;
}
bool Query_result_max_min_subquery::send_data(
THD *, const mem_root_deque<Item *> &items) {
DBUG_TRACE;
Item_maxmin_subselect *it = (Item_maxmin_subselect *)item;
Item *val_item = nullptr;
for (Item *item : VisibleFields(items)) {
val_item = item;
break;
}
it->register_value();
if (it->assigned()) {
cache->store(val_item);
if ((this->*op)()) it->store(0, cache);
} else {
if (!cache) {
cache = Item_cache::get_cache(val_item);
switch (val_item->result_type()) {
case REAL_RESULT:
op = &Query_result_max_min_subquery::cmp_real;
break;
case INT_RESULT:
op = &Query_result_max_min_subquery::cmp_int;
break;
case STRING_RESULT:
op = &Query_result_max_min_subquery::cmp_str;
break;
case DECIMAL_RESULT:
op = &Query_result_max_min_subquery::cmp_decimal;
break;
case ROW_RESULT:
case INVALID_RESULT:
// This case should never be chosen
assert(0);
op = nullptr;
}
}
cache->store(val_item);
it->store(0, cache);
}
it->assigned(true);
return false;
}
/**
Compare two floating point numbers for MAX or MIN.
Compare two numbers and decide if the number should be cached as the
maximum/minimum number seen this far. If fmax==true, this is a
comparison for MAX, otherwise it is a comparison for MIN.
val1 is the new number to compare against the current
maximum/minimum. val2 is the current maximum/minimum.
ignore_nulls is used to control behavior when comparing with a NULL
value. If ignore_nulls==false, the behavior is to store the first
NULL value discovered (i.e, return true, that it is larger than the
current maximum) and never replace it. If ignore_nulls==true, NULL
values are not stored. ANY subqueries use ignore_nulls==true, ALL
subqueries use ignore_nulls==false.
@retval true if the new number should be the new maximum/minimum.
@retval false if the maximum/minimum should stay unchanged.
*/
bool Query_result_max_min_subquery::cmp_real() {
Item *maxmin = ((Item_singlerow_subselect *)item)->element_index(0);
double val1 = cache->val_real(), val2 = maxmin->val_real();
/*
If we're ignoring NULLs and the current maximum/minimum is NULL
(must have been placed there as the first value iterated over) and
the new value is not NULL, return true so that a new, non-NULL
maximum/minimum is set. Otherwise, return false to keep the
current non-NULL maximum/minimum.
If we're not ignoring NULLs and the current maximum/minimum is not
NULL, return true to store NULL. Otherwise, return false to keep
the NULL we've already got.
*/
if (cache->null_value || maxmin->null_value)
return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
return (fmax) ? (val1 > val2) : (val1 < val2);
}
/**
Compare two integer numbers for MAX or MIN.
@see Query_result_max_min_subquery::cmp_real()
*/
bool Query_result_max_min_subquery::cmp_int() {
Item *maxmin = ((Item_singlerow_subselect *)item)->element_index(0);
longlong val1 = cache->val_int(), val2 = maxmin->val_int();
if (cache->null_value || maxmin->null_value)
return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
return (fmax) ? (val1 > val2) : (val1 < val2);
}
/**
Compare two decimal numbers for MAX or MIN.
@see Query_result_max_min_subquery::cmp_real()
*/
bool Query_result_max_min_subquery::cmp_decimal() {
Item *maxmin = ((Item_singlerow_subselect *)item)->element_index(0);
my_decimal cval, *cvalue = cache->val_decimal(&cval);
my_decimal mval, *mvalue = maxmin->val_decimal(&mval);
if (cache->null_value || maxmin->null_value)
return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
return (fmax) ? (my_decimal_cmp(cvalue, mvalue) > 0)
: (my_decimal_cmp(cvalue, mvalue) < 0);
}
/**
Compare two strings for MAX or MIN.
@see Query_result_max_min_subquery::cmp_real()
*/
bool Query_result_max_min_subquery::cmp_str() {
Item *maxmin = down_cast<Item_singlerow_subselect *>(item)->element_index(0);
/*
as far as both operand is Item_cache buf1 & buf2 will not be used,
but added for safety
*/
String buf1;
const String *val1 = cache->val_str(&buf1);
if (current_thd->is_error()) return false;
String buf2;
const String *val2 = maxmin->val_str(&buf2);
if (cache->null_value || maxmin->null_value)
return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
return (fmax) ? (sortcmp(val1, val2, cache->collation.collation) > 0)
: (sortcmp(val1, val2, cache->collation.collation) < 0);
}
Item_maxmin_subselect::Item_maxmin_subselect(Item_subselect *parent,
Query_block *query_block,
bool max_arg, bool ignore_nulls)
: Item_singlerow_subselect(), was_values(false) {
DBUG_TRACE;
max = max_arg;
init(query_block, new (*THR_MALLOC) Query_result_max_min_subquery(
this, max_arg, ignore_nulls));
max_columns = 1;
set_nullable(true);
max_columns = 1;
/*
Following information was collected during performing fix_fields()
of Items belonged to subquery, which will be not repeated
*/
used_tables_cache = parent->used_tables();
}
void Item_maxmin_subselect::cleanup() {
DBUG_TRACE;
Item_singlerow_subselect::cleanup();
was_values = false;
}
void Item_maxmin_subselect::print(const THD *thd, String *str,
enum_query_type query_type) const {
str->append(max ? "<max>" : "<min>", 5);
Item_singlerow_subselect::print(thd, str, query_type);
}
void Item_singlerow_subselect::reset() {
null_value = true;
if (value) value->null_value = true;
}
/**
@todo
- We can't change name of Item_field or Item_ref, because it will
prevent it's correct resolving, but we should save name of
removed item => we do not make optimization if top item of
list is field or reference.
- switch off this optimization for prepare statement,
because we do not rollback this changes.
Make rollback for it, or special name resolving mode in 5.0.
*/
bool Item_singlerow_subselect::select_transformer(THD *thd,
Query_block *select) {
DBUG_TRACE;
if (changed) return false;
Query_block *outer = select->outer_query_block();
Item *single_field = select->single_visible_field();
if (single_field != nullptr &&
single_field->type() == Item::VALUES_COLUMN_ITEM) {
/*
A subquery that is a VALUES clause can be used as a scalar subquery.
But VALUES clauses with a single row are transformed to their simple
components and will not be shown as VALUES_COLUMN_ITEM here.
*/
assert(select->row_value_list->size() > 1);
/*
Since scalar subqueries can have at most one row, reject VALUES
clauses (with more than one row) immediately:
*/
my_error(ER_SUBQUERY_NO_1_ROW, MYF(0));
return true;
}
if (!unit->is_set_operation() && !select->has_tables() &&
single_field != nullptr && !single_field->has_aggregation() &&
select->olap == UNSPECIFIED_OLAP_TYPE && !single_field->has_wf() &&
select->where_cond() == nullptr && select->having_cond() == nullptr) {
have_to_be_excluded = true;
if (thd->lex->is_explain()) {
char warn_buff[MYSQL_ERRMSG_SIZE];
sprintf(warn_buff, ER_THD(thd, ER_SELECT_REDUCED), select->select_number);
push_warning(thd, Sql_condition::SL_NOTE, ER_SELECT_REDUCED, warn_buff);
}
substitution = single_field;
if (substitution->type() == SUBSELECT_ITEM) {
Item_subselect *subs = down_cast<Item_subselect *>(substitution);
subs->unit->set_explain_marker_from(thd, unit);
}
// Merge subquery's name resolution contexts into parent's
outer->merge_contexts(select);
// Fix query block contexts after merging the subquery
substitution->fix_after_pullout(outer, select);
}
return false;
}
void Item_singlerow_subselect::store(uint i, Item *item) {
row[i]->store(item);
row[i]->cache_value();
}
enum Item_result Item_singlerow_subselect::result_type() const {
return subquery->type();
}
bool Item_singlerow_subselect::resolve_type(THD *thd) {
if ((max_columns = unit_cols()) == 1) {
subquery->fix_length_and_dec(row = &value);
} else {
row = thd->mem_root->ArrayAlloc<Item_cache *>(max_columns);
if (row == nullptr) {
return true;
}
subquery->fix_length_and_dec(row);
assert(*row != nullptr);
value = *row;
}
set_data_type(subquery->field_type());
unsigned_flag = value->unsigned_flag;
/*
Check if NULL values may be returned by the subquery. Either
because one or more of the columns could be NULL, or because the
subquery could return an empty result.
*/
set_nullable(subquery->may_be_null());
return false;
}
void Item_singlerow_subselect::no_rows_in_result() {
/*
This is only possible if we have a dependent subquery in the SELECT list
and an aggregated outer query based on zero rows, which is an illegal query
according to the SQL standard. ONLY_FULL_GROUP_BY rejects such queries.
*/
if (unit->uncacheable & UNCACHEABLE_DEPENDENT) no_rows = true;
}
bool Item_singlerow_subselect::check_cols(uint c) {
if (c != unit_cols()) {
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
return true;
}
return false;
}
bool Item_singlerow_subselect::null_inside() {
for (uint i = 0; i < max_columns; i++) {
if (row[i]->null_value) return true;
}
return false;
}
void Item_singlerow_subselect::bring_value() {
if (!exec(current_thd) && assigned())
null_value = false;
else
reset();
}
double Item_singlerow_subselect::val_real() {
assert(fixed == 1);
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->val_real();
} else {
reset();
return error_real();
}
}
longlong Item_singlerow_subselect::val_int() {
assert(fixed == 1);
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->val_int();
} else {
reset();
return error_int();
}
}
String *Item_singlerow_subselect::val_str(String *str) {
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->val_str(str);
} else {
reset();
return error_str();
}
}
my_decimal *Item_singlerow_subselect::val_decimal(my_decimal *decimal_value) {
if (!no_rows && !exec(current_thd) && !value->null_value) {
my_decimal *retval = value->val_decimal(decimal_value);
null_value = value->null_value;
return retval;
} else {
reset();
return error_decimal(decimal_value);
}
}
bool Item_singlerow_subselect::val_json(Json_wrapper *result) {
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->val_json(result);
} else {
reset();
return current_thd->is_error();
}
}
bool Item_singlerow_subselect::get_date(MYSQL_TIME *ltime,
my_time_flags_t fuzzydate) {
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->get_date(ltime, fuzzydate);
} else {
reset();
return true;
}
}
bool Item_singlerow_subselect::get_time(MYSQL_TIME *ltime) {
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->get_time(ltime);
} else {
reset();
return true;
}
}
bool Item_singlerow_subselect::val_bool() {
if (!no_rows && !exec(current_thd) && !value->null_value) {
null_value = false;
return value->val_bool();
} else {
reset();
return false;
}
}
/* EXISTS subselect interface class */
class Query_result_exists_subquery : public Query_result_subquery {
public:
explicit Query_result_exists_subquery(Item_subselect *item_arg)
: Query_result_subquery(item_arg) {}
bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
};
bool Query_result_exists_subquery::send_data(THD *,
const mem_root_deque<Item *> &) {
DBUG_TRACE;
Item_exists_subselect *it = (Item_exists_subselect *)item;
/*
A subquery may be evaluated 1) by executing the JOIN 2) by optimized
functions (index_subquery, subquery materialization).
It's only in (1) that we get here when we find a row. In (2) "value" is
set elsewhere.
*/
it->value = true;
it->assigned(true);
return false;
}
Item_exists_subselect::Item_exists_subselect(Query_block *select)
: Item_subselect() {
DBUG_TRACE;
init(select, new (*THR_MALLOC) Query_result_exists_subquery(this));
max_columns = UINT_MAX;
null_value = false; // can't be NULL
set_nullable(false); // can't be NULL
}
void Item_exists_subselect::print(const THD *thd, String *str,
enum_query_type query_type) const {
const char *tail = Item_bool_func::bool_transform_names[value_transform];
if (implicit_is_op) tail = "";
// Put () around NOT as it has lower associativity than IS TRUE, or '+'
if (value_transform == BOOL_NEGATED) str->append(STRING_WITH_LEN("(not "));
str->append(STRING_WITH_LEN("exists"));
Item_subselect::print(thd, str, query_type);
if (value_transform == BOOL_NEGATED) str->append(STRING_WITH_LEN(")"));
if (tail[0]) {
str->append(STRING_WITH_LEN(" "));
str->append(tail, strlen(tail));
}
}
/**
Translates the value of the naked EXISTS to a value taking into account the
optional NULL and IS [NOT] TRUE/FALSE.
@param[in,out] null_v NULL state of the value
@param v TRUE/FALSE state of the value
*/
bool Item_exists_subselect::translate(bool &null_v, bool v) {
if (null_v) // Naked IN returns UNKNOWN
{
assert(substype() != EXISTS_SUBS);
switch (value_transform) {
case BOOL_IDENTITY:
case BOOL_NEGATED:
return false;
case BOOL_IS_TRUE:
case BOOL_IS_FALSE:
null_v = false;
return false;
case BOOL_NOT_TRUE:
case BOOL_NOT_FALSE:
null_v = false;
return true;
default:
assert(false);
return false;
}
}
// Naked IN returns 'v'
switch (value_transform) {
case BOOL_IDENTITY:
case BOOL_IS_TRUE:
case BOOL_NOT_FALSE:
return v;
case BOOL_NEGATED:
case BOOL_NOT_TRUE:
case BOOL_IS_FALSE:
return !v;
default:
assert(false);
return v;
}
}
Item *Item_exists_subselect::truth_transformer(THD *, enum Bool_test test) {
// ALL_SUBS, ANY_SUBS are always wrapped in Item_func_{not|nop}_all
// so never come here. Which is good as they don't support all possible
// value transforms.
assert(substype() == EXISTS_SUBS || substype() == IN_SUBS);
switch (test) {
case BOOL_NEGATED:
case BOOL_IS_TRUE:
case BOOL_IS_FALSE:
case BOOL_NOT_TRUE:
case BOOL_NOT_FALSE:
break;
default:
assert(false);
}
// x IN (SELECT y FROM DUAL) may be replaced with x=y which alas doesn't
// support value transforms; we still want to allow this replacement, so
// let's not store the value transform in that case, and keep an explicit
// truth test Item at the outside.
if (!unit->is_set_operation() && !unit->first_query_block()->has_tables() &&
unit->first_query_block()->where_cond() == nullptr &&
substype() == IN_SUBS &&
unit->first_query_block()->single_visible_field() != nullptr)
return nullptr;
// Combine requested test with already present test, if any.
value_transform = Item_bool_func::bool_transform[value_transform][test];
return this;
}
bool Item_in_subselect::test_limit() {
for (auto qt : unit->query_terms<QTC_PRE_ORDER>())
if (qt->query_block()->test_limit()) return true;
return false;
}
Item_in_subselect::Item_in_subselect(Item *left_exp, Query_block *select)
: Item_exists_subselect(),
left_expr(left_exp),
left_expr_cache(nullptr),
left_expr_cache_filled(false),
need_expr_cache(true),
m_injected_left_expr(nullptr),
optimizer(nullptr),
was_null(false),
abort_on_null(false),
in2exists_info(nullptr),
pushed_cond_guards(nullptr),
upper_item(nullptr) {
DBUG_TRACE;
init(select, new (*THR_MALLOC) Query_result_exists_subquery(this));
max_columns = UINT_MAX;
set_nullable(true);
reset();
// if test_limit will fail then error will be reported to client
test_limit();
}
Item_in_subselect::Item_in_subselect(const POS &pos, Item *left_exp,
PT_subquery *pt_subquery_arg)
: super(pos),
left_expr(left_exp),
left_expr_cache(nullptr),
left_expr_cache_filled(false),
need_expr_cache(true),
m_injected_left_expr(nullptr),
optimizer(nullptr),
was_null(false),
abort_on_null(false),
in2exists_info(nullptr),
pushed_cond_guards(nullptr),
upper_item(nullptr),
pt_subselect(pt_subquery_arg) {
DBUG_TRACE;
max_columns = UINT_MAX;
set_nullable(true);
reset();
}
bool Item_in_subselect::itemize(Parse_context *pc, Item **res) {
if (skip_itemize(res)) return false;
if (super::itemize(pc, res) || left_expr->itemize(pc, &left_expr) ||
pt_subselect->contextualize(pc))
return true;
Query_block *query_block = pt_subselect->value();
init(query_block, new (*THR_MALLOC) Query_result_exists_subquery(this));
if (test_limit()) return true;
return false;
}
Item_allany_subselect::Item_allany_subselect(Item *left_exp,
chooser_compare_func_creator fc,
Query_block *select, bool all_arg)
: Item_in_subselect(), func_creator(fc), all(all_arg) {
DBUG_TRACE;
left_expr = left_exp;
func = func_creator(all_arg);
init(select, new (*THR_MALLOC) Query_result_exists_subquery(this));
max_columns = 1;
reset();
// if test_limit will fail then error will be reported to client
test_limit();
}
bool Item_exists_subselect::resolve_type(THD *thd) {
set_data_type_longlong();
max_length = 1;
max_columns = unit_cols();
if (strategy == Subquery_strategy::SUBQ_EXISTS) {
Prepared_stmt_arena_holder ps_arena_holder(thd);
/*
We need only 1 row to determine existence if LIMIT is not 0.
Note that if the subquery is "SELECT1 UNION SELECT2" then this is not
working optimally (Bug#14215895).
*/
if (unit->global_parameters()->select_limit == nullptr ||
unit->global_parameters()->select_limit->val_uint() > 0) {
unit->global_parameters()->select_limit = new Item_int(1);
if (unit->global_parameters()->select_limit == nullptr) return true;
unit->global_parameters()->m_internal_limit = true;
}
}
return false;
}
/**
Helper for resolve_subquery().
@returns true if semijoin or antijoin is allowed; if returning true, also
records in the Item's can_do_aj member if this will be an antijoin (true)
or semijoin (false) nest.
*/
bool Item_exists_subselect::choose_semijoin_or_antijoin() {
can_do_aj = false;
[[maybe_unused]] bool might_do_sj = false, might_do_aj = false;
bool null_problem = false;
switch (value_transform) {
case BOOL_IS_TRUE:
might_do_sj = true;
break;
case BOOL_NOT_TRUE:
might_do_aj = true;
break;
case BOOL_IS_FALSE:
might_do_aj = true;
null_problem = true;
break;
case BOOL_NOT_FALSE:
might_do_sj = true;
null_problem = true;
break;
default:
return false;
}
assert((might_do_sj ^ might_do_aj) == 1);
if (substype() == EXISTS_SUBS) // never returns NULL
null_problem = false;
if (null_problem) {
// antijoin/semijoin cannot work with NULLs on either side of IN
if (down_cast<Item_in_subselect *>(this)->left_expr->is_nullable())
return false;
for (Item *inner : unit->first_query_block()->visible_fields()) {
if (inner->is_nullable()) return false;
}
}
can_do_aj = might_do_aj;
return true;
}
double Item_exists_subselect::val_real() { return val_bool(); }
longlong Item_exists_subselect::val_int() { return val_bool(); }
/**
Return the result of EXISTS as a string value
Converts the true/false result into a string value.
@param [out] str buffer to hold the resulting string value
@retval Pointer to the converted string.
NULL if execution returns in error
*/
String *Item_exists_subselect::val_str(String *str) {
longlong val = val_bool();
if (null_value) return nullptr;
str->set(val, &my_charset_bin);
return str;
}
/**
Return the result of EXISTS as a decimal value
Converts the true/false result into a decimal value.
@param [out] decimal_value Buffer to hold the resulting decimal value
@retval Pointer to the converted decimal.
NULL if execution returns in error
*/
my_decimal *Item_exists_subselect::val_decimal(my_decimal *decimal_value) {
longlong val = val_bool();
if (null_value) return nullptr;
int2my_decimal(E_DEC_FATAL_ERROR, val, false, decimal_value);
return decimal_value;
}
bool Item_exists_subselect::val_bool() {
assert(fixed);
if (exec(current_thd)) {
reset();
return false;
}
// EXISTS can never return NULL value
assert(!null_value);
return translate(null_value, value);
}
double Item_in_subselect::val_real() {
// Substituted with Item_in_optimizer, so this function is never used
assert(false);
my_error(ER_INTERNAL_ERROR, MYF(0), "Invalid function call");
return error_real();
}
longlong Item_in_subselect::val_int() {
// Substituted with Item_in_optimizer, so this function is never used
assert(false);
my_error(ER_INTERNAL_ERROR, MYF(0), "Invalid function call");
return error_int();
}
String *Item_in_subselect::val_str(String *) {
// Substituted with Item_in_optimizer, so this function is never used
assert(false);
my_error(ER_INTERNAL_ERROR, MYF(0), "Invalid function call");
return error_str();
}
bool Item_in_subselect::val_bool() {
// Substituted with Item_in_optimizer, so this function is never used
assert(false);
my_error(ER_INTERNAL_ERROR, MYF(0), "Invalid function call");
return error_int();
}
bool Item_in_subselect::val_bool_naked() {
assert(fixed);
if (exec(current_thd)) {
reset();
return false;
}
if (was_null && !value) null_value = true;
/*
This is the value of the naked IN. Negation, or applying of IS TRUE/FALSE,
is left to the parent Item_in_optimizer, so make sure it's there:
*/
assert(optimizer);
return value;
}
my_decimal *Item_in_subselect::val_decimal(my_decimal *) {
// Substituted with Item_in_optimizer, so this function is never used
assert(false);
my_error(ER_INTERNAL_ERROR, MYF(0), "Invalid function call");
return nullptr;
}
/**
Rewrite a single-column IN/ALL/ANY subselect
DESCRIPTION
Rewrite a single-column subquery using rule-based approach. The subquery
oe $cmp$ (SELECT ie FROM ... WHERE subq_where ... HAVING subq_having)
First, try to convert the subquery to scalar-result subquery in one of
the forms:
- oe $cmp$ (SELECT MAX(...) ) // handled by Item_singlerow_subselect
- oe $cmp$ \<max\>(SELECT ...) // handled by Item_maxmin_subselect
If that fails, the subquery will be handled with class Item_in_optimizer.
There are two possibilities:
- If the subquery execution method is materialization, then the subquery is
not transformed any further.
- Otherwise the IN predicates is transformed into EXISTS by injecting
equi-join predicates and possibly other helper predicates. For details
see method single_value_in_like_transformer().
@param thd Thread handle
@param select Query block of the subquery
@param func Subquery comparison creator
@returns false if success, true if error
*/
bool Item_in_subselect::single_value_transformer(THD *thd, Query_block *select,
Comp_creator *func) {
bool subquery_maybe_null = false;
DBUG_TRACE;
/*
Check that the right part of the subselect contains no more than one
column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
*/
// psergey: duplicated_subselect_card_check
if (select->num_visible_fields() > 1) {
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
return true;
}
/*
Check the nullability of the subquery. The subquery should return
only one column, so we check the nullability of the first item in
Query_block::fields_list. In case the subquery is a union, check the
nullability of the first item of each query block belonging to the
union.
*/
for (Query_block *sel = unit->first_query_block(); sel != nullptr;
sel = sel->next_query_block()) {
Item *only_item = sel->single_visible_field();
if (only_item == nullptr) {
// There was more than one after all.
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
return true;
}
if ((subquery_maybe_null = only_item->is_nullable())) break;
}
/*
If this is an ALL/ANY single-value subquery predicate, try to rewrite
it with a MIN/MAX subquery.
E.g. SELECT * FROM t1 WHERE b > ANY (SELECT a FROM t2) can be rewritten
with SELECT * FROM t1 WHERE b > (SELECT MIN(a) FROM t2).
A predicate may be transformed to use a MIN/MAX subquery if it:
1. has a greater than/less than comparison operator, and
2. is not correlated with the outer query, and
3. UNKNOWN results are treated as FALSE, by this item or the outer item,
or can never be generated.
*/
if (!func->eqne_op() && // 1
!unit->uncacheable && // 2
(abort_on_null || (upper_item && upper_item->ignore_unknown()) || // 3
(!left_expr->is_nullable() && !subquery_maybe_null))) {
if (substitution != nullptr) {
// It is second (third, ...) query block of set operation => All is done
return false;
}
Item *subquery;
if (!select->is_explicitly_grouped() && select->having_cond() == nullptr &&
// MIN/MAX(agg_or_window_func) would not be valid
!select->with_sum_func && !select->has_windows() &&
!(select->next_query_block()) && select->has_tables() &&
// For ALL: MIN ignores NULL: 3<=ALL(4 and NULL) is UNKNOWN, while
// NOT(3>(SELECT MIN(4 and NULL)) is TRUE
!(substype() == ALL_SUBS && subquery_maybe_null)) {
OPT_TRACE_TRANSFORM(&thd->opt_trace, oto0, oto1, select->select_number,
"> ALL/ANY (SELECT)", "SELECT(MIN)");
oto1.add("chosen", true);
thd->lex->m_subquery_to_derived_is_impossible = true;
Item_sum_hybrid *item;
nesting_map save_allow_sum_func;
if (func->l_op()) {
/*
(ALL && (> || =>)) || (ANY && (< || =<))
for ALL condition is inverted
*/
item = new Item_sum_max(select->base_ref_items[0]);
} else {
/*
(ALL && (< || =<)) || (ANY && (> || =>))
for ALL condition is inverted
*/
item = new Item_sum_min(select->base_ref_items[0]);
}
if (item == nullptr) return true;
if (upper_item) upper_item->set_sum_test(item);
select->base_ref_items[0] = item;
// Find the correct position in the field list, and overwrite it with the
// item.
for (auto it = select->visible_fields().begin();
it != select->visible_fields().end(); ++it) {
*it = item;
break;
}
DBUG_EXECUTE("where", print_where(thd, item, "rewrite with MIN/MAX",
QT_ORDINARY););
save_allow_sum_func = thd->lex->allow_sum_func;
thd->lex->allow_sum_func |= (nesting_map)1 << select->nest_level;
/*
Item_sum_(max|min) can't substitute other item => we can use 0 as
reference, also Item_sum_(max|min) can't be fixed after creation, so
we do not check item->fixed
*/
if (item->fix_fields(thd, nullptr)) return true;
thd->lex->allow_sum_func = save_allow_sum_func;
subquery = new Item_singlerow_subselect(select);
if (subquery == nullptr) return true;
} else {
OPT_TRACE_TRANSFORM(&thd->opt_trace, oto0, oto1, select->select_number,
"> ALL/ANY (SELECT)", "MIN (SELECT)");
oto1.add("chosen", true);
Item_maxmin_subselect *item = new Item_maxmin_subselect(
this, select, func->l_op(), substype() == ANY_SUBS);
if (item == nullptr) return true;
subquery = item;
if (upper_item) upper_item->set_sub_test(item);
}
if (upper_item) upper_item->set_subselect(this);
substitution = func->create(left_expr, subquery);
if (substitution == nullptr) return true;
return false;
}
if (substitution == nullptr) {
// Invoked for the first or only query block in the subquery's query expr
substitution = optimizer;
thd->lex->set_current_query_block(select->outer_query_block());
// optimizer never use Item **ref => we can pass 0 as parameter
assert(optimizer != nullptr);
if (optimizer->fix_left(thd, nullptr)) return true;
thd->lex->set_current_query_block(select);
/*
As far as Item_ref_in_optimizer do not substitute itself on fix_fields
we can use same item for all query blocks.
*/
Item_ref *const left = new Item_ref(
&select->context, (Item **)optimizer->get_cache(), in_left_expr_name);
if (left == nullptr) return true;
if (mark_as_outer(left_expr, 0))
left->depended_from = select->outer_query_block();
m_injected_left_expr = left;
assert(in2exists_info == nullptr);
in2exists_info = new (thd->mem_root) In2exists_info;
if (in2exists_info == nullptr) return true;
in2exists_info->dependent_before =
unit->uncacheable & UNCACHEABLE_DEPENDENT;
if (!left_expr->const_item()) unit->uncacheable |= UNCACHEABLE_DEPENDENT;
in2exists_info->dependent_after = unit->uncacheable & UNCACHEABLE_DEPENDENT;
}
if (!abort_on_null && left_expr->is_nullable() &&
pushed_cond_guards == nullptr) {
pushed_cond_guards = (bool *)thd->alloc(sizeof(bool));
if (pushed_cond_guards == nullptr) return true;
pushed_cond_guards[0] = true;
}
/* Perform the IN=>EXISTS transformation. */
return single_value_in_to_exists_transformer(thd, select, func);
}
/**
Transform an IN predicate into EXISTS via predicate injection.
@details The transformation injects additional predicates into the subquery
(and makes the subquery correlated) as follows.
- If the subquery has aggregates, GROUP BY, or HAVING, convert to
SELECT ie FROM ... HAVING subq_having AND
trigcond(oe $cmp$ ref_or_null_helper<ie>)
the addition is wrapped into trigger only when we want to distinguish
between NULL and FALSE results.
- Otherwise (no aggregates/GROUP BY/HAVING) convert it to one of the
following:
= If we don't need to distinguish between NULL and FALSE subquery:
SELECT 1 FROM ... WHERE (oe $cmp$ ie) AND subq_where
= If we need to distinguish between those:
SELECT 1 FROM ...
WHERE subq_where AND trigcond((oe $cmp$ ie) OR (ie IS NULL))
HAVING trigcond(@<is_not_null_test@>(ie))
At JOIN::optimize() we will compare costs of materialization and EXISTS; if
the former is cheaper we will switch to it.
@param thd Thread handle
@param select Query block of the subquery
@param func Subquery comparison creator
@returns false if success, true if error
Success means that appropriate predicates were injected into
the query block of the subquery predicate, or the subquery
predicate was reduced to a simple predicate.
*/
bool Item_in_subselect::single_value_in_to_exists_transformer(
THD *thd, Query_block *select, Comp_creator *func) {
DBUG_TRACE;
Query_block *outer = select->outer_query_block();
OPT_TRACE_TRANSFORM(&thd->opt_trace, oto0, oto1, select->select_number,
"IN (SELECT)", "EXISTS (CORRELATED SELECT)");
oto1.add("chosen", true);
// Transformation will make the subquery a dependent one.
if (!left_expr->const_item()) select->uncacheable |= UNCACHEABLE_DEPENDENT;
in2exists_info->added_to_where = false;
if (select->having_cond() != nullptr || select->is_explicitly_grouped() ||
select->with_sum_func || select->has_windows()) {
Item_ref_null_helper *ref_null = new Item_ref_null_helper(
&select->context, this, &select->base_ref_items[0]);
Item_bool_func *item = func->create(m_injected_left_expr, ref_null);
if (item == nullptr) return true;
item->set_created_by_in2exists();
/*
Assume that the expression in the SELECT list, is a function of a group
aggregate which is aggregated in an outer query, for example
SELECT ... FROM t1 WHERE t1.b IN (SELECT <expr of SUM(t1.a)> FROM t2). We
are changing it to
SELECT ... FROM t1 WHERE t1.b IN (SELECT <expr of SUM(t1.a)> FROM t2
HAVING t1.b=ref-to-<expr of SUM(t1.a)>).
SUM is an "inner sum func", its fix_fields() has added it to
inner_sum_func_list of the outer query; the outer query will do
split_sum_func on it which will add SUM as a hidden item and replace it
in 'expr' with a pointer to an Item_ref.
If 'expr' is a function which has SUM as one of its arguments, the
SELECT list and HAVING access 'expr' through two different pointers, but
there's only one 'expr' Item, which accesses SUM through one pointer, so
there's a single referenced_by pointer to remember, we use
referenced_by[0]. But if 'expr' is directly the SUM, with no Item in
between, then there are two places where 'expr' should be replaced: the
iterator in the SELECT list, and the 'ref-to-expr' in HAVING above. So we
have to document those 2 places in referenced_by[0] and referenced_by[1].
*/
Item *selected = select->base_ref_items[0];
if (selected->type() == SUM_FUNC_ITEM) {
Item_sum *selected_sum = down_cast<Item_sum *>(selected);
if (!selected_sum->referenced_by[0])
selected_sum->referenced_by[0] = ref_null->ref_pointer();
else {
// Slot 0 already occupied, use 1.
assert(!selected_sum->referenced_by[1]);
selected_sum->referenced_by[1] = ref_null->ref_pointer();
}
}
if (!abort_on_null && left_expr->is_nullable()) {
/*
We can encounter "NULL IN (SELECT ...)". Wrap the added condition
within a trig_cond.
*/
item =
new Item_func_trig_cond(item, get_cond_guard(0), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (item == nullptr) return true;
item->set_created_by_in2exists();
}
/*
AND and comparison functions can't be changed during fix_fields()
we can assign query_block->having_cond here, and pass NULL as last
argument (reference) to fix_fields()
*/
select->set_having_cond(and_items(select->having_cond(), item));
select->having_cond()->apply_is_true();
select->having_fix_field = true;
/*
we do not check having_cond()->fixed, because Item_and (from and_items)
or comparison function (from func->create) can't be fixed after creation
*/
Opt_trace_array having_trace(&thd->opt_trace,
"evaluating_constant_having_conditions");
if (select->having_cond()->fix_fields(thd, nullptr)) return true;
select->having_fix_field = false;
} else {
Item *orig_item = select->single_visible_field();
if (!select->source_table_is_one_row() || select->where_cond() != nullptr) {
Item_bool_func *item = func->create(m_injected_left_expr, orig_item);
if (item == nullptr) return true;
/*
We may soon add a 'OR inner IS NULL' to 'item', but that may later be
removed if 'inner' is not nullable, so the in2exists mark must be on
'item' too. Not only on the OR node.
*/
item->set_created_by_in2exists();
if (!abort_on_null && orig_item->is_nullable()) {
Item_bool_func *having = new Item_is_not_null_test(this, orig_item);
if (having == nullptr) return true;
having->set_created_by_in2exists();
if (left_expr->is_nullable()) {
having = new Item_func_trig_cond(
having, get_cond_guard(0), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (having == nullptr) return true;
having->set_created_by_in2exists();
}
/*
Item_is_not_null_test can't be changed during fix_fields()
we can assign query_block->having_cond() here, and pass NULL as last
argument (reference) to fix_fields()
*/
select->set_having_cond(having);
select->having_fix_field = true;
/*
No need to check query_block->having_cond()->fixed, because Item_and
(from and_items) or comparison function (from func->create)
can't be fixed after creation.
*/
Opt_trace_array having_trace(&thd->opt_trace,
"evaluating_constant_having_conditions");
if (select->having_cond()->fix_fields(thd, nullptr)) return true;
select->having_fix_field = false;
item = new Item_cond_or(item, new Item_func_isnull(orig_item));
if (item == nullptr) return true;
item->set_created_by_in2exists();
}
/*
If we may encounter NULL IN (SELECT ...) and care whether subquery
result is NULL or FALSE, wrap condition in a trig_cond.
*/
if (!abort_on_null && left_expr->is_nullable()) {
item = new Item_func_trig_cond(
item, get_cond_guard(0), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (item == nullptr) return true;
item->set_created_by_in2exists();
}
/*
AND can't be changed during fix_fields()
we can assign query_block->having_cond() here, and pass NULL as last
argument (reference) to fix_fields()
Note that if query_block is the fake one of UNION, it does not make
much sense to give it a WHERE clause below... we already give one to
each member of the UNION.
*/
select->set_where_cond(and_items(select->where_cond(), item));
select->where_cond()->apply_is_true();
in2exists_info->added_to_where = true;
/*
No need to check query_block->where_cond()->fixed, because Item_and
can't be fixed after creation.
*/
Opt_trace_array where_trace(&thd->opt_trace,
"evaluating_constant_where_conditions");
if (select->where_cond()->fix_fields(thd, nullptr)) return true;
} else {
if (unit->is_set_operation()) {
/*
comparison functions can't be changed during fix_fields()
we can assign query_block->having_cond() here, and pass NULL as last
argument (reference) to fix_fields()
*/
Item_bool_func *new_having =
func->create(m_injected_left_expr,
new Item_ref_null_helper(&select->context, this,
&select->base_ref_items[0]));
if (new_having == nullptr) return true;
new_having->set_created_by_in2exists();
if (!abort_on_null && left_expr->is_nullable()) {
new_having = new Item_func_trig_cond(
new_having, get_cond_guard(0), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (new_having == nullptr) return true;
new_having->set_created_by_in2exists();
}
select->set_having_cond(new_having);
select->having_fix_field = true;
/*
No need to check query_block->having_cond()->fixed, because comparison
function (from func->create) can't be fixed after creation.
*/
Opt_trace_array having_trace(&thd->opt_trace,
"evaluating_constant_having_conditions");
if (select->having_cond()->fix_fields(thd, nullptr)) return true;
select->having_fix_field = false;
} else {
/*
Single query block, without tables, without WHERE, HAVING, LIMIT:
its content has one row and is equal to the item in the SELECT list,
so we can replace the IN(subquery) with an equality.
Keep applicability conditions in sync with
Item_exists_subselect::truth_transformer().
The expression is moved to the immediately outer query block, so it
may no longer contain outer references.
*/
outer->merge_contexts(select);
orig_item->fix_after_pullout(outer, select);
// Resolving of substitution item will be done in time of substituting
substitution = func->create(left_expr, orig_item);
if (substitution == nullptr) return true;
have_to_be_excluded = true;
if (thd->lex->is_explain()) {
char warn_buff[MYSQL_ERRMSG_SIZE];
sprintf(warn_buff, ER_THD(thd, ER_SELECT_REDUCED),
select->select_number);
push_warning(thd, Sql_condition::SL_NOTE, ER_SELECT_REDUCED,
warn_buff);
}
return false;
}
}
}
thd->lex->m_subquery_to_derived_is_impossible = true;
return false;
}
bool Item_in_subselect::row_value_transformer(THD *thd, Query_block *select) {
uint cols_num = left_expr->cols();
DBUG_TRACE;
// psergey: duplicated_subselect_card_check
if (select->num_visible_fields() != left_expr->cols()) {
my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols());
return true;
}
/*
Wrap the current IN predicate in an Item_in_optimizer. The actual
substitution in the Item tree takes place in Item_subselect::fix_fields.
*/
if (substitution == nullptr) {
// first call for this query expression
substitution = optimizer;
thd->lex->set_current_query_block(select->outer_query_block());
// optimizer never use Item **ref => we can pass 0 as parameter
assert(optimizer != nullptr);
if (optimizer->fix_left(thd, nullptr)) return true;
thd->lex->set_current_query_block(select);
assert(in2exists_info == nullptr);
in2exists_info = new (thd->mem_root) In2exists_info;
if (in2exists_info == nullptr) return true;
in2exists_info->dependent_before =
unit->uncacheable & UNCACHEABLE_DEPENDENT;
if (!left_expr->const_item()) unit->uncacheable |= UNCACHEABLE_DEPENDENT;
in2exists_info->dependent_after = unit->uncacheable & UNCACHEABLE_DEPENDENT;
if (!abort_on_null && left_expr->is_nullable() && !pushed_cond_guards) {
pushed_cond_guards = (bool *)thd->alloc(sizeof(bool) * left_expr->cols());
if (pushed_cond_guards == nullptr) return true;
for (uint i = 0; i < cols_num; i++) {
pushed_cond_guards[i] = true;
}
}
}
// Perform the IN=>EXISTS transformation.
return row_value_in_to_exists_transformer(thd, select);
}
/**
Transform a (possibly non-correlated) IN subquery into a correlated EXISTS.
@todo
The IF-ELSE below can be refactored so that there is no duplication of the
statements that create the new conditions. For this we have to invert the IF
and the FOR statements as this:
for (each left operand)
create the equi-join condition
if (is_having_used || !abort_on_null)
create the "is null" and is_not_null_test items
if (is_having_used)
add the equi-join and the null tests to HAVING
else
add the equi-join and the "is null" to WHERE
add the is_not_null_test to HAVING
*/
bool Item_in_subselect::row_value_in_to_exists_transformer(
THD *thd, Query_block *select) {
thd->lex->m_subquery_to_derived_is_impossible = true;
Item_bool_func *having_item = nullptr;
uint cols_num = left_expr->cols();
bool is_having_used =
select->having_cond() != nullptr || select->is_explicitly_grouped() ||
select->with_sum_func || !select->has_tables() || select->has_windows();
DBUG_TRACE;
OPT_TRACE_TRANSFORM(&thd->opt_trace, oto0, oto1, select->select_number,
"IN (SELECT)", "EXISTS (CORRELATED SELECT)");
oto1.add("chosen", true);
// Transformation will make the subquery a dependent one.
if (!left_expr->const_item()) select->uncacheable |= UNCACHEABLE_DEPENDENT;
in2exists_info->added_to_where = false;
if (is_having_used) {
/*
(l1, l2, l3) IN (SELECT v1, v2, v3 ... HAVING having) =>
EXISTS (SELECT ... HAVING having and
(l1 = v1 or is null v1) and
(l2 = v2 or is null v2) and
(l3 = v3 or is null v3) and
is_not_null_test(v1) and
is_not_null_test(v2) and
is_not_null_test(v3))
where is_not_null_test used to register nulls in case if we have
not found matching to return correct NULL value
TODO: say here explicitly if the order of AND parts matters or not.
*/
Item_bool_func *item_having_part2 = nullptr;
for (uint i = 0; i < cols_num; i++) {
Item *item_i = select->base_ref_items[i];
Item **pitem_i = &select->base_ref_items[i];
assert((left_expr->fixed && item_i->fixed) ||
(item_i->type() == REF_ITEM &&
((Item_ref *)(item_i))->ref_type() == Item_ref::OUTER_REF));
if (item_i->check_cols(left_expr->element_index(i)->cols())) return true;
Item_ref *const left =
new Item_ref(&select->context, (*optimizer->get_cache())->addr(i),
in_left_expr_name);
if (left == nullptr) return true; /* purecov: inspected */
if (mark_as_outer(left_expr, i))
left->depended_from = select->outer_query_block();
Item_bool_func *item_eq = new Item_func_eq(
left, new Item_ref(&select->context, pitem_i, "<list ref>"));
if (item_eq == nullptr) return true;
item_eq->set_created_by_in2exists();
Item_bool_func *item_isnull = new Item_func_isnull(
new Item_ref(&select->context, pitem_i, "<list ref>"));
if (item_isnull == nullptr) return true;
item_isnull->set_created_by_in2exists();
Item_bool_func *col_item = new Item_cond_or(item_eq, item_isnull);
if (col_item == nullptr) return true;
col_item->set_created_by_in2exists();
if (!abort_on_null && left_expr->element_index(i)->is_nullable()) {
col_item = new Item_func_trig_cond(
col_item, get_cond_guard(i), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (col_item == nullptr) return true;
col_item->set_created_by_in2exists();
}
having_item = and_items(having_item, col_item);
if (having_item == nullptr) return true;
having_item->set_created_by_in2exists();
Item_bool_func *item_nnull_test = new Item_is_not_null_test(
this, new Item_ref(&select->context, pitem_i, "<list ref>"));
if (item_nnull_test == nullptr) return true;
item_nnull_test->set_created_by_in2exists();
if (!abort_on_null && left_expr->element_index(i)->is_nullable()) {
item_nnull_test = new Item_func_trig_cond(
item_nnull_test, get_cond_guard(i), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (item_nnull_test == nullptr) return true;
item_nnull_test->set_created_by_in2exists();
}
item_having_part2 = and_items(item_having_part2, item_nnull_test);
if (item_having_part2 == nullptr) return true;
item_having_part2->set_created_by_in2exists();
}
having_item = and_items(having_item, item_having_part2);
if (having_item == nullptr) return true;
having_item->set_created_by_in2exists();
having_item->apply_is_true();
} else {
/*
(l1, l2, l3) IN (SELECT v1, v2, v3 ... WHERE where) =>
EXISTS (SELECT ... WHERE where and
(l1 = v1 or is null v1) and
(l2 = v2 or is null v2) and
(l3 = v3 or is null v3)
HAVING is_not_null_test(v1) and
is_not_null_test(v2) and
is_not_null_test(v3))
where is_not_null_test register NULLs values but reject rows
in case when we do not need correct NULL, we have a simpler construction:
EXISTS (SELECT ... WHERE where and
(l1 = v1) and
(l2 = v2) and
(l3 = v3)
*/
Item_bool_func *where_item = nullptr;
for (uint i = 0; i < cols_num; i++) {
Item *item_i = select->base_ref_items[i];
Item **pitem_i = &select->base_ref_items[i];
assert((left_expr->fixed && item_i->fixed) ||
(item_i->type() == REF_ITEM &&
((Item_ref *)(item_i))->ref_type() == Item_ref::OUTER_REF));
if (item_i->check_cols(left_expr->element_index(i)->cols())) return true;
Item_ref *const left =
new Item_ref(&select->context, (*optimizer->get_cache())->addr(i),
in_left_expr_name);
if (left == nullptr) return true;
if (mark_as_outer(left_expr, i))
left->depended_from = select->outer_query_block();
Item_bool_func *item = new Item_func_eq(
left, new Item_ref(&select->context, pitem_i, "<list ref>"));
if (item == nullptr) return true;
item->set_created_by_in2exists();
if (!abort_on_null) {
Item_bool_func *having_col_item = new Item_is_not_null_test(
this, new Item_ref(&select->context, pitem_i, "<list ref>"));
if (having_col_item == nullptr) return true;
having_col_item->set_created_by_in2exists();
Item_bool_func *item_isnull = new Item_func_isnull(
new Item_ref(&select->context, pitem_i, "<list ref>"));
if (item_isnull == nullptr) return true;
item_isnull->set_created_by_in2exists();
item = new Item_cond_or(item, item_isnull);
if (item == nullptr) return true;
item->set_created_by_in2exists();
/*
TODO: why we create the above for cases where the right part
can't be NULL?
*/
if (left_expr->element_index(i)->is_nullable()) {
item = new Item_func_trig_cond(
item, get_cond_guard(i), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (item == nullptr) return true;
item->set_created_by_in2exists();
having_col_item = new Item_func_trig_cond(
having_col_item, get_cond_guard(i), nullptr, NO_PLAN_IDX,
Item_func_trig_cond::OUTER_FIELD_IS_NOT_NULL);
if (having_col_item == nullptr) return true;
having_col_item->set_created_by_in2exists();
}
having_item = and_items(having_item, having_col_item);
if (having_item == nullptr) return true;
having_item->set_created_by_in2exists();
}
where_item = and_items(where_item, item);
if (where_item == nullptr) return true;
where_item->set_created_by_in2exists();
}
/*
AND can't be changed during fix_fields()
we can assign select->where_cond() here, and pass NULL as last
argument (reference) to fix_fields()
*/
select->set_where_cond(and_items(select->where_cond(), where_item));
select->where_cond()->apply_is_true();
in2exists_info->added_to_where = true;
Opt_trace_array where_trace(&thd->opt_trace,
"evaluating_constant_where_conditions");
if (select->where_cond()->fix_fields(thd, nullptr)) return true;
}
if (having_item != nullptr) {
select->set_having_cond(and_items(select->having_cond(), having_item));
select->having_cond()->apply_is_true();
/*
AND can't be changed during fix_fields()
we can assign select->having_cond() here, and pass 0 as last
argument (reference) to fix_fields()
*/
select->having_fix_field = true;
Opt_trace_array having_trace(&thd->opt_trace,
"evaluating_constant_having_conditions");
if (select->having_cond()->fix_fields(thd, nullptr)) return true;
select->having_fix_field = false;
}
return false;
}
bool Item_in_subselect::select_transformer(THD *thd, Query_block *select) {
return select_in_like_transformer(thd, select, &eq_creator);
}
/**
Prepare IN/ALL/ANY/SOME subquery transformation and call appropriate
transformation function.
To decide which transformation procedure (scalar or row) applicable here
we have to call fix_fields() for left expression to be able to call
cols() method on it. Also this method make arena management for
underlying transformation methods.
@param thd Thread handle
@param select Query block of subquery being transformed
@param func creator of condition function of subquery
@returns false if success, true if error
*/
bool Item_in_subselect::select_in_like_transformer(THD *thd,
Query_block *select,
Comp_creator *func) {
const char *save_where = thd->where;
DBUG_TRACE;
#ifndef NDEBUG
/*
IN/SOME/ALL/ANY subqueries don't support LIMIT clause. Without
it, ORDER BY becomes meaningless and should already have been
removed in resolve_subquery()
*/
for (Query_block *sl = unit->first_query_block(); sl;
sl = sl->next_query_block())
assert(!sl->order_list.first);
#endif
if (changed) return false;
thd->where = "IN/ALL/ANY subquery";
thd->lex->set_current_query_block(select->outer_query_block());
assert(left_expr->fixed);
/*
In some optimisation cases we will not need this Item_in_optimizer
object, but we can't know it here, but here we need address correct
reference on left expression.
*/
if (optimizer == nullptr) {
optimizer = new Item_in_optimizer(this);
if (optimizer == nullptr) return true;
}
thd->lex->set_current_query_block(select);
/*
If we didn't choose an execution method up to this point, we choose
the IN=>EXISTS transformation, at least temporarily.
*/
if (strategy == Subquery_strategy::UNSPECIFIED)
strategy = Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT;
/*
Both transformers call fix_fields() only for Items created inside them,
and all those items do not make permanent changes in the current item arena
which allows us to call them with changed arena (if we do not know the
nature of Item, we have to call fix_fields() for it only with the original
arena to avoid memory leak).
*/
if (left_expr->cols() == 1) {
if (single_value_transformer(thd, select, func)) return true;
} else {
// Row operation is only supported for =ANY (IN) and <>ALL (NOT IN):
if (func != &eq_creator) {
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
return true;
}
if (row_value_transformer(thd, select)) return true;
}
thd->where = save_where;
return false;
}
void Item_in_subselect::print(const THD *thd, String *str,
enum_query_type query_type) const {
const char *tail = Item_bool_func::bool_transform_names[value_transform];
if (implicit_is_op) tail = "";
bool paren = false;
if (strategy == Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT ||
strategy == Subquery_strategy::SUBQ_EXISTS) {
if (value_transform == BOOL_NEGATED) { // NOT has low associativity, but
// we're inside Item_in_optimizer,
// so () are needed only if IS TRUE/FALSE is coming.
if (tail[0]) {
paren = true;
str->append(STRING_WITH_LEN("("));
}
str->append(STRING_WITH_LEN("not "));
}
str->append(STRING_WITH_LEN("<exists>"));
} else {
left_expr->print(thd, str, query_type);
if (value_transform == BOOL_NEGATED) str->append(STRING_WITH_LEN(" not"));
str->append(STRING_WITH_LEN(" in "));
}
Item_subselect::print(thd, str, query_type);
if (paren) str->append(STRING_WITH_LEN(")"));
if (tail[0]) {
str->append(STRING_WITH_LEN(" "));
str->append(tail, strlen(tail));
}
}
/**
An object of class Item_in_subselect is always substituted with another
object of class Item_in_optimizer, and the substitution object contains
a pointer to the original Item_in_subselect.
This substitution is currently handled by calling fix_fields() twice:
The first call will resolve the underlying query expression and create
the Item_in_optimizer substitution object.
The second call is performed from Item_in_optimizer::fix_fields() and
will complete the resolving of the object.
Notice also that this process is partly managed by
Item_subselect::fix_fields().
*/
bool Item_in_subselect::fix_fields(THD *thd, Item **ref) {
assert(!fixed);
abort_on_null =
value_transform == BOOL_IS_TRUE || value_transform == BOOL_NOT_TRUE;
thd->where = "IN/ALL/ANY subquery";
assert(left_expr != nullptr);
if (!left_expr->fixed) {
if (left_expr->fix_fields(thd, &left_expr)) {
return true;
}
used_tables_cache = left_expr->used_tables();
}
if (Item_subselect::fix_fields(thd, ref)) return true;
return false;
}
void Item_in_subselect::fix_after_pullout(Query_block *parent_query_block,
Query_block *removed_query_block) {
Item_subselect::fix_after_pullout(parent_query_block, removed_query_block);
left_expr->fix_after_pullout(parent_query_block, removed_query_block);
used_tables_cache |= left_expr->used_tables();
}
void Item_in_subselect::update_used_tables() {
Item_subselect::update_used_tables();
left_expr->update_used_tables();
used_tables_cache |= left_expr->used_tables();
add_accum_properties(left_expr);
}
/**
Initialize the cache of the left operand of the IN predicate.
@note This method has the same purpose as alloc_group_fields(),
but it takes a different kind of collection of items, and the
list we push to is dynamically allocated.
@retval true if a memory allocation error occurred
@retval false if success
*/
bool Item_in_subselect::init_left_expr_cache(THD *thd) {
/*
Check if the left operand is a subquery that yields an empty set of rows.
If so, skip initializing a cache; for an empty set the subquery
exec won't read any rows and so lead to uninitialized reads if attempted.
*/
if (left_expr->type() == SUBSELECT_ITEM && left_expr->null_value) {
return false;
}
JOIN *outer_join = unit->outer_query_block()->join;
/*
An IN predicate might be evaluated in a query for which all tables have
been optimized away.
*/
if (!(outer_join && outer_join->qep_tab)) {
need_expr_cache = false;
return false;
}
if (!(left_expr_cache = new (thd->mem_root) List<Cached_item>)) return true;
for (uint i = 0; i < left_expr->cols(); i++) {
Cached_item *cur_item_cache =
new_Cached_item(thd, left_expr->element_index(i));
if (!cur_item_cache || left_expr_cache->push_front(cur_item_cache))
return true;
}
return false;
}
std::optional<ContainedSubquery> Item_in_subselect::get_contained_subquery(
const Query_block *outer_query_block) {
// TODO(sgunders): Respect subquery hints, which can force the
// strategy to be materialize.
Query_block *query_block = unit->first_query_block();
AccessPath *path = unit->root_access_path();
if (path == nullptr) {
// In rare situations involving IN subqueries on the left side of
// other IN subqueries, the query block may not be part of the
// parent query block's list of inner query blocks. If so, it has
// not been optimized here. Since this is a rare case, we'll just
// skip it and assign it zero cost.
return std::nullopt;
}
const bool materializable =
subquery_allows_materialization(current_thd, query_block,
outer_query_block) &&
query_block->subquery_strategy(current_thd) ==
Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT;
int row_width = 0;
for (const Item *qb_item : query_block->fields) {
row_width += std::min<size_t>(qb_item->max_length, kMaxItemLengthEstimate);
}
return ContainedSubquery(
{path,
materializable ? ContainedSubquery::Strategy::kMaterializable
: ContainedSubquery::Strategy::kNonMaterializable,
row_width});
}
bool IsItemInSubSelect(Item *item) {
if (item->type() != Item::SUBSELECT_ITEM) {
return false;
}
switch (down_cast<Item_subselect *>(item)->substype()) {
case Item_subselect::IN_SUBS:
case Item_subselect::ALL_SUBS:
case Item_subselect::ANY_SUBS:
return true;
default:
return false;
}
}
/**
Tells an Item that it is in the condition of a JOIN_TAB of a query block.
@param arg A std::pair: first argument is the query block, second is the
index of JOIN_TAB in JOIN's array.
The Item records this fact and can deduce from it the estimated number of
times that it will be evaluated.
If the JOIN_TAB doesn't belong to the query block owning this
Item_subselect, it must belong to a more inner query block (not a more
outer, as the walk() doesn't dive into subqueries); in that case, it must be
that Item_subselect is the left-hand-side of a subquery transformed with
IN-to-EXISTS and has been wrapped in Item_cache and then injected into the
WHERE/HAVING of that subquery; but then the Item_subselect will not be
evaluated when the JOIN_TAB's condition is evaluated (Item_cache will
short-circuit it); it will be evaluated when the IN(subquery)
(Item_in_optimizer) is - that's when the Item_cache is updated. Thus, we
will ignore JOIN_TAB in this case.
*/
bool Item_subselect::inform_item_in_cond_of_tab(uchar *arg) {
std::pair<Query_block *, int> *pair_object =
pointer_cast<std::pair<Query_block *, int> *>(arg);
if (pair_object->first == unit->outer_query_block())
in_cond_of_tab = pair_object->second;
return false;
}
/**
Mark the subquery as optimized away, for EXPLAIN.
*/
bool Item_subselect::subq_opt_away_processor(uchar *) {
unit->set_explain_marker(current_thd, CTX_OPTIMIZED_AWAY_SUBQUERY);
// Return false to continue marking all subqueries in the expression.
return false;
}
/**
Clean up after removing the subquery from the item tree.
Call Query_expression::exclude_tree() to unlink it from its
master and to unlink direct Query_block children from
all_query_blocks_list.
Don't unlink subqueries that are not descendants of the starting
point (root) of the removal and cleanup.
*/
bool Item_subselect::clean_up_after_removal(uchar *arg) {
Cleanup_after_removal_context *const ctx =
pointer_cast<Cleanup_after_removal_context *>(arg);
// Check whether this item should be removed
if (ctx->is_stopped(this)) return false;
if (reference_count() > 1) {
(void)decrement_ref_count();
ctx->stop_at(this);
return false;
}
// Remove item on upward traversal, not downward:
if (marker == MARKER_NONE) {
marker = MARKER_TRAVERSAL;
return false;
}
assert(marker == MARKER_TRAVERSAL);
marker = MARKER_NONE;
// There may be loops in the AST so make sure a part is not removed twice:
if (unit->outer_query_block() == nullptr) return false;
// Notify flatten_subqueries() that subquery has been removed.
notify_removal();
// Remove the underlying query expression
unit->exclude_tree();
return false;
}
bool Item_subselect::collect_subqueries(uchar *arg) {
Collect_subq_info *info = pointer_cast<Collect_subq_info *>(arg);
if (unit->outer_query_block() == info->m_query_block)
info->list.push_back(this);
return false;
}
bool Item_allany_subselect::select_transformer(THD *thd, Query_block *select) {
DBUG_TRACE;
if (upper_item) upper_item->show = true;
return select_in_like_transformer(thd, select, func);
}
bool Item_subselect::is_evaluated() const { return unit->is_executed(); }
void Item_allany_subselect::print(const THD *thd, String *str,
enum_query_type query_type) const {
if (strategy == Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT ||
strategy == Subquery_strategy::SUBQ_EXISTS)
str->append(STRING_WITH_LEN("<exists>"));
else {
left_expr->print(thd, str, query_type);
str->append(' ');
str->append(func->symbol(all));
str->append(all ? " all " : " any ", 5);
}
Item_subselect::print(thd, str, query_type);
}
bool Item_singlerow_subselect::collect_scalar_subqueries(uchar *arg) {
auto *info = pointer_cast<Collect_scalar_subquery_info *>(arg);
Item *i = unit->first_query_block()->single_visible_field();
// Skip transformations for row subqueries:
if (i == nullptr) return false;
if (!info->m_collect_unconditionally) {
// Skip transformation if column contains a non-deterministic function [2]
// Also exclude scalar subqueries with references to outer query blocks [1]
// and Item_maxmin_subselect (ALL/ANY -> MAX/MIN transform artifact) [3]
// Merely correlation to the current query block are ok
if (info->is_stopped(this) || is_outer_reference() || // [1]
is_non_deterministic() || // [2]
is_maxmin()) { // [3]
return false;
}
}
/*
Check if it has been already added. Can happen after other
transformations, eg. IN -> EXISTS and when aggregates are repeated in
HAVING clause:
SELECT SUM(a), (SELECT SUM(b) FROM t3) AS scalar
FROM t1 HAVING SUM(a) > scalar
*/
for (auto &e : info->m_list) {
if (e.item == this) {
e.m_location |= info->m_location;
return false;
}
}
const table_map correlated_map = used_tables() & ~PSEUDO_TABLE_BITS;
info->m_list.emplace_back(Css_info{
info->m_location, this, correlated_map, info->m_join_condition_context,
/*
Compute if we can skip run-time cardinality check:
[1] implicitly grouped queries for now, OR
[1.1] was implicitly grouped in a transformation on a deeper level, AND
[2] no set operations are present (union)
*/
((i->has_aggregation() &&
unit->first_query_block()->is_implicitly_grouped()) || // [1]
(unit->first_query_block()->m_was_implicitly_grouped)) && // [1.1]
!unit->is_set_operation(), // [2]
false});
return false;
}
/**
Find the scalar subquery in Query_block::fields if directly present,
i.e., not inside an expression.
@param select The query block owning the transformation.
@param subquery The scalar subquery to look for.
@return the corresponding ref in Query_block::base_ref_items, or nullptr if
not found.
*/
static Item **find_subquery_in_select_list(Query_block *select,
Item_singlerow_subselect *subquery) {
int item_idx = 0;
for (Item *item : select->visible_fields()) {
// All comparisons are done after unwrapping rollup group item.
// base_ref_items might be without rollup wrappers while the fields
// might be.
item = unwrap_rollup_group(item);
if (item == subquery) {
assert(select->base_ref_items[item_idx] == item);
return &select->base_ref_items[item_idx];
}
++item_idx;
}
return nullptr;
}
Item *Item_singlerow_subselect::replace_scalar_subquery(uchar *arg) {
auto *const info = pointer_cast<Scalar_subquery_replacement *>(arg);
if (info->m_target != this) return this;
auto *const scalar_item =
new (current_thd->mem_root) Item_field(info->m_field);
if (scalar_item == nullptr) return nullptr;
Item **ref = find_subquery_in_select_list(info->m_outer_query_block, this);
if (ref == nullptr) {
// This scalar subquery is not used directly in the select list, so we need
// to add it as a hidden field. (If it _is_ used directly in the list,
// we cannot add it; not only due to efficiency, but it would also cause
// a conflict in the value of item->hidden.)
ref = info->m_outer_query_block->add_hidden_item(scalar_item);
}
Item *result;
if (unit->place() == CTX_HAVING) {
result = new (current_thd->mem_root)
Item_ref(&info->m_outer_query_block->context, ref, scalar_item->db_name,
scalar_item->table_name, scalar_item->field_name);
// nullptr is error, but no separate return needed here
} else {
result = scalar_item;
}
if (info->m_add_coalesce) {
Item_int *zero = new (current_thd->mem_root) Item_int_0();
if (zero == nullptr) return nullptr;
Item *coa = new (current_thd->mem_root) Item_func_coalesce(result, zero);
if (coa == nullptr) return nullptr;
if (coa->fix_fields(current_thd, &coa)) return nullptr;
result = coa;
}
return result;
}
std::optional<ContainedSubquery>
Item_singlerow_subselect::get_contained_subquery(
const Query_block *outer_query_block [[maybe_unused]]) {
if (unit->root_access_path() == nullptr) {
// In rare situations involving IN subqueries on the left side of
// other IN subqueries, the query block may not be part of the
// parent query block's list of inner query blocks. If so, it has
// not been optimized here. Since this is a rare case, we'll just
// skip it and assign it zero cost.
return std::nullopt;
}
const ContainedSubquery::Strategy strategy =
unit->first_query_block()->is_cacheable()
? ContainedSubquery::Strategy::kIndependentSingleRow
: ContainedSubquery::Strategy::kNonMaterializable;
int row_width = 0;
for (const Item *qb_item : unit->first_query_block()->fields) {
row_width += std::min<size_t>(qb_item->max_length, kMaxItemLengthEstimate);
}
return ContainedSubquery({unit->root_access_path(), strategy, row_width});
}
Item *Item_subselect::replace_item(Item_transformer t, uchar *arg) {
auto replace_and_update = [arg, t](Item *expr, Item **ref) {
Item *new_expr = expr->transform(t, arg);
if (new_expr == nullptr) return true;
if (new_expr != expr) current_thd->change_item_tree(ref, new_expr);
new_expr->update_used_tables();
return false;
};
auto *info = pointer_cast<Item::Item_replacement *>(arg);
auto *old_current = info->m_curr_block;
for (Query_block *slave = unit->first_query_block(); slave != nullptr;
slave = slave->next_query_block()) {
info->m_curr_block = slave;
for (auto it = slave->fields.begin(); it != slave->fields.end(); ++it) {
Item *expr = *it;
if (replace_and_update(expr, &expr)) return nullptr;
*it = expr;
}
if (slave->where_cond() != nullptr &&
replace_and_update(slave->where_cond(), slave->where_cond_ref()))
return nullptr;
for (ORDER *ord = slave->group_list.first; ord != nullptr;
ord = ord->next) {
if (replace_and_update(*ord->item, ord->item)) return nullptr;
}
if (slave->having_cond() != nullptr &&
replace_and_update(slave->having_cond(), slave->having_cond_ref()))
return nullptr;
for (ORDER *ord = slave->order_list.first; ord != nullptr;
ord = ord->next) {
if (replace_and_update(*ord->item, ord->item)) return nullptr;
}
List_iterator<Window> wit(slave->m_windows);
Window *w;
while ((w = wit++)) {
for (auto itr : {w->first_order_by(), w->first_partition_by()}) {
if (itr != nullptr) {
for (ORDER *ord = itr; ord != nullptr; ord = ord->next) {
if (replace_and_update(*ord->item, ord->item)) return nullptr;
}
}
}
}
}
info->m_curr_block = old_current;
return this;
}
/**
Transform processor. Dives into a subquery's expressions.
See Item[_field]::replace_item_field for more details.
*/
Item *Item_subselect::replace_item_field(uchar *arg) {
return replace_item(&Item::replace_item_field, arg);
}
/**
Transform processor. Dives into a subquery's expressions.
See Item[_field]::replace_item_view_ref for more details.
*/
Item *Item_subselect::replace_item_view_ref(uchar *arg) {
return replace_item(&Item::replace_item_view_ref, arg);
}
void SubqueryWithResult::cleanup() {
DBUG_TRACE;
result->cleanup();
}
SubqueryWithResult::SubqueryWithResult(Query_expression *u,
Query_result_interceptor *res,
Item_subselect *si)
: result(res),
item(si),
res_type(STRING_RESULT),
res_field_type(MYSQL_TYPE_VAR_STRING),
maybe_null(false),
unit(u) {
unit->item = si;
}
/**
Prepare the query expression underlying the subquery.
@details
This function is called from Item_subselect::fix_fields. If the subquery is
transformed with an Item_in_optimizer object, this function may be called
twice, hence we need the check on 'is_prepared()' at the start, to avoid
redoing the preparation.
@returns false if success, true if error
*/
bool SubqueryWithResult::prepare(THD *thd) {
if (!unit->is_prepared())
return unit->prepare(thd, result, nullptr, SELECT_NO_UNLOCK, 0);
assert(result == unit->query_result());
return false;
}
/**
Makes storage for the output values for a scalar or row subquery and
calculates their data and column types and their nullability.
@param item_list list of items in the select list of the subquery
@param row cache objects to hold the result row of the subquery
@param possibly_empty true if the subquery could return empty result
*/
void SubqueryWithResult::set_row(const mem_root_deque<Item *> &item_list,
Item_cache **row, bool possibly_empty) {
/*
Empty scalar or row subqueries evaluate to NULL, so if it is
possibly empty, it is also possibly NULL.
*/
maybe_null = possibly_empty;
res_type = STRING_RESULT;
res_field_type = MYSQL_TYPE_VARCHAR;
uint i = 0;
for (Item *sel_item : VisibleFields(item_list)) {
item->max_length = sel_item->max_length;
res_type = sel_item->result_type();
res_field_type = sel_item->data_type();
item->decimals = sel_item->decimals;
item->unsigned_flag = sel_item->unsigned_flag;
maybe_null |= sel_item->is_nullable();
if (!(row[i] = Item_cache::get_cache(sel_item))) return;
row[i]->setup(sel_item);
row[i]->store(sel_item);
row[i]->set_nullable(possibly_empty || sel_item->is_nullable());
++i;
}
if (CountVisibleFields(item_list) > 1)
res_type = ROW_RESULT;
else
item->set_data_type(res_field_type);
}
/**
Check if a query block is guaranteed to return one row. We know that
this is the case if it has no tables and is not filtered with WHERE,
HAVING or LIMIT clauses.
@param query_block the Query_block of the query block to check
@return true if we are certain that the query block always returns
one row, false otherwise
*/
static bool guaranteed_one_row(const Query_block *query_block) {
return !query_block->has_tables() && query_block->where_cond() == nullptr &&
query_block->having_cond() == nullptr && !query_block->has_limit();
}
static bool wrapped_in_intersect_except(Query_term *qb) {
for (qb = qb->parent(); qb != nullptr; qb = qb->parent()) {
if (qb->term_type() == QT_EXCEPT || qb->term_type() == QT_INTERSECT)
return true;
}
return false;
}
void SubqueryWithResult::fix_length_and_dec(Item_cache **row) {
assert(row || unit->first_query_block()->single_visible_field() != nullptr);
// A UNION is possibly empty only if all of its SELECTs are possibly empty.
// Other set operations may always be empty
bool possibly_empty = true;
for (Query_block *sl = unit->first_query_block(); sl;
sl = sl->next_query_block()) {
if (guaranteed_one_row(sl) && !wrapped_in_intersect_except(sl)) {
possibly_empty = false;
break;
}
}
set_row(unit->query_term()->query_block()->fields, row, possibly_empty);
if (unit->first_query_block()->single_visible_field() != nullptr)
item->collation.set(row[0]->collation);
}
bool SubqueryWithResult::exec(THD *thd) {
assert(unit->is_optimized());
char const *save_where = thd->where;
const bool res = unit->execute(thd);
thd->where = save_where;
return res;
}
/**
Run a query to see if it returns at least one row (stops after the first
has been found, or on error). Unless there was an error, whether the row
was found in "found".
@retval true on error
*/
bool ExecuteExistsQuery(THD *thd, Query_expression *unit, RowIterator *iterator,
bool *found) {
Query_block *saved_query_block = thd->lex->current_query_block();
auto restore_query_block = create_scope_guard([thd, saved_query_block]() {
thd->lex->set_current_query_block(saved_query_block);
});
Opt_trace_context *const trace = &thd->opt_trace;
Opt_trace_object trace_wrapper(trace);
Opt_trace_object trace_exec(trace, "join_execution");
if (unit->is_simple()) {
trace_exec.add_select_number(unit->first_query_block()->select_number);
}
Opt_trace_array trace_steps(trace, "steps");
if (unit->ClearForExecution()) {
return true;
}
unit->set_executed();
thd->get_stmt_da()->reset_current_row_for_condition();
if (iterator->Init()) {
return true;
}
// See if we can get at least one row.
int error = iterator->Read();
if (error == 1 || thd->is_error()) {
return true;
}
*found = (error == 0);
return false;
}
/*
Index-lookup subselect 'engine' - run the subquery
DESCRIPTION
The engine is used to resolve subqueries in form
oe IN (SELECT key FROM tbl WHERE subq_where)
by asking the iterator for the inner query for a single row, and then
immediately stopping. The iterator would usually do a simple ref lookup,
but could in theory be anything.
*/
bool subselect_indexsubquery_engine::exec(THD *thd) {
Query_expression *unit = item->unit;
bool found;
if (ExecuteExistsQuery(thd, unit, unit->root_iterator(), &found)) {
return true;
}
item->value = found;
item->assigned(true);
return false;
}
uint Item_subselect::unit_cols() const {
assert(unit->is_prepared()); // should be called after fix_fields()
return unit->num_visible_fields();
}
bool Item_subselect::is_uncacheable() const { return unit->uncacheable; }
void SubqueryWithResult::print(const THD *thd, String *str,
enum_query_type query_type) {
unit->print(thd, str, query_type);
}
void subselect_indexsubquery_engine::print(const THD *thd, String *str,
enum_query_type query_type) {
const bool unique = type == JT_EQ_REF;
const bool check_null = type == JT_REF_OR_NULL;
if (unique)
str->append(STRING_WITH_LEN("<primary_index_lookup>("));
else
str->append(STRING_WITH_LEN("<index_lookup>("));
ref.items[0]->print(thd, str, query_type);
str->append(STRING_WITH_LEN(" in "));
if (table_ref && table_ref->uses_materialization()) {
/*
For materialized derived tables/views use table/view alias instead of
temporary table name, as it changes on each run and not acceptable for
EXPLAIN EXTENDED.
*/
str->append(table->alias, strlen(table->alias));
} else if (table->s->table_category == TABLE_CATEGORY_TEMPORARY) {
// Could be from subselect_hash_sj_engine.
str->append(STRING_WITH_LEN("<temporary table>"));
} else
str->append(table->s->table_name.str, table->s->table_name.length);
KEY *key_info = table->key_info + ref.key;
str->append(STRING_WITH_LEN(" on "));
str->append(key_info->name);
if (check_null) str->append(STRING_WITH_LEN(" checking NULL"));
if (cond) {
str->append(STRING_WITH_LEN(" where "));
cond->print(thd, str, query_type);
}
if (having) {
str->append(STRING_WITH_LEN(" having "));
having->print(thd, str, query_type);
}
str->append(')');
}
/**
change query result object of subquery.
@param thd thread handle
@param si new subselect Item
@param res new Query_result object
@retval
false OK
@retval
true error
*/
bool SubqueryWithResult::change_query_result(THD *thd, Item_subselect *si,
Query_result_subquery *res) {
item = si;
int rc = unit->change_query_result(thd, res, result);
result = res;
return rc;
}
Query_block *SubqueryWithResult::single_query_block() const {
assert(unit->is_simple());
return unit->first_query_block();
}
/******************************************************************************
WL#1110 - Implementation of class subselect_hash_sj_engine
******************************************************************************/
/**
Create all structures needed for subquery execution using hash semijoin.
@details
- Create a temporary table to store the result of the IN subquery. The
temporary table has one hash index on all its columns. If single-column,
the index allows at most one NULL row.
- Create a new result sink that sends the result stream of the subquery to
the temporary table,
- Create and initialize Index_lookup objects to perform lookups into
the indexed temporary table.
@param thd thread handle
@param tmp_columns columns of temporary table
@note
Currently Item_subselect::init() already chooses and creates at parse
time an engine with a corresponding JOIN to execute the subquery.
@retval true if error
@retval false otherwise
*/
bool subselect_hash_sj_engine::setup(
THD *thd, const mem_root_deque<Item *> &tmp_columns) {
/* The result sink where we will materialize the subquery result. */
Query_result_union *tmp_result_sink;
/* The table into which the subquery is materialized. */
TABLE *tmp_table;
KEY *tmp_key; /* The only index on the temporary table. */
uint tmp_key_parts; /* Number of keyparts in tmp_key. */
uint key_length;
DBUG_TRACE;
DBUG_EXECUTE_IF("hash_semijoin_fail_in_setup", {
my_error(ER_UNKNOWN_ERROR, MYF(0));
return true;
});
/* 1. Create/initialize materialization related objects. */
/*
Create and initialize a select result interceptor that stores the
result stream in a temporary table. The temporary table itself is
managed (created/filled/etc) internally by the interceptor.
*/
if (!(tmp_result_sink = new (thd->mem_root) Query_result_union()))
return true;
if (tmp_result_sink->create_result_table(
thd, tmp_columns,
true, // Eliminate duplicates
thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS,
"<materialized_subquery>", true, true))
return true;
tmp_table = tmp_result_sink->table;
tmp_key = tmp_table->key_info;
if (tmp_table->hash_field) {
tmp_key_parts = CountVisibleFields(tmp_columns);
key_length = ALIGN_SIZE(tmp_table->s->reclength);
} else {
tmp_key_parts = tmp_key->user_defined_key_parts;
key_length = ALIGN_SIZE(tmp_key->key_length) * 2;
}
result = tmp_result_sink;
/*
Make sure there is only one index on the temp table.
*/
assert(CountVisibleFields(tmp_columns) == tmp_table->s->fields ||
// Unique constraint is used and a hash field was added
(tmp_table->hash_field &&
CountVisibleFields(tmp_columns) == tmp_table->s->fields - 1));
/* 2. Create/initialize execution related objects. */
/*
Create and initialize the Index_lookup used by the index lookup iterator
into the materialized subquery result.
*/
table = tmp_table;
ref.key = 0; /* The only temp table index. */
ref.key_length = tmp_key->key_length;
type = (tmp_table->key_info[0].flags & HA_NOSAME) ? JT_EQ_REF : JT_REF;
if (!(ref.key_buff = (uchar *)thd->mem_calloc(key_length)) ||
!(ref.key_copy =
(store_key **)thd->alloc((sizeof(store_key *) * tmp_key_parts))) ||
!(ref.items = (Item **)thd->alloc(sizeof(Item *) * tmp_key_parts)))
return true;
if (tmp_table->hash_field) {
ref.keypart_hash = &hash;
}
uchar *cur_ref_buff = ref.key_buff;
/*
Create an artificial condition to post-filter those rows matched by index
lookups that cannot be distinguished by the index lookup procedure, for
example:
- because of truncation (if the outer column type's length is bigger than
the inner column type's, index lookup will use a truncated outer
value as search key, yielding false positives).
- because the index is over hash_field and thus not unique.
Prepared statements execution requires that fix_fields is called
for every execution. In order to call fix_fields we need to create a
Name_resolution_context and a corresponding Table_ref for the
temporary table for the subquery, so that all column references to the
materialized subquery table can be resolved correctly.
*/
assert(cond == nullptr);
if (!(cond = new Item_cond_and)) return true;
/*
Table reference for tmp_table that is used to resolve column references
(Item_fields) to columns in tmp_table.
*/
Table_ref *tmp_table_ref =
new (thd->mem_root) Table_ref(tmp_table, "<materialized_subquery>");
if (tmp_table_ref == nullptr) return true;
// Assign Table_ref pointer temporarily, while creatung fields:
tmp_table->pos_in_table_list = tmp_table_ref;
tmp_table_ref->query_block = unit->first_query_block();
KEY_PART_INFO *key_parts = tmp_key->key_part;
for (uint part_no = 0; part_no < tmp_key_parts; part_no++) {
/* New equi-join condition for the current column. */
Item_func_eq *eq_cond;
/* Item for the corresponding field from the materialized temp table. */
Item_field *right_col_item;
Field *field = tmp_table->visible_field_ptr()[part_no];
const bool nullable = field->is_nullable();
ref.items[part_no] = item->left_expr->element_index(part_no);
if (!(right_col_item =
new Item_field(thd, &tmp_table_ref->query_block->context,
tmp_table_ref, field)) ||
!(eq_cond = new Item_func_eq(ref.items[part_no], right_col_item)) ||
((Item_cond_and *)cond)->add(eq_cond)) {
delete cond;
cond = nullptr;
return true;
}
if (tmp_table->hash_field) {
ref.key_copy[part_no] = new (thd->mem_root)
store_key_hash_item(thd, field, cur_ref_buff, nullptr,
field->pack_length(), ref.items[part_no], &hash);
} else {
ref.key_copy[part_no] = new (thd->mem_root) store_key(
thd, field,
/* TODO:
the NULL byte is taken into account in
key_parts[part_no].store_length, so instead of
cur_ref_buff + test(maybe_null), we could
use that information instead.
*/
cur_ref_buff + (nullable ? 1 : 0), nullable ? cur_ref_buff : nullptr,
key_parts[part_no].length, ref.items[part_no]);
}
if (nullable && // nullable column in tmp table,
// and UNKNOWN should not be interpreted as FALSE
!item->abort_on_null) {
// It must be the single column, or we wouldn't be here
assert(tmp_key_parts == 1);
// Be ready to search for NULL into inner column:
ref.null_ref_key = cur_ref_buff;
mat_table_has_nulls = NEX_UNKNOWN;
} else {
ref.null_ref_key = nullptr;
mat_table_has_nulls = NEX_IRRELEVANT_OR_FALSE;
}
if (tmp_table->hash_field)
cur_ref_buff += field->pack_length();
else
cur_ref_buff += key_parts[part_no].store_length;
}
tmp_table->pos_in_table_list = nullptr;
ref.key_err = true;
ref.key_parts = tmp_key_parts;
table_ref = tmp_table_ref;
if (cond->fix_fields(thd, &cond)) return true;
assert(unit->is_prepared());
return false;
}
void subselect_hash_sj_engine::create_iterators(THD *thd) {
if (unit->root_access_path() == nullptr) {
m_root_access_path =
NewZeroRowsAccessPath(thd, "Not optimized, outer query is empty");
m_iterator =
CreateIteratorFromAccessPath(thd, m_root_access_path, /*join=*/nullptr,
/*eligible_for_batch_mode=*/true);
return;
}
// We're only ever reading one row from the iterator, and record[1] isn't
// properly set up at this point, so we're not using EQRefIterator.
// (As a microoptimization, we add a LIMIT 1 if there's a filter and the
// index is unique, so that any filter added doesn't try to read a second row
// if the condition fails -- there wouldn't be one anyway.)
//
// Also, note that we never need to worry about searching for NULLs
// (which would require the AlternativeIterator); subqueries with
// JT_REF_OR_NULL are always transformed with IN-to-EXISTS, and thus,
// their artificial HAVING rejects NULL values.
assert(type != JT_REF_OR_NULL);
AccessPath *path = NewRefAccessPath(thd, table, &ref,
/*use_order=*/false, /*reverse=*/false,
/*count_examined_rows=*/false);
if (type == JT_EQ_REF && (cond != nullptr || having != nullptr)) {
path = NewLimitOffsetAccessPath(thd, path, /*limit=*/1, /*offset=*/0,
/*count_all_rows=*/false,
/* reject_multiple_rows=*/false,
/*send_records_override=*/nullptr);
}
if (cond != nullptr) {
path = NewFilterAccessPath(thd, path, cond);
}
if (having != nullptr) {
path = NewFilterAccessPath(thd, path, having);
}
/*
This impersonates the materialized table as a derived table. However, there
are certain aspects of a derived table that are NOT set, such as
effective_algorithm, so this assignment is incomplete.
However, it works for the time being (partially because TABLE object's
pos_in_table_list is nullptr).
*/
table_ref->set_derived_query_expression(unit);
if (table_ref->is_table_function()) {
path = NewMaterializedTableFunctionAccessPath(
thd, table, table_ref->table_function, path);
} else {
path = GetAccessPathForDerivedTable(
thd, table_ref, table, /*rematerialize=*/false,
/*invalidators=*/nullptr, /*need_rowid=*/false, path);
}
m_root_access_path = path;
JOIN *join =
unit->is_set_operation() ? nullptr : unit->first_query_block()->join;
unit->finalize(thd);
m_iterator = CreateIteratorFromAccessPath(thd, path, join,
/*eligible_for_batch_mode=*/true);
// The unit is not supposed to be executed by itself now.
unit->clear_root_access_path();
}
subselect_hash_sj_engine::~subselect_hash_sj_engine() {
/* Assure that cleanup has been called for this engine. */
assert(!table);
destroy(result);
}
/**
Cleanup performed after each execution.
*/
void subselect_hash_sj_engine::cleanup() {
DBUG_TRACE;
is_materialized = false;
if (result != nullptr) result->cleanup(); // Resets the temp table as well
DEBUG_SYNC(current_thd, "before_index_end_in_subselect");
m_root_access_path = nullptr;
m_iterator.reset();
if (table != nullptr) {
if (table->file->inited)
table->file->ha_index_end(); // Close the scan over the index
close_tmp_table(table);
free_tmp_table(table);
// Note that tab->qep_cleanup() is not called
table = nullptr;
}
if (unit->is_executed()) unit->reset_executed();
}
static int safe_index_read(TABLE *table, const Index_lookup &ref) {
int error = table->file->ha_index_read_map(
table->record[0], ref.key_buff, make_prev_keypart_map(ref.key_parts),
HA_READ_KEY_EXACT);
if (error) return report_handler_error(table, error);
return 0;
}
/**
Execute a subquery IN predicate via materialization.
If needed materialize the subquery into a temporary table, then
compute the predicate via a lookup into this table.
@retval true if error
@retval false otherwise
*/
bool subselect_hash_sj_engine::exec(THD *thd) {
DBUG_TRACE;
/*
Optimize and materialize the subquery during the first execution of
the subquery predicate.
*/
if (!is_materialized) {
thd->lex->set_current_query_block(unit->first_query_block());
assert(
unit->first_query_block()->master_query_expression()->is_optimized());
// Init() triggers materialization.
// (It also triggers some unneeded setup of the RefIterator, but it is
// cheap.)
bool error = m_iterator->Init();
if (error || thd->is_fatal_error()) return true;
/*
TODO:
- Unlock all subquery tables as we don't need them. To implement this
we need to add new functionality to JOIN::join_free that can unlock
all tables in a subquery (and all its subqueries).
- The temp table used for grouping in the subquery can be freed
immediately after materialization (yet it's done together with
unlocking).
*/
is_materialized = true;
// See if we have zero rows or not.
table->file->info(HA_STATUS_VARIABLE);
if (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) {
has_zero_rows = (table->file->stats.records == 0);
} else {
// Index must be closed before starting to scan.
if (table->file->inited) table->file->ha_index_or_rnd_end();
TableScanIterator scan(thd, table, /*expected_rows=*/-1.0,
/*examined_rows=*/nullptr);
int ret = scan.Read();
if (ret == 1 || thd->is_error()) {
return true;
}
has_zero_rows = (ret == -1);
}
} // if (!is_materialized)
if (has_zero_rows) {
// The correct answer is FALSE.
item->value = false;
return false;
}
/*
Here we could be brutal and set item->null_value. But we prefer to be
well-behaved and rather set the properties which
Item_in_subselect::val_bool() and Item_in_optimizer::val_int() expect,
and then those functions will set null_value based on those properties.
*/
if (item->left_expr->element_index(0)->null_value) {
/*
The first outer expression oe1 is NULL. It is the single outer
expression because if there would be more ((oe1,oe2,...)IN(...)) then
either they would be non-nullable (so we wouldn't be here) or the
predicate would be top-level (so we wouldn't be here,
Item_in_optimizer::val_int() would have short-cut). The correct answer
is UNKNOWN. Do as if searching with all triggered conditions disabled:
this would surely find a row. The caller will translate this to UNKNOWN.
*/
assert(item->left_expr->cols() == 1);
item->value = true;
return false;
}
hash = 0;
bool found;
if (ExecuteExistsQuery(thd, item->unit, m_iterator.get(), &found)) {
return true;
}
item->value = found;
item->assigned(true);
if (!found && // no exact match
mat_table_has_nulls != NEX_IRRELEVANT_OR_FALSE) {
/*
There is only one outer expression. It's not NULL. exec() above has set
the answer to FALSE, but if there exists an inner NULL in the temporary
table, then the correct answer is UNKNOWN, so let's find out.
*/
if (mat_table_has_nulls == NEX_UNKNOWN) // We do not know yet
{
// Search for NULL inside tmp table, and remember the outcome.
*ref.null_ref_key = 1;
if (!table->file->inited &&
table->file->ha_index_init(ref.key, false /* sorted */))
return true;
if (safe_index_read(table, ref) == 1) return true;
*ref.null_ref_key = 0; // prepare for next searches of non-NULL
mat_table_has_nulls =
table->has_row() ? NEX_TRUE : NEX_IRRELEVANT_OR_FALSE;
}
if (mat_table_has_nulls == NEX_TRUE) {
/*
There exists an inner NULL. The correct answer is UNKNOWN.
Do as if searching with all triggered conditions enabled; that
would not find any match, but Item_is_not_null_test would notice a
NULL:
*/
item->value = false;
item->was_null = true;
}
}
return false;
}
/**
Print the state of this engine into a string for debugging and views.
*/
void subselect_hash_sj_engine::print(const THD *thd, String *str,
enum_query_type query_type) {
str->append(STRING_WITH_LEN(" <materialize> ("));
unit->print(thd, str, query_type);
str->append(STRING_WITH_LEN(" ), "));
if (table)
subselect_indexsubquery_engine::print(thd, str, query_type);
else
str->append(
STRING_WITH_LEN("<the access method for lookups is not yet created>"));
}
|