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
|
/* Copyright (c) 2021, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/join_optimizer/interesting_orders.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <type_traits>
#include "map_helpers.h"
#include "my_hash_combine.h"
#include "my_pointer_arithmetic.h"
#include "sql/item.h"
#include "sql/item_func.h"
#include "sql/item_sum.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/print_utils.h"
#include "sql/mem_root_array.h"
#include "sql/parse_tree_nodes.h"
#include "sql/sql_array.h"
#include "sql/sql_class.h"
#include "sql/sql_executor.h"
using std::all_of;
using std::bind;
using std::distance;
using std::equal;
using std::fill;
using std::lower_bound;
using std::make_pair;
using std::max;
using std::none_of;
using std::pair;
using std::sort;
using std::string;
using std::swap;
using std::unique;
using std::upper_bound;
/**
A scope-guard class for allocating an Ordering::Elements instance
which is automatically returned to the pool when we exit the scope of
the OrderingElementsGuard instance.
*/
class OrderingElementsGuard final {
public:
/**
@param context The object containing the pool.
@param mem_root For allocating additional Ordering::Elements instances if
needed.
*/
OrderingElementsGuard(LogicalOrderings *context, MEM_ROOT *mem_root)
: m_context{context} {
m_elements = context->RetrieveElements(mem_root);
}
// No copying of this class.
OrderingElementsGuard(const OrderingElementsGuard &) = delete;
OrderingElementsGuard &operator=(const OrderingElementsGuard &) = delete;
~OrderingElementsGuard() { m_context->ReturnElements(m_elements); }
Ordering::Elements &Get() { return m_elements; }
private:
/// The object containing the pool.
LogicalOrderings *m_context;
/// The instance fetched from the pool.
Ordering::Elements m_elements;
};
namespace {
// Set some maximum limits on the size of the FSMs, in order to prevent runaway
// computation on pathological queries. As rough reference: As of 8.0.26,
// there is a single query in the test suite hitting these limits (it wants 8821
// NFSM states and an estimated 2^50 DFSM states). Excluding that query, the
// test suite contains the following largest FSMs:
//
// - Largest NFSM: 63 NFSM states => 2 DFSM states
// - Largest DFSM: 37 NFSM states => 152 DFSM states
//
// And for DBT-3:
//
// - Largest NFSM: 43 NFSM states => 3 DFSM states
// - Largest DFSM: 8 NFSM states => 8 DFSM states
//
// We could make system variables out of these if needed, but they would
// probably have to be settable by superusers only, in order to prevent runaway
// unabortable queries from taking down the server. Having them as fixed limits
// is good enough for now.
constexpr int kMaxNFSMStates = 200;
constexpr int kMaxDFSMStates = 2000;
/**
Check if 'elements' contains 'item'.
*/
bool Contains(Ordering::Elements elements, int item) {
return std::any_of(elements.cbegin(), elements.cend(),
[item](OrderElement elem) { return elem.item == item; });
}
// Calculates the hash for a DFSM state given by an index into
// LogicalOrderings::m_dfsm_states. The hash is based on the set of NFSM states
// the DFSM state corresponds to.
template <typename DFSMState>
struct DFSMStateHash {
const Mem_root_array<DFSMState> *dfsm_states;
size_t operator()(int idx) const {
size_t hash = 0;
for (int nfsm_state : (*dfsm_states)[idx].nfsm_states) {
my_hash_combine<size_t>(hash, nfsm_state);
}
return hash;
}
};
// Checks if two DFSM states represent the same set of NFSM states.
template <typename DFSMState>
struct DFSMStateEqual {
const Mem_root_array<DFSMState> *dfsm_states;
bool operator()(int idx1, int idx2) const {
return equal((*dfsm_states)[idx1].nfsm_states.begin(),
(*dfsm_states)[idx1].nfsm_states.end(),
(*dfsm_states)[idx2].nfsm_states.begin(),
(*dfsm_states)[idx2].nfsm_states.end());
}
};
} // namespace
void Ordering::Deduplicate() {
assert(Valid());
size_t length = 0;
for (size_t i = 0; i < m_elements.size(); ++i) {
if (!Contains(m_elements.prefix(length), m_elements[i].item)) {
m_elements[length++] = m_elements[i];
}
}
m_elements.resize(length);
}
bool Ordering::Valid() const {
switch (m_kind) {
case Kind::kEmpty:
return m_elements.empty();
case Kind::kOrder:
return !m_elements.empty() &&
std::all_of(m_elements.cbegin(), m_elements.cend(),
[](OrderElement e) {
return e.direction != ORDER_NOT_RELEVANT;
});
case Kind::kRollup:
case Kind::kGroup:
return !m_elements.empty() &&
std::all_of(m_elements.cbegin(), m_elements.cend(),
[](OrderElement e) {
return e.direction == ORDER_NOT_RELEVANT;
});
}
assert(false);
return false;
}
LogicalOrderings::LogicalOrderings(THD *thd)
: m_items(thd->mem_root),
m_orderings(thd->mem_root),
m_fds(thd->mem_root),
m_states(thd->mem_root),
m_dfsm_states(thd->mem_root),
m_dfsm_edges(thd->mem_root),
m_elements_pool(thd->mem_root) {
GetHandle(nullptr); // Always has the zero handle.
// Add the empty ordering/grouping.
m_orderings.push_back(OrderingWithInfo{Ordering(),
OrderingWithInfo::UNINTERESTING,
/*used_at_end=*/true});
FunctionalDependency decay_fd;
decay_fd.type = FunctionalDependency::DECAY;
decay_fd.tail = 0;
decay_fd.always_active = true;
m_fds.push_back(decay_fd);
}
int LogicalOrderings::AddOrderingInternal(THD *thd, Ordering order,
OrderingWithInfo::Type type,
bool used_at_end,
table_map homogenize_tables) {
assert(!m_built);
#ifndef NDEBUG
if (order.GetKind() == Ordering::Kind::kGroup) {
Ordering::Elements elements = order.GetElements();
// Verify that the grouping is sorted and deduplicated.
for (size_t i = 1; i < elements.size(); ++i) {
assert(elements[i].item > elements[i - 1].item);
assert(elements[i].direction == ORDER_NOT_RELEVANT);
}
// Verify that none of the items are of ROW_RESULT,
// as RemoveDuplicatesIterator cannot handle them.
// (They would theoretically be fine for orderings.)
for (size_t i = 0; i < elements.size(); ++i) {
assert(m_items[elements[i].item].item->result_type() != ROW_RESULT);
}
}
#endif
if (type != OrderingWithInfo::UNINTERESTING) {
for (OrderElement element : order.GetElements()) {
if (element.direction == ORDER_ASC) {
m_items[element.item].used_asc = true;
}
if (element.direction == ORDER_DESC) {
m_items[element.item].used_desc = true;
}
if (element.direction == ORDER_NOT_RELEVANT) {
m_items[element.item].used_in_grouping = true;
}
}
}
// Deduplicate against all the existing ones.
for (size_t i = 0; i < m_orderings.size(); ++i) {
if (m_orderings[i].ordering == order) {
// Potentially promote the existing one.
m_orderings[i].type = std::max(m_orderings[i].type, type);
m_orderings[i].homogenize_tables |= homogenize_tables;
return i;
}
}
m_orderings.push_back(OrderingWithInfo{order.Clone(thd->mem_root), type,
used_at_end, homogenize_tables});
m_longest_ordering = std::max<int>(m_longest_ordering, order.size());
return m_orderings.size() - 1;
}
int LogicalOrderings::AddFunctionalDependency(THD *thd,
FunctionalDependency fd) {
assert(!m_built);
// Deduplicate against all the existing ones.
for (size_t i = 0; i < m_fds.size(); ++i) {
if (m_fds[i].type != fd.type) {
continue;
}
if (fd.type == FunctionalDependency::EQUIVALENCE) {
// Equivalences are symmetric.
if (m_fds[i].head[0] == fd.head[0] && m_fds[i].tail == fd.tail) {
return i;
}
if (m_fds[i].tail == fd.head[0] && m_fds[i].head[0] == fd.tail) {
return i;
}
} else {
if (m_fds[i].tail == fd.tail &&
equal(m_fds[i].head.begin(), m_fds[i].head.end(), fd.head.begin(),
fd.head.end())) {
return i;
}
}
}
fd.head = fd.head.Clone(thd->mem_root);
m_fds.push_back(fd);
return m_fds.size() - 1;
}
void LogicalOrderings::Build(THD *thd, string *trace) {
// If we have no interesting orderings or groupings, just create a DFSM
// directly with a single state for the empty ordering.
if (m_orderings.size() == 1) {
m_dfsm_states.reserve(1);
m_dfsm_states.emplace_back();
DFSMState &initial = m_dfsm_states.back();
initial.nfsm_states.init(thd->mem_root);
initial.nfsm_states.reserve(1);
initial.nfsm_states.push_back(0);
initial.next_state =
Bounds_checked_array<int>::Alloc(thd->mem_root, m_fds.size());
m_optimized_ordering_mapping =
Bounds_checked_array<int>::Alloc(thd->mem_root, 1);
m_built = true;
return;
}
BuildEquivalenceClasses();
RecanonicalizeGroupings();
AddFDsFromComputedItems(thd);
AddFDsFromConstItems(thd);
AddFDsFromAggregateItems(thd);
PreReduceOrderings(thd);
CreateOrderingsFromGroupings(thd);
CreateHomogenizedOrderings(thd);
PruneFDs(thd);
if (trace != nullptr) {
PrintFunctionalDependencies(trace);
}
FindElementsThatCanBeAddedByFDs();
PruneUninterestingOrders(thd);
if (trace != nullptr) {
PrintInterestingOrders(trace);
}
BuildNFSM(thd);
if (trace != nullptr) {
*trace += "NFSM for interesting orders, before pruning:\n";
PrintNFSMDottyGraph(trace);
if (m_states.size() >= kMaxNFSMStates) {
*trace += "NOTE: NFSM is incomplete, because it became too big.\n";
}
}
PruneNFSM(thd);
if (trace != nullptr) {
*trace += "\nNFSM for interesting orders, after pruning:\n";
PrintNFSMDottyGraph(trace);
}
ConvertNFSMToDFSM(thd);
if (trace != nullptr) {
*trace += "\nDFSM for interesting orders:\n";
PrintDFSMDottyGraph(trace);
if (m_dfsm_states.size() >= kMaxDFSMStates) {
*trace +=
"NOTE: DFSM does not contain all NFSM states, because it became too "
"big.\n";
}
}
FindInitialStatesForOrdering();
m_built = true;
}
LogicalOrderings::StateIndex LogicalOrderings::ApplyFDs(
LogicalOrderings::StateIndex state_idx, FunctionalDependencySet fds) const {
for (;;) { // Termination condition within loop.
FunctionalDependencySet relevant_fds =
m_dfsm_states[state_idx].can_use_fd & fds;
if (relevant_fds.none()) {
return state_idx;
}
// Pick an arbitrary one and follow it. Note that this part assumes
// kMaxSupportedFDs <= 64.
static_assert(kMaxSupportedFDs <= sizeof(unsigned long long) * CHAR_BIT);
int fd_idx = FindLowestBitSet(relevant_fds.to_ullong()) + 1;
state_idx = m_dfsm_states[state_idx].next_state[fd_idx];
// Now continue for as long as we have anything to follow;
// we'll converge on the right answer eventually. Typically,
// there will be one or two edges to follow, but in extreme cases,
// there could be O(k²) in the number of FDs.
}
}
/**
Try to get rid of uninteresting orders, possibly by discarding irrelevant
suffixes and merging them with others. In a typical query, this removes a
large amount of index-created orderings that will never get to something
interesting, reducing the end FSM size (and thus, reducing the number of
different access paths we have to keep around).
This step is the only one that can move orderings around, and thus also
populates m_optimized_ordering_mapping.
*/
void LogicalOrderings::PruneUninterestingOrders(THD *thd) {
m_optimized_ordering_mapping =
Bounds_checked_array<int>::Alloc(thd->mem_root, m_orderings.size());
int new_length = 0;
for (size_t ordering_idx = 0; ordering_idx < m_orderings.size();
++ordering_idx) {
if (m_orderings[ordering_idx].type == OrderingWithInfo::UNINTERESTING) {
Ordering &ordering = m_orderings[ordering_idx].ordering;
// We are not prepared for uninteresting groupings yet.
assert(ordering.GetKind() != Ordering::Kind::kGroup);
// Find the longest prefix that contains only elements that are used in
// interesting groupings. We will never shorten the uninteresting ordering
// below this; it is overconservative in some cases, but it makes sure
// we never miss a path to an interesting grouping.
size_t minimum_prefix_len = 0;
const Ordering::Elements &elements = ordering.GetElements();
while (elements.size() > minimum_prefix_len &&
m_items[m_items[elements[minimum_prefix_len].item].canonical_item]
.used_in_grouping) {
++minimum_prefix_len;
}
// Shorten this ordering one by one element, until it can (heuristically)
// become an interesting ordering with the FDs we have. Note that it might
// become the empty ordering, and if so, it will be deleted entirely
// in the step below.
while (elements.size() > minimum_prefix_len &&
!CouldBecomeInterestingOrdering(ordering)) {
if (elements.size() > 1) {
ordering = Ordering(elements.without_back(), ordering.GetKind());
} else {
ordering = Ordering();
}
}
}
// Since some orderings may have changed, we need to re-deduplicate.
// Note that at this point, we no longer care about used_at_end;
// it was only used for reducing orderings in homogenization.
m_optimized_ordering_mapping[ordering_idx] = new_length;
for (int i = 0; i < new_length; ++i) {
if (m_orderings[i].ordering == m_orderings[ordering_idx].ordering) {
m_optimized_ordering_mapping[ordering_idx] = i;
m_orderings[i].type =
std::max(m_orderings[i].type, m_orderings[ordering_idx].type);
break;
}
}
if (m_optimized_ordering_mapping[ordering_idx] == new_length) {
// Not a duplicate of anything earlier, so keep it.
m_orderings[new_length++] = m_orderings[ordering_idx];
}
}
m_orderings.resize(new_length);
}
void LogicalOrderings::PruneFDs(THD *thd) {
// The definition of prunable FDs in the papers seems to be very abstract
// and not practically realizable, so we use a simple heuristic instead:
// A FD is useful iff it produces an item that is part of some ordering.
// Discard all useless FDs. (Items not part of some ordering will cause
// the new proposed ordering to immediately be pruned away, so this is
// safe. See also the comment in the .h file about transitive dependencies.)
//
// Note that this will sometimes leave useless FDs; if we have e.g. a → b
// and b is useful, we will mark the FD as useful even if nothing can
// produce a. However, such FDs don't induce more NFSM states (which is
// the main point of the pruning), it just slows the NFSM down slightly,
// and by far the dominant FDs to prune in our cases are the ones
// induced by keys, e.g. S → k where S is always the same and k
// is useless. These are caught by this heuristic.
m_optimized_fd_mapping =
Bounds_checked_array<int>::Alloc(thd->mem_root, m_fds.size());
size_t old_length = m_fds.size();
// We always need to keep the decay FD, so start at 1.
m_optimized_fd_mapping[0] = 0;
int new_length = 1;
for (size_t fd_idx = 1; fd_idx < old_length; ++fd_idx) {
const FunctionalDependency &fd = m_fds[fd_idx];
// See if we this FDs is useful, ie., can produce an item used in an
// ordering.
bool used_fd = false;
ItemHandle tail = m_items[fd.tail].canonical_item;
if (m_items[tail].used_asc || m_items[tail].used_desc ||
m_items[tail].used_in_grouping) {
used_fd = true;
} else if (fd.type == FunctionalDependency::EQUIVALENCE) {
ItemHandle head = m_items[fd.head[0]].canonical_item;
if (m_items[head].used_asc || m_items[head].used_desc ||
m_items[head].used_in_grouping) {
used_fd = true;
}
}
if (!used_fd) {
m_optimized_fd_mapping[fd_idx] = -1;
continue;
}
if (m_fds[fd_idx].always_active) {
// Defer these for now, by moving them to the end. We will need to keep
// them in the array so that we can apply them under FSM construction,
// but they should not get a FD bitmap, and thus also not priority for
// the lowest index. We could have used a separate array, but the m_fds
// array probably already has the memory.
m_optimized_fd_mapping[fd_idx] = -1;
m_fds.push_back(m_fds[fd_idx]);
} else {
m_optimized_fd_mapping[fd_idx] = new_length;
m_fds[new_length++] = m_fds[fd_idx];
}
}
// Now include the always-on FDs we deferred earlier.
for (size_t fd_idx = old_length; fd_idx < m_fds.size(); ++fd_idx) {
m_fds[new_length++] = m_fds[fd_idx];
}
m_fds.resize(new_length);
}
void LogicalOrderings::BuildEquivalenceClasses() {
for (size_t i = 0; i < m_items.size(); ++i) {
m_items[i].canonical_item = i;
}
// In the worst case, for n items, all equal, m FDs ordered optimally bad,
// this algorithm is O(nm) (all items shifted one step down each loop).
// In practice, it should be much better.
bool done_anything;
do {
done_anything = false;
for (const FunctionalDependency &fd : m_fds) {
if (fd.type != FunctionalDependency::EQUIVALENCE) {
continue;
}
ItemHandle left_item = fd.head[0];
ItemHandle right_item = fd.tail;
if (m_items[left_item].canonical_item ==
m_items[right_item].canonical_item) {
// Already fully applied.
continue;
}
// Merge the classes so that the lowest index always is the canonical one
// of its equivalence class.
ItemHandle canonical_item, duplicate_item;
if (m_items[right_item].canonical_item <
m_items[left_item].canonical_item) {
canonical_item = m_items[right_item].canonical_item;
duplicate_item = left_item;
} else {
canonical_item = m_items[left_item].canonical_item;
duplicate_item = right_item;
}
m_items[duplicate_item].canonical_item = canonical_item;
m_items[canonical_item].used_asc |= m_items[duplicate_item].used_asc;
m_items[canonical_item].used_desc |= m_items[duplicate_item].used_desc;
m_items[canonical_item].used_in_grouping |=
m_items[duplicate_item].used_in_grouping;
done_anything = true;
}
} while (done_anything);
}
// Put all groupings into a canonical form that we can compare them
// as orderings without further logic. (It needs to be on a form that
// does not change markedly after applying equivalences, and it needs
// to be deterministic, but apart from that, the order is pretty arbitrary.)
// We can only do this after BuildEquivalenceClasses().
void LogicalOrderings::RecanonicalizeGroupings() {
for (OrderingWithInfo &ordering : m_orderings) {
if (ordering.ordering.GetKind() == Ordering::Kind::kGroup) {
SortElements(ordering.ordering.GetElements());
}
}
}
// Window functions depend on both the function argument and on the PARTITION BY
// clause, so we need to add both to the functional dependency's head.
// The order of elements is arbitrary.
Bounds_checked_array<ItemHandle>
LogicalOrderings::CollectHeadForStaticWindowFunction(THD *thd,
ItemHandle argument_item,
Window *window) {
const PT_order_list *partition_by = window->effective_partition_by();
int partition_len = 0;
if (partition_by != nullptr) {
for (ORDER *order = partition_by->value.first; order != nullptr;
order = order->next) {
++partition_len;
}
}
auto head =
Bounds_checked_array<ItemHandle>::Alloc(thd->mem_root, partition_len + 1);
if (partition_by != nullptr) {
for (ORDER *order = partition_by->value.first; order != nullptr;
order = order->next) {
head[partition_len--] = GetHandle(*order->item);
}
}
head[0] = argument_item;
return head;
}
/**
Try to add new FDs from items that are not base items; e.g., if we have
an item (a + 1), we add {a} → (a + 1) (since addition is deterministic).
This can help reducing orderings that are on such derived items.
For simplicity, we only bother doing this for items that derive from a
single base field; i.e., from (a + b), we don't add {a,b} → (a + b)
even though we could. Also note that these are functional dependencies,
not equivalences; even though ORDER BY (a + 1) could be satisfied by an
ordering on (a) (barring overflow issues), this does not hold in general,
e.g. ORDER BY (-a) is _not_ satisfied by an ordering on (a), not to mention
ORDER BY (a*a). We do not have the framework in Item to understand which
functions are monotonous, so we do not attempt to create equivalences.
This is really the only the case where we can get transitive FDs that are not
equivalences. Since our approach does not apply FDs transitively without
adding the intermediate item (e.g., for {a} → b and {b} → c, we won't extend
(a) to (ac), only to (abc)), we extend any existing FDs here when needed.
*/
void LogicalOrderings::AddFDsFromComputedItems(THD *thd) {
int num_original_items = m_items.size();
int num_original_fds = m_fds.size();
for (int item_idx = 0; item_idx < num_original_items; ++item_idx) {
// We only care about items that are used in some ordering,
// not any used as base in FDs or the likes.
const ItemHandle canonical_idx = m_items[item_idx].canonical_item;
if (!m_items[canonical_idx].used_asc && !m_items[canonical_idx].used_desc &&
!m_items[canonical_idx].used_in_grouping) {
continue;
}
// We only want to look at items that are not already Item_field
// or aggregate functions (the latter are handled in
// AddFDsFromAggregateItems()), and that are generated from a single field.
// Some quick heuristics will eliminate most of these for us.
Item *item = m_items[item_idx].item;
const table_map used_tables = item->used_tables();
if (item->type() == Item::FIELD_ITEM || item->has_aggregation() ||
Overlaps(used_tables, PSEUDO_TABLE_BITS) ||
!IsSingleBitSet(used_tables)) {
continue;
}
// Window functions have much more state than just the parameter,
// so we cannot say that e.g. {a} → SUM(a) OVER (...), unless we
// know that the function is over the entire frame (unbounded).
//
// TODO(sgunders): We could also add FDs for window functions
// where could guarantee that the partition is only one row.
bool is_static_wf = false;
if (item->has_wf()) {
if (item->m_is_window_function &&
down_cast<Item_sum *>(item)->framing() &&
down_cast<Item_sum *>(item)->window()->static_aggregates()) {
is_static_wf = true;
} else {
continue;
}
}
Item_field *base_field = nullptr;
bool error =
WalkItem(item, enum_walk::POSTFIX, [&base_field](Item *sub_item) {
if (sub_item->type() == Item::FUNC_ITEM &&
down_cast<Item_func *>(sub_item)->functype() ==
Item_func::ROLLUP_GROUP_ITEM_FUNC) {
// Rollup items are nondeterministic, yet don't always set
// RAND_TABLE_BIT.
return true;
}
if (sub_item->type() == Item::FIELD_ITEM) {
if (base_field != nullptr &&
!base_field->eq(sub_item, /*binary_cmp=*/true)) {
// More than one field in use.
return true;
}
base_field = down_cast<Item_field *>(sub_item);
}
return false;
});
if (error || base_field == nullptr) {
// More than one field in use, or no fields in use
// (can happen even when used_tables is set, e.g. for
// an Item_view_ref to a constant).
continue;
}
if (!base_field->field->binary()) {
// Fields with collations can have equality (with no tiebreaker)
// even with fields that contain differing binary data.
// Thus, functions do not always preserve equality; a == b
// does not mean f(a) == f(b), and thus, the FD does not
// hold either.
continue;
}
ItemHandle head_item = GetHandle(base_field);
FunctionalDependency fd;
fd.type = FunctionalDependency::FD;
if (is_static_wf) {
fd.head = CollectHeadForStaticWindowFunction(
thd, head_item, down_cast<Item_sum *>(item)->window());
} else {
fd.head = Bounds_checked_array<ItemHandle>(&head_item, 1);
}
fd.tail = item_idx;
fd.always_active = true;
AddFunctionalDependency(thd, fd);
if (fd.head.size() == 1) {
// Extend existing FDs transitively (see function comment).
// E.g. if we have S → base, also add S → item.
for (int fd_idx = 0; fd_idx < num_original_fds; ++fd_idx) {
if (m_fds[fd_idx].type == FunctionalDependency::FD &&
m_fds[fd_idx].tail == head_item && m_fds[fd_idx].always_active) {
fd = m_fds[fd_idx];
fd.tail = item_idx;
AddFunctionalDependency(thd, fd);
}
}
}
}
}
/**
Try to add FDs from items that are constant by themselves, e.g. if someone
does ORDER BY 'x', add a new FD {} → 'x' so that the ORDER BY can be elided.
TODO(sgunders): This can potentially remove subqueries or other functions
that would throw errors if actually executed, potentially modifying
semantics. See if that is illegal, and thus, if we need to test-execute them
at least once somehow (ideally not during optimization).
*/
void LogicalOrderings::AddFDsFromConstItems(THD *thd) {
int num_original_items = m_items.size();
for (int item_idx = 0; item_idx < num_original_items; ++item_idx) {
// We only care about items that are used in some ordering,
// not any used as base in FDs or the likes.
const ItemHandle canonical_idx = m_items[item_idx].canonical_item;
if (!m_items[canonical_idx].used_asc && !m_items[canonical_idx].used_desc &&
!m_items[canonical_idx].used_in_grouping) {
continue;
}
if (m_items[item_idx].item->const_for_execution()) {
// Add {} → item.
FunctionalDependency fd;
fd.type = FunctionalDependency::FD;
fd.head = Bounds_checked_array<ItemHandle>();
fd.tail = item_idx;
fd.always_active = true;
AddFunctionalDependency(thd, fd);
}
}
}
void LogicalOrderings::AddFDsFromAggregateItems(THD *thd) {
// If ROLLUP is active, and we have nullable GROUP BY expressions, we could
// get two different NULL groups with different aggregates; one for the actual
// NULL value, and one for the rollup group. If so, these FDs no longer hold,
// and we cannot add them.
if (m_rollup) {
for (ItemHandle item : m_aggregate_head) {
if (m_items[item].item->is_nullable()) {
return;
}
}
}
int num_original_items = m_items.size();
for (int item_idx = 0; item_idx < num_original_items; ++item_idx) {
// We only care about items that are used in some ordering,
// not any used as base in FDs or the likes.
const ItemHandle canonical_idx = m_items[item_idx].canonical_item;
if (!m_items[canonical_idx].used_asc && !m_items[canonical_idx].used_desc &&
!m_items[canonical_idx].used_in_grouping) {
continue;
}
if (m_items[item_idx].item->has_aggregation() &&
!m_items[item_idx].item->has_wf()) {
// Add {all GROUP BY items} → item.
// Note that the head might be empty, for implicit grouping,
// which means all aggregate items are constant (there is only one row).
FunctionalDependency fd;
fd.type = FunctionalDependency::FD;
fd.head = m_aggregate_head;
fd.tail = item_idx;
fd.always_active = true;
AddFunctionalDependency(thd, fd);
}
}
}
void LogicalOrderings::FindElementsThatCanBeAddedByFDs() {
for (const FunctionalDependency &fd : m_fds) {
m_items[m_items[fd.tail].canonical_item].can_be_added_by_fd = true;
if (fd.type == FunctionalDependency::EQUIVALENCE) {
m_items[m_items[fd.head[0]].canonical_item].can_be_added_by_fd = true;
}
}
}
/**
Checks whether the given item is redundant given previous elements in
the ordering; ie., whether adding it will never change the ordering.
This could either be because it's a duplicate, or because it is implied
by functional dependencies. When this is applied to all elements in turn,
it is called “reducing” the ordering. [Neu04] claims that this operation
is not confluent, which is erroneous (their example is faulty, ignoring
that Simmen reduces from the back). [Neu04b] has modified the claim to
be that it is not confluent for _groupings_, which is correct.
We make no attempt at optimality.
If all_fds is true, we consider all functional dependencies, including those
that may not always be active; e.g. a FD a=b may come from a join, and thus
does not hold before the join is actually done, but we assume it holds anyway.
This is OK from order homogenization, which is concerned with making orderings
that will turn into the desired interesting ordering (e.g. for ORDER BY) only
after all joins have been done. It would not be OK if we were to use it for
merge joins somehow.
*/
bool LogicalOrderings::ImpliedByEarlierElements(ItemHandle item,
Ordering::Elements prefix,
bool all_fds) const {
// First, search for straight-up duplicates (ignoring ASC/DESC).
if (Contains(prefix, item)) {
return true;
}
// Check if this item is implied by any of the functional dependencies.
for (size_t fd_idx = 1; fd_idx < m_fds.size(); ++fd_idx) {
const FunctionalDependency &fd = m_fds[fd_idx];
if (!all_fds && !fd.always_active) {
continue;
}
if (fd.type == FunctionalDependency::FD) {
if (fd.tail != item) {
continue;
}
// Check if we have all the required head items.
bool all_found = true;
for (ItemHandle other_item : fd.head) {
if (!Contains(prefix, other_item)) {
all_found = false;
break;
}
}
if (all_found) {
return true;
}
} else {
// a = b implies that a → b and b → a, so we check for both of those.
assert(fd.type == FunctionalDependency::EQUIVALENCE);
assert(fd.head.size() == 1);
if (fd.tail == item && Contains(prefix, fd.head[0])) {
return true;
}
if (fd.head[0] == item && Contains(prefix, fd.tail)) {
return true;
}
}
}
return false;
}
/**
Do safe reduction on all orderings (some of them may get merged by
PruneUninterestingOrders() later), ie., remove all items that may be removed
using only FDs that always are active.
There's a problem in [Neu04] that is never adequately addressed; orderings are
only ever expanded, and then eventually compared against interesting orders.
But the interesting order itself is not necessarily extended, due to pruning.
For instance, if an index could yield (x,y) and we have {} → x, there's no way
we could get it to match the interesting order (y) even though they are
logically equivalent. For an even trickier case, imagine an index (x,y) and
an interesting order (y,z), with {} → x and y → z. For this to match, we'd
need to have a “super-order” (x,y,z) and infer that from both orderings.
Instead, we do a pre-step related to Simmen's “Test Ordering” procedure;
we reduce the orderings. In the example above, both will be reduced to (y),
and then match. This is mostly a band-aid around the problem; for instance,
it cannot deal with FDs that are not always active, and it does not deal
adequately with groupings (since reduction does not).
Note that this could make the empty ordering interesting after merging.
*/
void LogicalOrderings::PreReduceOrderings(THD *thd) {
for (OrderingWithInfo &ordering : m_orderings) {
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering reduced_ordering =
ReduceOrdering(ordering.ordering,
/*all_fds=*/false, tmp_guard.Get());
if (reduced_ordering.size() < ordering.ordering.size()) {
ordering.ordering = reduced_ordering.Clone(thd->mem_root);
}
}
}
/**
We don't currently have any operators that only group and do not sort
(e.g. hash grouping), so we always implement grouping by sorting.
This function makes that representation explicit -- for each grouping,
it will make sure there is at least one ordering representing that
grouping. This means we never need to “sort by a grouping”, which
would destroy ordering information that could be useful later.
As an example, take SELECT ... GROUP BY a, b ORDER BY a. This needs to
group first by {a,b} (assume we're using filesort, not an index),
then sort by (a). If we just represent the sort we're doing as going
directly to {a,b}, we can't elide the sort on (a). Instead, we create
a sort (a,b) (implicitly convertible to {a,b}), which makes the FSM
understand that we're _both_ sorted on (a,b) and grouped on {a,b},
and then also sorted on (a).
Any given grouping would be satisfied by lots of different orderings:
{a,b} could be (a,b), (b,a), (a DESC, b) etc.. We look through all
interesting orders that are a subset of our grouping, and if they are,
we extend them arbitrarily to complete the grouping. E.g., if our
grouping is {a,b,c,d} and the ordering (c DESC, b) is interesting,
we make a homogenized ordering (c DESC, b, a, d). This is roughly
equivalent to Simmen's “Cover Order” procedure. If we cannot make
such a cover, we simply make a new last-resort ordering (a,b,c,d).
We don't consider equivalences here; perhaps we should, at least
for at-end groupings.
*/
void LogicalOrderings::CreateOrderingsFromGroupings(THD *thd) {
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
int num_original_orderings = m_orderings.size();
for (int grouping_idx = 1; grouping_idx < num_original_orderings;
++grouping_idx) {
const Ordering &grouping = m_orderings[grouping_idx].ordering;
if (grouping.GetKind() != Ordering::Kind::kGroup ||
m_orderings[grouping_idx].type != OrderingWithInfo::INTERESTING) {
continue;
}
bool has_cover = false;
for (int ordering_idx = 1; ordering_idx < num_original_orderings;
++ordering_idx) {
const Ordering &ordering = m_orderings[ordering_idx].ordering;
if (ordering.GetKind() != Ordering::Kind::kOrder ||
m_orderings[ordering_idx].type != OrderingWithInfo::INTERESTING ||
ordering.size() > grouping.size()) {
continue;
}
bool can_cover =
all_of(ordering.GetElements().begin(), ordering.GetElements().end(),
[&grouping](const OrderElement &element) {
return Contains(grouping.GetElements(), element.item);
});
if (!can_cover) {
continue;
}
has_cover = true;
// On a full match, just note that we have a cover, don't make a new
// ordering. We assume both are free of duplicates.
if (ordering.size() == grouping.size()) {
continue;
}
for (size_t i = 0; i < ordering.size(); ++i) {
tmp[i] = ordering.GetElements()[i];
}
int len = ordering.size();
for (const OrderElement &element : grouping.GetElements()) {
if (!Contains(ordering.GetElements(), element.item)) {
tmp[len].item = element.item;
tmp[len].direction = ORDER_ASC; // Arbitrary.
++len;
}
}
assert(len == static_cast<int>(grouping.size()));
AddOrderingInternal(
thd, Ordering(tmp.prefix(len), Ordering::Kind::kOrder),
OrderingWithInfo::HOMOGENIZED, m_orderings[grouping_idx].used_at_end,
/*homogenize_tables=*/0);
}
// Make a fallback ordering if no cover was found.
if (!has_cover) {
for (size_t i = 0; i < grouping.size(); ++i) {
tmp[i].item = grouping.GetElements()[i].item;
tmp[i].direction = ORDER_ASC; // Arbitrary.
}
AddOrderingInternal(
thd, Ordering(tmp.prefix(grouping.size()), Ordering::Kind::kOrder),
OrderingWithInfo::HOMOGENIZED, m_orderings[grouping_idx].used_at_end,
/*homogenize_tables=*/0);
}
}
}
/**
For each interesting ordering, see if we can homogenize it onto each table.
A homogenized ordering is one that refers to fewer tables than the original
one -- in our case, a single table. (If we wanted to, we could homogenize down
to sets of tables instead of single tables only. However, that would open up
for O(2^n) orderings, so we restrict to single-table.)
The idea is to enable sort-ahead; find an ordering we can sort a single table
in that, after later applying functional dependencies, eventually gives the
desired ordering. This is just a heuristic (in particular, we only consider
equivalences, not other functional dependencies), but in most cases will give
us an ordering if any exist.
Neumann et al do not talk much about this, so this comes from the Simmen
paper, where it is called “Homogenize Order”.
*/
void LogicalOrderings::CreateHomogenizedOrderings(THD *thd) {
// Collect all tables we have seen referred to in items. (Actually, we could
// limit ourselves to the ones we've seen in functional dependencies, but this
// is simpler.)
table_map seen_tables = 0;
for (const ItemInfo &item : m_items) {
if (item.item != nullptr) {
seen_tables |= item.item->used_tables();
}
}
seen_tables &= ~PSEUDO_TABLE_BITS;
// Build a reverse table of canonical items to items,
// and sort it, so that we can fairly efficiently make lookups into it.
auto reverse_canonical =
Bounds_checked_array<pair<ItemHandle, ItemHandle>>::Alloc(thd->mem_root,
m_items.size());
for (size_t item_idx = 0; item_idx < m_items.size(); ++item_idx) {
reverse_canonical[item_idx].first = m_items[item_idx].canonical_item;
reverse_canonical[item_idx].second = item_idx;
}
sort(reverse_canonical.begin(), reverse_canonical.end());
// Now, for each table, try to see if we can rewrite an ordering
// to something only referring to that table, by swapping out non-conforming
// items for others.
int num_original_orderings = m_orderings.size();
for (int ordering_idx = 1; ordering_idx < num_original_orderings;
++ordering_idx) {
if (m_orderings[ordering_idx].type == OrderingWithInfo::UNINTERESTING) {
continue;
}
if (m_orderings[ordering_idx].ordering.GetKind() ==
Ordering::Kind::kGroup) {
// We've already made orderings out of these, which will be
// homogenized, so we don't need to homogenize the grouping itself,
// too.
continue;
}
OrderingElementsGuard tmp_guard(this, thd->mem_root);
const Ordering &reduced_ordering = ReduceOrdering(
m_orderings[ordering_idx].ordering,
/*all_fds=*/m_orderings[ordering_idx].used_at_end, tmp_guard.Get());
if (reduced_ordering.GetElements().empty()) {
continue;
}
// Now try to homogenize it onto all tables in turn.
table_map homogenize_tables;
if (m_orderings[ordering_idx].used_at_end) {
// Try all tables.
homogenize_tables = seen_tables;
} else {
// Try only the ones we were asked to (because it's not relevant
// for later tables anyway).
homogenize_tables = m_orderings[ordering_idx].homogenize_tables;
}
for (int table_idx : BitsSetIn(homogenize_tables)) {
AddHomogenizedOrderingIfPossible(thd, reduced_ordering,
m_orderings[ordering_idx].used_at_end,
table_idx, reverse_canonical);
}
}
}
/**
Remove redundant elements using the functional dependencies that we have,
to give a more canonical form before homogenization. Note that we assume
here that every functional dependency holds, so this is not applicable
generally throughout the tree, only at the end (e.g. final ORDER BY).
This is called “Reduce Order” in the Simmen paper.
*/
Ordering LogicalOrderings::ReduceOrdering(Ordering ordering, bool all_fds,
Ordering::Elements tmp) const {
size_t reduced_length = 0;
for (size_t part_idx = 0; part_idx < ordering.size(); ++part_idx) {
if (ImpliedByEarlierElements(ordering.GetElements()[part_idx].item,
ordering.GetElements().prefix(part_idx),
all_fds)) {
// Delete this element.
} else {
tmp[reduced_length++] = ordering.GetElements()[part_idx];
}
}
return {tmp.prefix(reduced_length),
reduced_length > 0 ? ordering.GetKind() : Ordering::Kind::kEmpty};
}
/// Helper function for CreateHomogenizedOrderings().
void LogicalOrderings::AddHomogenizedOrderingIfPossible(
THD *thd, const Ordering &reduced_ordering, bool used_at_end, int table_idx,
Bounds_checked_array<pair<ItemHandle, ItemHandle>> reverse_canonical) {
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
const table_map available_tables = table_map{1} << table_idx;
int length = 0;
for (OrderElement element : reduced_ordering.GetElements()) {
if (IsSubset(m_items[element.item].item->used_tables(), available_tables)) {
// Already OK.
if (!ImpliedByEarlierElements(element.item, tmp.prefix(length),
/*all_fds=*/used_at_end)) {
tmp[length++] = element;
}
continue;
}
// Find all equivalent items.
ItemHandle canonical_item = m_items[element.item].canonical_item;
auto first = lower_bound(reverse_canonical.begin(), reverse_canonical.end(),
canonical_item,
[](const pair<ItemHandle, ItemHandle> &a,
ItemHandle b) { return a.first < b; });
auto last =
upper_bound(first, reverse_canonical.end(), canonical_item,
[](ItemHandle a, const pair<ItemHandle, ItemHandle> &b) {
return a < b.first;
});
assert(last - first >= 1);
bool found = false;
for (auto it = first; it != last; ++it) {
if (IsSubset(m_items[it->second].item->used_tables(), available_tables)) {
if (ImpliedByEarlierElements(it->second, tmp.prefix(length),
/*all_fds=*/used_at_end)) {
// Unneeded in the new order, so delete it.
// Similar to the reduction process above.
} else {
tmp[length].item = it->second;
tmp[length].direction = element.direction;
++length;
}
found = true;
break;
}
}
if (!found) {
// Not possible to homogenize this ordering.
return;
}
}
if (length > 0) {
if (reduced_ordering.GetKind() == Ordering::Kind::kGroup) {
// We've replaced some items, so we need to re-sort.
SortElements(tmp.prefix(length));
}
AddOrderingInternal(
thd, Ordering(tmp.prefix(length), reduced_ordering.GetKind()),
OrderingWithInfo::HOMOGENIZED, used_at_end,
/*homogenize_tables=*/0);
}
}
void LogicalOrderings::SortElements(Ordering::Elements elements) const {
assert(std::all_of(elements.cbegin(), elements.cend(), [](OrderElement e) {
return e.direction == ORDER_NOT_RELEVANT;
}));
sort(elements.begin(), elements.end(),
[this](const OrderElement &a, const OrderElement &b) {
return this->ItemBeforeInGroup(a, b);
});
}
ItemHandle LogicalOrderings::GetHandle(Item *item) {
for (size_t i = 1; i < m_items.size(); ++i) {
if (item == m_items[i].item ||
item->eq(m_items[i].item, /*binary_cmp=*/true)) {
return i;
}
}
m_items.push_back(ItemInfo{item, /*canonical_item=*/0});
return m_items.size() - 1;
}
/**
For a given ordering, check whether it ever has the hope of becoming an
interesting ordering. In its base form, this is a prefix check; if we
have an ordering (a,b) and an interesting order (a,b,c), it passes.
However, we add some slightly more lax heuristics in order to make the
graph a bit wider at build time (and thus require fewer FD applications at
runtime); namely, if there's a prefix mismatch but the item could be added
by some FD later (without the ordering becoming too long), we let it slide
and just skip that item.
E.g.: If we have an ordering (a,b) and an interesting order (a,x,b),
we first match a. x does not match b, but we check whether x is ever on the
right side of any FD (for instance because there might be an FD a → x).
If it is, we skip it and match b with b. There's an example of this in the
DoesNotStrictlyPruneOnPrefixes unit test.
Obviously, this leads to false positives, but that is fine; this is just
to prune down the amount of states in the NFSM. [Neu04] points out that
such pruning is pretty much essential for performance, and our experience
is the same.
There is one extra quirk; the prefix check needs to take equivalences into
account, or we would prune away orderings that could become interesting
after equivalences. We solve this by always mapping to an equivalence class
when doing the prefix comparison. There's an example of this in the
TwoEquivalences unit test.
*/
bool LogicalOrderings::CouldBecomeInterestingOrdering(
const Ordering &ordering) const {
for (OrderingWithInfo other_ordering : m_orderings) {
const Ordering interesting_ordering = other_ordering.ordering;
if (other_ordering.type != OrderingWithInfo::INTERESTING ||
interesting_ordering.size() < ordering.size()) {
continue;
}
// Groupings can never become orderings. Orderings can become groupings,
// but for simplicity, we require them to immediately become groupings then,
// or else be pruned away.
if (ordering.GetKind() != interesting_ordering.GetKind()) {
continue;
}
// Since groupings are ordered by item (actually canonical item;
// see RecanonicalizeGroupings(), ItemBeforeInGroup() and
// the GroupReordering unit test), we can use the same comparison
// for ordering-ordering and grouping-grouping comparisons.
bool match = true;
for (size_t i = 0, j = 0;
i < ordering.size() || j < interesting_ordering.size();) {
if (ordering.size() - i > interesting_ordering.size() - j) {
// We have excess items at the end, so give up.
match = false;
break;
}
const ItemHandle needed_item =
m_items[interesting_ordering.GetElements()[j].item].canonical_item;
if (i < ordering.size() &&
m_items[ordering.GetElements()[i].item].canonical_item ==
needed_item &&
ordering.GetElements()[i].direction ==
interesting_ordering.GetElements()[j].direction) {
// We have a matching item, so move both iterators along.
++i, ++j;
continue;
}
if (m_items[needed_item].can_be_added_by_fd) {
// We don't have this item, but it could be generated, so skip it.
++j;
continue;
}
// We don't have this item, and it can not be added later, so give up.
match = false;
break;
}
if (match) {
return true;
}
}
return false;
}
int LogicalOrderings::AddArtificialState(THD *thd, const Ordering &ordering) {
for (size_t i = 0; i < m_states.size(); ++i) {
if (m_states[i].satisfied_ordering == ordering) {
return i;
}
}
NFSMState state;
state.satisfied_ordering = ordering.Clone(thd->mem_root);
state.satisfied_ordering_idx = -1; // Irrelevant, but placate the compiler.
state.outgoing_edges.init(thd->mem_root);
state.type = NFSMState::ARTIFICIAL;
m_states.push_back(std::move(state));
return m_states.size() - 1;
}
void LogicalOrderings::AddEdge(THD *thd, int state_idx, int required_fd_idx,
const Ordering &ordering) {
NFSMEdge edge;
edge.required_fd_idx = required_fd_idx;
edge.state_idx = AddArtificialState(thd, ordering);
if (edge.state_idx == state_idx) {
// Don't add self-edges; they are already implicit.
return;
}
assert(std::find(m_states[state_idx].outgoing_edges.cbegin(),
m_states[state_idx].outgoing_edges.cend(),
edge) == m_states[state_idx].outgoing_edges.cend());
m_states[state_idx].outgoing_edges.push_back(edge);
}
bool LogicalOrderings::FunctionalDependencyApplies(
const FunctionalDependency &fd, const Ordering &ordering,
int *start_point) const {
assert(fd.type != FunctionalDependency::DECAY);
*start_point = -1;
for (ItemHandle head_item : fd.head) {
bool matched = false;
for (size_t i = 0; i < ordering.size(); ++i) {
if (ordering.GetElements()[i].item == head_item ||
(fd.type == FunctionalDependency::EQUIVALENCE &&
ordering.GetElements()[i].item == fd.tail)) {
*start_point = max<int>(*start_point, i);
matched = true;
break;
}
}
if (!matched) {
return false;
}
}
return true;
}
/**
Given an order O and a functional dependency FD: S → x where S
is a subset of O, create new orderings by inserting x into O at
different positions, and add those to the set of orderings if they
could become interesting (@see
LogicalOrderings::CouldBecomeInterestingOrdering(Ordering ordering)).
This operation is implemented as a class to avoid an excessively
long parameter list.
*/
class LogicalOrderings::OrderWithElementInserted final {
public:
OrderWithElementInserted &SetContext(LogicalOrderings *context) {
m_context = context;
return *this;
}
OrderWithElementInserted &SetStateIdx(int state_idx) {
m_state_idx = state_idx;
return *this;
}
OrderWithElementInserted &SetFdIdx(int fd_idx) {
m_fd_idx = fd_idx;
return *this;
}
OrderWithElementInserted &SetOldOrdering(Ordering old_ordering) {
m_old_ordering = old_ordering;
return *this;
}
OrderWithElementInserted &SetStartPoint(size_t start_point) {
m_start_point = start_point;
return *this;
}
OrderWithElementInserted &SetItemToAdd(ItemHandle item_to_add) {
m_item_to_add = item_to_add;
return *this;
}
OrderWithElementInserted &SetDirection(enum_order direction) {
m_direction = direction;
return *this;
}
/// Add any potentially interesting orders.
void AddPotentiallyInterestingOrders(THD *thd);
private:
/// The enclosing LogicalOrderings instance.
LogicalOrderings *m_context;
/// The originator state.
int m_state_idx;
/// The functional dependency with which we will extend m_old_ordering.
int m_fd_idx;
/// The ordering to be extended.
Ordering m_old_ordering;
/// The first position at which m_item_to_add. If ordering is needed,
/// this must be behind the last element of the FD head.
size_t m_start_point;
/// The item to add to the ordering.
ItemHandle m_item_to_add;
/// The desired direction of the extended ordering.
enum_order m_direction;
};
void LogicalOrderings::OrderWithElementInserted::
AddPotentiallyInterestingOrders(THD *thd) {
assert(m_direction == ORDER_NOT_RELEVANT ||
m_old_ordering.GetKind() != Ordering::Kind::kGroup);
if (static_cast<int>(m_old_ordering.size()) >=
m_context->m_longest_ordering) {
return;
}
for (size_t add_pos = m_start_point; add_pos <= m_old_ordering.size();
++add_pos) {
if (m_direction == ORDER_NOT_RELEVANT) {
// For groupings, only insert in the sorted sequence.
// (If we have found the right insertion spot, we immediately
// exit after this at the end of the loop.)
if (add_pos < m_old_ordering.size() &&
m_context->ItemHandleBeforeInGroup(
m_old_ordering.GetElements()[add_pos].item, m_item_to_add)) {
continue;
}
// For groupings, we just deduplicate right away.
// TODO(sgunders): When we get C++20, use operator<=> so that we
// can use a == b here instead of !(a < b) && !(b < a) as we do now.
if (add_pos < m_old_ordering.size() &&
!m_context->ItemHandleBeforeInGroup(
m_item_to_add, m_old_ordering.GetElements()[add_pos].item)) {
break;
}
}
OrderingElementsGuard tmp_guard(m_context, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
const Ordering::Kind kind =
m_old_ordering.GetKind() == Ordering::Kind::kEmpty
? Ordering::Kind::kOrder
: m_old_ordering.GetKind();
std::copy(m_old_ordering.GetElements().cbegin(),
m_old_ordering.GetElements().cbegin() + add_pos, tmp.begin());
tmp[add_pos].item = m_item_to_add;
tmp[add_pos].direction =
kind == Ordering::Kind::kOrder ? m_direction : ORDER_NOT_RELEVANT;
std::copy(m_old_ordering.GetElements().cbegin() + add_pos,
m_old_ordering.GetElements().cend(), tmp.begin() + add_pos + 1);
Ordering new_ordering{tmp.prefix(m_old_ordering.size() + 1), kind};
new_ordering.Deduplicate();
if (m_context->CouldBecomeInterestingOrdering(new_ordering)) {
// AddEdge() makes a deep copy of new_ordering, so reusing tmp is ok.
m_context->AddEdge(thd, m_state_idx, m_fd_idx, new_ordering);
}
if (m_direction == ORDER_NOT_RELEVANT) {
break;
}
}
}
void LogicalOrderings::BuildNFSM(THD *thd) {
// Add a state for each producible ordering.
for (size_t i = 0; i < m_orderings.size(); ++i) {
NFSMState state;
state.satisfied_ordering = m_orderings[i].ordering;
state.satisfied_ordering_idx = i;
state.outgoing_edges.init(thd->mem_root);
state.type = m_orderings[i].type == OrderingWithInfo::INTERESTING
? NFSMState::INTERESTING
: NFSMState::ARTIFICIAL;
m_states.push_back(std::move(state));
}
// Add an edge from the initial state to each producible ordering/grouping.
for (size_t i = 1; i < m_orderings.size(); ++i) {
if (m_orderings[i].ordering.GetKind() == Ordering::Kind::kGroup) {
// Not directly producible, but we've made an ordering out of it earlier.
continue;
}
NFSMEdge edge;
edge.required_fd_idx = INT_MIN + i;
edge.state_idx = i;
m_states[0].outgoing_edges.push_back(edge);
}
// Add edges from functional dependencies, in a breadth-first search
// (the array of m_states will expand as we go).
for (size_t state_idx = 0; state_idx < m_states.size(); ++state_idx) {
// Refuse to apply FDs for nondeterministic orderings other than possibly
// ordering -> grouping; ie., (a) can _not_ be satisfied by (a, rand()).
// This is to avoid evaluating such a nondeterministic function unexpectedly
// early, e.g. in GROUP BY when the user didn't expect it to be used in
// ORDER BY. (We still allow it on exact matches, though.See also comments
// on RAND_TABLE_BIT in SortAheadOrdering.)
const Ordering old_ordering = m_states[state_idx].satisfied_ordering;
const bool deterministic =
none_of(old_ordering.GetElements().begin(),
old_ordering.GetElements().end(), [this](OrderElement element) {
return Overlaps(m_items[element.item].item->used_tables(),
RAND_TABLE_BIT);
});
// Apply the special decay FD; first to convert it into a grouping or rollup
// (which we always allow, even for nondeterministic items),
// then to shorten the ordering.
switch (old_ordering.GetKind()) {
case Ordering::Kind::kOrder:
if (m_rollup) {
AddRollupFromOrder(thd, state_idx, old_ordering);
} else {
// We do not add rollups if the query block does not do a grouping
// with rollup.
AddGroupingFromOrder(thd, state_idx, old_ordering);
}
break;
case Ordering::Kind::kRollup:
assert(m_rollup);
AddGroupingFromRollup(thd, state_idx, old_ordering);
break;
default:
break;
}
if (!deterministic) {
continue;
}
if (old_ordering.GetKind() != Ordering::Kind::kGroup &&
old_ordering.size() > 1) {
AddEdge(thd, state_idx, /*required_fd_idx=*/0,
Ordering(old_ordering.GetElements().without_back(),
old_ordering.GetKind()));
}
if (m_states.size() >= kMaxNFSMStates) {
// Stop adding more states. We won't necessarily find the optimal query,
// but we'll keep all essential information, and not throw away any of the
// information we have already gathered (unless the DFSM gets too large,
// too; see ConvertNFSMToDFSM()).
break;
}
for (size_t fd_idx = 1; fd_idx < m_fds.size(); ++fd_idx) {
const FunctionalDependency &fd = m_fds[fd_idx];
int start_point;
if (!FunctionalDependencyApplies(fd, old_ordering, &start_point)) {
continue;
}
ItemHandle item_to_add = fd.tail;
// On a = b, try to replace a with b or b with a.
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
Ordering base_ordering;
if (fd.type == FunctionalDependency::EQUIVALENCE) {
std::copy(old_ordering.GetElements().cbegin(),
old_ordering.GetElements().cend(), tmp.begin());
ItemHandle other_item = fd.head[0];
if (tmp[start_point].item == item_to_add) {
// b already existed, so it's a we must add.
swap(item_to_add, other_item);
}
tmp[start_point].item = item_to_add; // Keep the direction.
Ordering new_ordering{tmp.prefix(old_ordering.size()),
old_ordering.GetKind()};
new_ordering.Deduplicate();
if (CouldBecomeInterestingOrdering(new_ordering)) {
AddEdge(thd, state_idx, fd_idx, new_ordering);
}
// Now we can add back the item we just replaced,
// at any point after this. E.g., if we had an order abc
// and applied b=d to get adc, we can add back b to get
// adbc or adcb. Also, we'll fall through afterwards
// to _not_ replacing but just adding d, e.g. abdc and abcd.
// So fall through.
base_ordering = new_ordering;
item_to_add = other_item;
} else {
base_ordering = old_ordering;
}
auto extended_order = [&]() {
return OrderWithElementInserted()
.SetContext(this)
.SetStateIdx(state_idx)
.SetFdIdx(fd_idx)
.SetOldOrdering(base_ordering)
.SetItemToAdd(item_to_add);
};
// On S -> b, try to add b everywhere after the last element of S.
switch (base_ordering.GetKind()) {
case Ordering::Kind::kGroup:
case Ordering::Kind::kRollup:
if (m_items[m_items[item_to_add].canonical_item].used_in_grouping) {
extended_order()
// For GROUP BY without ROLLUP, any ordering on the
// grouping terms T1..TN will work, as it ensures that all
// rows with the same values for those grouping terms will
// appear consecutively. But the mechanism for generating
// the ROLLUP rows also requires the rows to be sorted on
// T1..TN. Therefore we cannot reorder the terms in
// 'ordering' according to the GROUP BY sequence if we
// have ROLLUP. (See also bug #34670701.)
.SetStartPoint(base_ordering.GetKind() ==
Ordering::Kind::kRollup
? start_point + 1
: 0)
.SetDirection(ORDER_NOT_RELEVANT)
.AddPotentiallyInterestingOrders(thd);
}
break;
default:
// NOTE: We could have neither add_asc nor add_desc, if the item is
// used only in groupings. If so, we don't add it at all, before we
// convert it to a grouping.
bool add_asc = m_items[m_items[item_to_add].canonical_item].used_asc;
bool add_desc =
m_items[m_items[item_to_add].canonical_item].used_desc;
if (add_asc) {
extended_order()
.SetStartPoint(start_point + 1)
.SetDirection(ORDER_ASC)
.AddPotentiallyInterestingOrders(thd);
}
if (add_desc) {
extended_order()
.SetStartPoint(start_point + 1)
.SetDirection(ORDER_DESC)
.AddPotentiallyInterestingOrders(thd);
}
}
}
}
}
void LogicalOrderings::AddGroupingFromOrder(THD *thd, int state_idx,
const Ordering &ordering) {
assert(ordering.GetKind() == Ordering::Kind::kOrder);
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
std::copy(ordering.GetElements().cbegin(), ordering.GetElements().cend(),
tmp.begin());
for (size_t i = 0; i < ordering.size(); ++i) {
tmp[i].direction = ORDER_NOT_RELEVANT;
if (!m_items[m_items[tmp[i].item].canonical_item].used_in_grouping) {
// Pruned away.
return;
}
}
SortElements(tmp.prefix(ordering.size()));
AddEdge(thd, state_idx, /*required_fd_idx=*/0,
Ordering(tmp.prefix(ordering.size()), Ordering::Kind::kGroup));
}
void LogicalOrderings::AddGroupingFromRollup(THD *thd, int state_idx,
const Ordering &ordering) {
assert(ordering.GetKind() == Ordering::Kind::kRollup);
assert(std::all_of(
ordering.GetElements().cbegin(), ordering.GetElements().cend(),
[this](const OrderElement &elem) {
// Not pruned away.
return m_items[m_items[elem.item].canonical_item].used_in_grouping;
}));
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
std::copy(ordering.GetElements().cbegin(), ordering.GetElements().cend(),
tmp.begin());
SortElements(tmp.prefix(ordering.size()));
AddEdge(thd, state_idx, /*required_fd_idx=*/0,
Ordering(tmp.prefix(ordering.size()), Ordering::Kind::kGroup));
}
void LogicalOrderings::AddRollupFromOrder(THD *thd, int state_idx,
const Ordering &ordering) {
assert(m_rollup);
assert(ordering.GetKind() == Ordering::Kind::kOrder);
OrderingElementsGuard tmp_guard(this, thd->mem_root);
Ordering::Elements &tmp = tmp_guard.Get();
std::copy(ordering.GetElements().cbegin(), ordering.GetElements().cend(),
tmp.begin());
for (size_t i = 0; i < ordering.size(); ++i) {
tmp[i].direction = ORDER_NOT_RELEVANT;
if (!m_items[m_items[tmp[i].item].canonical_item].used_in_grouping) {
// Pruned away.
return;
}
}
Ordering rollup =
Ordering(tmp.prefix(ordering.size()), Ordering::Kind::kRollup);
AddEdge(thd, state_idx, /*required_fd_idx=*/0, rollup);
}
// Clang vectorizes the inner loop below with -O2, but GCC does not. Enable
// vectorization with GCC too, since this loop is a bottleneck when there are
// many NFSM states.
#if defined(NDEBUG) && defined(__GNUC__) && !defined(__clang__)
#pragma GCC push_options
#pragma GCC optimize("tree-loop-vectorize")
#endif
// Calculates the transitive closure of the reachability graph.
static void FindAllReachable(Bounds_checked_array<bool *> reachable) {
const int N = reachable.size();
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
if (reachable[i][k]) {
for (int j = 0; j < N; ++j) {
// If there are edges i -> k -> j, add an edge i -> j.
reachable[i][j] |= reachable[k][j];
}
}
}
}
}
#if defined(NDEBUG) && defined(__GNUC__) && !defined(__clang__)
#pragma GCC pop_options
#endif
/**
Try to prune away irrelevant nodes from the NFSM; it is worth spending some
time on this, since the number of NFSM states can explode the size of the
DFSM. Like with PruneFDs(), we don't do any of the pruning described in
[Neu04]; it is unclear exactly what is meant, but it would seem the state
removal/merging there is either underdefined or simply does not do anything
except remove trivially bad nodes (those that cannot reach anything).
This also sets the can_reach_interesting_order bitmap on each NFSM node.
*/
void LogicalOrderings::PruneNFSM(THD *thd) {
// Find the transitive closure of the NFSM; ie., whether state A can reach
// state B, either directly or through some other state (possibly many).
// We use the standard Floyd-Warshall algorithm, which is O(n³); if n gets
// to be very large, we can flip the direction of all edges and use
// Dijkstra from each interesting order instead (since we're only interested
// in reachability to interesting orders, and our graph is quite sparse),
// but Floyd-Warshall is simple and has a low constant factor.
const int N = m_states.size();
// Create a two-dimensional array with N elements in each dimension. Each line
// starts at an eight byte word boundary, as that seems to improve the
// performance of the inner loop in Floyd-Warshall. reachable[i][j] == true
// means that state j is reachable from state i.
const size_t N_aligned = ALIGN_SIZE(m_states.size());
auto reachable = Bounds_checked_array<bool *>::Alloc(thd->mem_root, N);
auto reachable_buffer =
Bounds_checked_array<bool>::Alloc(thd->mem_root, N * N_aligned);
for (int i = 0; i < N; ++i) {
reachable[i] = reachable_buffer.data() + i * N_aligned;
}
// We have multiple pruning techniques, all heuristic in nature.
// If one removes something, it may help to run the others again,
// so keep running until we've stabilized.
bool pruned_anything;
do {
pruned_anything = false;
fill(reachable_buffer.begin(), reachable_buffer.end(), false);
for (int i = 0; i < N; ++i) {
if (m_states[i].type == NFSMState::DELETED) {
continue;
}
// There's always an implicit self-edge.
reachable[i][i] = true;
for (const NFSMEdge &edge : m_states[i].outgoing_edges) {
reachable[i][edge.state_idx] = true;
}
}
FindAllReachable(reachable);
// Now prune away artificial m_states that cannot reach any
// interesting orders, and m_states that are not reachable from
// the initial node (the latter can only happen as the result
// of other prunings).
for (int i = 1; i < N; ++i) {
if (m_states[i].type != NFSMState::ARTIFICIAL) {
continue;
}
if (!reachable[0][i]) {
m_states[i].type = NFSMState::DELETED;
pruned_anything = true;
continue;
}
bool can_reach_interesting = false;
for (int j = 1; j < static_cast<int>(m_orderings.size()); ++j) {
if (reachable[i][j] && m_states[j].type == NFSMState::INTERESTING) {
can_reach_interesting = true;
break;
}
}
if (!can_reach_interesting) {
m_states[i].type = NFSMState::DELETED;
pruned_anything = true;
}
}
// For each producing order, remove edges to m_states that cannot
// reach any _other_ interesting orders. This often helps dislodging
// such m_states from the graph as a whole, removing them in some later
// step. This supersedes the same-destination merging step from [Neu04].
for (size_t i = 1; i < m_orderings.size(); ++i) {
NFSMState &state = m_states[i];
for (size_t j = 0; j < state.outgoing_edges.size(); ++j) {
const int next_state_idx = state.outgoing_edges[j].state_idx;
bool can_reach_other_interesting = false;
for (size_t k = 1; k < m_orderings.size(); ++k) {
if (k != i && m_states[k].type == NFSMState::INTERESTING &&
reachable[next_state_idx][k]) {
can_reach_other_interesting = true;
break;
}
}
if (!can_reach_other_interesting) {
// Remove this edge.
state.outgoing_edges[j] =
state.outgoing_edges[state.outgoing_edges.size() - 1];
state.outgoing_edges.resize(state.outgoing_edges.size() - 1);
pruned_anything = true;
}
}
}
// Remove any edges to deleted m_states.
for (int i = 0; i < N; ++i) {
NFSMState &state = m_states[i];
if (state.type == NFSMState::DELETED) {
continue;
}
int num_kept = 0;
for (const NFSMEdge &edge : state.outgoing_edges) {
if (edge.state(this)->type != NFSMState::DELETED) {
state.outgoing_edges[num_kept++] = edge;
}
}
state.outgoing_edges.resize(num_kept);
}
} while (pruned_anything);
// Set the bitmask of what each node can reach.
for (size_t order_idx = 0; order_idx < m_orderings.size(); ++order_idx) {
if (m_orderings[order_idx].type != OrderingWithInfo::INTERESTING ||
order_idx >= kMaxSupportedOrderings) {
continue;
}
for (int i = 0; i < N; ++i) {
if (m_states[i].type == NFSMState::DELETED) {
continue;
}
if (reachable[i][order_idx]) {
m_states[i].can_reach_interesting_order.set(order_idx);
}
}
}
}
bool LogicalOrderings::AlwaysActiveFD(int fd_idx) {
// Note: Includes ϵ-edges.
return fd_idx >= 0 && m_fds[fd_idx].always_active;
}
void LogicalOrderings::FinalizeDFSMState(THD *thd, int state_idx) {
LogicalOrderings::DFSMState &state = m_dfsm_states[state_idx];
for (int nfsm_state_idx : state.nfsm_states) {
int ordering_idx = m_states[nfsm_state_idx].satisfied_ordering_idx;
if (m_states[nfsm_state_idx].type == NFSMState::INTERESTING &&
ordering_idx < kMaxSupportedOrderings &&
m_orderings[ordering_idx].type == OrderingWithInfo::INTERESTING) {
state.follows_interesting_order.set(ordering_idx);
}
state.can_reach_interesting_order |=
m_states[nfsm_state_idx].can_reach_interesting_order;
}
state.next_state =
Bounds_checked_array<int>::Alloc(thd->mem_root, m_fds.size());
fill(state.next_state.begin(), state.next_state.end(), state_idx);
}
void LogicalOrderings::ExpandThroughAlwaysActiveFDs(
Mem_root_array<int> *nfsm_states, int *generation,
int extra_allowed_fd_idx) {
++*generation; // Effectively clear the “seen” flag in all NFSM states.
for (size_t i = 0; i < nfsm_states->size(); ++i) {
const NFSMState &state = m_states[(*nfsm_states)[i]];
for (const NFSMEdge &edge : state.outgoing_edges) {
if ((AlwaysActiveFD(edge.required_fd_idx) ||
edge.required_fd_idx == extra_allowed_fd_idx) &&
m_states[edge.state_idx].seen != *generation) {
nfsm_states->push_back(edge.state_idx);
m_states[edge.state_idx].seen = *generation;
}
}
}
}
/**
From the NFSM, convert an equivalent DFSM.
This is by means of the so-called powerset conversion, which is more commonly
used to convert NFAs to DFAs. (The only real difference is that FAs have
accepting states, while our FSM instead needs to store information about
constituent interesting order states.)
The powerset algorithm works by creating DFSM states that represent sets of
NFSM states we could be in. E.g., if we have a state (a) and an FD {} → x can
lead to new states () (ϵ-edge), (a) (implicit self-edge), (x), (ax), (xa),
then we create a single new DFSM state that represent all those five states,
and an {} → x edge from {(a)} to that new state. When creating edges from
such superstates, we need to follow that FD from _all_ of them, so the list
of constituent states can be fairly large.
In theory, we could get 2^n DFSM states from n NFSM states, but in practice,
we get fewer since our orderings generally only increase, not decrease.
We only generate DFSM states by following FDs from the initial NFSM state;
we don't create states eagerly for all 2^n possibilities.
When creating DFSM states, we always include states that can be reached by
means of always-active FDs. The ϵ edge (drop the last element from the
ordering) is always active, and the client can also mark others as such.
This means we get fewer DFSM states and fewer FDs to follow. See
FunctionalDependency::always_active.
*/
void LogicalOrderings::ConvertNFSMToDFSM(THD *thd) {
// See NFSMState::seen.
int generation = 0;
// Keep track of which sets of NFSM states we've already seen, and which DFSM
// state we created for that set.
mem_root_unordered_set<int, DFSMStateHash<DFSMState>,
DFSMStateEqual<DFSMState>>
constructed_states(thd->mem_root,
DFSMStateHash<DFSMState>{&m_dfsm_states},
DFSMStateEqual<DFSMState>{&m_dfsm_states});
// Create the initial DFSM state. It consists of everything in the initial
// NFSM state, and everything reachable from it with only always-active FDs.
DFSMState initial;
initial.nfsm_states.init(thd->mem_root);
initial.nfsm_states.push_back(0);
ExpandThroughAlwaysActiveFDs(&initial.nfsm_states, &generation,
/*extra_allowed_fd_idx=*/0);
m_dfsm_states.push_back(std::move(initial));
constructed_states.insert(0);
FinalizeDFSMState(thd, /*state_idx=*/0);
// Reachability information set by FinalizeDFSMState() will include those
// that can be reached through SetOrder() nodes, so it's misleading.
// Clear it; this isn't 100% active if interesting orderings can be reached
// through FDs only, but it will ever cause too little pruning, not too much.
m_dfsm_states[0].can_reach_interesting_order.reset();
// Used in iteration below.
Mem_root_array<int> nfsm_states(thd->mem_root);
Mem_root_array<NFSMEdge> nfsm_edges(thd->mem_root);
for (size_t dfsm_state_idx = 0; dfsm_state_idx < m_dfsm_states.size();
++dfsm_state_idx) {
// Take the union of all outgoing edges from the constituent NFSM m_states,
// ignoring ϵ-edges and always active FDs, since we have special handling of
// them below.
nfsm_edges.clear();
for (int nfsm_state_idx : m_dfsm_states[dfsm_state_idx].nfsm_states) {
assert(m_states[nfsm_state_idx].satisfied_ordering.GetKind() !=
Ordering::Kind::kRollup ||
m_rollup);
for (const NFSMEdge &edge : m_states[nfsm_state_idx].outgoing_edges) {
if (!AlwaysActiveFD(edge.required_fd_idx)) {
nfsm_edges.push_back(edge);
}
}
}
if (m_dfsm_states.size() >= kMaxDFSMStates) {
// Stop creating new states, causing us to end fairly soon. Note that
// since the paths representing explicit sorts are put first, they will
// never be lost unless kMaxDFSMStates is set extremely low.
continue;
}
{
// Sort and deduplicate the edges. Note that we sort on FD first,
// since we'll be grouping on that when creating new m_states.
sort(nfsm_edges.begin(), nfsm_edges.end(),
[](const NFSMEdge &a, const NFSMEdge &b) {
return make_pair(a.required_fd_idx, a.state_idx) <
make_pair(b.required_fd_idx, b.state_idx);
});
auto new_end =
unique(nfsm_edges.begin(), nfsm_edges.end(),
[](const NFSMEdge &a, const NFSMEdge &b) {
return make_pair(a.required_fd_idx, a.state_idx) ==
make_pair(b.required_fd_idx, b.state_idx);
});
nfsm_edges.resize(distance(nfsm_edges.begin(), new_end));
}
// For each relevant FD, find out which set of m_states we could reach.
m_dfsm_states[dfsm_state_idx].outgoing_edges.init(thd->mem_root);
nfsm_states.clear();
for (size_t edge_idx = 0; edge_idx < nfsm_edges.size(); ++edge_idx) {
nfsm_states.push_back(nfsm_edges[edge_idx].state_idx);
// Is this the last state in the group? If not, keep iterating.
if (edge_idx != nfsm_edges.size() - 1 &&
nfsm_edges[edge_idx].required_fd_idx ==
nfsm_edges[edge_idx + 1].required_fd_idx) {
continue;
}
// Add the implicit self-edges.
for (int nfsm_state_idx : m_dfsm_states[dfsm_state_idx].nfsm_states) {
if (nfsm_state_idx != 0) {
nfsm_states.push_back(nfsm_state_idx);
}
}
// Expand the set to contain any ϵ-edges and always active FDs,
// in a breadth-first manner. Note that now, we might see new
// edges for the same FD, so we should follow those as well.
ExpandThroughAlwaysActiveFDs(&nfsm_states, &generation,
nfsm_edges[edge_idx].required_fd_idx);
// Canonicalize: Sort and deduplicate.
sort(nfsm_states.begin(), nfsm_states.end());
auto new_end = unique(nfsm_states.begin(), nfsm_states.end());
nfsm_states.resize(distance(nfsm_states.begin(), new_end));
// Add a new DFSM state for the NFSM states we've collected.
int target_dfsm_state_idx = m_dfsm_states.size();
m_dfsm_states.push_back(DFSMState{});
m_dfsm_states.back().nfsm_states = std::move(nfsm_states);
// See if there is an existing DFSM state that matches the set of
// NFSM states we've collected.
if (auto [place, inserted] =
constructed_states.insert(target_dfsm_state_idx);
inserted) {
// There's none, so create a new one. The type doesn't really matter,
// except for printing out the graph.
FinalizeDFSMState(thd, target_dfsm_state_idx);
} else {
// Already had a DFSM state for this set of NFSM states. Remove the
// newly added duplicate and use the original one.
target_dfsm_state_idx = *place;
// Allow reuse of the memory in the next iteration.
nfsm_states = std::move(m_dfsm_states.back().nfsm_states);
m_dfsm_states.pop_back();
}
// Finally, add an edge in the DFSM. Ignore self-edges; they are implicit.
if (static_cast<size_t>(target_dfsm_state_idx) != dfsm_state_idx) {
DFSMEdge edge;
edge.required_fd_idx = nfsm_edges[edge_idx].required_fd_idx;
edge.state_idx = target_dfsm_state_idx;
m_dfsm_edges.push_back(edge);
DFSMState &dfsm_state = m_dfsm_states[dfsm_state_idx];
dfsm_state.outgoing_edges.push_back(m_dfsm_edges.size() - 1);
if (edge.required_fd_idx >= 0) {
dfsm_state.next_state[edge.required_fd_idx] = target_dfsm_state_idx;
if (edge.required_fd_idx >= 1 &&
edge.required_fd_idx <= kMaxSupportedFDs) {
dfsm_state.can_use_fd.set(edge.required_fd_idx - 1);
}
}
}
// Prepare for the next group.
nfsm_states.clear();
}
}
}
void LogicalOrderings::FindInitialStatesForOrdering() {
// Find all constructor edges from the initial state, and use them
// to populate the table.
for (int outgoing_edge_idx : m_dfsm_states[0].outgoing_edges) {
const DFSMEdge &edge = m_dfsm_edges[outgoing_edge_idx];
if (edge.required_fd_idx < 0) {
const int ordering_idx = edge.required_fd_idx - INT_MIN;
m_orderings[ordering_idx].state_idx = edge.state_idx;
}
}
}
string LogicalOrderings::PrintOrdering(const Ordering &ordering) const {
const bool is_grouping = ordering.GetKind() == Ordering::Kind::kGroup;
string ret = is_grouping ? "{" : "(";
if (ordering.GetKind() == Ordering::Kind::kRollup) {
ret += "rollup: ";
}
for (size_t i = 0; i < ordering.size(); ++i) {
if (i != 0) ret += ", ";
ret += ItemToString(m_items[ordering.GetElements()[i].item].item);
if (ordering.GetElements()[i].direction == ORDER_ASC) {
ret += " ASC";
} else if (ordering.GetElements()[i].direction == ORDER_DESC) {
ret += " DESC";
}
}
ret += is_grouping ? '}' : ')';
return ret;
}
string LogicalOrderings::PrintFunctionalDependency(
const FunctionalDependency &fd, bool html) const {
switch (fd.type) {
case FunctionalDependency::DECAY:
if (html) {
return "ε";
} else {
return "eps";
}
case FunctionalDependency::EQUIVALENCE:
return ItemToString(m_items[fd.head[0]].item) + "=" +
ItemToString(m_items[fd.tail].item);
case FunctionalDependency::FD: {
string ret = "{";
for (size_t i = 0; i < fd.head.size(); ++i) {
if (i != 0) {
ret += ", ";
}
ret += ItemToString(m_items[fd.head[i]].item);
}
if (html) {
ret += "} → ";
} else {
ret += "} -> ";
}
ret += ItemToString(m_items[fd.tail].item);
return ret;
}
}
assert(false);
return "";
}
void LogicalOrderings::PrintFunctionalDependencies(string *trace) {
if (m_fds.size() <= 1) {
*trace += "\nNo functional dependencies (after pruning).\n\n";
} else {
*trace += "\nFunctional dependencies (after pruning):\n";
for (size_t fd_idx = 1; fd_idx < m_fds.size(); ++fd_idx) {
*trace +=
" - " + PrintFunctionalDependency(m_fds[fd_idx], /*html=*/false);
if (m_fds[fd_idx].always_active) {
*trace += " [always active]";
}
*trace += "\n";
}
*trace += "\n";
}
}
void LogicalOrderings::PrintInterestingOrders(string *trace) {
*trace += "Interesting orders:\n";
for (size_t order_idx = 0; order_idx < m_orderings.size(); ++order_idx) {
const OrderingWithInfo &ordering = m_orderings[order_idx];
if (order_idx == 0 && ordering.type == OrderingWithInfo::UNINTERESTING) {
continue;
}
*trace += StringPrintf(" - %zu: ", order_idx);
bool first = true;
switch (ordering.ordering.GetKind()) {
case Ordering::Kind::kRollup:
*trace += "rollup ";
break;
case Ordering::Kind::kGroup:
*trace += "group ";
break;
default:
break;
}
for (OrderElement element : ordering.ordering.GetElements()) {
if (!first) {
*trace += ", ";
}
first = false;
*trace += ItemToString(m_items[element.item].item);
if (element.direction == ORDER_ASC) {
*trace += " ASC";
} else if (element.direction == ORDER_DESC) {
*trace += " DESC";
}
}
if (ordering.ordering.GetElements().empty()) {
*trace += "()";
}
if (ordering.type == OrderingWithInfo::HOMOGENIZED) {
*trace += " [homogenized from other ordering]";
} else if (ordering.type == OrderingWithInfo::UNINTERESTING) {
*trace += " [support order]";
}
*trace += "\n";
}
*trace += "\n";
}
void LogicalOrderings::PrintNFSMDottyGraph(string *trace) const {
*trace += "digraph G {\n";
for (size_t state_idx = 0; state_idx < m_states.size(); ++state_idx) {
const NFSMState &state = m_states[state_idx];
if (state.type == NFSMState::DELETED) {
continue;
}
// We're printing the NFSM.
*trace += StringPrintf(" s%zu [label=\"%s\"", state_idx,
PrintOrdering(state.satisfied_ordering).c_str());
if (state.type == NFSMState::INTERESTING) {
*trace += ", peripheries=2";
}
*trace += "]\n";
for (const NFSMEdge &edge : state.outgoing_edges) {
if (edge.required_fd_idx < 0) {
// Pseudo-edge without a FD (from initial state only).
*trace +=
StringPrintf(" s%zu -> s%d [label=\"ordering %d\"]\n", state_idx,
edge.state_idx, edge.required_fd_idx - INT_MIN);
} else {
const FunctionalDependency *fd = edge.required_fd(this);
*trace += StringPrintf(
" s%zu -> s%d [label=\"%s\"]\n", state_idx, edge.state_idx,
PrintFunctionalDependency(*fd, /*html=*/true).c_str());
}
}
}
*trace += "}\n";
}
void LogicalOrderings::PrintDFSMDottyGraph(string *trace) const {
*trace += "digraph G {\n";
for (size_t state_idx = 0; state_idx < m_dfsm_states.size(); ++state_idx) {
const DFSMState &state = m_dfsm_states[state_idx];
*trace += StringPrintf(" s%zu [label=< ", state_idx);
bool any_interesting = false;
for (size_t i = 0; i < state.nfsm_states.size(); ++i) {
const NFSMState &nsfm_state = m_states[state.nfsm_states[i]];
if (i != 0) {
*trace += ", ";
}
if (nsfm_state.type == NFSMState::INTERESTING) {
any_interesting = true;
*trace += "<b>";
}
*trace += PrintOrdering(nsfm_state.satisfied_ordering);
if (nsfm_state.type == NFSMState::INTERESTING) {
*trace += "</b>";
}
}
*trace += " >";
if (any_interesting) {
*trace += ", peripheries=2";
}
*trace += "]\n";
for (size_t edge_idx : state.outgoing_edges) {
const DFSMEdge &edge = m_dfsm_edges[edge_idx];
if (edge.required_fd_idx < 0) {
// Pseudo-edge without a FD (from initial state only).
*trace +=
StringPrintf(" s%zu -> s%d [label=\"ordering %d\"]\n", state_idx,
edge.state_idx, edge.required_fd_idx - INT_MIN);
} else {
const FunctionalDependency *fd = edge.required_fd(this);
*trace += StringPrintf(
" s%zu -> s%d [label=\"%s\"]\n", state_idx, edge.state_idx,
PrintFunctionalDependency(*fd, /*html=*/true).c_str());
}
}
}
*trace += "}\n";
}
|