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
|
/* Copyright (c) 2016, 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 sql/histograms/histogram.cc
Histogram base class (implementation).
*/
#include "sql/histograms/histogram.h" // Histogram, Histogram_comparator
#include <sys/types.h>
#include <algorithm>
#include <map>
#include <memory> // std::unique_ptr
#include <new>
#include <random>
#include <string>
#include <vector>
#include "base64.h" // base64_*
#include "decimal.h" // *2decimal
#include "field_types.h" // enum_field_types
#include "lex_string.h"
#include "m_ctype.h"
#include "my_alloc.h"
#include "my_bitmap.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sys.h" // my_micro_time, get_charset
#include "my_systime.h"
#include "my_time.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "scope_guard.h" // create_scope_guard
#include "sql-common/json_dom.h" // Json_*
#include "sql/auth/auth_common.h"
#include "sql/create_field.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/dd.h"
#include "sql/dd/string_type.h"
#include "sql/dd/types/column.h"
#include "sql/dd/types/column_statistics.h"
#include "sql/dd/types/table.h" // dd::Table
#include "sql/debug_sync.h"
#include "sql/error_handler.h" // Internal_error_handler
#include "sql/handler.h"
#include "sql/histograms/equi_height.h" // Equi_height<T>
#include "sql/histograms/singleton.h" // Singleton<T>
#include "sql/histograms/value_map.h" // Value_map
#include "sql/item.h"
#include "sql/item_json_func.h" // parse_json
#include "sql/key.h"
#include "sql/mdl.h" // MDL_request
#include "sql/my_decimal.h"
#include "sql/psi_memory_key.h" // key_memory_histograms
#include "sql/sql_base.h" // open_and_lock_tables,
#include "sql/sql_bitmap.h"
// close_thread_tables
#include "sql/sql_class.h" // make_lex_string_root
#include "sql/sql_const.h"
#include "sql/sql_time.h" // str_to_time
#include "sql/strfunc.h" // find_type2, find_set
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h" // trans_commit_stmt, trans_rollback_stmt
#include "sql/tztime.h" // my_tz_UTC
#include "sql_string.h" // String
#include "template_utils.h"
struct TYPELIB;
namespace histograms {
// Same as MAX_NUMBER_OF_HISTOGRAM_BUCKETS defined in sql_yacc.yy
static constexpr int MAX_NUMBER_OF_HISTOGRAM_BUCKETS = 1024;
/*
This type represents a instrumented map of value maps, indexed by field
number.
TODO: convert to simple datatype since dynamic containers should not be
used for fixed collection.
*/
using value_map_collection = std::map<
uint16, std::unique_ptr<histograms::Value_map_base>, std::less<uint16>,
Histogram_key_allocator<
std::pair<const uint16, std::unique_ptr<histograms::Value_map_base>>>>;
static std::map<const Value_map_type, const std::string> value_map_type_to_str =
{{Value_map_type::DATETIME, "datetime"}, {Value_map_type::DATE, "date"},
{Value_map_type::TIME, "time"}, {Value_map_type::INT, "int"},
{Value_map_type::UINT, "uint"}, {Value_map_type::DOUBLE, "double"},
{Value_map_type::DECIMAL, "decimal"}, {Value_map_type::STRING, "string"},
{Value_map_type::ENUM, "enum"}, {Value_map_type::SET, "set"}};
void *Histogram_psi_key_alloc::operator()(size_t s) const {
return my_malloc(key_memory_histograms, s, MYF(MY_WME | ME_FATALERROR));
}
/**
Convert from enum_field_types to Value_map_type.
@param field_type the field type
@param is_unsigned whether the field type is unsigned or not. This is only
considered if the field type is LONGLONG
@return A Value_map_type. May be INVALID if the Value_map does not support
the field type.
*/
static Value_map_type field_type_to_value_map_type(
const enum_field_types field_type, const bool is_unsigned) {
switch (field_type) {
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL:
return Value_map_type::DECIMAL;
case MYSQL_TYPE_BOOL:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_BIT:
return Value_map_type::INT;
case MYSQL_TYPE_ENUM:
return Value_map_type::ENUM;
case MYSQL_TYPE_SET:
return Value_map_type::SET;
case MYSQL_TYPE_LONGLONG:
return is_unsigned ? Value_map_type::UINT : Value_map_type::INT;
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
return Value_map_type::DOUBLE;
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_TIME2:
return Value_map_type::TIME;
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE:
return Value_map_type::DATE;
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_DATETIME2:
return Value_map_type::DATETIME;
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VARCHAR:
return Value_map_type::STRING;
case MYSQL_TYPE_JSON:
case MYSQL_TYPE_GEOMETRY:
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_INVALID:
default:
return Value_map_type::INVALID;
}
// All cases should be handled, so this should not be hit.
/* purecov: begin inspected */
assert(false);
return Value_map_type::INVALID;
/* purecov: end */
}
/**
Get the Value_map_type from a Field object.
This effectively looks at the real_type() of a Field, and converts this to
a Value_map_type
@param field The field to convert from
@return A Value_map_type. May be INVALID if the Value_map does not support
the field type.
*/
static Value_map_type field_type_to_value_map_type(const Field *field) {
bool is_unsigned = false;
if (field->real_type() == MYSQL_TYPE_LONGLONG) {
/*
For most integer types, the Value_map_type will be INT (int64). This type
will not cover the entire value range for the SQL data type UNSIGNED
BIGINT, so we need to distinguish between SIGNED BIGINT and UNSIGNED
BIGINT so that we can switch the Value_map_type to UINT (uint64).
*/
is_unsigned = field->is_unsigned();
}
return field_type_to_value_map_type(field->real_type(), is_unsigned);
}
void Error_context::report_global(Message err_code) {
assert(err_code == Message::JSON_NUM_BUCKETS_MORE_THAN_SPECIFIED ||
err_code == Message::JSON_IMPOSSIBLE_EMPTY_EQUI_HEIGHT ||
err_code == Message::JSON_INVALID_NULL_VALUES_FRACTION ||
err_code == Message::JSON_INVALID_TOTAL_FREQUENCY);
if (m_results == nullptr) return;
Json_path path(1);
String str;
path.to_string(&str);
m_results->emplace(to_string(str), err_code);
}
void Error_context::report_missing_attribute(const std::string &name) {
// In histogram json, attributes are always top-level.
if (m_results == nullptr) return;
Json_path path(1);
Json_path_leg leg(name);
path.append(leg);
String str;
path.to_string(&str);
m_results->emplace(to_string(str), Message::JSON_MISSING_ATTRIBUTE);
}
void Error_context::report_node(const Json_dom *dom, Message err_code) {
assert(!(err_code == Message::JSON_INVALID_NULL_VALUES_FRACTION ||
err_code == Message::JSON_INVALID_TOTAL_FREQUENCY ||
err_code == Message::JSON_NUM_BUCKETS_MORE_THAN_SPECIFIED ||
err_code == Message::JSON_IMPOSSIBLE_EMPTY_EQUI_HEIGHT ||
err_code == Message::JSON_MISSING_ATTRIBUTE));
if (m_results == nullptr) return;
String str;
dom->get_location().to_string(&str);
m_results->emplace(to_string(str), err_code);
}
/// RAII class to trap lower-level errors.
class Histogram_error_handler : public Internal_error_handler {
public:
Histogram_error_handler(THD *thd)
: Internal_error_handler{}, m_thd(thd), m_has_error{false} {
m_thd->push_internal_handler(this);
}
~Histogram_error_handler() override { m_thd->pop_internal_handler(); }
/// @return true if the condition is handled
bool handle_condition(THD *, uint, const char *,
Sql_condition::enum_severity_level *,
const char *) override {
m_has_error = true;
return true;
}
bool has_error() const { return m_has_error; }
private:
THD *m_thd;
bool m_has_error;
};
/**
Helper function for check_value().
It uses Field::store() on the actual Field that the histogram belongs to in
order to test if the value is in the field definition domain.
*/
static type_conversion_status check_value_aux(Field *field, const double *nr) {
return field->store(*nr);
}
static type_conversion_status check_value_aux(Field *field, const String *str) {
return field->store(str->ptr(), str->length(), str->charset());
}
static type_conversion_status check_value_aux(Field *field,
const longlong *nr) {
return field->store(*nr, false);
}
static type_conversion_status check_value_aux(Field *field,
const ulonglong *nr) {
return field->store(*nr, true);
}
// Field::store_time() should be updated to use a const pointer. We assume that
// the input value is not modified.
static type_conversion_status check_value_aux(Field *field, MYSQL_TIME *ltime) {
return field->store_time(ltime);
}
static type_conversion_status check_value_aux(Field *field,
const my_decimal *mdec) {
return field->store_decimal(mdec);
}
template <typename T>
bool Error_context::check_value(T *v) {
if (m_thd) {
Histogram_error_handler error_handler(m_thd);
return (m_field &&
check_value_aux(m_field, v) != type_conversion_status::TYPE_OK) ||
error_handler.has_error();
}
return false;
}
// Explicit template instantiations.
template bool Error_context::check_value(double *);
template bool Error_context::check_value(String *);
template bool Error_context::check_value(longlong *);
template bool Error_context::check_value(ulonglong *);
template bool Error_context::check_value(MYSQL_TIME *);
template bool Error_context::check_value(my_decimal *);
/**
Lock a column statistic MDL key for writing (exclusive lock).
@param thd thread handle
@param mdl_key the MDL key to lock
@return true on error, false on success
*/
static bool lock_for_write(THD *thd, const MDL_key &mdl_key) {
DBUG_EXECUTE_IF("histogram_fail_during_lock_for_write", { return true; });
MDL_request mdl_request;
MDL_REQUEST_INIT_BY_KEY(&mdl_request, &mdl_key, MDL_EXCLUSIVE,
MDL_TRANSACTION);
// If locking fails, an error has already been flagged.
return thd->mdl_context.acquire_lock(&mdl_request,
thd->variables.lock_wait_timeout);
}
Histogram::Histogram(MEM_ROOT *mem_root, const std::string &db_name,
const std::string &tbl_name, const std::string &col_name,
enum_histogram_type type, Value_map_type data_type,
bool *error)
: m_null_values_fraction(INVALID_NULL_VALUES_FRACTION),
m_charset(nullptr),
m_num_buckets_specified(0),
m_mem_root(mem_root),
m_hist_type(type),
m_data_type(data_type) {
if (lex_string_strmake(m_mem_root, &m_database_name, db_name.c_str(),
db_name.length()) ||
lex_string_strmake(m_mem_root, &m_table_name, tbl_name.c_str(),
tbl_name.length()) ||
lex_string_strmake(m_mem_root, &m_column_name, col_name.c_str(),
col_name.length())) {
*error = true;
}
}
Histogram::Histogram(MEM_ROOT *mem_root, const Histogram &other, bool *error)
: m_sampling_rate(other.m_sampling_rate),
m_null_values_fraction(other.m_null_values_fraction),
m_charset(other.m_charset),
m_num_buckets_specified(other.m_num_buckets_specified),
m_mem_root(mem_root),
m_hist_type(other.m_hist_type),
m_data_type(other.m_data_type) {
if (lex_string_strmake(m_mem_root, &m_database_name,
other.m_database_name.str,
other.m_database_name.length) ||
lex_string_strmake(m_mem_root, &m_table_name, other.m_table_name.str,
other.m_table_name.length) ||
lex_string_strmake(m_mem_root, &m_column_name, other.m_column_name.str,
other.m_column_name.length)) {
*error = true;
}
}
bool Histogram::histogram_to_json(Json_object *json_object) const {
// Get the current time in GMT timezone with microsecond accuracy.
my_timeval time_value;
my_micro_time_to_timeval(my_micro_time(), &time_value);
MYSQL_TIME current_time;
my_tz_UTC->gmt_sec_to_TIME(¤t_time, time_value);
// last-updated
const Json_datetime last_updated(current_time, MYSQL_TYPE_DATETIME);
if (json_object->add_clone(last_updated_str(), &last_updated))
return true; /* purecov: inspected */
// histogram-type
const Json_string histogram_type(histogram_type_to_str());
if (json_object->add_clone(histogram_type_str(), &histogram_type))
return true; /* purecov: inspected */
// Sampling rate
assert(get_sampling_rate() >= 0.0);
assert(get_sampling_rate() <= 1.0);
const Json_double sampling_rate(get_sampling_rate());
if (json_object->add_clone(sampling_rate_str(), &sampling_rate))
return true; /* purecov: inspected */
// The number of buckets specified in the ANALYZE TABLE command
const Json_int num_buckets_specified(get_num_buckets_specified());
if (json_object->add_clone(numer_of_buckets_specified_str(),
&num_buckets_specified))
return true; /* purecov: inspected */
// Fraction of NULL values.
assert(get_null_values_fraction() >= 0.0);
assert(get_null_values_fraction() <= 1.0);
const Json_double null_values(get_null_values_fraction());
if (json_object->add_clone(null_values_str(), &null_values))
return true; /* purecov: inspected */
// charset-id
const Json_uint charset_id(get_character_set()->number);
if (json_object->add_clone(collation_id_str(), &charset_id))
return true; /* purecov: inspected */
return false;
}
double Histogram::get_null_values_fraction() const {
if (m_null_values_fraction != INVALID_NULL_VALUES_FRACTION) {
assert(m_null_values_fraction >= 0.0);
assert(m_null_values_fraction <= 1.0);
}
return m_null_values_fraction;
}
template <class T>
Histogram *build_histogram(MEM_ROOT *mem_root, const Value_map<T> &value_map,
size_t num_buckets, const std::string &db_name,
const std::string &tbl_name,
const std::string &col_name) {
Histogram *histogram = nullptr;
/*
If the number of buckets specified is greater or equal to the number
of distinct values, we create a Singleton histogram. Otherwise we create
an equi-height histogram.
*/
if (num_buckets >= value_map.size()) {
Singleton<T> *singleton = Singleton<T>::create(
mem_root, db_name, tbl_name, col_name, value_map.get_data_type());
if (singleton == nullptr) return nullptr;
if (singleton->build_histogram(value_map, num_buckets))
return nullptr; /* purecov: inspected */
histogram = singleton;
} else {
Equi_height<T> *equi_height = Equi_height<T>::create(
mem_root, db_name, tbl_name, col_name, value_map.get_data_type());
if (equi_height == nullptr) return nullptr;
if (equi_height->build_histogram(value_map, num_buckets))
return nullptr; /* purecov: inspected */
histogram = equi_height;
}
// We should not have a nullptr at this point.
assert(histogram != nullptr);
// Verify that the original number of buckets specified is set.
assert(histogram->get_num_buckets_specified() == num_buckets);
// Verify that we haven't created more buckets than requested.
assert(histogram->get_num_buckets() <= num_buckets);
// Ensure that the character set is set.
assert(histogram->get_character_set() != nullptr);
// Check that the fraction of NULL values has been set properly.
assert(histogram->get_null_values_fraction() >= 0.0);
assert(histogram->get_null_values_fraction() <= 1.0);
return histogram;
}
Histogram *Histogram::json_to_histogram(MEM_ROOT *mem_root,
const std::string &schema_name,
const std::string &table_name,
const std::string &column_name,
const Json_object &json_object,
Error_context *context) {
// Histogram type (equi-height or singleton).
const Json_dom *histogram_type_dom =
json_object.get(Histogram::histogram_type_str());
if (histogram_type_dom == nullptr) {
context->report_missing_attribute(Histogram::histogram_type_str());
return nullptr;
}
if (histogram_type_dom->json_type() != enum_json_type::J_STRING) {
context->report_node(histogram_type_dom,
Message::JSON_WRONG_ATTRIBUTE_TYPE);
return nullptr;
}
// Histogram data type
const Json_dom *data_type_dom = json_object.get(Histogram::data_type_str());
if (data_type_dom == nullptr) {
context->report_missing_attribute(Histogram::data_type_str());
return nullptr;
}
if (data_type_dom->json_type() != enum_json_type::J_STRING) {
context->report_node(data_type_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return nullptr;
}
const Json_string *histogram_type =
down_cast<const Json_string *>(histogram_type_dom);
const Json_string *data_type = down_cast<const Json_string *>(data_type_dom);
Field *field = context->field();
// compare field data type with histogram data type if context has field info
if (field) {
const Value_map_type value_map_type =
histograms::field_type_to_value_map_type(field);
std::string field_data_type = value_map_type_to_str[value_map_type];
if (field_data_type.compare(data_type->value()) != 0) {
context->report_node(data_type_dom, Message::JSON_WRONG_DATA_TYPE);
return nullptr;
}
}
Histogram *histogram = nullptr;
if (histogram_type->value() == Histogram::equi_height_str()) {
// Equi-height histogram
if (data_type->value() == "double") {
histogram =
Equi_height<double>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::DOUBLE);
} else if (data_type->value() == "int") {
histogram = Equi_height<longlong>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::INT);
} else if (data_type->value() == "enum") {
histogram = Equi_height<longlong>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::ENUM);
} else if (data_type->value() == "set") {
histogram = Equi_height<longlong>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::SET);
} else if (data_type->value() == "uint") {
histogram = Equi_height<ulonglong>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::UINT);
} else if (data_type->value() == "string") {
histogram =
Equi_height<String>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::STRING);
} else if (data_type->value() == "date") {
histogram = Equi_height<MYSQL_TIME>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::DATE);
} else if (data_type->value() == "time") {
histogram = Equi_height<MYSQL_TIME>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::TIME);
} else if (data_type->value() == "datetime") {
histogram = Equi_height<MYSQL_TIME>::create(mem_root, schema_name,
table_name, column_name,
Value_map_type::DATETIME);
} else if (data_type->value() == "decimal") {
histogram =
Equi_height<my_decimal>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::DECIMAL);
} else {
context->report_node(data_type_dom, Message::JSON_UNSUPPORTED_DATA_TYPE);
return nullptr;
}
} else if (histogram_type->value() == Histogram::singleton_str()) {
// Singleton histogram
if (data_type->value() == "double") {
histogram =
Singleton<double>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::DOUBLE);
} else if (data_type->value() == "int") {
histogram = Singleton<longlong>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::INT);
} else if (data_type->value() == "enum") {
histogram = Singleton<longlong>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::ENUM);
} else if (data_type->value() == "set") {
histogram = Singleton<longlong>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::SET);
} else if (data_type->value() == "uint") {
histogram = Singleton<ulonglong>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::UINT);
} else if (data_type->value() == "string") {
histogram =
Singleton<String>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::STRING);
} else if (data_type->value() == "datetime") {
histogram =
Singleton<MYSQL_TIME>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::DATETIME);
} else if (data_type->value() == "date") {
histogram = Singleton<MYSQL_TIME>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::DATE);
} else if (data_type->value() == "time") {
histogram = Singleton<MYSQL_TIME>::create(
mem_root, schema_name, table_name, column_name, Value_map_type::TIME);
} else if (data_type->value() == "decimal") {
histogram =
Singleton<my_decimal>::create(mem_root, schema_name, table_name,
column_name, Value_map_type::DECIMAL);
} else {
context->report_node(data_type_dom, Message::JSON_UNSUPPORTED_DATA_TYPE);
return nullptr;
}
} else {
// Unsupported histogram type.
context->report_node(histogram_type_dom,
Message::JSON_UNSUPPORTED_HISTOGRAM_TYPE);
return nullptr;
}
if (histogram != nullptr &&
histogram->json_to_histogram(json_object, context))
return nullptr;
// Global post-check
if (histogram->get_num_buckets_specified() < histogram->get_num_buckets()) {
context->report_global(Message::JSON_NUM_BUCKETS_MORE_THAN_SPECIFIED);
return nullptr;
}
return histogram;
}
/*
All subclasses should also call this function in order to populate fields that
are shared among all histogram types (character set, null values fraction).
*/
bool Histogram::json_to_histogram(const Json_object &json_object,
Error_context *context) {
// The sampling rate that was used to create the histogram.
const Json_dom *sampling_rate_dom = json_object.get(sampling_rate_str());
if (sampling_rate_dom == nullptr) {
context->report_missing_attribute(Histogram::sampling_rate_str());
return true;
}
if (sampling_rate_dom->json_type() != enum_json_type::J_DOUBLE) {
context->report_node(sampling_rate_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
const Json_double *sampling_rate =
down_cast<const Json_double *>(sampling_rate_dom);
m_sampling_rate = sampling_rate->value();
// The number of buckets originally specified by the user.
const Json_dom *num_buckets_specified_dom =
json_object.get(numer_of_buckets_specified_str());
if (num_buckets_specified_dom == nullptr) {
context->report_missing_attribute(
Histogram::numer_of_buckets_specified_str());
return true;
}
if (num_buckets_specified_dom->json_type() != enum_json_type::J_INT) {
context->report_node(num_buckets_specified_dom,
Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
const Json_int *num_buckets_specified =
down_cast<const Json_int *>(num_buckets_specified_dom);
m_num_buckets_specified = num_buckets_specified->value();
// Fraction of SQL null-values in the original data set.
const Json_dom *null_values_dom = json_object.get(null_values_str());
if (null_values_dom == nullptr) {
context->report_missing_attribute(Histogram::null_values_str());
return true;
}
if (null_values_dom->json_type() != enum_json_type::J_DOUBLE) {
context->report_node(null_values_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
const Json_double *null_values =
down_cast<const Json_double *>(null_values_dom);
m_null_values_fraction = null_values->value();
// Character set ID
const Json_dom *charset_id_dom = json_object.get(collation_id_str());
if (charset_id_dom == nullptr) {
context->report_missing_attribute(Histogram::collation_id_str());
return true;
}
/*
In the JSON object of the histogram, charset_id is defined as an unsigned
integer, but it may become a signed integer when re-parsed into a JSON
object.
*/
if (charset_id_dom->json_type() == enum_json_type::J_UINT) {
const Json_uint *charset_id = down_cast<const Json_uint *>(charset_id_dom);
m_charset = get_charset(static_cast<uint>(charset_id->value()), MYF(0));
} else if (!context->binary() &&
charset_id_dom->json_type() == enum_json_type::J_INT) {
const Json_int *charset_id = down_cast<const Json_int *>(charset_id_dom);
m_charset = get_charset(static_cast<uint>(charset_id->value()), MYF(0));
} else {
context->report_node(charset_id_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
// Common attributes post-check
{
if (sampling_rate->value() < 0.0 || sampling_rate->value() > 1.0) {
context->report_node(sampling_rate_dom,
Message::JSON_INVALID_SAMPLING_RATE);
return true;
}
if (num_buckets_specified->value() < 1 ||
num_buckets_specified->value() > MAX_NUMBER_OF_HISTOGRAM_BUCKETS) {
context->report_node(num_buckets_specified_dom,
Message::JSON_INVALID_NUM_BUCKETS_SPECIFIED);
return true;
}
if (null_values->value() < 0.0 || null_values->value() > 1.0) {
context->report_node(null_values_dom, Message::JSON_INVALID_FREQUENCY);
return true;
}
if (m_charset == nullptr) {
context->report_node(charset_id_dom, Message::JSON_UNSUPPORTED_CHARSET);
return true;
}
}
return false;
}
bool Histogram::histogram_data_type_to_json(Json_object *json_object) const {
std::string foo = value_map_type_to_str[get_data_type()];
const Json_string json_value(foo);
return json_object->add_clone(data_type_str(), &json_value);
}
template <>
bool Histogram::extract_json_dom_value(const Json_dom *json_dom, double *out,
Error_context *context) {
if (json_dom->json_type() != enum_json_type::J_DOUBLE) {
context->report_node(json_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
*out = down_cast<const Json_double *>(json_dom)->value();
return false;
}
template <>
bool Histogram::extract_json_dom_value(const Json_dom *json_dom, String *out,
Error_context *context) {
assert(get_character_set() != nullptr);
char *value_dup_data = nullptr;
size_t value_dup_length = 0;
if (json_dom->json_type() == enum_json_type::J_OPAQUE) {
assert(context->binary());
const Json_opaque *json_opaque = down_cast<const Json_opaque *>(json_dom);
String value(json_opaque->value(), json_opaque->size(),
get_character_set());
value_dup_length = value.length();
/*
Make a copy of the data, since the JSON opaque will free it before we need
it.
*/
value_dup_data = value.dup(get_mem_root());
if (value_dup_data == nullptr) {
return true; // OOM
}
} else if (!context->binary() &&
json_dom->json_type() == enum_json_type::J_STRING) {
/*
When a histogram is converted to binary json by histogram_to_json()
to be persisted, a String-typed value is converted to Json_opaque with
field type enum_field_types::MYSQL_TYPE_STRING.
The opaque data is base64-encoded by Json_wrapper::to_string() before it
goes to the outside as standard json. Json_wrapper::to_string() returns
an encoded string in the format 'base64:typeN:encoded_data'.
So when the outside data comes back and is processed by parse_json(), it
is J_STRING and needs to be decoded here.
*/
const Json_string *json_string = down_cast<const Json_string *>(json_dom);
const std::string &encoded_str = json_string->value();
std::string prefix_builder("base64:type");
prefix_builder.append(
std::to_string(static_cast<int>(enum_field_types::MYSQL_TYPE_STRING)));
prefix_builder.append(":");
const char *prefix = prefix_builder.c_str();
int prefix_len = prefix_builder.length();
size_t pos = encoded_str.find(prefix, 0, prefix_len);
if (pos == encoded_str.npos) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
const char *encoded_data = encoded_str.c_str() + prefix_len;
const size_t encoded_data_len = encoded_str.size() - prefix_len;
if (encoded_data_len > base64_decode_max_arg_length()) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
const size_t needed =
static_cast<size_t>(base64_needed_decoded_length(encoded_data_len));
String base64_buffer;
if (base64_buffer.reserve(needed)) {
return true;
}
const char *end_ptr = nullptr;
const int64 decoded_str_len = base64_decode(encoded_data, encoded_data_len,
&base64_buffer[0], &end_ptr, 0);
if (decoded_str_len < 0 ||
(end_ptr &&
(static_cast<size_t>(end_ptr - encoded_data) != encoded_data_len))) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
base64_buffer.length(decoded_str_len);
value_dup_data = base64_buffer.dup(get_mem_root());
if (value_dup_data == nullptr) {
return true; // OOM
}
value_dup_length = decoded_str_len;
} else {
context->report_node(json_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
out->set(value_dup_data, value_dup_length, get_character_set());
return false;
}
template <>
bool Histogram::extract_json_dom_value(const Json_dom *json_dom, ulonglong *out,
Error_context *context) {
if (json_dom->json_type() == enum_json_type::J_UINT)
*out = down_cast<const Json_uint *>(json_dom)->value();
else if (!context->binary() &&
json_dom->json_type() == enum_json_type::J_INT) {
longlong val = down_cast<const Json_int *>(json_dom)->value();
if (val < 0) {
context->report_node(json_dom, Message::JSON_VALUE_OUT_OF_RANGE);
return true;
}
*out = static_cast<ulonglong>(val);
} else {
context->report_node(json_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
return false;
}
template <>
bool Histogram::extract_json_dom_value(const Json_dom *json_dom, longlong *out,
Error_context *context) {
if (json_dom->json_type() != enum_json_type::J_INT) {
if (json_dom->json_type() == enum_json_type::J_UINT)
context->report_node(json_dom, Message::JSON_VALUE_OUT_OF_RANGE);
else
context->report_node(json_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
*out = down_cast<const Json_int *>(json_dom)->value();
return false;
}
template <>
bool Histogram::extract_json_dom_value(const Json_dom *json_dom,
MYSQL_TIME *out,
Error_context *context) {
if (json_dom->json_type() == enum_json_type::J_DATE ||
json_dom->json_type() == enum_json_type::J_TIME ||
json_dom->json_type() == enum_json_type::J_DATETIME ||
json_dom->json_type() == enum_json_type::J_TIMESTAMP) {
assert(context->binary());
*out = *down_cast<const Json_datetime *>(json_dom)->value();
} else if (!context->binary() &&
json_dom->json_type() == enum_json_type::J_STRING) {
const Json_string *json_string = down_cast<const Json_string *>(json_dom);
String str{json_string->value().c_str(), json_string->value().size(),
&my_charset_utf8mb4_bin};
MYSQL_TIME_STATUS status;
if (get_data_type() == Value_map_type::TIME) {
if (str_to_time(&str, out, 0, &status) || status.warnings) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
out->time_type = enum_mysql_timestamp_type::MYSQL_TIMESTAMP_TIME;
} else if (get_data_type() == Value_map_type::DATE) {
if (str_to_datetime(&str, out, 0, &status) || status.warnings) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
out->time_type = enum_mysql_timestamp_type::MYSQL_TIMESTAMP_DATE;
} else if (get_data_type() == Value_map_type::DATETIME) {
if (str_to_datetime(&str, out, 0, &status) || status.warnings) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
out->time_type = enum_mysql_timestamp_type::MYSQL_TIMESTAMP_DATETIME;
} else {
return true;
}
} else {
context->report_node(json_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
return false;
}
template <>
bool Histogram::extract_json_dom_value(const Json_dom *json_dom,
my_decimal *out,
Error_context *context) {
if (json_dom->json_type() == enum_json_type::J_DECIMAL)
*out = *down_cast<const Json_decimal *>(json_dom)->value();
else if (!context->binary()) {
if (json_dom->json_type() == enum_json_type::J_INT) {
if (longlong2decimal(down_cast<const Json_int *>(json_dom)->value(),
out)) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
} else if (json_dom->json_type() == enum_json_type::J_UINT) {
if (ulonglong2decimal(down_cast<const Json_uint *>(json_dom)->value(),
out)) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
} else if (json_dom->json_type() == enum_json_type::J_DOUBLE) {
if (double2decimal(down_cast<const Json_double *>(json_dom)->value(),
out)) {
context->report_node(json_dom, Message::JSON_VALUE_FORMAT_ERROR);
return true;
}
} else {
context->report_node(json_dom, Message::JSON_WRONG_ATTRIBUTE_TYPE);
return true;
}
} else {
return true;
}
return false;
}
/**
Check if a field is covered by a single-part unique index (primary key or
unique index). Indexes that are marked as invisible are ignored.
@param thd The current session.
@param field The field to check.
@return true if the field is covered by a single-part unique index. False
otherwise.
*/
static bool covered_by_single_part_index(const THD *thd, const Field *field) {
Key_map possible_keys;
possible_keys.merge(field->table->s->usable_indexes(thd));
possible_keys.intersect(field->key_start);
assert(field->table->s->keys <= possible_keys.length());
for (uint i = 0; i < field->table->s->keys; ++i) {
if (possible_keys.is_set(i) &&
field->table->s->key_info[i].user_defined_key_parts == 1 &&
(field->table->s->key_info[i].flags & HA_NOSAME)) {
return true;
}
}
return false;
}
/**
Prepare one Value_map for each field we are creating histogram statistics for.
We will also estimate how many bytes one row will consume. For example, if we
are creating histogram statistics for two INTEGER columns, we estimate that
one row will consume (sizeof(longlong) * 2) bytes (16 bytes).
@param fields A vector with all the fields we are creating
histogram statistics for.
@param[out] value_maps A map where the Value_maps will be initialized.
@param[out] row_size_bytes An estimation of how many bytes one row will
consume.
@return true on error, false otherwise.
*/
static bool prepare_value_maps(
std::vector<Field *, Histogram_key_allocator<Field *>> &fields,
value_map_collection &value_maps, size_t *row_size_bytes) {
*row_size_bytes = 0;
for (const Field *field : fields) {
histograms::Value_map_base *value_map = nullptr;
const Value_map_type value_map_type =
histograms::field_type_to_value_map_type(field);
switch (value_map_type) {
case histograms::Value_map_type::STRING: {
size_t max_field_length =
std::min(static_cast<size_t>(field->field_length),
histograms::HISTOGRAM_MAX_COMPARE_LENGTH);
*row_size_bytes += max_field_length * field->charset()->mbmaxlen;
value_map =
new histograms::Value_map<String>(field->charset(), value_map_type);
break;
}
case histograms::Value_map_type::DOUBLE: {
value_map =
new histograms::Value_map<double>(field->charset(), value_map_type);
break;
}
case histograms::Value_map_type::INT:
case histograms::Value_map_type::ENUM:
case histograms::Value_map_type::SET: {
value_map = new histograms::Value_map<longlong>(field->charset(),
value_map_type);
break;
}
case histograms::Value_map_type::UINT: {
value_map = new histograms::Value_map<ulonglong>(field->charset(),
value_map_type);
break;
}
case histograms::Value_map_type::DATETIME:
case histograms::Value_map_type::DATE:
case histograms::Value_map_type::TIME: {
value_map = new histograms::Value_map<MYSQL_TIME>(field->charset(),
value_map_type);
break;
}
case histograms::Value_map_type::DECIMAL: {
value_map = new histograms::Value_map<my_decimal>(field->charset(),
value_map_type);
break;
}
case histograms::Value_map_type::INVALID: {
assert(false); /* purecov: deadcode */
return true;
}
}
// Overhead for each element
*row_size_bytes += value_map->element_overhead();
value_maps.emplace(field->field_index(),
std::unique_ptr<histograms::Value_map_base>(value_map));
}
return false;
}
/**
Read data from a table into the provided Value_maps. We will read data using
sampling with the provided sampling percentage.
@param fields A vector with the fields we are reading data from.
@param sample_percentage The sampling percentage we will use for sampling.
Must be between 0.0 and 100.0.
@param table The table we are reading the data from.
@param value_maps The Value_maps we are reading data into.
@return true on error, false otherwise.
*/
static bool fill_value_maps(
const std::vector<Field *, Histogram_key_allocator<Field *>> &fields,
double sample_percentage, const TABLE *table,
value_map_collection &value_maps) {
assert(sample_percentage > 0.0);
assert(sample_percentage <= 100.0);
assert(fields.size() == value_maps.size());
std::random_device rd;
std::uniform_int_distribution<int> dist;
int sampling_seed = dist(rd);
DBUG_EXECUTE_IF("histogram_force_sampling", {
sampling_seed = 1;
sample_percentage = 50.0;
});
void *scan_ctx = nullptr;
for (auto &value_map : value_maps)
value_map.second->set_sampling_rate(sample_percentage / 100.0);
/* This is not a tablesample request. */
bool tablesample = false;
if (table->file->ha_sample_init(scan_ctx, sample_percentage, sampling_seed,
enum_sampling_method::SYSTEM, tablesample)) {
return true;
}
auto handler_guard = create_scope_guard([table, scan_ctx]() {
table->file->ha_sample_end(scan_ctx); /* purecov: deadcode */
});
// Read the data from each column into its own Value_map.
int res = table->file->ha_sample_next(scan_ctx, table->record[0]);
while (res == 0) {
for (Field *field : fields) {
histograms::Value_map_base *value_map =
value_maps.at(field->field_index()).get();
switch (histograms::field_type_to_value_map_type(field)) {
case histograms::Value_map_type::STRING: {
StringBuffer<MAX_FIELD_WIDTH> str_buf(field->charset());
field->val_str(&str_buf);
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(static_cast<String>(str_buf), 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::DOUBLE: {
double value = field->val_real();
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::INT:
case histograms::Value_map_type::ENUM:
case histograms::Value_map_type::SET: {
longlong value = field->val_int();
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::UINT: {
ulonglong value = static_cast<ulonglong>(field->val_int());
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::DATE: {
MYSQL_TIME time_value;
TIME_from_longlong_date_packed(&time_value,
field->val_date_temporal());
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(time_value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::TIME: {
MYSQL_TIME time_value;
TIME_from_longlong_time_packed(&time_value,
field->val_time_temporal());
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(time_value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::DATETIME: {
MYSQL_TIME time_value;
TIME_from_longlong_datetime_packed(&time_value,
field->val_date_temporal());
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(time_value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::DECIMAL: {
my_decimal buffer;
my_decimal *value;
value = field->val_decimal(&buffer);
if (field->is_null())
value_map->add_null_values(1);
else if (value_map->add_values(*value, 1))
return true; /* purecov: deadcode */
break;
}
case histograms::Value_map_type::INVALID: {
assert(false); /* purecov: deadcode */
break;
}
}
}
res = table->file->ha_sample_next(scan_ctx, table->record[0]);
DBUG_EXECUTE_IF(
"sample_read_sample_half", static uint count = 1;
if (count == std::max(1ULL, table->file->stats.records) / 2) {
res = HA_ERR_END_OF_FILE;
break;
} ++count;);
}
if (res != HA_ERR_END_OF_FILE) return true; /* purecov: deadcode */
// Close the handler
handler_guard.release();
if (table->file->ha_sample_end(scan_ctx)) {
assert(false); /* purecov: deadcode */
return true;
}
return false;
}
bool update_histogram(THD *thd, Table_ref *table, const columns_set &columns,
int num_buckets, LEX_STRING data, results_map &results) {
dd::cache::Dictionary_client::Auto_releaser auto_releaser(thd->dd_client());
// Read only should have been stopped at an earlier stage.
assert(!check_readonly(thd, false));
assert(!thd->tx_read_only);
assert(results.empty());
assert(!columns.empty());
// Only one table should be specified in ANALYZE TABLE .. UPDATE HISTOGRAM
assert(table->next_local == nullptr);
if (table->table != nullptr && table->table->s->tmp_table != NO_TMP_TABLE) {
/*
Normally, the table we are going to read data from is not initialized at
this point. But if table->table is not a null-pointer, it has already been
initialized at an earlier stage. This will happen if the table is a
temporary table.
*/
results.emplace("", Message::TEMPORARY_TABLE);
return true;
}
/*
Create two scope guards; one for disabling autocommit and one that will do a
rollback and ensure that any open tables are closed before returning.
*/
Disable_autocommit_guard autocommit_guard(thd);
auto tables_guard = create_scope_guard([thd]() {
if (trans_rollback_stmt(thd) || trans_rollback(thd))
assert(false); /* purecov: deadcode */
close_thread_tables(thd);
});
if (open_and_lock_tables(thd, table, 0)) {
return true;
}
DBUG_EXECUTE_IF("histogram_fail_after_open_table", { return true; });
if (table->is_view()) {
results.emplace("", Message::VIEW);
return true;
}
assert(table->table != nullptr);
TABLE *tbl = table->table;
if (tbl->s->encrypt_type.length > 0 &&
my_strcasecmp(system_charset_info, "n", tbl->s->encrypt_type.str) != 0) {
results.emplace("", Message::ENCRYPTED_TABLE);
return true;
}
/*
Check if the provided column names exist, and that they have a supported
data type. If they do, mark them in the read set.
*/
bitmap_clear_all(tbl->write_set);
bitmap_clear_all(tbl->read_set);
std::vector<Field *, Histogram_key_allocator<Field *>> resolved_fields;
for (const std::string &column_name : columns) {
Field *field = find_field_in_table_sef(tbl, column_name.c_str());
if (field == nullptr) {
// Field not found in table
results.emplace(column_name, Message::FIELD_NOT_FOUND);
continue;
} else if (histograms::field_type_to_value_map_type(field) ==
histograms::Value_map_type::INVALID) {
// Unsupported data type
results.emplace(column_name, Message::UNSUPPORTED_DATA_TYPE);
continue;
}
/*
Check if this field is covered by a single-part unique index. If it is, we
don't want to create histogram statistics for it.
*/
if (covered_by_single_part_index(thd, field)) {
results.emplace(column_name,
Message::COVERED_BY_SINGLE_PART_UNIQUE_INDEX);
continue;
}
resolved_fields.push_back(field);
bitmap_set_bit(tbl->read_set, field->field_index());
if (field->is_gcol()) {
bitmap_set_bit(tbl->write_set, field->field_index());
/*
The base columns needs to be in the write set in case of nested
generated columns:
CREATE TABLE t1 (
col1 INT,
col2 INT AS (col1 + 1) VIRTUAL,
col3 INT AS (col2 + 1) VIRTUAL);
If we are reading data from "col3", we also need to update the data in
"col2" in order for the generated value to be correct.
*/
bitmap_union(tbl->write_set, &field->gcol_info->base_columns_map);
bitmap_union(tbl->read_set, &field->gcol_info->base_columns_map);
}
}
/*
If we don't have any fields, we just quit here. Return "true" so we don't
write empty transactions/statements to the binlog.
*/
if (resolved_fields.empty()) {
return true;
}
if (data.str != nullptr) {
assert(!resolved_fields.empty());
if (resolved_fields.size() > 1) {
results.emplace("", Message::MULTIPLE_COLUMNS_SPECIFIED);
return true;
}
Field *field = resolved_fields.front();
/*
The column needs to be in the write set because Field::store()
is used on a copy of the histogram Field to test value domain.
*/
bitmap_set_bit(tbl->write_set, field->field_index());
// Parse the literal for a standard JSON object.
String parse_input{data.str, static_cast<uint32>(data.length),
&my_charset_utf8mb4_bin};
Json_dom_ptr dom;
{
Histogram_error_handler error_handler(thd);
JsonParseDefaultErrorHandler parse_handler("UPDATE HISTOGRAM", 0);
if (parse_json(parse_input, &dom, true, parse_handler,
JsonDocumentDefaultDepthHandler) ||
error_handler.has_error()) {
results.emplace("", Message::JSON_FORMAT_ERROR);
return true;
}
if (dom->json_type() != enum_json_type::J_OBJECT) {
results.emplace("", Message::JSON_NOT_AN_OBJECT);
return true;
}
}
MEM_ROOT local_mem_root;
// Create a histogram for the json object.
Error_context context(thd, field, &results);
std::string col_name(field->field_name);
// Convert JSON to histogram
histograms::Histogram *histogram = Histogram::json_to_histogram(
&local_mem_root, std::string(table->db, table->db_length),
std::string(table->table_name, table->table_name_length), col_name,
*down_cast<Json_object *>(dom.get()), &context);
// Store it to persistent storage.
if (histogram == nullptr || histogram->store_histogram(thd)) {
my_error(ER_UNABLE_TO_BUILD_HISTOGRAM, MYF(0), field->field_name,
table->db, table->table_name);
return true;
}
results.emplace(col_name, Message::HISTOGRAM_CREATED);
bool ret = trans_commit_stmt(thd) || trans_commit(thd);
close_thread_tables(thd);
tables_guard.release();
return ret;
}
/*
Prepare one Value_map for each field we are creating histogram statistics
for. Also, estimate how many bytes one row will consume so that we can
estimate how many rows we can fit into memory permitted by
histogram_generation_max_mem_size.
*/
size_t row_size_bytes = 0;
value_map_collection value_maps;
if (prepare_value_maps(resolved_fields, value_maps, &row_size_bytes))
return true; /* purecov: deadcode */
/*
Caclulate how many rows we can fit into memory permitted by
histogram_generation_max_mem_size.
*/
double rows_in_memory = thd->variables.histogram_generation_max_mem_size /
static_cast<double>(row_size_bytes);
/*
Ensure that we estimate at least one row in the table, so we avoid
division by zero error.
NOTE: We ignore errors from "fetch_number_of_rows()" on purpose, since we
don't consider it fatal not having the correct row estimate.
*/
table->fetch_number_of_rows();
ha_rows rows_in_table = std::max(1ULL, tbl->file->stats.records);
double sample_percentage = rows_in_memory / rows_in_table * 100.0;
sample_percentage = std::min(sample_percentage, 100.0);
// Read data from the table into the Value_maps we have prepared.
if (fill_value_maps(resolved_fields, sample_percentage, tbl, value_maps))
return true; /* purecov: deadcode */
// Create a histogram for each Value_map, and store it to persistent storage.
for (const Field *field : resolved_fields) {
/*
The MEM_ROOT is transferred to the dictionary object when
histogram->store_histogram is called.
*/
MEM_ROOT local_mem_root(key_memory_histograms, 256);
std::string col_name(field->field_name);
histograms::Histogram *histogram =
value_maps.at(field->field_index())
->build_histogram(
&local_mem_root, num_buckets,
std::string(table->db, table->db_length),
std::string(table->table_name, table->table_name_length),
col_name);
if (histogram == nullptr) {
/* purecov: begin inspected */
my_error(ER_UNABLE_TO_BUILD_HISTOGRAM, MYF(0), field->field_name,
table->db, table->table_name);
return true;
/* purecov: end */
} else if (histogram->store_histogram(thd)) {
// errors have already been reported
return true; /* purecov: deadcode */
}
results.emplace(col_name, Message::HISTOGRAM_CREATED);
}
bool ret = trans_commit_stmt(thd) || trans_commit(thd);
close_thread_tables(thd);
tables_guard.release();
return ret;
}
bool drop_all_histograms(THD *thd, Table_ref &table,
const dd::Table &table_definition,
results_map &results) {
columns_set columns;
for (const auto &col : table_definition.columns())
columns.emplace(col->name().c_str());
return drop_histograms(thd, table, columns, false, results);
}
bool drop_histograms(THD *thd, Table_ref &table, const columns_set &columns,
bool needs_lock, results_map &results) {
dd::cache::Dictionary_client *client = thd->dd_client();
dd::cache::Dictionary_client::Auto_releaser auto_releaser(client);
if (needs_lock) {
/*
At this point ANALYZE TABLE DROP HISTOGRAM is the only caller of this
function that requires it to acquire lock on table and individual
column statistics.
Error out on temporary table for consistency with update histograms case.
*/
if (table.table != nullptr && table.table->s->tmp_table != NO_TMP_TABLE) {
results.emplace("", Message::TEMPORARY_TABLE);
return true;
}
/*
Acquire shared metadata lock on the table (or check that it is locked
under LOCK TABLES) so this table and all column statistics for it are
not dropped under our feet.
*/
if (thd->locked_tables_mode) {
if (!find_locked_table(thd->open_tables, table.db, table.table_name)) {
my_error(ER_TABLE_NOT_LOCKED, MYF(0), table.table_name);
return true;
}
} else {
if (thd->mdl_context.acquire_lock(&table.mdl_request,
thd->variables.lock_wait_timeout))
return true; // error is already reported.
}
} else {
/*
In this case we assume that caller has acquired exclusive metadata
lock on table so there is no need to lock individual column statistics.
It is also caller's responsibility to ensure that table is non-temporary.
*/
assert(thd->mdl_context.owns_equal_or_stronger_lock(
MDL_key::TABLE, table.db, table.table_name, MDL_EXCLUSIVE));
}
for (const std::string &column_name : columns) {
if (needs_lock) {
MDL_key mdl_key;
dd::Column_statistics::create_mdl_key(
{table.db, table.db_length},
{table.table_name, table.table_name_length}, column_name.c_str(),
&mdl_key);
if (lock_for_write(thd, mdl_key))
return true; // error is already reported.
}
dd::String_type dd_name = dd::Column_statistics::create_name(
{table.db, table.db_length},
{table.table_name, table.table_name_length}, column_name.c_str());
// Do we have an existing histogram for this column?
const dd::Column_statistics *column_statistics = nullptr;
if (client->acquire(dd_name, &column_statistics)) {
// error is already reported.
return true; /* purecov: deadcode */
}
if (column_statistics == nullptr) {
results.emplace(column_name, Message::NO_HISTOGRAM_FOUND);
continue;
}
if (client->drop(column_statistics)) {
/* purecov: begin inspected */
my_error(ER_UNABLE_TO_DROP_COLUMN_STATISTICS, MYF(0), column_name.c_str(),
table.db, table.table_name);
return true;
/* purecov: end */
}
results.emplace(column_name, Message::HISTOGRAM_DELETED);
}
return false;
}
bool Histogram::store_histogram(THD *thd) const {
dd::cache::Dictionary_client *client = thd->dd_client();
MDL_key mdl_key;
dd::Column_statistics::create_mdl_key(get_database_name().str,
get_table_name().str,
get_column_name().str, &mdl_key);
if (lock_for_write(thd, mdl_key)) {
// Error has already been reported
return true; /* purecov: deadcode */
}
DEBUG_SYNC(thd, "store_histogram_after_write_lock");
dd::String_type dd_name = dd::Column_statistics::create_name(
get_database_name().str, get_table_name().str, get_column_name().str);
// Do we have an existing histogram for this column?
dd::Column_statistics *column_stats = nullptr;
if (client->acquire_for_modification(dd_name, &column_stats)) {
// Error has already been reported
return true; /* purecov: deadcode */
}
if (column_stats != nullptr) {
// Update the existing object.
column_stats->set_histogram(this);
if (client->update(column_stats)) {
/* purecov: begin inspected */
my_error(ER_UNABLE_TO_UPDATE_COLUMN_STATISTICS, MYF(0),
get_column_name().str, get_database_name().str,
get_table_name().str);
return true;
/* purecov: end */
}
} else {
// Create a new object
std::unique_ptr<dd::Column_statistics> column_statistics(
dd::create_object<dd::Column_statistics>());
column_statistics.get()->set_schema_name(get_database_name().str);
column_statistics.get()->set_table_name(get_table_name().str);
column_statistics.get()->set_column_name(get_column_name().str);
column_statistics.get()->set_name(dd_name);
column_statistics.get()->set_histogram(this);
if (client->store(column_statistics.get())) {
/* purecov: begin inspected */
my_error(ER_UNABLE_TO_STORE_COLUMN_STATISTICS, MYF(0),
get_column_name().str, get_database_name().str,
get_table_name().str);
return true;
/* purecov: end */
}
}
return false;
}
/**
Rename a single histogram from a old schema/table name to a new schema/table
name. It is used for instance by RENAME TABLE, where the contents of the
histograms doesn't change.
@param thd Thread handler.
@param old_schema_name The old schema name.
@param old_table_name The old table name.
@param new_schema_name The new schema name.
@param new_table_name The new table name.
@param column_name The column name.
@param results A map where the result of the operation is stored.
@return false on success, true on error.
*/
static bool rename_histogram(THD *thd, const char *old_schema_name,
const char *old_table_name,
const char *new_schema_name,
const char *new_table_name,
const char *column_name, results_map &results) {
dd::cache::Dictionary_client *client = thd->dd_client();
dd::cache::Dictionary_client::Auto_releaser auto_releaser(client);
// First find the histogram with the old name.
MDL_key mdl_key;
dd::Column_statistics::create_mdl_key(old_schema_name, old_table_name,
column_name, &mdl_key);
if (lock_for_write(thd, mdl_key)) {
// Error has already been reported
return true; /* purecov: deadcode */
}
dd::String_type dd_name = dd::Column_statistics::create_name(
old_schema_name, old_table_name, column_name);
dd::Column_statistics *column_statistics = nullptr;
if (client->acquire_for_modification(dd_name, &column_statistics)) {
// Error has already been reported
return true; /* purecov: deadcode */
}
if (column_statistics == nullptr) {
results.emplace(column_name, Message::NO_HISTOGRAM_FOUND);
return false;
}
dd::Column_statistics::create_mdl_key(new_schema_name, new_table_name,
column_name, &mdl_key);
if (lock_for_write(thd, mdl_key)) {
// Error has already been reported
return true; /* purecov: deadcode */
}
column_statistics->set_schema_name(new_schema_name);
column_statistics->set_table_name(new_table_name);
column_statistics->set_column_name(column_name);
column_statistics->set_name(column_statistics->create_name());
if (client->update(column_statistics)) {
/* purecov: begin inspected */
my_error(ER_UNABLE_TO_UPDATE_COLUMN_STATISTICS, MYF(0), column_name,
old_schema_name, old_table_name);
return true;
/* purecov: end */
}
results.emplace(column_name, Message::HISTOGRAM_DELETED);
return false;
}
bool rename_histograms(THD *thd, const char *old_schema_name,
const char *old_table_name, const char *new_schema_name,
const char *new_table_name, results_map &results) {
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
MDL_request mdl_request;
MDL_REQUEST_INIT(&mdl_request, MDL_key::TABLE, old_schema_name,
old_table_name, MDL_SHARED_READ_ONLY, MDL_TRANSACTION);
if (thd->mdl_context.acquire_lock(&mdl_request,
thd->variables.lock_wait_timeout)) {
// error has already been reported
return true; /* purecov: deadcode */
}
/*
We have to look up the new table since it already will be renamed at this
point.
*/
const dd::Table *table_def = nullptr;
if (thd->dd_client()->acquire(new_schema_name, new_table_name, &table_def)) {
// error has already been reported
return false; /* purecov: deadcode */
}
if (table_def == nullptr) {
assert(false); /* purecov: deadcode */
return false;
}
for (const auto &col : table_def->columns()) {
if (rename_histogram(thd, old_schema_name, old_table_name, new_schema_name,
new_table_name, col->name().c_str(), results))
return true; /* purecov: deadcode */
}
return false;
}
bool find_histogram(THD *thd, const std::string &schema_name,
const std::string &table_name,
const std::string &column_name,
const Histogram **histogram) {
assert(*histogram == nullptr);
if (schema_name == "mysql" || table_name == "column_statistics") return false;
dd::String_type dd_name = dd::Column_statistics::create_name(
schema_name.c_str(), table_name.c_str(), column_name.c_str());
const dd::Column_statistics *column_statistics = nullptr;
dd::cache::Dictionary_client *client = thd->dd_client();
if (client->acquire<dd::Column_statistics>(dd_name, &column_statistics))
return true; /* purecov: deadcode */
if (column_statistics == nullptr) return false;
*histogram = column_statistics->histogram();
return false;
}
template <class T>
double Histogram::get_less_than_selectivity_dispatcher(const T &value) const {
switch (get_histogram_type()) {
case enum_histogram_type::SINGLETON: {
const Singleton<T> *singleton = down_cast<const Singleton<T> *>(this);
return singleton->get_less_than_selectivity(value);
}
case enum_histogram_type::EQUI_HEIGHT: {
const Equi_height<T> *equi_height =
down_cast<const Equi_height<T> *>(this);
return equi_height->get_less_than_selectivity(value);
}
}
/* purecov: begin deadcode */
assert(false);
return 0.0;
/* purecov: end deadcode */
}
template <class T>
double Histogram::get_greater_than_selectivity_dispatcher(
const T &value) const {
switch (get_histogram_type()) {
case enum_histogram_type::SINGLETON: {
const Singleton<T> *singleton = down_cast<const Singleton<T> *>(this);
return singleton->get_greater_than_selectivity(value);
}
case enum_histogram_type::EQUI_HEIGHT: {
const Equi_height<T> *equi_height =
down_cast<const Equi_height<T> *>(this);
return equi_height->get_greater_than_selectivity(value);
}
}
/* purecov: begin deadcode */
assert(false);
return 0.0;
/* purecov: end deadcode */
}
template <class T>
double Histogram::get_equal_to_selectivity_dispatcher(const T &value) const {
switch (get_histogram_type()) {
case enum_histogram_type::SINGLETON: {
const Singleton<T> *singleton = down_cast<const Singleton<T> *>(this);
return singleton->get_equal_to_selectivity(value);
}
case enum_histogram_type::EQUI_HEIGHT: {
const Equi_height<T> *equi_height =
down_cast<const Equi_height<T> *>(this);
return equi_height->get_equal_to_selectivity(value);
}
}
/* purecov: begin deadcode */
assert(false);
return 0.0;
/* purecov: end deadcode */
}
static bool get_temporal(Item *item, Value_map_type preferred_type,
MYSQL_TIME *time_value) {
if (item->is_temporal_with_date_and_time()) {
TIME_from_longlong_datetime_packed(time_value, item->val_date_temporal());
} else if (item->is_temporal_with_date()) {
TIME_from_longlong_date_packed(time_value, item->val_date_temporal());
} else if (item->is_temporal_with_time()) {
TIME_from_longlong_time_packed(time_value, item->val_time_temporal());
} else {
switch (preferred_type) {
case Value_map_type::DATE:
case Value_map_type::DATETIME:
if (item->get_date_from_non_temporal(time_value, 0)) return true;
break;
case Value_map_type::TIME:
if (item->get_time_from_non_temporal(time_value)) return true;
break;
default:
/* purecov: begin deadcode */
assert(0);
break;
/* purecov: end deadcode */
}
}
return false;
}
template <class T>
double Histogram::apply_operator(const enum_operator op, const T &value) const {
switch (op) {
case enum_operator::LESS_THAN:
return get_less_than_selectivity_dispatcher(value);
case enum_operator::GREATER_THAN:
return get_greater_than_selectivity_dispatcher(value);
case enum_operator::EQUALS_TO:
return get_equal_to_selectivity_dispatcher(value);
default:
/* purecov: begin deadcode */
assert(false);
return 1.0;
/* purecov: end deadcode */
}
}
bool Histogram::get_selectivity_dispatcher(Item *item, const enum_operator op,
const TYPELIB *typelib,
double *selectivity) const {
switch (this->get_data_type()) {
case Value_map_type::INVALID: {
/* purecov: begin deadcode */
assert(false);
return true;
/* purecov: end deadcode */
}
case Value_map_type::STRING: {
// Is the character set the same? If not, we cannot use the histogram
if (item->collation.collation->number != get_character_set()->number)
return true;
StringBuffer<MAX_FIELD_WIDTH> str_buf(item->collation.collation);
const String *str = item->val_str(&str_buf);
if (item->is_null()) return true;
*selectivity =
apply_operator(op, str->substr(0, HISTOGRAM_MAX_COMPARE_LENGTH));
return false;
}
case Value_map_type::INT: {
const longlong value = item->val_int();
if (item->is_null()) return true;
*selectivity = apply_operator(op, value);
return false;
}
case Value_map_type::ENUM: {
assert(typelib != nullptr);
longlong value;
if (item->data_type() == MYSQL_TYPE_VARCHAR) {
StringBuffer<MAX_FIELD_WIDTH> str_buf(item->collation.collation);
const String *str = item->val_str(&str_buf);
if (item->is_null()) return true;
// Remove any trailing whitespace
size_t length = str->charset()->cset->lengthsp(
str->charset(), str->ptr(), str->length());
value = find_type2(typelib, str->ptr(), length, str->charset());
} else {
value = item->val_int();
if (item->is_null()) return true;
}
if (op == enum_operator::EQUALS_TO) {
*selectivity = get_equal_to_selectivity_dispatcher(value);
return false;
}
return true; /* purecov: deadcode */
}
case Value_map_type::SET: {
assert(typelib != nullptr);
longlong value;
if (item->data_type() == MYSQL_TYPE_VARCHAR) {
StringBuffer<MAX_FIELD_WIDTH> str_buf(item->collation.collation);
const String *str = item->val_str(&str_buf);
if (item->is_null()) return true;
bool got_warning;
const char *not_used;
uint not_used2;
ulonglong tmp_value =
find_set(typelib, str->ptr(), str->length(), str->charset(),
¬_used, ¬_used2, &got_warning);
value = static_cast<ulonglong>(tmp_value);
} else {
value = item->val_int();
if (item->is_null()) return true;
}
if (op == enum_operator::EQUALS_TO) {
*selectivity = get_equal_to_selectivity_dispatcher(value);
return false;
}
return true; /* purecov: deadcode */
}
case Value_map_type::UINT: {
const ulonglong value = static_cast<ulonglong>(item->val_int());
if (item->is_null()) return true;
*selectivity = apply_operator(op, value);
return false;
}
case Value_map_type::DOUBLE: {
const double value = item->val_real();
if (item->is_null()) return true;
*selectivity = apply_operator(op, value);
return false;
}
case Value_map_type::DECIMAL: {
my_decimal buffer;
const my_decimal *value = item->val_decimal(&buffer);
if (item->is_null()) return true;
*selectivity = apply_operator(op, *value);
return false;
}
case Value_map_type::DATE:
case Value_map_type::TIME:
case Value_map_type::DATETIME: {
MYSQL_TIME temporal_value;
if (get_temporal(item, get_data_type(), &temporal_value) ||
item->is_null())
return true;
*selectivity = apply_operator(op, temporal_value);
return false;
}
}
/* purecov: begin deadcode */
assert(false);
return true;
/* purecov: end deadcode */
}
bool Histogram::get_selectivity(Item **items, size_t item_count,
enum_operator op, double *selectivity) const {
if (get_raw_selectivity(items, item_count, op, selectivity)) return true;
/*
We return a selectivity of at least 0.001 in order to avoid returning very
low estimates in the following cases:
1) We miss a value or underestimate its frequency during sampling. With our
current histogram format this causes "holes" between buckets where we
estimate a selectivity of zero.
2) We miss a range of values. With our format we are particularly vulnerable
around the min and max of the distribution as the sampled min is likely
greater than the true min and the sampled max likely smaller than the
true max.
3) Within-bucket heuristics produce very low estimates. This can for example
happen for range-queries within a bucket. Another example is if we have
many infrequent values and one highly frequent value in a bucket.
4) The histogram has gone stale. While the usual assumption is that the
value distribution remains nearly constant this assumption fails in some
common use cases. Consider for example a date column where the current
date is inserted.
The reason for the choice of 0.001 for the lower bound is that we typically
sample fewer than 1000 pages with the default settings. With a sample of
1000 pages the probablity of missing a value or range of values with a
selectivity of 0.001 is around 1/e (~0.368) as the size of the table goes to
infinity in the worst case when the values of interest are concentrated on
few pages.
The cost of using a minimum selectivity of 0.001 is that we may sometimes
over-estimate the selectivity. For very large tables 0.1% of the rows is
still a lot in absolute terms -- 1000 rows for a table with 1 million rows,
and 1 million rows for a table with 1 billion rows.
We could improve this estimate by considering the actual number of pages
sampled when the histogram was constructed.
*/
const double minimum_selectivity = 0.001;
*selectivity = std::max(*selectivity, minimum_selectivity);
return false;
}
bool Histogram::get_raw_selectivity(Item **items, size_t item_count,
enum_operator op,
double *selectivity) const {
// Do some sanity checking first
switch (op) {
case enum_operator::EQUALS_TO:
case enum_operator::GREATER_THAN:
case enum_operator::LESS_THAN:
case enum_operator::LESS_THAN_OR_EQUAL:
case enum_operator::GREATER_THAN_OR_EQUAL:
case enum_operator::NOT_EQUALS_TO:
assert(item_count == 2);
/*
Verify that one side of the predicate is a column/field, and that the
other side is a constant value.
Make sure that we have the constant item as the right side argument of
the predicate internally.
*/
if (items[0]->const_item() && items[1]->type() == Item::FIELD_ITEM) {
// Flip the operators as well as the operator itself.
switch (op) {
case enum_operator::GREATER_THAN:
op = enum_operator::LESS_THAN;
break;
case enum_operator::LESS_THAN:
op = enum_operator::GREATER_THAN;
break;
case enum_operator::LESS_THAN_OR_EQUAL:
op = enum_operator::GREATER_THAN_OR_EQUAL;
break;
case enum_operator::GREATER_THAN_OR_EQUAL:
op = enum_operator::LESS_THAN_OR_EQUAL;
break;
default:
break;
}
Item *items_flipped[2];
items_flipped[0] = items[1];
items_flipped[1] = items[0];
return get_selectivity(items_flipped, item_count, op, selectivity);
} else if (items[0]->type() != Item::FIELD_ITEM ||
!items[1]->const_item()) {
return true;
}
break;
case enum_operator::BETWEEN:
case enum_operator::NOT_BETWEEN:
assert(item_count == 3);
if (items[0]->type() != Item::FIELD_ITEM || !items[1]->const_item() ||
!items[2]->const_item()) {
return true;
}
break;
case enum_operator::IN_LIST:
case enum_operator::NOT_IN_LIST:
assert(item_count >= 2);
if (items[0]->type() != Item::FIELD_ITEM)
return true; /* purecov: deadcode */
// This will only work if all items are const_items
for (size_t i = 1; i < item_count; ++i) {
if (!items[i]->const_item()) return true;
}
break;
case enum_operator::IS_NULL:
case enum_operator::IS_NOT_NULL:
assert(item_count == 1);
if (items[0]->type() != Item::FIELD_ITEM) return true;
}
assert(items[0]->type() == Item::FIELD_ITEM);
const TYPELIB *typelib = nullptr;
const Item_field *item_field = down_cast<const Item_field *>(items[0]);
if (item_field->field->real_type() == MYSQL_TYPE_ENUM ||
item_field->field->real_type() == MYSQL_TYPE_SET) {
const Field_enum *field_enum =
down_cast<const Field_enum *>(item_field->field);
typelib = field_enum->typelib;
}
switch (op) {
case enum_operator::LESS_THAN:
case enum_operator::EQUALS_TO:
case enum_operator::GREATER_THAN: {
return get_selectivity_dispatcher(items[1], op, typelib, selectivity);
}
case enum_operator::LESS_THAN_OR_EQUAL: {
double greater_than_selectivity;
if (get_selectivity_dispatcher(items[1], enum_operator::GREATER_THAN,
typelib, &greater_than_selectivity))
return true;
*selectivity = std::max(
get_non_null_values_fraction() - greater_than_selectivity, 0.0);
return false;
}
case enum_operator::GREATER_THAN_OR_EQUAL: {
double less_than_selectivity;
if (get_selectivity_dispatcher(items[1], enum_operator::LESS_THAN,
typelib, &less_than_selectivity))
return true;
*selectivity =
std::max(get_non_null_values_fraction() - less_than_selectivity, 0.0);
return false;
}
case enum_operator::NOT_EQUALS_TO: {
double equals_to_selectivity;
if (get_selectivity_dispatcher(items[1], enum_operator::EQUALS_TO,
typelib, &equals_to_selectivity))
return true;
*selectivity =
std::max(get_non_null_values_fraction() - equals_to_selectivity, 0.0);
return false;
}
case enum_operator::BETWEEN: {
double less_than_selectivity;
double greater_than_selectivity;
if (get_selectivity_dispatcher(items[1], enum_operator::LESS_THAN,
typelib, &less_than_selectivity) ||
get_selectivity_dispatcher(items[2], enum_operator::GREATER_THAN,
typelib, &greater_than_selectivity))
return true;
*selectivity = this->get_non_null_values_fraction() -
(less_than_selectivity + greater_than_selectivity);
/*
Make sure that we don't return a value less than 0.0. This might happen
with a query like:
EXPLAIN SELECT a FROM t1 WHERE t1.a BETWEEN 3 AND 0;
*/
*selectivity = std::max(0.0, *selectivity);
return false;
}
case enum_operator::NOT_BETWEEN: {
double less_than_selectivity;
double greater_than_selectivity;
if (get_selectivity_dispatcher(items[1], enum_operator::LESS_THAN,
typelib, &less_than_selectivity) ||
get_selectivity_dispatcher(items[2], enum_operator::GREATER_THAN,
typelib, &greater_than_selectivity))
return true;
/*
Make sure that we don't return a value greater than 1.0. This might
happen with a query like:
EXPLAIN SELECT a FROM t1 WHERE t1.a NOT BETWEEN 3 AND 0;
*/
*selectivity = std::min(less_than_selectivity + greater_than_selectivity,
get_non_null_values_fraction());
return false;
}
/*
TODO(Tobias Christiani): Improve IN selectivity estimates by ensuring that
selectivity estimates from within each bucket do not exceed the bucket
frequency. This can be done without allocating additional memory if we
sort the list of items and "merge" them with the histogram buckets.
*/
case enum_operator::IN_LIST: {
*selectivity = 0.0;
for (size_t i = 1; i < item_count; ++i) {
double equals_to_selectivity;
if (get_selectivity_dispatcher(items[i], enum_operator::EQUALS_TO,
typelib, &equals_to_selectivity))
return true;
*selectivity += equals_to_selectivity;
if (*selectivity >= get_non_null_values_fraction()) break;
}
/*
Long in-lists may easily exceed a selectivity of
get_non_null_values_fraction() in certain cases.
*/
*selectivity = std::min(*selectivity, get_non_null_values_fraction());
return false;
}
case enum_operator::NOT_IN_LIST: {
*selectivity = this->get_non_null_values_fraction();
for (size_t i = 1; i < item_count; ++i) {
double equals_to_selectivity;
if (get_selectivity_dispatcher(items[i], enum_operator::EQUALS_TO,
typelib, &equals_to_selectivity)) {
if (items[i]->null_value) {
// WHERE col1 NOT IN (..., NULL, ...) will return zero rows.
*selectivity = 0.0;
return false;
}
return true; /* purecov: deadcode */
}
*selectivity -= equals_to_selectivity;
if (*selectivity <= 0.0) break;
}
/*
Long in-lists may easily estimate a selectivity less than 0.0 in certain
cases.
*/
*selectivity = std::max(*selectivity, 0.0);
return false;
}
case enum_operator::IS_NULL:
*selectivity = this->get_null_values_fraction();
return false;
case enum_operator::IS_NOT_NULL:
*selectivity = 1.0 - this->get_null_values_fraction();
return false;
}
/* purecov: begin deadcode */
assert(false);
return true;
/* purecov: end deadcode */
}
// Explicit template instantiations.
template Histogram *build_histogram(MEM_ROOT *, const Value_map<double> &,
size_t, const std::string &,
const std::string &, const std::string &);
template Histogram *build_histogram(MEM_ROOT *, const Value_map<String> &,
size_t, const std::string &,
const std::string &, const std::string &);
template Histogram *build_histogram(MEM_ROOT *, const Value_map<ulonglong> &,
size_t, const std::string &,
const std::string &, const std::string &);
template Histogram *build_histogram(MEM_ROOT *, const Value_map<longlong> &,
size_t, const std::string &,
const std::string &, const std::string &);
template Histogram *build_histogram(MEM_ROOT *, const Value_map<MYSQL_TIME> &,
size_t, const std::string &,
const std::string &, const std::string &);
template Histogram *build_histogram(MEM_ROOT *, const Value_map<my_decimal> &,
size_t, const std::string &,
const std::string &, const std::string &);
} // namespace histograms
|