1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
use std::borrow::Borrow;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::convert::{TryFrom, TryInto};
use std::fmt::{self, Debug};
use std::mem::{size_of, transmute};
use std::ops::Deref;
use std::rc::Rc;
use std::str;
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike, Utc};
use compact_bytes::CompactBytes;
use mz_ore::cast::{CastFrom, ReinterpretCast};
use mz_ore::soft_assert_no_log;
use mz_ore::vec::Vector;
use mz_persist_types::Codec64;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use ordered_float::OrderedFloat;
use proptest::prelude::*;
use proptest::strategy::{BoxedStrategy, Strategy};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::adt::array::{
Array, ArrayDimension, ArrayDimensions, InvalidArrayError, MAX_ARRAY_DIMENSIONS,
};
use crate::adt::date::Date;
use crate::adt::interval::Interval;
use crate::adt::mz_acl_item::{AclItem, MzAclItem};
use crate::adt::numeric;
use crate::adt::numeric::Numeric;
use crate::adt::range::{
self, InvalidRangeError, Range, RangeBound, RangeInner, RangeLowerBound, RangeUpperBound,
};
use crate::adt::timestamp::CheckedTimestamp;
use crate::scalar::{arb_datum, DatumKind};
use crate::{Datum, RelationDesc, Timestamp};
pub mod collection;
pub(crate) mod encode;
pub mod iter;
include!(concat!(env!("OUT_DIR"), "/mz_repr.row.rs"));
/// A packed representation for `Datum`s.
///
/// `Datum` is easy to work with but very space inefficient. A `Datum::Int32(42)`
/// is laid out in memory like this:
///
/// tag: 3
/// padding: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
/// data: 0 0 0 42
/// padding: 0 0 0 0 0 0 0 0 0 0 0 0
///
/// For a total of 32 bytes! The second set of padding is needed in case we were
/// to write a 16-byte datum into this location. The first set of padding is
/// needed to align that hypothetical decimal to a 16 bytes boundary.
///
/// A `Row` stores zero or more `Datum`s without any padding. We avoid the need
/// for the first set of padding by only providing access to the `Datum`s via
/// calls to `ptr::read_unaligned`, which on modern x86 is barely penalized. We
/// avoid the need for the second set of padding by not providing mutable access
/// to the `Datum`. Instead, `Row` is append-only.
///
/// A `Row` can be built from a collection of `Datum`s using `Row::pack`, but it
/// is more efficient to use `Row::pack_slice` so that a right-sized allocation
/// can be created. If that is not possible, consider using the row buffer
/// pattern: allocate one row, pack into it, and then call [`Row::clone`] to
/// receive a copy of that row, leaving behind the original allocation to pack
/// future rows.
///
/// Creating a row via [`Row::pack_slice`]:
///
/// ```
/// # use mz_repr::{Row, Datum};
/// let row = Row::pack_slice(&[Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)]);
/// assert_eq!(row.unpack(), vec![Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)])
/// ```
///
/// `Row`s can be unpacked by iterating over them:
///
/// ```
/// # use mz_repr::{Row, Datum};
/// let row = Row::pack_slice(&[Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)]);
/// assert_eq!(row.iter().nth(1).unwrap(), Datum::Int32(1));
/// ```
///
/// If you want random access to the `Datum`s in a `Row`, use `Row::unpack` to create a `Vec<Datum>`
/// ```
/// # use mz_repr::{Row, Datum};
/// let row = Row::pack_slice(&[Datum::Int32(0), Datum::Int32(1), Datum::Int32(2)]);
/// let datums = row.unpack();
/// assert_eq!(datums[1], Datum::Int32(1));
/// ```
///
/// # Performance
///
/// Rows are dynamically sized, but up to a fixed size their data is stored in-line.
/// It is best to re-use a `Row` across multiple `Row` creation calls, as this
/// avoids the allocations involved in `Row::new()`.
#[derive(Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Row {
data: CompactBytes,
}
impl Row {
const SIZE: usize = CompactBytes::MAX_INLINE;
/// A variant of `Row::from_proto` that allows for reuse of internal allocs
/// and validates the decoding against a provided [`RelationDesc`].
pub fn decode_from_proto(
&mut self,
proto: &ProtoRow,
desc: &RelationDesc,
) -> Result<(), String> {
let mut col_idx = 0;
let mut packer = self.packer();
for d in proto.datums.iter() {
packer.try_push_proto(d)?;
col_idx += 1;
}
let num_columns = desc.typ().column_types.len();
if col_idx < num_columns {
let missing_columns = col_idx..num_columns;
for _ in missing_columns {
packer.push(Datum::Null);
col_idx += 1;
}
}
mz_ore::soft_assert_eq_or_log!(
col_idx,
num_columns,
"wrong number of columns when decoding a Row!, got {row:?}, expected {desc:?}",
row = self,
desc = desc,
);
Ok(())
}
/// Allocate an empty `Row` with a pre-allocated capacity.
#[inline]
pub fn with_capacity(cap: usize) -> Self {
Self {
data: CompactBytes::with_capacity(cap),
}
}
/// Creates a new row from supplied bytes.
///
/// # Safety
///
/// This method relies on `data` being an appropriate row encoding, and can
/// result in unsafety if this is not the case.
pub unsafe fn from_bytes_unchecked(data: &[u8]) -> Self {
Row {
data: CompactBytes::new(data),
}
}
/// Constructs a [`RowPacker`] that will pack datums into this row's
/// allocation.
///
/// This method clears the existing contents of the row, but retains the
/// allocation.
pub fn packer(&mut self) -> RowPacker<'_> {
self.data.clear();
RowPacker { row: self }
}
/// Take some `Datum`s and pack them into a `Row`.
///
/// This method builds a `Row` by repeatedly increasing the backing
/// allocation. If the contents of the iterator are known ahead of
/// time, consider [`Row::with_capacity`] to right-size the allocation
/// first, and then [`RowPacker::extend`] to populate it with `Datum`s.
/// This avoids the repeated allocation resizing and copying.
pub fn pack<'a, I, D>(iter: I) -> Row
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
let mut row = Row::default();
row.packer().extend(iter);
row
}
/// Use `self` to pack `iter`, and then clone the result.
///
/// This is a convenience method meant to reduce boilerplate around row
/// formation.
pub fn pack_using<'a, I, D>(&mut self, iter: I) -> Row
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
self.packer().extend(iter);
self.clone()
}
/// Like [`Row::pack`], but the provided iterator is allowed to produce an
/// error, in which case the packing operation is aborted and the error
/// returned.
pub fn try_pack<'a, I, D, E>(iter: I) -> Result<Row, E>
where
I: IntoIterator<Item = Result<D, E>>,
D: Borrow<Datum<'a>>,
{
let mut row = Row::default();
row.packer().try_extend(iter)?;
Ok(row)
}
/// Pack a slice of `Datum`s into a `Row`.
///
/// This method has the advantage over `pack` that it can determine the required
/// allocation before packing the elements, ensuring only one allocation and no
/// redundant copies required.
pub fn pack_slice<'a>(slice: &[Datum<'a>]) -> Row {
// Pre-allocate the needed number of bytes.
let mut row = Row::with_capacity(datums_size(slice.iter()));
row.packer().extend(slice.iter());
row
}
/// Returns the total amount of bytes used by this row.
pub fn byte_len(&self) -> usize {
let heap_size = if self.data.spilled() {
self.data.len()
} else {
0
};
let inline_size = std::mem::size_of::<Self>();
inline_size.saturating_add(heap_size)
}
/// Returns the total capacity in bytes used by this row.
pub fn byte_capacity(&self) -> usize {
self.data.capacity()
}
/// Extracts a Row slice containing the entire [`Row`].
#[inline]
pub fn as_row_ref(&self) -> &RowRef {
RowRef::from_slice(self.data.as_slice())
}
}
impl Borrow<RowRef> for Row {
#[inline]
fn borrow(&self) -> &RowRef {
self.as_row_ref()
}
}
impl AsRef<RowRef> for Row {
#[inline]
fn as_ref(&self) -> &RowRef {
self.as_row_ref()
}
}
impl Deref for Row {
type Target = RowRef;
#[inline]
fn deref(&self) -> &Self::Target {
self.as_row_ref()
}
}
// Nothing depends on Row being exactly 24, we just want to add visibility to the size.
static_assertions::const_assert_eq!(std::mem::size_of::<Row>(), 24);
impl Clone for Row {
fn clone(&self) -> Self {
Row {
data: self.data.clone(),
}
}
fn clone_from(&mut self, source: &Self) {
self.data.clone_from(&source.data);
}
}
impl Arbitrary for Row {
type Parameters = prop::collection::SizeRange;
type Strategy = BoxedStrategy<Row>;
fn arbitrary_with(size: Self::Parameters) -> Self::Strategy {
prop::collection::vec(arb_datum(), size)
.prop_map(|items| {
let mut row = Row::default();
let mut packer = row.packer();
for item in items.iter() {
let datum: Datum<'_> = item.into();
packer.push(datum);
}
row
})
.boxed()
}
}
impl PartialOrd for Row {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Row {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_ref().cmp(other.as_ref())
}
}
#[allow(missing_debug_implementations)]
mod columnation {
use columnation::{Columnation, Region};
use mz_ore::region::LgAllocRegion;
use crate::Row;
/// Region allocation for `Row` data.
///
/// Content bytes are stored in stable contiguous memory locations,
/// and then a `Row` referencing them is falsified.
pub struct RowStack {
region: LgAllocRegion<u8>,
}
impl RowStack {
const LIMIT: usize = 2 << 20;
}
// Implement `Default` manually to specify a region allocation limit.
impl Default for RowStack {
fn default() -> Self {
Self {
// Limit the region size to 2MiB.
region: LgAllocRegion::with_limit(Self::LIMIT),
}
}
}
impl Columnation for Row {
type InnerRegion = RowStack;
}
impl Region for RowStack {
type Item = Row;
#[inline]
fn clear(&mut self) {
self.region.clear();
}
#[inline(always)]
unsafe fn copy(&mut self, item: &Row) -> Row {
if item.data.spilled() {
let bytes = self.region.copy_slice(&item.data[..]);
Row {
data: compact_bytes::CompactBytes::from_raw_parts(
bytes.as_mut_ptr(),
item.data.len(),
item.data.capacity(),
),
}
} else {
item.clone()
}
}
fn reserve_items<'a, I>(&mut self, items: I)
where
Self: 'a,
I: Iterator<Item = &'a Self::Item> + Clone,
{
let size = items
.filter(|row| row.data.spilled())
.map(|row| row.data.len())
.sum();
let size = std::cmp::min(size, Self::LIMIT);
self.region.reserve(size);
}
fn reserve_regions<'a, I>(&mut self, regions: I)
where
Self: 'a,
I: Iterator<Item = &'a Self> + Clone,
{
let size = regions.map(|r| r.region.len()).sum();
let size = std::cmp::min(size, Self::LIMIT);
self.region.reserve(size);
}
fn heap_size(&self, callback: impl FnMut(usize, usize)) {
self.region.heap_size(callback)
}
}
}
/// A contiguous slice of bytes that are row data.
///
/// A [`RowRef`] is to [`Row`] as [`prim@str`] is to [`String`].
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct RowRef([u8]);
impl RowRef {
/// Create a [`RowRef`] from a slice of data.
///
/// We do not check that the provided slice is valid [`Row`] data, will panic on read
/// if the data is invalid.
pub fn from_slice(row: &[u8]) -> &RowRef {
#[allow(clippy::as_conversions)]
let ptr = row as *const [u8] as *const RowRef;
// SAFETY: We know `ptr` is non-null and aligned because it came from a &[u8].
unsafe { &*ptr }
}
/// Unpack `self` into a `Vec<Datum>` for efficient random access.
pub fn unpack(&self) -> Vec<Datum> {
// It's usually cheaper to unpack twice to figure out the right length than it is to grow the vec as we go
let len = self.iter().count();
let mut vec = Vec::with_capacity(len);
vec.extend(self.iter());
vec
}
/// Return the first [`Datum`] in `self`
///
/// Panics if the [`RowRef`] is empty.
pub fn unpack_first(&self) -> Datum {
self.iter().next().unwrap()
}
/// Iterate the [`Datum`] elements of the [`RowRef`].
pub fn iter(&self) -> DatumListIter {
DatumListIter {
data: &self.0,
offset: 0,
}
}
/// For debugging only.
pub fn data(&self) -> &[u8] {
&self.0
}
/// True iff there is no data in this [`RowRef`].
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl ToOwned for RowRef {
type Owned = Row;
fn to_owned(&self) -> Self::Owned {
// SAFETY: RowRef has the invariant that the wrapped data must be a valid Row encoding.
unsafe { Row::from_bytes_unchecked(&self.0) }
}
}
impl<'a> IntoIterator for &'a RowRef {
type Item = Datum<'a>;
type IntoIter = DatumListIter<'a>;
fn into_iter(self) -> DatumListIter<'a> {
DatumListIter {
data: &self.0,
offset: 0,
}
}
}
/// These implementations order first by length, and then by slice contents.
/// This allows many comparisons to complete without dereferencing memory.
/// Warning: These order by the u8 array representation, and NOT by Datum::cmp.
impl PartialOrd for RowRef {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RowRef {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.0.len().cmp(&other.0.len()) {
std::cmp::Ordering::Less => std::cmp::Ordering::Less,
std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
std::cmp::Ordering::Equal => self.0.cmp(&other.0),
}
}
}
impl fmt::Debug for RowRef {
/// Debug representation using the internal datums
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("RowRef{")?;
f.debug_list().entries(self.into_iter()).finish()?;
f.write_str("}")
}
}
/// Packs datums into a [`Row`].
///
/// Creating a `RowPacker` via [`Row::packer`] starts a packing operation on the
/// row. A packing operation always starts from scratch: the existing contents
/// of the underlying row are cleared.
///
/// To complete a packing operation, drop the `RowPacker`.
#[derive(Debug)]
pub struct RowPacker<'a> {
row: &'a mut Row,
}
#[derive(Debug, Clone)]
pub struct DatumListIter<'a> {
data: &'a [u8],
offset: usize,
}
#[derive(Debug, Clone)]
pub struct DatumDictIter<'a> {
data: &'a [u8],
offset: usize,
prev_key: Option<&'a str>,
}
/// `RowArena` is used to hold on to temporary `Row`s for functions like `eval` that need to create complex `Datum`s but don't have a `Row` to put them in yet.
#[derive(Debug)]
pub struct RowArena {
// Semantically, this field would be better represented by a `Vec<Box<[u8]>>`,
// as once the arena takes ownership of a byte vector the vector is never
// modified. But `RowArena::push_bytes` takes ownership of a `Vec<u8>`, so
// storing that `Vec<u8>` directly avoids an allocation. The cost is
// additional memory use, as the vector may have spare capacity, but row
// arenas are short lived so this is the better tradeoff.
inner: RefCell<Vec<Vec<u8>>>,
}
// DatumList and DatumDict defined here rather than near Datum because we need private access to the unsafe data field
/// A sequence of Datums
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct DatumList<'a> {
/// Points at the serialized datums
data: &'a [u8],
}
impl<'a> Debug for DatumList<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl Ord for DatumList<'_> {
fn cmp(&self, other: &DatumList) -> Ordering {
self.iter().cmp(other.iter())
}
}
impl PartialOrd for DatumList<'_> {
fn partial_cmp(&self, other: &DatumList) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// A mapping from string keys to Datums
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct DatumMap<'a> {
/// Points at the serialized datums, which should be sorted in key order
data: &'a [u8],
}
/// Represents a single `Datum`, appropriate to be nested inside other
/// `Datum`s.
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct DatumNested<'a> {
val: &'a [u8],
}
impl<'a> std::fmt::Display for DatumNested<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
std::fmt::Display::fmt(&self.datum(), f)
}
}
impl<'a> std::fmt::Debug for DatumNested<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DatumNested")
.field("val", &self.datum())
.finish()
}
}
impl<'a> DatumNested<'a> {
// Figure out which bytes `read_datum` returns (e.g. including the tag),
// and then store a reference to those bytes, so we can "replay" this same
// call later on without storing the datum itself.
pub fn extract(data: &'a [u8], offset: &mut usize) -> DatumNested<'a> {
let start = *offset;
let _ = unsafe { read_datum(data, offset) };
DatumNested {
val: &data[start..*offset],
}
}
/// Returns the datum `self` contains.
pub fn datum(&self) -> Datum<'a> {
unsafe { read_datum(self.val, &mut 0) }
}
}
impl<'a> Ord for DatumNested<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.datum().cmp(&other.datum())
}
}
impl<'a> PartialOrd for DatumNested<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
// Prefer adding new tags to the end of the enum. Certain behavior, like row ordering and EXPLAIN
// PHYSICAL PLAN, rely on the ordering of this enum. Neither of these are breaking changes, but
// it's annoying when they change.
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
enum Tag {
Null,
False,
True,
Int16,
Int32,
Int64,
UInt8,
UInt32,
Float32,
Float64,
Date,
Time,
Timestamp,
TimestampTz,
Interval,
BytesTiny,
BytesShort,
BytesLong,
BytesHuge,
StringTiny,
StringShort,
StringLong,
StringHuge,
Uuid,
Array,
ListTiny,
ListShort,
ListLong,
ListHuge,
Dict,
JsonNull,
Dummy,
Numeric,
UInt16,
UInt64,
MzTimestamp,
Range,
MzAclItem,
AclItem,
// Everything except leap seconds and times beyond the range of
// i64 nanoseconds. (Note that Materialize does not support leap
// seconds, but this module does).
CheapTimestamp,
// Everything except leap seconds and times beyond the range of
// i64 nanoseconds. (Note that Materialize does not support leap
// seconds, but this module does).
CheapTimestampTz,
// The next several tags are for variable-length signed integer encoding.
// The basic idea is that `NonNegativeIntN_K` is used to encode a datum of type
// IntN whose actual value is positive or zero and fits in K bits, and similarly for
// NegativeIntN_K with negative values.
//
// The order of these tags matters, because we want to be able to choose the
// tag for a given datum quickly, with arithmetic, rather than slowly, with a
// stack of `if` statements.
//
// Separate tags for non-negative and negative numbers are used to avoid having to
// waste one bit in the actual data space to encode the sign.
NonNegativeInt16_0, // i.e., 0
NonNegativeInt16_8,
NonNegativeInt16_16,
NonNegativeInt32_0,
NonNegativeInt32_8,
NonNegativeInt32_16,
NonNegativeInt32_24,
NonNegativeInt32_32,
NonNegativeInt64_0,
NonNegativeInt64_8,
NonNegativeInt64_16,
NonNegativeInt64_24,
NonNegativeInt64_32,
NonNegativeInt64_40,
NonNegativeInt64_48,
NonNegativeInt64_56,
NonNegativeInt64_64,
NegativeInt16_0, // i.e., -1
NegativeInt16_8,
NegativeInt16_16,
NegativeInt32_0,
NegativeInt32_8,
NegativeInt32_16,
NegativeInt32_24,
NegativeInt32_32,
NegativeInt64_0,
NegativeInt64_8,
NegativeInt64_16,
NegativeInt64_24,
NegativeInt64_32,
NegativeInt64_40,
NegativeInt64_48,
NegativeInt64_56,
NegativeInt64_64,
// These are like the ones above, but for unsigned types. The
// situation is slightly simpler as we don't have negatives.
UInt8_0, // i.e., 0
UInt8_8,
UInt16_0,
UInt16_8,
UInt16_16,
UInt32_0,
UInt32_8,
UInt32_16,
UInt32_24,
UInt32_32,
UInt64_0,
UInt64_8,
UInt64_16,
UInt64_24,
UInt64_32,
UInt64_40,
UInt64_48,
UInt64_56,
UInt64_64,
}
impl Tag {
fn actual_int_length(self) -> Option<usize> {
use Tag::*;
let val = match self {
NonNegativeInt16_0 | NonNegativeInt32_0 | NonNegativeInt64_0 | UInt8_0 | UInt16_0
| UInt32_0 | UInt64_0 => 0,
NonNegativeInt16_8 | NonNegativeInt32_8 | NonNegativeInt64_8 | UInt8_8 | UInt16_8
| UInt32_8 | UInt64_8 => 1,
NonNegativeInt16_16 | NonNegativeInt32_16 | NonNegativeInt64_16 | UInt16_16
| UInt32_16 | UInt64_16 => 2,
NonNegativeInt32_24 | NonNegativeInt64_24 | UInt32_24 | UInt64_24 => 3,
NonNegativeInt32_32 | NonNegativeInt64_32 | UInt32_32 | UInt64_32 => 4,
NonNegativeInt64_40 | UInt64_40 => 5,
NonNegativeInt64_48 | UInt64_48 => 6,
NonNegativeInt64_56 | UInt64_56 => 7,
NonNegativeInt64_64 | UInt64_64 => 8,
NegativeInt16_0 | NegativeInt32_0 | NegativeInt64_0 => 0,
NegativeInt16_8 | NegativeInt32_8 | NegativeInt64_8 => 1,
NegativeInt16_16 | NegativeInt32_16 | NegativeInt64_16 => 2,
NegativeInt32_24 | NegativeInt64_24 => 3,
NegativeInt32_32 | NegativeInt64_32 => 4,
NegativeInt64_40 => 5,
NegativeInt64_48 => 6,
NegativeInt64_56 => 7,
NegativeInt64_64 => 8,
_ => return None,
};
Some(val)
}
}
// --------------------------------------------------------------------------------
// reading data
/// Read a byte slice starting at byte `offset`.
///
/// Updates `offset` to point to the first byte after the end of the read region.
fn read_untagged_bytes<'a>(data: &'a [u8], offset: &mut usize) -> &'a [u8] {
let len = u64::from_le_bytes(read_byte_array(data, offset));
let len = usize::cast_from(len);
let bytes = &data[*offset..(*offset + len)];
*offset += len;
bytes
}
/// Read a data whose length is encoded in the row before its contents.
///
/// Updates `offset` to point to the first byte after the end of the read region.
///
/// # Safety
///
/// This function is safe if the datum's length and contents were previously written by `push_lengthed_bytes`,
/// and it was only written with a `String` tag if it was indeed UTF-8.
unsafe fn read_lengthed_datum<'a>(data: &'a [u8], offset: &mut usize, tag: Tag) -> Datum<'a> {
let len = match tag {
Tag::BytesTiny | Tag::StringTiny | Tag::ListTiny => usize::from(read_byte(data, offset)),
Tag::BytesShort | Tag::StringShort | Tag::ListShort => {
usize::from(u16::from_le_bytes(read_byte_array(data, offset)))
}
Tag::BytesLong | Tag::StringLong | Tag::ListLong => {
usize::cast_from(u32::from_le_bytes(read_byte_array(data, offset)))
}
Tag::BytesHuge | Tag::StringHuge | Tag::ListHuge => {
usize::cast_from(u64::from_le_bytes(read_byte_array(data, offset)))
}
_ => unreachable!(),
};
let bytes = &data[*offset..(*offset + len)];
*offset += len;
match tag {
Tag::BytesTiny | Tag::BytesShort | Tag::BytesLong | Tag::BytesHuge => Datum::Bytes(bytes),
Tag::StringTiny | Tag::StringShort | Tag::StringLong | Tag::StringHuge => {
Datum::String(str::from_utf8_unchecked(bytes))
}
Tag::ListTiny | Tag::ListShort | Tag::ListLong | Tag::ListHuge => {
Datum::List(DatumList { data: bytes })
}
_ => unreachable!(),
}
}
fn read_byte(data: &[u8], offset: &mut usize) -> u8 {
let byte = data[*offset];
*offset += 1;
byte
}
/// Read `length` bytes from `data` at `offset`, updating the
/// latter. Extend the resulting buffer to an array of `N` bytes by
/// inserting `FILL` in the k most significant bytes, where k = N - length.
///
/// SAFETY:
/// * length <= N
/// * offset + length <= data.len()
unsafe fn read_byte_array_sign_extending<const N: usize, const FILL: u8>(
data: &[u8],
offset: &mut usize,
length: usize,
) -> [u8; N] {
let mut raw = [FILL; N];
for i in 0..length {
debug_assert!(i < raw.len());
debug_assert!(*offset + i < data.len());
*raw.get_unchecked_mut(i) = *data.get_unchecked(*offset + i);
}
*offset += length;
raw
}
/// Read `length` bytes from `data` at `offset`, updating the
/// latter. Extend the resulting buffer to a negative `N`-byte
/// twos complement integer by filling the remaining bits with 1.
///
/// SAFETY:
/// * length <= N
/// * offset + length <= data.len()
unsafe fn read_byte_array_extending_negative<const N: usize>(
data: &[u8],
offset: &mut usize,
length: usize,
) -> [u8; N] {
read_byte_array_sign_extending::<N, 255>(data, offset, length)
}
/// Read `length` bytes from `data` at `offset`, updating the
/// latter. Extend the resulting buffer to a positive or zero `N`-byte
/// twos complement integer by filling the remaining bits with 0.
///
/// SAFETY:
/// * length <= N
/// * offset + length <= data.len()
unsafe fn read_byte_array_extending_nonnegative<const N: usize>(
data: &[u8],
offset: &mut usize,
length: usize,
) -> [u8; N] {
read_byte_array_sign_extending::<N, 0>(data, offset, length)
}
pub(super) fn read_byte_array<const N: usize>(data: &[u8], offset: &mut usize) -> [u8; N] {
let mut raw = [0; N];
raw.copy_from_slice(&data[*offset..*offset + N]);
*offset += N;
raw
}
pub(super) fn read_date(data: &[u8], offset: &mut usize) -> Date {
let days = i32::from_le_bytes(read_byte_array(data, offset));
Date::from_pg_epoch(days).expect("unexpected date")
}
pub(super) fn read_naive_date(data: &[u8], offset: &mut usize) -> NaiveDate {
let year = i32::from_le_bytes(read_byte_array(data, offset));
let ordinal = u32::from_le_bytes(read_byte_array(data, offset));
NaiveDate::from_yo_opt(year, ordinal).unwrap()
}
pub(super) fn read_time(data: &[u8], offset: &mut usize) -> NaiveTime {
let secs = u32::from_le_bytes(read_byte_array(data, offset));
let nanos = u32::from_le_bytes(read_byte_array(data, offset));
NaiveTime::from_num_seconds_from_midnight_opt(secs, nanos).unwrap()
}
/// Read a datum starting at byte `offset`.
///
/// Updates `offset` to point to the first byte after the end of the read region.
///
/// # Safety
///
/// This function is safe if a `Datum` was previously written at this offset by `push_datum`.
/// Otherwise it could return invalid values, which is Undefined Behavior.
pub unsafe fn read_datum<'a>(data: &'a [u8], offset: &mut usize) -> Datum<'a> {
let tag = Tag::try_from_primitive(read_byte(data, offset)).expect("unknown row tag");
match tag {
Tag::Null => Datum::Null,
Tag::False => Datum::False,
Tag::True => Datum::True,
Tag::UInt8_0 | Tag::UInt8_8 => {
let i = u8::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::UInt8(i)
}
Tag::Int16 => {
let i = i16::from_le_bytes(read_byte_array(data, offset));
Datum::Int16(i)
}
Tag::NonNegativeInt16_0 | Tag::NonNegativeInt16_16 | Tag::NonNegativeInt16_8 => {
// SAFETY:`tag.actual_int_length()` is <= 16 for these tags,
// and `data` is big enough because it was encoded validly. These assumptions
// are checked in debug asserts.
let i = i16::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::Int16(i)
}
Tag::UInt16_0 | Tag::UInt16_8 | Tag::UInt16_16 => {
let i = u16::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::UInt16(i)
}
Tag::Int32 => {
let i = i32::from_le_bytes(read_byte_array(data, offset));
Datum::Int32(i)
}
Tag::NonNegativeInt32_0
| Tag::NonNegativeInt32_32
| Tag::NonNegativeInt32_8
| Tag::NonNegativeInt32_16
| Tag::NonNegativeInt32_24 => {
// SAFETY:`tag.actual_int_length()` is <= 32 for these tags,
// and `data` is big enough because it was encoded validly. These assumptions
// are checked in debug asserts.
let i = i32::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::Int32(i)
}
Tag::UInt32_0 | Tag::UInt32_8 | Tag::UInt32_16 | Tag::UInt32_24 | Tag::UInt32_32 => {
let i = u32::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::UInt32(i)
}
Tag::Int64 => {
let i = i64::from_le_bytes(read_byte_array(data, offset));
Datum::Int64(i)
}
Tag::NonNegativeInt64_0
| Tag::NonNegativeInt64_64
| Tag::NonNegativeInt64_8
| Tag::NonNegativeInt64_16
| Tag::NonNegativeInt64_24
| Tag::NonNegativeInt64_32
| Tag::NonNegativeInt64_40
| Tag::NonNegativeInt64_48
| Tag::NonNegativeInt64_56 => {
// SAFETY:`tag.actual_int_length()` is <= 64 for these tags,
// and `data` is big enough because it was encoded validly. These assumptions
// are checked in debug asserts.
let i = i64::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::Int64(i)
}
Tag::UInt64_0
| Tag::UInt64_8
| Tag::UInt64_16
| Tag::UInt64_24
| Tag::UInt64_32
| Tag::UInt64_40
| Tag::UInt64_48
| Tag::UInt64_56
| Tag::UInt64_64 => {
let i = u64::from_le_bytes(read_byte_array_extending_nonnegative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::UInt64(i)
}
Tag::NegativeInt16_0 | Tag::NegativeInt16_16 | Tag::NegativeInt16_8 => {
// SAFETY:`tag.actual_int_length()` is <= 16 for these tags,
// and `data` is big enough because it was encoded validly. These assumptions
// are checked in debug asserts.
let i = i16::from_le_bytes(read_byte_array_extending_negative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::Int16(i)
}
Tag::NegativeInt32_0
| Tag::NegativeInt32_32
| Tag::NegativeInt32_8
| Tag::NegativeInt32_16
| Tag::NegativeInt32_24 => {
// SAFETY:`tag.actual_int_length()` is <= 32 for these tags,
// and `data` is big enough because it was encoded validly. These assumptions
// are checked in debug asserts.
let i = i32::from_le_bytes(read_byte_array_extending_negative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::Int32(i)
}
Tag::NegativeInt64_0
| Tag::NegativeInt64_64
| Tag::NegativeInt64_8
| Tag::NegativeInt64_16
| Tag::NegativeInt64_24
| Tag::NegativeInt64_32
| Tag::NegativeInt64_40
| Tag::NegativeInt64_48
| Tag::NegativeInt64_56 => {
// SAFETY:`tag.actual_int_length()` is <= 64 for these tags,
// and `data` is big enough because the row was encoded validly. These assumptions
// are checked in debug asserts.
let i = i64::from_le_bytes(read_byte_array_extending_negative(
data,
offset,
tag.actual_int_length()
.expect("returns a value for variable-length-encoded integer tags"),
));
Datum::Int64(i)
}
Tag::UInt8 => {
let i = u8::from_le_bytes(read_byte_array(data, offset));
Datum::UInt8(i)
}
Tag::UInt16 => {
let i = u16::from_le_bytes(read_byte_array(data, offset));
Datum::UInt16(i)
}
Tag::UInt32 => {
let i = u32::from_le_bytes(read_byte_array(data, offset));
Datum::UInt32(i)
}
Tag::UInt64 => {
let i = u64::from_le_bytes(read_byte_array(data, offset));
Datum::UInt64(i)
}
Tag::Float32 => {
let f = f32::from_bits(u32::from_le_bytes(read_byte_array(data, offset)));
Datum::Float32(OrderedFloat::from(f))
}
Tag::Float64 => {
let f = f64::from_bits(u64::from_le_bytes(read_byte_array(data, offset)));
Datum::Float64(OrderedFloat::from(f))
}
Tag::Date => Datum::Date(read_date(data, offset)),
Tag::Time => Datum::Time(read_time(data, offset)),
Tag::CheapTimestamp => {
let ts = i64::from_le_bytes(read_byte_array(data, offset));
let secs = ts.div_euclid(1_000_000_000);
let nsecs: u32 = ts.rem_euclid(1_000_000_000).try_into().unwrap();
let ndt = DateTime::from_timestamp(secs, nsecs)
.expect("We only write round-trippable timestamps")
.naive_utc();
Datum::Timestamp(
CheckedTimestamp::from_timestamplike(ndt).expect("unexpected timestamp"),
)
}
Tag::CheapTimestampTz => {
let ts = i64::from_le_bytes(read_byte_array(data, offset));
let secs = ts.div_euclid(1_000_000_000);
let nsecs: u32 = ts.rem_euclid(1_000_000_000).try_into().unwrap();
let dt = DateTime::from_timestamp(secs, nsecs)
.expect("We only write round-trippable timestamps");
Datum::TimestampTz(
CheckedTimestamp::from_timestamplike(dt).expect("unexpected timestamp"),
)
}
Tag::Timestamp => {
let date = read_naive_date(data, offset);
let time = read_time(data, offset);
Datum::Timestamp(
CheckedTimestamp::from_timestamplike(date.and_time(time))
.expect("unexpected timestamp"),
)
}
Tag::TimestampTz => {
let date = read_naive_date(data, offset);
let time = read_time(data, offset);
Datum::TimestampTz(
CheckedTimestamp::from_timestamplike(DateTime::from_naive_utc_and_offset(
date.and_time(time),
Utc,
))
.expect("unexpected timestamptz"),
)
}
Tag::Interval => {
let months = i32::from_le_bytes(read_byte_array(data, offset));
let days = i32::from_le_bytes(read_byte_array(data, offset));
let micros = i64::from_le_bytes(read_byte_array(data, offset));
Datum::Interval(Interval {
months,
days,
micros,
})
}
Tag::BytesTiny
| Tag::BytesShort
| Tag::BytesLong
| Tag::BytesHuge
| Tag::StringTiny
| Tag::StringShort
| Tag::StringLong
| Tag::StringHuge
| Tag::ListTiny
| Tag::ListShort
| Tag::ListLong
| Tag::ListHuge => read_lengthed_datum(data, offset, tag),
Tag::Uuid => Datum::Uuid(Uuid::from_bytes(read_byte_array(data, offset))),
Tag::Array => {
// See the comment in `Row::push_array` for details on the encoding
// of arrays.
let ndims = read_byte(data, offset);
let dims_size = usize::from(ndims) * size_of::<u64>() * 2;
let dims = &data[*offset..*offset + dims_size];
*offset += dims_size;
let data = read_untagged_bytes(data, offset);
Datum::Array(Array {
dims: ArrayDimensions { data: dims },
elements: DatumList { data },
})
}
Tag::Dict => {
let bytes = read_untagged_bytes(data, offset);
Datum::Map(DatumMap { data: bytes })
}
Tag::JsonNull => Datum::JsonNull,
Tag::Dummy => Datum::Dummy,
Tag::Numeric => {
let digits = read_byte(data, offset).into();
let exponent = i8::reinterpret_cast(read_byte(data, offset));
let bits = read_byte(data, offset);
let lsu_u16_len = Numeric::digits_to_lsu_elements_len(digits);
let lsu_u8_len = lsu_u16_len * 2;
let lsu_u8 = &data[*offset..(*offset + lsu_u8_len)];
*offset += lsu_u8_len;
// TODO: if we refactor the decimal library to accept the owned
// array as a parameter to `from_raw_parts` below, we could likely
// avoid a copy because it is exactly the value we want
let mut lsu = [0; numeric::NUMERIC_DATUM_WIDTH_USIZE];
for (i, c) in lsu_u8.chunks(2).enumerate() {
lsu[i] = u16::from_le_bytes(c.try_into().unwrap());
}
let d = Numeric::from_raw_parts(digits, exponent.into(), bits, lsu);
Datum::from(d)
}
Tag::MzTimestamp => {
let t = Timestamp::decode(read_byte_array(data, offset));
Datum::MzTimestamp(t)
}
Tag::Range => {
// See notes on `push_range_with` for details about encoding.
let flag_byte = read_byte(data, offset);
let flags = range::InternalFlags::from_bits(flag_byte)
.expect("range flags must be encoded validly");
if flags.contains(range::InternalFlags::EMPTY) {
assert!(
flags == range::InternalFlags::EMPTY,
"empty ranges contain only RANGE_EMPTY flag"
);
return Datum::Range(Range { inner: None });
}
let lower_bound = if flags.contains(range::InternalFlags::LB_INFINITE) {
None
} else {
Some(DatumNested::extract(data, offset))
};
let lower = RangeBound {
inclusive: flags.contains(range::InternalFlags::LB_INCLUSIVE),
bound: lower_bound,
};
let upper_bound = if flags.contains(range::InternalFlags::UB_INFINITE) {
None
} else {
Some(DatumNested::extract(data, offset))
};
let upper = RangeBound {
inclusive: flags.contains(range::InternalFlags::UB_INCLUSIVE),
bound: upper_bound,
};
Datum::Range(Range {
inner: Some(RangeInner { lower, upper }),
})
}
Tag::MzAclItem => {
const N: usize = MzAclItem::binary_size();
let mz_acl_item = MzAclItem::decode_binary(&read_byte_array::<N>(data, offset))
.expect("invalid mz_aclitem");
Datum::MzAclItem(mz_acl_item)
}
Tag::AclItem => {
const N: usize = AclItem::binary_size();
let acl_item = AclItem::decode_binary(&read_byte_array::<N>(data, offset))
.expect("invalid aclitem");
Datum::AclItem(acl_item)
}
}
}
// --------------------------------------------------------------------------------
// writing data
fn push_untagged_bytes<D>(data: &mut D, bytes: &[u8])
where
D: Vector<u8>,
{
let len = u64::cast_from(bytes.len());
data.extend_from_slice(&len.to_le_bytes());
data.extend_from_slice(bytes);
}
fn push_lengthed_bytes<D>(data: &mut D, bytes: &[u8], tag: Tag)
where
D: Vector<u8>,
{
match tag {
Tag::BytesTiny | Tag::StringTiny | Tag::ListTiny => {
let len = bytes.len().to_le_bytes();
data.push(len[0]);
}
Tag::BytesShort | Tag::StringShort | Tag::ListShort => {
let len = bytes.len().to_le_bytes();
data.extend_from_slice(&len[0..2]);
}
Tag::BytesLong | Tag::StringLong | Tag::ListLong => {
let len = bytes.len().to_le_bytes();
data.extend_from_slice(&len[0..4]);
}
Tag::BytesHuge | Tag::StringHuge | Tag::ListHuge => {
let len = bytes.len().to_le_bytes();
data.extend_from_slice(&len);
}
_ => unreachable!(),
}
data.extend_from_slice(bytes);
}
pub(super) fn date_to_array(date: Date) -> [u8; size_of::<i32>()] {
i32::to_le_bytes(date.pg_epoch_days())
}
fn push_date<D>(data: &mut D, date: Date)
where
D: Vector<u8>,
{
data.extend_from_slice(&date_to_array(date));
}
pub(super) fn naive_date_to_arrays(
date: NaiveDate,
) -> ([u8; size_of::<i32>()], [u8; size_of::<u32>()]) {
(
i32::to_le_bytes(date.year()),
u32::to_le_bytes(date.ordinal()),
)
}
fn push_naive_date<D>(data: &mut D, date: NaiveDate)
where
D: Vector<u8>,
{
let (ds1, ds2) = naive_date_to_arrays(date);
data.extend_from_slice(&ds1);
data.extend_from_slice(&ds2);
}
pub(super) fn time_to_arrays(time: NaiveTime) -> ([u8; size_of::<u32>()], [u8; size_of::<u32>()]) {
(
u32::to_le_bytes(time.num_seconds_from_midnight()),
u32::to_le_bytes(time.nanosecond()),
)
}
fn push_time<D>(data: &mut D, time: NaiveTime)
where
D: Vector<u8>,
{
let (ts1, ts2) = time_to_arrays(time);
data.extend_from_slice(&ts1);
data.extend_from_slice(&ts2);
}
/// Returns an i64 representing a `NaiveDateTime`, if
/// said i64 can be round-tripped back to a `NaiveDateTime`.
///
/// The only exotic NDTs for which this can't happen are those that
/// are hundreds of years in the future or past, or those that
/// represent a leap second. (Note that Materialize does not support
/// leap seconds, but this module does).
// This function is inspired by `NaiveDateTime::timestamp_nanos`,
// with extra checking.
fn checked_timestamp_nanos(dt: NaiveDateTime) -> Option<i64> {
let subsec_nanos = dt.timestamp_subsec_nanos();
if subsec_nanos >= 1_000_000_000 {
return None;
}
let as_ns = dt.and_utc().timestamp().checked_mul(1_000_000_000)?;
as_ns.checked_add(i64::from(subsec_nanos))
}
// This function is extremely hot, so
// we just use `as` to avoid the overhead of
// `try_into` followed by `unwrap`.
// `leading_ones` and `leading_zeros`
// can never return values greater than 64, so the conversion is safe.
#[inline(always)]
#[allow(clippy::as_conversions)]
fn min_bytes_signed<T>(i: T) -> u8
where
T: Into<i64>,
{
let i: i64 = i.into();
// To fit in n bytes, we require that
// everything but the leading sign bits fits in n*8
// bits.
let n_sign_bits = if i.is_negative() {
i.leading_ones() as u8
} else {
i.leading_zeros() as u8
};
(64 - n_sign_bits + 7) / 8
}
// In principle we could just use `min_bytes_signed`, rather than
// having a separate function here, as long as we made that one take
// `T: Into<i128>` instead of 64. But LLVM doesn't seem smart enough
// to realize that that function is the same as the current version,
// and generates worse code.
//
// Justification for `as` is the same as in `min_bytes_signed`.
#[inline(always)]
#[allow(clippy::as_conversions)]
fn min_bytes_unsigned<T>(i: T) -> u8
where
T: Into<u64>,
{
let i: u64 = i.into();
let n_sign_bits = i.leading_zeros() as u8;
(64 - n_sign_bits + 7) / 8
}
const TINY: usize = 1 << 8;
const SHORT: usize = 1 << 16;
const LONG: usize = 1 << 32;
fn push_datum<D>(data: &mut D, datum: Datum)
where
D: Vector<u8>,
{
match datum {
Datum::Null => data.push(Tag::Null.into()),
Datum::False => data.push(Tag::False.into()),
Datum::True => data.push(Tag::True.into()),
Datum::Int16(i) => {
let mbs = min_bytes_signed(i);
let tag = u8::from(if i.is_negative() {
Tag::NegativeInt16_0
} else {
Tag::NonNegativeInt16_0
}) + mbs;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbs)]);
}
Datum::Int32(i) => {
let mbs = min_bytes_signed(i);
let tag = u8::from(if i.is_negative() {
Tag::NegativeInt32_0
} else {
Tag::NonNegativeInt32_0
}) + mbs;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbs)]);
}
Datum::Int64(i) => {
let mbs = min_bytes_signed(i);
let tag = u8::from(if i.is_negative() {
Tag::NegativeInt64_0
} else {
Tag::NonNegativeInt64_0
}) + mbs;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbs)]);
}
Datum::UInt8(i) => {
let mbu = min_bytes_unsigned(i);
let tag = u8::from(Tag::UInt8_0) + mbu;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbu)]);
}
Datum::UInt16(i) => {
let mbu = min_bytes_unsigned(i);
let tag = u8::from(Tag::UInt16_0) + mbu;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbu)]);
}
Datum::UInt32(i) => {
let mbu = min_bytes_unsigned(i);
let tag = u8::from(Tag::UInt32_0) + mbu;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbu)]);
}
Datum::UInt64(i) => {
let mbu = min_bytes_unsigned(i);
let tag = u8::from(Tag::UInt64_0) + mbu;
data.push(tag);
data.extend_from_slice(&i.to_le_bytes()[0..usize::from(mbu)]);
}
Datum::Float32(f) => {
data.push(Tag::Float32.into());
data.extend_from_slice(&f.to_bits().to_le_bytes());
}
Datum::Float64(f) => {
data.push(Tag::Float64.into());
data.extend_from_slice(&f.to_bits().to_le_bytes());
}
Datum::Date(d) => {
data.push(Tag::Date.into());
push_date(data, d);
}
Datum::Time(t) => {
data.push(Tag::Time.into());
push_time(data, t);
}
Datum::Timestamp(t) => {
let datetime = t.to_naive();
if let Some(nanos) = checked_timestamp_nanos(datetime) {
data.push(Tag::CheapTimestamp.into());
data.extend_from_slice(&nanos.to_le_bytes());
} else {
data.push(Tag::Timestamp.into());
push_naive_date(data, datetime.date());
push_time(data, datetime.time());
}
}
Datum::TimestampTz(t) => {
let datetime = t.to_naive();
if let Some(nanos) = checked_timestamp_nanos(datetime) {
data.push(Tag::CheapTimestampTz.into());
data.extend_from_slice(&nanos.to_le_bytes());
} else {
data.push(Tag::TimestampTz.into());
push_naive_date(data, datetime.date());
push_time(data, datetime.time());
}
}
Datum::Interval(i) => {
data.push(Tag::Interval.into());
data.extend_from_slice(&i.months.to_le_bytes());
data.extend_from_slice(&i.days.to_le_bytes());
data.extend_from_slice(&i.micros.to_le_bytes());
}
Datum::Bytes(bytes) => {
let tag = match bytes.len() {
0..TINY => Tag::BytesTiny,
TINY..SHORT => Tag::BytesShort,
SHORT..LONG => Tag::BytesLong,
_ => Tag::BytesHuge,
};
data.push(tag.into());
push_lengthed_bytes(data, bytes, tag);
}
Datum::String(string) => {
let tag = match string.len() {
0..TINY => Tag::StringTiny,
TINY..SHORT => Tag::StringShort,
SHORT..LONG => Tag::StringLong,
_ => Tag::StringHuge,
};
data.push(tag.into());
push_lengthed_bytes(data, string.as_bytes(), tag);
}
Datum::List(list) => {
let tag = match list.data.len() {
0..TINY => Tag::ListTiny,
TINY..SHORT => Tag::ListShort,
SHORT..LONG => Tag::ListLong,
_ => Tag::ListHuge,
};
data.push(tag.into());
push_lengthed_bytes(data, list.data, tag);
}
Datum::Uuid(u) => {
data.push(Tag::Uuid.into());
data.extend_from_slice(u.as_bytes());
}
Datum::Array(array) => {
// See the comment in `Row::push_array` for details on the encoding
// of arrays.
data.push(Tag::Array.into());
data.push(array.dims.ndims());
data.extend_from_slice(array.dims.data);
push_untagged_bytes(data, array.elements.data);
}
Datum::Map(dict) => {
data.push(Tag::Dict.into());
push_untagged_bytes(data, dict.data);
}
Datum::JsonNull => data.push(Tag::JsonNull.into()),
Datum::MzTimestamp(t) => {
data.push(Tag::MzTimestamp.into());
data.extend_from_slice(&t.encode());
}
Datum::Dummy => data.push(Tag::Dummy.into()),
Datum::Numeric(mut n) => {
// Pseudo-canonical representation of decimal values with
// insignificant zeroes trimmed. This compresses the number further
// than `Numeric::trim` by removing all zeroes, and not only those in
// the fractional component.
numeric::cx_datum().reduce(&mut n.0);
let (digits, exponent, bits, lsu) = n.0.to_raw_parts();
data.push(Tag::Numeric.into());
data.push(u8::try_from(digits).expect("digits to fit within u8; should not exceed 39"));
data.push(
i8::try_from(exponent)
.expect("exponent to fit within i8; should not exceed +/- 39")
.to_le_bytes()[0],
);
data.push(bits);
let lsu = &lsu[..Numeric::digits_to_lsu_elements_len(digits)];
// Little endian machines can take the lsu directly from u16 to u8.
if cfg!(target_endian = "little") {
// SAFETY: `lsu` (returned by `coefficient_units()`) is a `&[u16]`, so
// each element can safely be transmuted into two `u8`s.
let (prefix, lsu_bytes, suffix) = unsafe { lsu.align_to::<u8>() };
// The `u8` aligned version of the `lsu` should have twice as many
// elements as we expect for the `u16` version.
soft_assert_no_log!(
lsu_bytes.len() == Numeric::digits_to_lsu_elements_len(digits) * 2,
"u8 version of numeric LSU contained the wrong number of elements; expected {}, but got {}",
Numeric::digits_to_lsu_elements_len(digits) * 2,
lsu_bytes.len()
);
// There should be no unaligned elements in the prefix or suffix.
soft_assert_no_log!(prefix.is_empty() && suffix.is_empty());
data.extend_from_slice(lsu_bytes);
} else {
for u in lsu {
data.extend_from_slice(&u.to_le_bytes());
}
}
}
Datum::Range(range) => {
// See notes on `push_range_with` for details about encoding.
data.push(Tag::Range.into());
data.push(range.internal_flag_bits());
if let Some(RangeInner { lower, upper }) = range.inner {
for bound in [lower.bound, upper.bound] {
if let Some(bound) = bound {
match bound.datum() {
Datum::Null => panic!("cannot push Datum::Null into range"),
d => push_datum::<D>(data, d),
}
}
}
}
}
Datum::MzAclItem(mz_acl_item) => {
data.push(Tag::MzAclItem.into());
data.extend_from_slice(&mz_acl_item.encode_binary());
}
Datum::AclItem(acl_item) => {
data.push(Tag::AclItem.into());
data.extend_from_slice(&acl_item.encode_binary());
}
}
}
/// Return the number of bytes these Datums would use if packed as a Row.
pub fn row_size<'a, I>(a: I) -> usize
where
I: IntoIterator<Item = Datum<'a>>,
{
// Using datums_size instead of a.data().len() here is safer because it will
// return the size of the datums if they were packed into a Row. Although
// a.data().len() happens to give the correct answer (and is faster), data()
// is documented as for debugging only.
let sz = datums_size::<_, _>(a);
let size_of_row = std::mem::size_of::<Row>();
// The Row struct attempts to inline data until it can't fit in the
// preallocated size. Otherwise it spills to heap, and uses the Row to point
// to that.
if sz > Row::SIZE {
sz + size_of_row
} else {
size_of_row
}
}
/// Number of bytes required by the datum.
/// This is used to optimistically pre-allocate buffers for packing rows.
pub fn datum_size(datum: &Datum) -> usize {
match datum {
Datum::Null => 1,
Datum::False => 1,
Datum::True => 1,
Datum::Int16(i) => 1 + usize::from(min_bytes_signed(*i)),
Datum::Int32(i) => 1 + usize::from(min_bytes_signed(*i)),
Datum::Int64(i) => 1 + usize::from(min_bytes_signed(*i)),
Datum::UInt8(i) => 1 + usize::from(min_bytes_unsigned(*i)),
Datum::UInt16(i) => 1 + usize::from(min_bytes_unsigned(*i)),
Datum::UInt32(i) => 1 + usize::from(min_bytes_unsigned(*i)),
Datum::UInt64(i) => 1 + usize::from(min_bytes_unsigned(*i)),
Datum::Float32(_) => 1 + size_of::<f32>(),
Datum::Float64(_) => 1 + size_of::<f64>(),
Datum::Date(_) => 1 + size_of::<i32>(),
Datum::Time(_) => 1 + 8,
Datum::Timestamp(t) => {
1 + if checked_timestamp_nanos(t.to_naive()).is_some() {
8
} else {
16
}
}
Datum::TimestampTz(t) => {
1 + if checked_timestamp_nanos(t.naive_utc()).is_some() {
8
} else {
16
}
}
Datum::Interval(_) => 1 + size_of::<i32>() + size_of::<i32>() + size_of::<i64>(),
Datum::Bytes(bytes) => {
// We use a variable length representation of slice length.
let bytes_for_length = match bytes.len() {
0..TINY => 1,
TINY..SHORT => 2,
SHORT..LONG => 4,
_ => 8,
};
1 + bytes_for_length + bytes.len()
}
Datum::String(string) => {
// We use a variable length representation of slice length.
let bytes_for_length = match string.len() {
0..TINY => 1,
TINY..SHORT => 2,
SHORT..LONG => 4,
_ => 8,
};
1 + bytes_for_length + string.len()
}
Datum::Uuid(_) => 1 + size_of::<uuid::Bytes>(),
Datum::Array(array) => {
1 + size_of::<u8>()
+ array.dims.data.len()
+ size_of::<u64>()
+ array.elements.data.len()
}
Datum::List(list) => 1 + size_of::<u64>() + list.data.len(),
Datum::Map(dict) => 1 + size_of::<u64>() + dict.data.len(),
Datum::JsonNull => 1,
Datum::MzTimestamp(_) => 1 + size_of::<Timestamp>(),
Datum::Dummy => 1,
Datum::Numeric(d) => {
let mut d = d.0.clone();
// Values must be reduced to determine appropriate number of
// coefficient units.
numeric::cx_datum().reduce(&mut d);
// 4 = 1 bit each for tag, digits, exponent, bits
4 + (d.coefficient_units().len() * 2)
}
Datum::Range(Range { inner }) => {
// Tag + flags
2 + match inner {
None => 0,
Some(RangeInner { lower, upper }) => [lower.bound, upper.bound]
.iter()
.map(|bound| match bound {
None => 0,
Some(bound) => bound.val.len(),
})
.sum(),
}
}
Datum::MzAclItem(_) => 1 + MzAclItem::binary_size(),
Datum::AclItem(_) => 1 + AclItem::binary_size(),
}
}
/// Number of bytes required by a sequence of datums.
///
/// This method can be used to right-size the allocation for a `Row`
/// before calling [`RowPacker::extend`].
pub fn datums_size<'a, I, D>(iter: I) -> usize
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
iter.into_iter().map(|d| datum_size(d.borrow())).sum()
}
/// Number of bytes required by a list of datums. This computes the size that would be required if
/// the given datums were packed into a list.
///
/// This is used to optimistically pre-allocate buffers for packing rows.
pub fn datum_list_size<'a, I, D>(iter: I) -> usize
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
1 + size_of::<u64>() + datums_size(iter)
}
impl RowPacker<'_> {
/// Constructs a row packer that will pack additional datums into the
/// provided row.
///
/// This function is intentionally somewhat inconvenient to call. You
/// usually want to call [`Row::packer`] instead to start packing from
/// scratch.
pub fn for_existing_row(row: &mut Row) -> RowPacker {
RowPacker { row }
}
/// Extend an existing `Row` with a `Datum`.
#[inline]
pub fn push<'a, D>(&mut self, datum: D)
where
D: Borrow<Datum<'a>>,
{
push_datum(&mut self.row.data, *datum.borrow());
}
/// Extend an existing `Row` with additional `Datum`s.
#[inline]
pub fn extend<'a, I, D>(&mut self, iter: I)
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
for datum in iter {
push_datum(&mut self.row.data, *datum.borrow())
}
}
/// Extend an existing `Row` with additional `Datum`s.
///
/// In the case the iterator produces an error, the pushing of
/// datums in terminated and the error returned. The `Row` will
/// be incomplete, but it will be safe to read datums from it.
#[inline]
pub fn try_extend<'a, I, E, D>(&mut self, iter: I) -> Result<(), E>
where
I: IntoIterator<Item = Result<D, E>>,
D: Borrow<Datum<'a>>,
{
for datum in iter {
push_datum(&mut self.row.data, *datum?.borrow());
}
Ok(())
}
/// Appends the datums of an entire `Row`.
pub fn extend_by_row(&mut self, row: &Row) {
self.row.data.extend_from_slice(row.data.as_slice());
}
/// Appends the slice of data representing an entire `Row`. The data is not validated.
///
/// # Safety
///
/// The requirements from [`Row::from_bytes_unchecked`] apply here, too:
/// This method relies on `data` being an appropriate row encoding, and can
/// result in unsafety if this is not the case.
#[inline]
pub unsafe fn extend_by_slice_unchecked(&mut self, data: &[u8]) {
self.row.data.extend_from_slice(data)
}
/// Pushes a [`DatumList`] that is built from a closure.
///
/// The supplied closure will be invoked once with a `Row` that can be used
/// to populate the list. It is valid to call any method on the
/// [`RowPacker`] except for [`RowPacker::clear`], [`RowPacker::truncate`],
/// or [`RowPacker::truncate_datums`].
///
/// Returns the value returned by the closure, if any.
///
/// ```
/// # use mz_repr::{Row, Datum};
/// let mut row = Row::default();
/// row.packer().push_list_with(|row| {
/// row.push(Datum::String("age"));
/// row.push(Datum::Int64(42));
/// });
/// assert_eq!(
/// row.unpack_first().unwrap_list().iter().collect::<Vec<_>>(),
/// vec![Datum::String("age"), Datum::Int64(42)],
/// );
/// ```
#[inline]
pub fn push_list_with<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut RowPacker) -> R,
{
// First, assume that the list will fit in 255 bytes, and thus the length will fit in
// 1 byte. If not, we'll fix it up later.
let start = self.row.data.len();
self.row.data.push(Tag::ListTiny.into());
// Write a dummy len, will fix it up later.
self.row.data.push(0);
let out = f(self);
// The `- 1 - 1` is for the tag and the len.
let len = self.row.data.len() - start - 1 - 1;
// We now know the real len.
if len < TINY {
// If the len fits in 1 byte, we just need to fix up the len.
self.row.data[start + 1] = len.to_le_bytes()[0];
} else {
// Note: We move this code path into its own function, so that the common case can be
// inlined.
long_list(&mut self.row.data, start, len);
}
/// 1. Fix up the tag.
/// 2. Move the actual data a bit (for which we also need to make room at the end).
/// 3. Fix up the len.
/// `data`: The row's backing data.
/// `start`: where `push_list_with` started writing in `data`.
/// `len`: the length of the data, excluding the tag and the length.
#[cold]
fn long_list(data: &mut CompactBytes, start: usize, len: usize) {
// `len_len`: the length of the length. (Possible values are: 2, 4, 8. 1 is handled
// elsewhere.) The other parameters are the same as for `long_list`.
let long_list_inner = |data: &mut CompactBytes, len_len| {
// We'll need memory for the new, bigger length, so make the `CompactBytes` bigger.
// The `- 1` is because the old length was 1 byte.
const ZEROS: [u8; 8] = [0; 8];
data.extend_from_slice(&ZEROS[0..len_len - 1]);
// Move the data to the end of the `CompactBytes`, to make space for the new length.
// Originally, it started after the 1-byte tag and the 1-byte length, now it will
// start after the 1-byte tag and the len_len-byte length.
//
// Note that this is the only operation in `long_list` whose cost is proportional
// to `len`. Since `len` is at least 256 here, the other operations' cost are
// negligible. `copy_within` is a memmove, which is probably a fair bit faster per
// Datum than a Datum encoding in the `f` closure.
data.copy_within(start + 1 + 1..start + 1 + 1 + len, start + 1 + len_len);
// Write the new length.
data[start + 1..start + 1 + len_len]
.copy_from_slice(&len.to_le_bytes()[0..len_len]);
};
match len {
0..TINY => {
unreachable!()
}
TINY..SHORT => {
data[start] = Tag::ListShort.into();
long_list_inner(data, 2);
}
SHORT..LONG => {
data[start] = Tag::ListLong.into();
long_list_inner(data, 4);
}
_ => {
data[start] = Tag::ListHuge.into();
long_list_inner(data, 8);
}
};
}
out
}
/// Pushes a [`DatumMap`] that is built from a closure.
///
/// The supplied closure will be invoked once with a `Row` that can be used
/// to populate the dict.
///
/// The closure **must** alternate pushing string keys and arbitrary values,
/// otherwise reading the dict will cause a panic.
///
/// The closure **must** push keys in ascending order, otherwise equality
/// checks on the resulting `Row` may be wrong and reading the dict IN DEBUG
/// MODE will cause a panic.
///
/// The closure **must not** call [`RowPacker::clear`],
/// [`RowPacker::truncate`], or [`RowPacker::truncate_datums`].
///
/// # Example
///
/// ```
/// # use mz_repr::{Row, Datum};
/// let mut row = Row::default();
/// row.packer().push_dict_with(|row| {
///
/// // key
/// row.push(Datum::String("age"));
/// // value
/// row.push(Datum::Int64(42));
///
/// // key
/// row.push(Datum::String("name"));
/// // value
/// row.push(Datum::String("bob"));
/// });
/// assert_eq!(
/// row.unpack_first().unwrap_map().iter().collect::<Vec<_>>(),
/// vec![("age", Datum::Int64(42)), ("name", Datum::String("bob"))]
/// );
/// ```
pub fn push_dict_with<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut RowPacker) -> R,
{
self.row.data.push(Tag::Dict.into());
let start = self.row.data.len();
// write a dummy len, will fix it up later
self.row.data.extend_from_slice(&[0; size_of::<u64>()]);
let res = f(self);
let len = u64::cast_from(self.row.data.len() - start - size_of::<u64>());
// fix up the len
self.row.data[start..start + size_of::<u64>()].copy_from_slice(&len.to_le_bytes());
res
}
/// Convenience function to construct an array from an iter of `Datum`s.
///
/// Returns an error if the number of elements in `iter` does not match
/// the cardinality of the array as described by `dims`, or if the
/// number of dimensions exceeds [`MAX_ARRAY_DIMENSIONS`]. If an error
/// occurs, the packer's state will be unchanged.
pub fn push_array<'a, I, D>(
&mut self,
dims: &[ArrayDimension],
iter: I,
) -> Result<(), InvalidArrayError>
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
// Arrays are encoded as follows.
//
// u8 ndims
// u64 dim_0 lower bound
// u64 dim_0 length
// ...
// u64 dim_n lower bound
// u64 dim_n length
// u64 element data size in bytes
// u8 element data, where elements are encoded in row-major order
if dims.len() > usize::from(MAX_ARRAY_DIMENSIONS) {
return Err(InvalidArrayError::TooManyDimensions(dims.len()));
}
let start = self.row.data.len();
self.row.data.push(Tag::Array.into());
// Write dimension information.
self.row
.data
.push(dims.len().try_into().expect("ndims verified to fit in u8"));
for dim in dims {
self.row
.data
.extend_from_slice(&i64::cast_from(dim.lower_bound).to_le_bytes());
self.row
.data
.extend_from_slice(&u64::cast_from(dim.length).to_le_bytes());
}
// Write elements.
let off = self.row.data.len();
self.row.data.extend_from_slice(&[0; size_of::<u64>()]);
let mut nelements = 0;
for datum in iter {
self.push(*datum.borrow());
nelements += 1;
}
let len = u64::cast_from(self.row.data.len() - off - size_of::<u64>());
self.row.data[off..off + size_of::<u64>()].copy_from_slice(&len.to_le_bytes());
// Check that the number of elements written matches the dimension
// information.
let cardinality = match dims {
[] => 0,
dims => dims.iter().map(|d| d.length).product(),
};
if nelements != cardinality {
self.row.data.truncate(start);
return Err(InvalidArrayError::WrongCardinality {
actual: nelements,
expected: cardinality,
});
}
Ok(())
}
/// Pushes an [`Array`] that is built from a closure.
///
/// __WARNING__: This is fairly "sharp" tool that is easy to get wrong. You
/// should prefer [`RowPacker::push_array`] when possible.
///
/// Returns an error if the number of elements pushed does not match
/// the cardinality of the array as described by `dims`, or if the
/// number of dimensions exceeds [`MAX_ARRAY_DIMENSIONS`]. If an error
/// occurs, the packer's state will be unchanged.
pub fn push_array_with_row_major<F, I>(
&mut self,
dims: I,
f: F,
) -> Result<(), InvalidArrayError>
where
I: IntoIterator<Item = ArrayDimension>,
F: FnOnce(&mut RowPacker) -> usize,
{
let start = self.row.data.len();
self.row.data.push(Tag::Array.into());
// Write dummy dimension length for now, we'll fix it up.
let dims_start = self.row.data.len();
self.row.data.push(42);
let mut num_dims: u8 = 0;
let mut cardinality: usize = 1;
for dim in dims {
num_dims += 1;
cardinality *= dim.length;
self.row
.data
.extend_from_slice(&i64::cast_from(dim.lower_bound).to_le_bytes());
self.row
.data
.extend_from_slice(&u64::cast_from(dim.length).to_le_bytes());
}
if num_dims > MAX_ARRAY_DIMENSIONS {
// Reset the packer state so we don't have invalid data.
self.row.data.truncate(start);
return Err(InvalidArrayError::TooManyDimensions(usize::from(num_dims)));
}
// Fix up our dimension length.
self.row.data[dims_start..dims_start + size_of::<u8>()]
.copy_from_slice(&num_dims.to_le_bytes());
// Write elements.
let off = self.row.data.len();
self.row.data.extend_from_slice(&[0; size_of::<u64>()]);
let nelements = f(self);
let len = u64::cast_from(self.row.data.len() - off - size_of::<u64>());
self.row.data[off..off + size_of::<u64>()].copy_from_slice(&len.to_le_bytes());
// Check that the number of elements written matches the dimension
// information.
let cardinality = match num_dims {
0 => 0,
_ => cardinality,
};
if nelements != cardinality {
self.row.data.truncate(start);
return Err(InvalidArrayError::WrongCardinality {
actual: nelements,
expected: cardinality,
});
}
Ok(())
}
/// Convenience function to push a `DatumList` from an iter of `Datum`s
///
/// See [`RowPacker::push_dict_with`] if you need to be able to handle errors
pub fn push_list<'a, I, D>(&mut self, iter: I)
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
self.push_list_with(|packer| {
for elem in iter {
packer.push(*elem.borrow())
}
});
}
/// Convenience function to push a `DatumMap` from an iter of `(&str, Datum)` pairs
pub fn push_dict<'a, I, D>(&mut self, iter: I)
where
I: IntoIterator<Item = (&'a str, D)>,
D: Borrow<Datum<'a>>,
{
self.push_dict_with(|packer| {
for (k, v) in iter {
packer.push(Datum::String(k));
packer.push(*v.borrow())
}
})
}
/// Pushes a `Datum::Range` derived from the `Range<Datum<'a>`.
///
/// # Panics
/// - If lower and upper express finite values and they are datums of
/// different types.
/// - If lower or upper express finite values and are equal to
/// `Datum::Null`. To handle `Datum::Null` properly, use
/// [`RangeBound::new`].
///
/// # Notes
/// - This function canonicalizes the range before pushing it to the row.
/// - Prefer this function over `push_range_with` because of its
/// canonicaliztion.
/// - Prefer creating [`RangeBound`]s using [`RangeBound::new`], which
/// handles `Datum::Null` in a SQL-friendly way.
pub fn push_range<'a>(&mut self, mut range: Range<Datum<'a>>) -> Result<(), InvalidRangeError> {
range.canonicalize()?;
match range.inner {
None => {
self.row.data.push(Tag::Range.into());
// Untagged bytes only contains the `RANGE_EMPTY` flag value.
self.row.data.push(range::InternalFlags::EMPTY.bits());
Ok(())
}
Some(inner) => self.push_range_with(
RangeLowerBound {
inclusive: inner.lower.inclusive,
bound: inner
.lower
.bound
.map(|value| move |row: &mut RowPacker| Ok(row.push(value))),
},
RangeUpperBound {
inclusive: inner.upper.inclusive,
bound: inner
.upper
.bound
.map(|value| move |row: &mut RowPacker| Ok(row.push(value))),
},
),
}
}
/// Pushes a `DatumRange` built from the specified arguments.
///
/// # Warning
/// Unlike `push_range`, `push_range_with` _does not_ canonicalize its
/// inputs. Consequentially, this means it's possible to generate ranges
/// that will not reflect the proper ordering and equality.
///
/// # Panics
/// - If lower or upper expresses a finite value and does not push exactly
/// one value into the `RowPacker`.
/// - If lower and upper express finite values and they are datums of
/// different types.
/// - If lower or upper express finite values and push `Datum::Null`.
///
/// # Notes
/// - Prefer `push_range_with` over this function. This function should be
/// used only when you are not pushing `Datum`s to the inner row.
/// - Range encoding is `[<flag bytes>,<lower>?,<upper>?]`, where `lower`
/// and `upper` are optional, contingent on the flag value expressing an
/// empty range (where neither will be present) or infinite bounds (where
/// each infinite bound will be absent).
/// - To push an emtpy range, use `push_range` using `Range { inner: None }`.
pub fn push_range_with<L, U, E>(
&mut self,
lower: RangeLowerBound<L>,
upper: RangeUpperBound<U>,
) -> Result<(), E>
where
L: FnOnce(&mut RowPacker) -> Result<(), E>,
U: FnOnce(&mut RowPacker) -> Result<(), E>,
E: From<InvalidRangeError>,
{
let start = self.row.data.len();
self.row.data.push(Tag::Range.into());
let mut flags = range::InternalFlags::empty();
flags.set(range::InternalFlags::LB_INFINITE, lower.bound.is_none());
flags.set(range::InternalFlags::UB_INFINITE, upper.bound.is_none());
flags.set(range::InternalFlags::LB_INCLUSIVE, lower.inclusive);
flags.set(range::InternalFlags::UB_INCLUSIVE, upper.inclusive);
let mut expected_datums = 0;
self.row.data.push(flags.bits());
let mut datum_check = self.row.data.len();
if let Some(value) = lower.bound {
let start = self.row.data.len();
value(self)?;
assert!(
start < self.row.data.len(),
"finite values must each push exactly one value; expected 1 but got 0"
);
expected_datums += 1;
}
if let Some(value) = upper.bound {
let start = self.row.data.len();
value(self)?;
assert!(
start < self.row.data.len(),
"finite values must each push exactly one value; expected 1 but got 0"
);
expected_datums += 1;
}
// Validate that what was written maintains the correct invariants.
let mut actual_datums = 0;
let mut seen = None;
while datum_check < self.row.data.len() {
let d = unsafe { read_datum(&self.row.data, &mut datum_check) };
assert!(d != Datum::Null, "cannot push Datum::Null into range");
match seen {
None => seen = Some(d),
Some(seen) => {
let seen_kind = DatumKind::from(seen);
let d_kind = DatumKind::from(d);
assert!(seen_kind == d_kind, "range contains inconsistent data; expected {seen_kind:?} but got {d_kind:?}");
if seen > d {
self.row.data.truncate(start);
return Err(InvalidRangeError::MisorderedRangeBounds.into());
}
}
}
actual_datums += 1;
}
assert!(actual_datums == expected_datums, "finite values must each push exactly one value; expected {expected_datums} but got {actual_datums}");
// Anything that triggers this check is undefined behavior, so
// unnecessary but also trivial to perform the check in our case.
assert!(
datum_check == self.row.data.len(),
"non-Datum data packed into row"
);
Ok(())
}
/// Clears the contents of the packer without de-allocating its backing memory.
pub fn clear(&mut self) {
self.row.data.clear();
}
/// Truncates the underlying storage to the specified byte position.
///
/// # Safety
///
/// `pos` MUST specify a byte offset that lies on a datum boundary.
/// If `pos` specifies a byte offset that is *within* a datum, the row
/// packer will produce an invalid row, the unpacking of which may
/// trigger undefined behavior!
///
/// To find the byte offset of a datum boundary, inspect the packer's
/// byte length by calling `packer.data().len()` after pushing the desired
/// number of datums onto the packer.
pub unsafe fn truncate(&mut self, pos: usize) {
self.row.data.truncate(pos)
}
/// Truncates the underlying row to contain at most the first `n` datums.
pub fn truncate_datums(&mut self, n: usize) {
let mut iter = self.row.iter();
for _ in iter.by_ref().take(n) {}
let offset = iter.offset;
// SAFETY: iterator offsets always lie on a datum boundary.
unsafe { self.truncate(offset) }
}
/// Returns the total amount of bytes used by the underlying row.
pub fn byte_len(&self) -> usize {
self.row.byte_len()
}
}
impl<'a> IntoIterator for &'a Row {
type Item = Datum<'a>;
type IntoIter = DatumListIter<'a>;
fn into_iter(self) -> DatumListIter<'a> {
self.iter()
}
}
impl fmt::Debug for Row {
/// Debug representation using the internal datums
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Row{")?;
f.debug_list().entries(self.iter()).finish()?;
f.write_str("}")
}
}
impl fmt::Display for Row {
/// Display representation using the internal datums
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("(")?;
for (i, datum) in self.iter().enumerate() {
if i != 0 {
f.write_str(", ")?;
}
write!(f, "{}", datum)?;
}
f.write_str(")")
}
}
impl<'a> DatumList<'a> {
pub fn empty() -> DatumList<'static> {
DatumList { data: &[] }
}
pub fn iter(&self) -> DatumListIter<'a> {
DatumListIter {
data: self.data,
offset: 0,
}
}
/// For debugging only
pub fn data(&self) -> &'a [u8] {
self.data
}
}
impl<'a> IntoIterator for &'a DatumList<'a> {
type Item = Datum<'a>;
type IntoIter = DatumListIter<'a>;
fn into_iter(self) -> DatumListIter<'a> {
self.iter()
}
}
impl<'a> Iterator for DatumListIter<'a> {
type Item = Datum<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.data.len() {
None
} else {
Some(unsafe { read_datum(self.data, &mut self.offset) })
}
}
}
impl<'a> DatumMap<'a> {
pub fn empty() -> DatumMap<'static> {
DatumMap { data: &[] }
}
pub fn iter(&self) -> DatumDictIter<'a> {
DatumDictIter {
data: self.data,
offset: 0,
prev_key: None,
}
}
/// For debugging only
pub fn data(&self) -> &'a [u8] {
self.data
}
}
impl<'a> Debug for DatumMap<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
impl<'a> IntoIterator for &'a DatumMap<'a> {
type Item = (&'a str, Datum<'a>);
type IntoIter = DatumDictIter<'a>;
fn into_iter(self) -> DatumDictIter<'a> {
self.iter()
}
}
impl<'a> Iterator for DatumDictIter<'a> {
type Item = (&'a str, Datum<'a>);
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.data.len() {
None
} else {
let key_tag = Tag::try_from_primitive(read_byte(self.data, &mut self.offset))
.expect("unknown row tag");
assert!(
key_tag == Tag::StringTiny
|| key_tag == Tag::StringShort
|| key_tag == Tag::StringLong
|| key_tag == Tag::StringHuge,
"Dict keys must be strings, got {:?}",
key_tag
);
let key =
unsafe { read_lengthed_datum(self.data, &mut self.offset, key_tag).unwrap_str() };
let val = unsafe { read_datum(self.data, &mut self.offset) };
// if in debug mode, sanity check keys
if cfg!(debug_assertions) {
if let Some(prev_key) = self.prev_key {
debug_assert!(
prev_key < key,
"Dict keys must be unique and given in ascending order: {} came before {}",
prev_key,
key
);
}
self.prev_key = Some(key);
}
Some((key, val))
}
}
}
impl RowArena {
pub fn new() -> Self {
RowArena {
inner: RefCell::new(vec![]),
}
}
/// Creates a `RowArena` with a hint of how many rows will be created in the arena, to avoid
/// reallocations of its internal vector.
pub fn with_capacity(capacity: usize) -> Self {
RowArena {
inner: RefCell::new(Vec::with_capacity(capacity)),
}
}
/// Does a `reserve` on the underlying `Vec`. Call this when you expect `additional` more datums
/// to be created in this arena.
pub fn reserve(&self, additional: usize) {
self.inner.borrow_mut().reserve(additional);
}
/// Take ownership of `bytes` for the lifetime of the arena.
#[allow(clippy::transmute_ptr_to_ptr)]
pub fn push_bytes<'a>(&'a self, bytes: Vec<u8>) -> &'a [u8] {
let mut inner = self.inner.borrow_mut();
inner.push(bytes);
let owned_bytes = &inner[inner.len() - 1];
unsafe {
// This is safe because:
// * We only ever append to self.inner, so the byte vector
// will live as long as the arena.
// * We return a reference to the byte vector's contents, so it's
// okay if self.inner reallocates and moves the byte
// vector.
// * We don't allow access to the byte vector itself, so it will
// never reallocate.
transmute::<&[u8], &'a [u8]>(owned_bytes)
}
}
/// Take ownership of `string` for the lifetime of the arena.
pub fn push_string<'a>(&'a self, string: String) -> &'a str {
let owned_bytes = self.push_bytes(string.into_bytes());
unsafe {
// This is safe because we know it was a `String` just before.
std::str::from_utf8_unchecked(owned_bytes)
}
}
/// Take ownership of `row` for the lifetime of the arena, returning a
/// reference to the first datum in the row.
///
/// If we had an owned datum type, this method would be much clearer, and
/// would be called `push_owned_datum`.
pub fn push_unary_row<'a>(&'a self, row: Row) -> Datum<'a> {
let mut inner = self.inner.borrow_mut();
inner.push(row.data.into_vec());
unsafe {
// This is safe because:
// * We only ever append to self.inner, so the row data will live
// as long as the arena.
// * We force the row data into its own heap allocation--
// importantly, we do NOT store the SmallVec, which might be
// storing data inline--so it's okay if self.inner reallocates
// and moves the row.
// * We don't allow access to the byte vector itself, so it will
// never reallocate.
let datum = read_datum(&inner[inner.len() - 1], &mut 0);
transmute::<Datum<'_>, Datum<'a>>(datum)
}
}
/// Equivalent to `push_unary_row` but returns a `DatumNested` rather than a
/// `Datum`.
fn push_unary_row_datum_nested<'a>(&'a self, row: Row) -> DatumNested<'a> {
let mut inner = self.inner.borrow_mut();
inner.push(row.data.into_vec());
unsafe {
// This is safe because:
// * We only ever append to self.inner, so the row data will live
// as long as the arena.
// * We force the row data into its own heap allocation--
// importantly, we do NOT store the SmallVec, which might be
// storing data inline--so it's okay if self.inner reallocates
// and moves the row.
// * We don't allow access to the byte vector itself, so it will
// never reallocate.
let nested = DatumNested::extract(&inner[inner.len() - 1], &mut 0);
transmute::<DatumNested<'_>, DatumNested<'a>>(nested)
}
}
/// Convenience function to make a new `Row` containing a single datum, and
/// take ownership of it for the lifetime of the arena
///
/// ```
/// # use mz_repr::{RowArena, Datum};
/// let arena = RowArena::new();
/// let datum = arena.make_datum(|packer| {
/// packer.push_list(&[Datum::String("hello"), Datum::String("world")]);
/// });
/// assert_eq!(datum.unwrap_list().iter().collect::<Vec<_>>(), vec![Datum::String("hello"), Datum::String("world")]);
/// ```
pub fn make_datum<'a, F>(&'a self, f: F) -> Datum<'a>
where
F: FnOnce(&mut RowPacker),
{
let mut row = Row::default();
f(&mut row.packer());
self.push_unary_row(row)
}
/// Convenience function identical to `make_datum` but instead returns a
/// `DatumNested`.
pub fn make_datum_nested<'a, F>(&'a self, f: F) -> DatumNested<'a>
where
F: FnOnce(&mut RowPacker),
{
let mut row = Row::default();
f(&mut row.packer());
self.push_unary_row_datum_nested(row)
}
/// Like [`RowArena::make_datum`], but the provided closure can return an error.
pub fn try_make_datum<'a, F, E>(&'a self, f: F) -> Result<Datum<'a>, E>
where
F: FnOnce(&mut RowPacker) -> Result<(), E>,
{
let mut row = Row::default();
f(&mut row.packer())?;
Ok(self.push_unary_row(row))
}
}
impl Default for RowArena {
fn default() -> RowArena {
RowArena::new()
}
}
/// A thread-local row, which can be borrowed and returned.
/// # Example
///
/// Use this type instead of creating a new row:
/// ```
/// use mz_repr::SharedRow;
///
/// let binding = SharedRow::get();
/// let mut row_builder = binding.borrow_mut();
/// ```
///
/// This allows us to reuse an existing row allocation instead of creating a new one or retaining
/// an allocation locally. Additionally, we can observe the size of the local row in a central
/// place and potentially reallocate to reduce memory needs.
///
/// # Panic
///
/// [`SharedRow::get`] panics when trying to obtain multiple references to the shared row.
#[derive(Debug)]
pub struct SharedRow(Rc<RefCell<Row>>);
impl SharedRow {
thread_local! {
static SHARED_ROW: Rc<RefCell<Row>> = Rc::new(RefCell::new(Row::default()));
}
/// Get the shared row.
///
/// The row's contents are cleared before returning it.
///
/// # Panic
///
/// Panics when the row is already borrowed elsewhere.
pub fn get() -> Self {
let row = Self::SHARED_ROW.with(Rc::clone);
// Clear row
row.borrow_mut().packer();
Self(row)
}
/// Gets the shared row and uses it to pack `iter`.
pub fn pack<'a, I, D>(iter: I) -> Row
where
I: IntoIterator<Item = D>,
D: Borrow<Datum<'a>>,
{
let binding = Self::SHARED_ROW.with(Rc::clone);
let mut row_builder = binding.borrow_mut();
let mut row_packer = row_builder.packer();
row_packer.extend(iter);
row_builder.clone()
}
}
impl std::ops::Deref for SharedRow {
type Target = RefCell<Row>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(test)]
mod tests {
use chrono::{DateTime, NaiveDate};
use mz_ore::assert_none;
use crate::ScalarType;
use super::*;
#[mz_ore::test]
fn test_assumptions() {
assert_eq!(size_of::<Tag>(), 1);
#[cfg(target_endian = "big")]
{
// if you want to run this on a big-endian cpu, we'll need big-endian versions of the serialization code
assert!(false);
}
}
#[mz_ore::test]
fn miri_test_arena() {
let arena = RowArena::new();
assert_eq!(arena.push_string("".to_owned()), "");
assert_eq!(arena.push_string("العَرَبِيَّة".to_owned()), "العَرَبِيَّة");
let empty: &[u8] = &[];
assert_eq!(arena.push_bytes(vec![]), empty);
assert_eq!(arena.push_bytes(vec![0, 2, 1, 255]), &[0, 2, 1, 255]);
let mut row = Row::default();
let mut packer = row.packer();
packer.push_dict_with(|row| {
row.push(Datum::String("a"));
row.push_list_with(|row| {
row.push(Datum::String("one"));
row.push(Datum::String("two"));
row.push(Datum::String("three"));
});
row.push(Datum::String("b"));
row.push(Datum::String("c"));
});
assert_eq!(arena.push_unary_row(row.clone()), row.unpack_first());
}
#[mz_ore::test]
fn miri_test_round_trip() {
fn round_trip(datums: Vec<Datum>) {
let row = Row::pack(datums.clone());
// When run under miri this catches undefined bytes written to data
// eg by calling push_copy! on a type which contains undefined padding values
println!("{:?}", row.data());
let datums2 = row.iter().collect::<Vec<_>>();
let datums3 = row.unpack();
assert_eq!(datums, datums2);
assert_eq!(datums, datums3);
}
round_trip(vec![]);
round_trip(
ScalarType::enumerate()
.iter()
.flat_map(|r#type| r#type.interesting_datums())
.collect(),
);
round_trip(vec![
Datum::Null,
Datum::Null,
Datum::False,
Datum::True,
Datum::Int16(-21),
Datum::Int32(-42),
Datum::Int64(-2_147_483_648 - 42),
Datum::UInt8(0),
Datum::UInt8(1),
Datum::UInt16(0),
Datum::UInt16(1),
Datum::UInt16(1 << 8),
Datum::UInt32(0),
Datum::UInt32(1),
Datum::UInt32(1 << 8),
Datum::UInt32(1 << 16),
Datum::UInt32(1 << 24),
Datum::UInt64(0),
Datum::UInt64(1),
Datum::UInt64(1 << 8),
Datum::UInt64(1 << 16),
Datum::UInt64(1 << 24),
Datum::UInt64(1 << 32),
Datum::UInt64(1 << 40),
Datum::UInt64(1 << 48),
Datum::UInt64(1 << 56),
Datum::Float32(OrderedFloat::from(-42.12)),
Datum::Float64(OrderedFloat::from(-2_147_483_648.0 - 42.12)),
Datum::Date(Date::from_pg_epoch(365 * 45 + 21).unwrap()),
Datum::Timestamp(
CheckedTimestamp::from_timestamplike(
NaiveDate::from_isoywd_opt(2019, 30, chrono::Weekday::Wed)
.unwrap()
.and_hms_opt(14, 32, 11)
.unwrap(),
)
.unwrap(),
),
Datum::TimestampTz(
CheckedTimestamp::from_timestamplike(DateTime::from_timestamp(61, 0).unwrap())
.unwrap(),
),
Datum::Interval(Interval {
months: 312,
..Default::default()
}),
Datum::Interval(Interval::new(0, 0, 1_012_312)),
Datum::Bytes(&[]),
Datum::Bytes(&[0, 2, 1, 255]),
Datum::String(""),
Datum::String("العَرَبِيَّة"),
]);
}
#[mz_ore::test]
fn test_array() {
// Construct an array using `Row::push_array` and verify that it unpacks
// correctly.
const DIM: ArrayDimension = ArrayDimension {
lower_bound: 2,
length: 2,
};
let mut row = Row::default();
let mut packer = row.packer();
packer
.push_array(&[DIM], vec![Datum::Int32(1), Datum::Int32(2)])
.unwrap();
let arr1 = row.unpack_first().unwrap_array();
assert_eq!(arr1.dims().into_iter().collect::<Vec<_>>(), vec![DIM]);
assert_eq!(
arr1.elements().into_iter().collect::<Vec<_>>(),
vec![Datum::Int32(1), Datum::Int32(2)]
);
// Pack a previously-constructed `Datum::Array` and verify that it
// unpacks correctly.
let row = Row::pack_slice(&[Datum::Array(arr1)]);
let arr2 = row.unpack_first().unwrap_array();
assert_eq!(arr1, arr2);
}
#[mz_ore::test]
fn test_multidimensional_array() {
let datums = vec![
Datum::Int32(1),
Datum::Int32(2),
Datum::Int32(3),
Datum::Int32(4),
Datum::Int32(5),
Datum::Int32(6),
Datum::Int32(7),
Datum::Int32(8),
];
let mut row = Row::default();
let mut packer = row.packer();
packer
.push_array(
&[
ArrayDimension {
lower_bound: 1,
length: 1,
},
ArrayDimension {
lower_bound: 1,
length: 4,
},
ArrayDimension {
lower_bound: 1,
length: 2,
},
],
&datums,
)
.unwrap();
let array = row.unpack_first().unwrap_array();
assert_eq!(array.elements().into_iter().collect::<Vec<_>>(), datums);
}
#[mz_ore::test]
fn test_array_max_dimensions() {
let mut row = Row::default();
let max_dims = usize::from(MAX_ARRAY_DIMENSIONS);
// An array with one too many dimensions should be rejected.
let res = row.packer().push_array(
&vec![
ArrayDimension {
lower_bound: 1,
length: 1
};
max_dims + 1
],
vec![Datum::Int32(4)],
);
assert_eq!(res, Err(InvalidArrayError::TooManyDimensions(max_dims + 1)));
assert!(row.data.is_empty());
// An array with exactly the maximum allowable dimensions should be
// accepted.
row.packer()
.push_array(
&vec![
ArrayDimension {
lower_bound: 1,
length: 1
};
max_dims
],
vec![Datum::Int32(4)],
)
.unwrap();
}
#[mz_ore::test]
fn test_array_wrong_cardinality() {
let mut row = Row::default();
let res = row.packer().push_array(
&[
ArrayDimension {
lower_bound: 1,
length: 2,
},
ArrayDimension {
lower_bound: 1,
length: 3,
},
],
vec![Datum::Int32(1), Datum::Int32(2)],
);
assert_eq!(
res,
Err(InvalidArrayError::WrongCardinality {
actual: 2,
expected: 6,
})
);
assert!(row.data.is_empty());
}
#[mz_ore::test]
fn test_nesting() {
let mut row = Row::default();
row.packer().push_dict_with(|row| {
row.push(Datum::String("favourites"));
row.push_list_with(|row| {
row.push(Datum::String("ice cream"));
row.push(Datum::String("oreos"));
row.push(Datum::String("cheesecake"));
});
row.push(Datum::String("name"));
row.push(Datum::String("bob"));
});
let mut iter = row.unpack_first().unwrap_map().iter();
let (k, v) = iter.next().unwrap();
assert_eq!(k, "favourites");
assert_eq!(
v.unwrap_list().iter().collect::<Vec<_>>(),
vec![
Datum::String("ice cream"),
Datum::String("oreos"),
Datum::String("cheesecake"),
]
);
let (k, v) = iter.next().unwrap();
assert_eq!(k, "name");
assert_eq!(v, Datum::String("bob"));
}
#[mz_ore::test]
fn test_dict_errors() -> Result<(), Box<dyn std::error::Error>> {
let pack = |ok| {
let mut row = Row::default();
row.packer().push_dict_with(|row| {
if ok {
row.push(Datum::String("key"));
row.push(Datum::Int32(42));
Ok(7)
} else {
Err("fail")
}
})?;
Ok(row)
};
assert_eq!(pack(false), Err("fail"));
let row = pack(true)?;
let mut dict = row.unpack_first().unwrap_map().iter();
assert_eq!(dict.next(), Some(("key", Datum::Int32(42))));
assert_eq!(dict.next(), None);
Ok(())
}
#[mz_ore::test]
#[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `decNumberFromInt32` on OS `linux`
fn test_datum_sizes() {
let arena = RowArena::new();
// Test the claims about various datum sizes.
let values_of_interest = vec![
Datum::Null,
Datum::False,
Datum::Int16(0),
Datum::Int32(0),
Datum::Int64(0),
Datum::UInt8(0),
Datum::UInt8(1),
Datum::UInt16(0),
Datum::UInt16(1),
Datum::UInt16(1 << 8),
Datum::UInt32(0),
Datum::UInt32(1),
Datum::UInt32(1 << 8),
Datum::UInt32(1 << 16),
Datum::UInt32(1 << 24),
Datum::UInt64(0),
Datum::UInt64(1),
Datum::UInt64(1 << 8),
Datum::UInt64(1 << 16),
Datum::UInt64(1 << 24),
Datum::UInt64(1 << 32),
Datum::UInt64(1 << 40),
Datum::UInt64(1 << 48),
Datum::UInt64(1 << 56),
Datum::Float32(OrderedFloat(0.0)),
Datum::Float64(OrderedFloat(0.0)),
Datum::from(numeric::Numeric::from(0)),
Datum::from(numeric::Numeric::from(1000)),
Datum::from(numeric::Numeric::from(9999)),
Datum::Date(
NaiveDate::from_ymd_opt(1, 1, 1)
.unwrap()
.try_into()
.unwrap(),
),
Datum::Timestamp(
CheckedTimestamp::from_timestamplike(
DateTime::from_timestamp(0, 0).unwrap().naive_utc(),
)
.unwrap(),
),
Datum::TimestampTz(
CheckedTimestamp::from_timestamplike(DateTime::from_timestamp(0, 0).unwrap())
.unwrap(),
),
Datum::Interval(Interval::default()),
Datum::Bytes(&[]),
Datum::String(""),
Datum::JsonNull,
Datum::Range(Range { inner: None }),
arena.make_datum(|packer| {
packer
.push_range(Range::new(Some((
RangeLowerBound::new(Datum::Int32(-1), true),
RangeUpperBound::new(Datum::Int32(1), true),
))))
.unwrap();
}),
];
for value in values_of_interest {
if datum_size(&value) != Row::pack_slice(&[value]).data.len() {
panic!("Disparity in claimed size for {:?}", value);
}
}
}
#[mz_ore::test]
fn test_range_errors() {
fn test_range_errors_inner<'a>(
datums: Vec<Vec<Datum<'a>>>,
) -> Result<(), InvalidRangeError> {
let mut row = Row::default();
let row_len = row.byte_len();
let mut packer = row.packer();
let r = packer.push_range_with(
RangeLowerBound {
inclusive: true,
bound: Some(|row: &mut RowPacker| {
for d in &datums[0] {
row.push(d);
}
Ok(())
}),
},
RangeUpperBound {
inclusive: true,
bound: Some(|row: &mut RowPacker| {
for d in &datums[1] {
row.push(d);
}
Ok(())
}),
},
);
assert_eq!(row_len, row.byte_len());
r
}
for panicking_case in [
vec![vec![Datum::Int32(1)], vec![]],
vec![
vec![Datum::Int32(1), Datum::Int32(2)],
vec![Datum::Int32(3)],
],
vec![
vec![Datum::Int32(1)],
vec![Datum::Int32(2), Datum::Int32(3)],
],
vec![vec![Datum::Int32(1), Datum::Int32(2)], vec![]],
vec![vec![Datum::Int32(1)], vec![Datum::UInt16(2)]],
vec![vec![Datum::Null], vec![Datum::Int32(2)]],
vec![vec![Datum::Int32(1)], vec![Datum::Null]],
] {
assert!(
mz_ore::panic::catch_unwind(|| test_range_errors_inner(panicking_case)).is_err()
);
}
let e = test_range_errors_inner(vec![vec![Datum::Int32(2)], vec![Datum::Int32(1)]]);
assert_eq!(e, Err(InvalidRangeError::MisorderedRangeBounds));
}
/// Lists have a variable-length encoding for their lengths. We test each case here.
#[mz_ore::test]
#[cfg_attr(miri, ignore)] // slow
fn test_list_encoding() {
fn test_list_encoding_inner(len: usize) {
let list_elem = |i: usize| {
if i % 2 == 0 {
Datum::False
} else {
Datum::True
}
};
let mut row = Row::default();
{
// Push some stuff.
let mut packer = row.packer();
packer.push(Datum::String("start"));
packer.push_list_with(|packer| {
for i in 0..len {
packer.push(list_elem(i));
}
});
packer.push(Datum::String("end"));
}
// Check that we read back exactly what we pushed.
let mut row_it = row.iter();
assert_eq!(row_it.next().unwrap(), Datum::String("start"));
match row_it.next().unwrap() {
Datum::List(list) => {
let mut list_it = list.iter();
for i in 0..len {
assert_eq!(list_it.next().unwrap(), list_elem(i));
}
assert_none!(list_it.next());
}
_ => panic!("expected Datum::List"),
}
assert_eq!(row_it.next().unwrap(), Datum::String("end"));
assert_none!(row_it.next());
}
test_list_encoding_inner(0);
test_list_encoding_inner(1);
test_list_encoding_inner(10);
test_list_encoding_inner(TINY - 1); // tiny
test_list_encoding_inner(TINY + 1); // short
test_list_encoding_inner(SHORT + 1); // long
// The biggest one takes 40 s on my laptop, probably not worth it.
//test_list_encoding_inner(LONG + 1); // huge
}
}