Datasets:
Tasks:
Tabular Classification
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
economics
quantitative-finance
causal-inference
macroeconomics
housing-economics
market-microstructure
License:
File size: 97,709 Bytes
ebcde1f | 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 | """Bounded, manifest-anchored loading of normalized public trade data.
The reader deliberately starts from an immutable ingestion manifest rather than
discovering Parquet files in a directory. It verifies the caller's ingestion
manifest digest, the referenced normalized-data manifest digest, every declared
part and sidecar digest, and the coverage/evidence claims before returning data.
"""
from __future__ import annotations
import re
import sqlite3
import tempfile
from collections.abc import Generator, Iterator, Mapping
from dataclasses import dataclass
from datetime import UTC, datetime
from decimal import Decimal, InvalidOperation
from pathlib import Path
from types import MappingProxyType
from typing import Any, Literal, cast
from urllib.parse import parse_qsl, urlsplit
import duckdb
import polars as pl
import pyarrow as pa # type: ignore[import-untyped]
import pyarrow.parquet as pq # type: ignore[import-untyped]
from microstructure.config import ProjectConfig, datetime_to_ns
from microstructure.data.quality import IncrementalQualityValidator, ValidationReport
from microstructure.data.schemas import SCHEMA_VERSION, ensure_schema, get_schema
from microstructure.data.storage import MANIFEST_VERSION
from microstructure.provenance import read_json, sha256_file
PublicEvidenceTier = Literal["PUBLIC_SAMPLE_PARTIAL", "FULL_DATA"]
_SHA256 = re.compile(r"^[0-9a-f]{64}$")
_NS_PER_SECOND = 1_000_000_000
# Matches the downloader's public-response ceiling. The reader checks this
# before JSON parsing so a recomputed manifest cannot turn one "page" into an
# unbounded-memory payload by inserting giant unused strings.
_MAX_RAW_ARTIFACT_BYTES = 8 * 1024 * 1024
_MAX_JSON_STRING_BYTES = 64 * 1024
_JSON_BASE_BYTES = 256 * 1024
_JSON_BYTES_PER_STRUCTURE_TOKEN = 32 * 1024
_PARQUET_BASE_BYTES = 1024 * 1024
_PARQUET_BYTES_PER_TRADE_ROW = 4096
_MAX_PARQUET_PART_ENCODED_BYTES = 64 * 1024 * 1024
_MAX_PARQUET_PART_DECODED_BYTES = 128 * 1024 * 1024
__all__ = [
"ObservedUtcCoverage",
"PublicDataError",
"PublicEvidenceTier",
"PublicTradeDataset",
"PublicTrades",
"SymbolObservedCoverage",
"VerifiedPublicTradeBatchStream",
"read_public_trades",
"verify_public_trade_dataset",
]
class PublicDataError(RuntimeError):
"""Raised when a public normalized input cannot be verified safely."""
@dataclass(frozen=True, slots=True)
class ObservedUtcCoverage:
"""Exact inclusive event-time coverage for a bounded set of rows."""
start_ns: int
end_inclusive_ns: int
start_utc: str
end_inclusive_utc: str
@dataclass(frozen=True, slots=True)
class SymbolObservedCoverage:
"""Actual rows and event-time coverage for one manifested symbol."""
symbol: str
rows: int
complete_range: bool
tick_size: Decimal
lot_size: Decimal
observed: ObservedUtcCoverage
@dataclass(frozen=True, slots=True)
class PublicTrades:
"""Verified public trades in Arrow and Polars representations."""
arrow_trades: pa.Table
polars_trades: pl.DataFrame
observed: ObservedUtcCoverage
symbols: tuple[SymbolObservedCoverage, ...]
evidence_tier: PublicEvidenceTier
all_requested_ranges_complete: bool
ingestion_manifest_path: Path
ingestion_manifest_sha256: str
dataset_manifest_path: Path
dataset_manifest_sha256: str
part_paths: tuple[Path, ...]
raw_artifact_paths: tuple[Path, ...]
raw_manifest_paths: tuple[Path, ...]
raw_artifact_sha256s: tuple[str, ...]
validation: ValidationReport
row_bound: int
canonical_order: tuple[str, ...]
@property
def rows(self) -> int:
return cast(int, self.arrow_trades.num_rows)
@property
def input_manifest_sha256s(self) -> tuple[str, ...]:
return tuple(sorted({self.ingestion_manifest_sha256, self.dataset_manifest_sha256}))
@dataclass(frozen=True, slots=True)
class _ParquetPartDescriptor:
"""Verified metadata needed to stream one normalized Parquet part."""
data_path: Path
data_sha256: str
sidecar_path: Path
rows: int
write_ordinal: int
venue: str
symbol: str
partition_date: str
observed_start_ns: int
observed_end_inclusive_ns: int
@dataclass(frozen=True, slots=True)
class _RawPageDescriptor:
"""A bounded raw page descriptor; raw records are deliberately not retained."""
path: Path
sha256: str
symbol: str
from_id: int | None
rows: int
first_id: int | None
last_id: int | None
@dataclass(frozen=True, slots=True)
class _VerifiedStreamSummary:
validation: ValidationReport
observed: ObservedUtcCoverage
symbols: tuple[SymbolObservedCoverage, ...]
class VerifiedPublicTradeBatchStream(Iterator[pa.RecordBatch]):
"""One fresh, bounded validation operation over a public trade data set.
The final validation and observed-coverage summary become available only
after the iterator is exhausted. Closing early releases DuckDB, SQLite,
and temporary spill resources without claiming that the data were fully
validated. One upstream Parquet pass is audited in immutable physical/write
order and fed directly into DuckDB's bounded external sort; the resulting
research batches are deterministic without rereading the source parts.
"""
def __init__(
self,
generator: Generator[pa.RecordBatch, None, _VerifiedStreamSummary],
*,
fail_on_error: bool,
) -> None:
self._generator = generator
self._fail_on_error = fail_on_error
self._summary: _VerifiedStreamSummary | None = None
self._closed = False
def __iter__(self) -> VerifiedPublicTradeBatchStream:
return self
def __next__(self) -> pa.RecordBatch:
if self._closed:
raise StopIteration
try:
return next(self._generator)
except StopIteration as stop:
self._closed = True
self._summary = cast(_VerifiedStreamSummary, stop.value)
if self._fail_on_error and self._summary.validation.has_errors:
raise PublicDataError(
"public normalized trades failed quality validation with "
f"{self._summary.validation.error_count} error findings"
) from None
raise
except BaseException:
self._closed = True
raise
@property
def summary(self) -> _VerifiedStreamSummary:
if self._summary is None:
raise RuntimeError("verified public batch stream has not been fully consumed")
return self._summary
@property
def validation(self) -> ValidationReport:
return self.summary.validation
def close(self) -> None:
if not self._closed:
self._generator.close()
self._closed = True
class _PhysicalAuditBatchSource(Iterator[pa.RecordBatch]):
"""Adapt an audited generator to Arrow's one-input-pass reader API."""
def __init__(
self,
generator: Generator[pa.RecordBatch, None, _VerifiedStreamSummary],
) -> None:
self._generator = generator
self._summary: _VerifiedStreamSummary | None = None
self._closed = False
def __iter__(self) -> _PhysicalAuditBatchSource:
return self
def __next__(self) -> pa.RecordBatch:
if self._closed:
raise StopIteration
try:
return next(self._generator)
except StopIteration as stop:
self._closed = True
self._summary = cast(_VerifiedStreamSummary, stop.value)
raise
except BaseException:
self._closed = True
raise
@property
def summary(self) -> _VerifiedStreamSummary:
if self._summary is None:
raise RuntimeError("physical public-data audit has not been fully consumed")
return self._summary
def close(self) -> None:
if not self._closed:
self._generator.close()
self._closed = True
@dataclass(frozen=True, slots=True)
class PublicTradeDataset:
"""Manifest-verified descriptors for a potentially large public data set.
Construction reads and hashes manifests, sidecars, Parquet footers, and
bounded raw API pages, but never materializes normalized Parquet rows.
Every call to :meth:`iter_verified_batches` creates an independent bounded
operation whose Arrow batch size and DuckDB sort memory are explicit.
"""
rows: int
observed: ObservedUtcCoverage
symbols: tuple[SymbolObservedCoverage, ...]
evidence_tier: PublicEvidenceTier
all_requested_ranges_complete: bool
ingestion_manifest_path: Path
ingestion_manifest_sha256: str
dataset_manifest_path: Path
dataset_manifest_sha256: str
part_paths: tuple[Path, ...]
raw_artifact_paths: tuple[Path, ...]
raw_manifest_paths: tuple[Path, ...]
raw_artifact_sha256s: tuple[str, ...]
row_bound: int
canonical_order: tuple[str, ...]
_config: ProjectConfig
_requested_range: tuple[int, int]
_symbol_claims: Mapping[str, _SymbolClaim]
_parts: tuple[_ParquetPartDescriptor, ...]
_raw_pages_by_digest: Mapping[str, _RawPageDescriptor]
_ordered_raw_pages: Mapping[str, tuple[_RawPageDescriptor, ...]]
@property
def input_manifest_sha256s(self) -> tuple[str, ...]:
return tuple(sorted({self.ingestion_manifest_sha256, self.dataset_manifest_sha256}))
def iter_verified_batches(
self,
*,
batch_rows: int = 65_536,
memory_limit: str = "256MB",
temp_directory: str | Path | None = None,
) -> VerifiedPublicTradeBatchStream:
"""Return a fresh bounded stream in deterministic canonical order."""
if isinstance(batch_rows, bool) or not isinstance(batch_rows, int) or batch_rows < 1:
raise ValueError("batch_rows must be a positive integer")
if not isinstance(memory_limit, str) or not memory_limit.strip():
raise ValueError("memory_limit must be a non-empty DuckDB memory size")
return VerifiedPublicTradeBatchStream(
_stream_verified_batches(
self,
batch_rows=batch_rows,
memory_limit=memory_limit,
temp_directory=temp_directory,
),
fail_on_error=self._config.quality.fail_on_error,
)
def validate(
self,
*,
batch_rows: int = 65_536,
memory_limit: str = "256MB",
temp_directory: str | Path | None = None,
) -> ValidationReport:
"""Validate all rows incrementally without retaining normalized data."""
stream = self.iter_verified_batches(
batch_rows=batch_rows,
memory_limit=memory_limit,
temp_directory=temp_directory,
)
try:
for _ in stream:
pass
return stream.validation
finally:
stream.close()
@dataclass(frozen=True, slots=True)
class _SymbolClaim:
rows: int
complete_range: bool
tick_size: Decimal
lot_size: Decimal
terminal: _TerminalClaim | None
@dataclass(frozen=True, slots=True)
class _TerminalClaim:
raw_page_count: int
stop_reason: str
last_raw_page_sha256: str
last_path: str
last_manifest_path: str
last_request_uri: str
last_row_count: int
@dataclass(frozen=True, slots=True)
class _VerifiedRawArtifacts:
paths: tuple[Path, ...]
manifest_paths: tuple[Path, ...]
sha256s: frozenset[str]
pages_by_digest: Mapping[str, _RawPageDescriptor]
ordered_pages: Mapping[str, tuple[_RawPageDescriptor, ...]]
@dataclass(frozen=True, slots=True)
class _RawAggregateTrade:
aggregate_id: int
first_trade_id: int
last_trade_id: int
event_ts_ns: int
price: Decimal
quantity: Decimal
buyer_is_maker: bool
@dataclass(frozen=True, slots=True)
class _AggregatePage:
path: Path
manifest_path: Path
sha256: str
request_uri: str
downloaded_at_ns: int
from_id: int | None
rows: int
first_id: int | None
last_id: int | None
first_event_ts_ns: int | None
last_event_ts_ns: int | None
def _object(value: object, label: str) -> Mapping[str, Any]:
if not isinstance(value, Mapping) or not all(isinstance(key, str) for key in value):
raise PublicDataError(f"{label} must be a JSON object with string keys")
return cast(Mapping[str, Any], value)
def _array(value: object, label: str) -> list[Any]:
if not isinstance(value, list):
raise PublicDataError(f"{label} must be a JSON array")
return value
def _text(value: object, label: str) -> str:
if not isinstance(value, str) or not value:
raise PublicDataError(f"{label} must be a non-empty string")
return value
def _integer(value: object, label: str, *, minimum: int | None = None) -> int:
if isinstance(value, bool) or not isinstance(value, int):
raise PublicDataError(f"{label} must be an integer")
if minimum is not None and value < minimum:
raise PublicDataError(f"{label} must be at least {minimum}")
return value
def _boolean(value: object, label: str) -> bool:
if not isinstance(value, bool):
raise PublicDataError(f"{label} must be a boolean")
return value
def _digest(value: object, label: str) -> str:
digest = _text(value, label).lower()
if _SHA256.fullmatch(digest) is None:
raise PublicDataError(f"{label} must be a SHA-256 hex digest")
return digest
def _positive_decimal(value: object, label: str) -> Decimal:
if not isinstance(value, str) or not value:
raise PublicDataError(f"{label} must be a non-empty decimal string")
try:
result = Decimal(value)
except InvalidOperation as exc:
raise PublicDataError(f"{label} is not a valid decimal") from exc
if not result.is_finite() or result <= 0:
raise PublicDataError(f"{label} must be positive and finite")
return result
def _positive_decimal_number(value: object, label: str) -> Decimal:
if isinstance(value, bool) or not isinstance(value, (Decimal, float, int, str)):
raise PublicDataError(f"{label} must be a decimal number")
try:
result = Decimal(str(value))
except InvalidOperation as exc:
raise PublicDataError(f"{label} is not a valid decimal") from exc
if not result.is_finite() or result <= 0:
raise PublicDataError(f"{label} must be positive and finite")
return result
def _scaled_integer(value: Decimal, quantum: Decimal, label: str) -> int:
scaled = value / quantum
integral = scaled.to_integral_value()
if scaled != integral:
raise PublicDataError(f"{label} is not aligned to exchangeInfo scale")
return int(integral)
def _unsigned_integer_text(value: str, label: str, *, minimum: int = 0) -> int:
if not value.isascii() or not value.isdecimal():
raise PublicDataError(f"{label} must be an unsigned decimal integer")
result = int(value)
if result < minimum:
raise PublicDataError(f"{label} must be at least {minimum}")
return result
def _load_json_value(path: Path, label: str) -> object:
_preflight_json_structure(path, label)
try:
return cast(object, read_json(path))
except (OSError, UnicodeError, ValueError) as exc:
raise PublicDataError(f"cannot read {label} at {path}: {exc}") from exc
def _preflight_json_structure(path: Path, label: str) -> None:
"""Bound JSON token width and bytes relative to its structural cardinality.
Manifests legitimately grow with O(parts/pages), so a fixed whole-file cap
would defeat full-history use. This streaming preflight instead permits
bytes proportional to JSON structure while rejecting giant padding strings
before ``read_json`` allocates them.
"""
total_bytes = 0
structure_tokens = 0
current_string_bytes = 0
in_string = False
escaped = False
try:
with path.open("rb") as handle:
while chunk := handle.read(64 * 1024):
total_bytes += len(chunk)
for byte in chunk:
if in_string:
current_string_bytes += 1
if current_string_bytes > _MAX_JSON_STRING_BYTES:
raise PublicDataError(
f"{label} has a JSON string above bounded token size "
f"{_MAX_JSON_STRING_BYTES} bytes"
)
if escaped:
escaped = False
elif byte == 0x5C: # backslash
escaped = True
elif byte == 0x22: # quote
in_string = False
elif byte == 0x22:
in_string = True
current_string_bytes = 0
elif byte in {0x7B, 0x7D, 0x5B, 0x5D, 0x2C}: # {}[],
structure_tokens += 1
except PublicDataError:
raise
except OSError as exc:
raise PublicDataError(f"cannot preflight {label} at {path}: {exc}") from exc
allowed_bytes = _JSON_BASE_BYTES + (structure_tokens * _JSON_BYTES_PER_STRUCTURE_TOKEN)
if total_bytes > allowed_bytes:
raise PublicDataError(
f"{label} has {total_bytes} bytes above its structural JSON bound {allowed_bytes}"
)
def _request_query(
source_uri: str,
*,
base_url: str,
endpoint: str,
label: str,
) -> dict[str, str]:
"""Parse a raw request URI only when it is rooted at the configured API."""
try:
configured = urlsplit(base_url)
requested = urlsplit(source_uri)
configured_port = configured.port
requested_port = requested.port
except ValueError as exc:
raise PublicDataError(f"{label} is not a valid URL: {exc}") from exc
if (
configured.scheme.lower() not in {"http", "https"}
or configured.hostname is None
or configured.username is not None
or configured.password is not None
or configured.query
or configured.fragment
):
raise PublicDataError("configured data.base_url is not a plain HTTP(S) base URL")
if (
requested.scheme.lower() != configured.scheme.lower()
or requested.hostname is None
or requested.hostname.lower() != configured.hostname.lower()
or requested_port != configured_port
or requested.username is not None
or requested.password is not None
):
raise PublicDataError(f"{label} is not bound to configured data.base_url")
base_path = configured.path.rstrip("/")
expected_path = f"{base_path}{endpoint}"
if requested.path != expected_path:
raise PublicDataError(f"{label} does not use exact endpoint {expected_path!r}")
if requested.fragment:
raise PublicDataError(f"{label} must not contain a fragment")
try:
pairs = parse_qsl(requested.query, keep_blank_values=True, strict_parsing=True)
except ValueError as exc:
raise PublicDataError(f"{label} has a malformed query string") from exc
query: dict[str, str] = {}
for key, value in pairs:
if not key or not value:
raise PublicDataError(f"{label} query keys and values must not be empty")
if key in query:
raise PublicDataError(f"{label} has duplicate query parameter {key!r}")
query[key] = value
return query
def _validate_exchange_info_payload(
path: Path,
*,
symbol: str,
claim: _SymbolClaim,
label: str,
) -> None:
payload = _object(_load_json_value(path, f"{label} payload"), f"{label} payload")
symbols = _array(payload.get("symbols"), f"{label} payload.symbols")
if len(symbols) != 1:
raise PublicDataError(f"{label} must contain exactly one exchangeInfo symbol")
item = _object(symbols[0], f"{label} payload.symbols[0]")
payload_symbol = _text(item.get("symbol"), f"{label} payload.symbol")
if payload_symbol != symbol:
raise PublicDataError(f"{label} payload symbol does not match its request URI")
status = _text(item.get("status"), f"{label} payload.status")
if status != "TRADING":
raise PublicDataError(f"{label} payload status is not TRADING")
_text(item.get("baseAsset"), f"{label} payload.baseAsset")
_text(item.get("quoteAsset"), f"{label} payload.quoteAsset")
filters = _array(item.get("filters"), f"{label} payload.filters")
selected: dict[str, Mapping[str, Any]] = {}
for filter_index, raw_filter in enumerate(filters):
filter_item = _object(raw_filter, f"{label} payload.filters[{filter_index}]")
filter_type = _text(
filter_item.get("filterType"),
f"{label} payload.filters[{filter_index}].filterType",
)
if filter_type in {"PRICE_FILTER", "LOT_SIZE"}:
if filter_type in selected:
raise PublicDataError(f"{label} payload has duplicate {filter_type}")
selected[filter_type] = filter_item
if set(selected) != {"PRICE_FILTER", "LOT_SIZE"}:
raise PublicDataError(f"{label} payload lacks PRICE_FILTER or LOT_SIZE")
tick_size = _positive_decimal(
selected["PRICE_FILTER"].get("tickSize"),
f"{label} payload.PRICE_FILTER.tickSize",
)
lot_size = _positive_decimal(
selected["LOT_SIZE"].get("stepSize"),
f"{label} payload.LOT_SIZE.stepSize",
)
if tick_size != claim.tick_size or lot_size != claim.lot_size:
raise PublicDataError(f"{label} payload scales do not match ingestion claim for {symbol}")
def _aggregate_records(
path: Path,
label: str,
*,
request_limit: int,
requested_range: tuple[int, int],
) -> dict[int, _RawAggregateTrade]:
# ``requested_range`` is retained in the signature to bind the cache to
# the verified request. Binance may legitimately return a terminal
# sentinel row at/after endTime; normalized rows, not untouched raw pages,
# are required to fall inside the requested half-open interval.
del requested_range
payload = _array(_load_json_value(path, f"{label} payload"), f"{label} payload")
if len(payload) > request_limit:
raise PublicDataError(
f"{label} contains {len(payload)} rows, above configured page limit {request_limit}"
)
records: dict[int, _RawAggregateTrade] = {}
previous_event_ts_ns: int | None = None
for record_index, raw_record in enumerate(payload):
record = _object(raw_record, f"{label} payload[{record_index}]")
aggregate_id = _integer(
record.get("a"),
f"{label} payload[{record_index}].a",
minimum=0,
)
if records and aggregate_id <= next(reversed(records)):
raise PublicDataError(f"{label} aggregate-trade IDs are not strictly increasing")
first_trade_id = _integer(
record.get("f"),
f"{label} payload[{record_index}].f",
minimum=0,
)
last_trade_id = _integer(
record.get("l"),
f"{label} payload[{record_index}].l",
minimum=first_trade_id,
)
event_time_ms = _integer(
record.get("T"),
f"{label} payload[{record_index}].T",
minimum=0,
)
event_ts_ns = event_time_ms * 1_000_000
if previous_event_ts_ns is not None and event_ts_ns < previous_event_ts_ns:
raise PublicDataError(f"{label} aggregate-trade event times are not nondecreasing")
previous_event_ts_ns = event_ts_ns
price = _positive_decimal(record.get("p"), f"{label} payload[{record_index}].p")
quantity = _positive_decimal(record.get("q"), f"{label} payload[{record_index}].q")
buyer_is_maker = _boolean(
record.get("m"),
f"{label} payload[{record_index}].m",
)
records[aggregate_id] = _RawAggregateTrade(
aggregate_id=aggregate_id,
first_trade_id=first_trade_id,
last_trade_id=last_trade_id,
event_ts_ns=event_ts_ns,
price=price,
quantity=quantity,
buyer_is_maker=buyer_is_maker,
)
return records
class _RawPageCache:
"""Load at most one bounded aggregate-trade page at a time."""
def __init__(
self,
pages_by_digest: Mapping[str, _RawPageDescriptor],
*,
request_limit: int,
requested_range: tuple[int, int],
) -> None:
self._pages_by_digest = pages_by_digest
self._request_limit = request_limit
self._requested_range = requested_range
self._digest: str | None = None
self._records: Mapping[int, _RawAggregateTrade] = MappingProxyType({})
def record(
self,
digest: str,
*,
symbol: str,
trade_id: int,
label: str,
) -> _RawAggregateTrade:
descriptor = self._pages_by_digest.get(digest)
if descriptor is None:
raise PublicDataError(f"{label} references an undeclared or empty raw artifact")
if descriptor.symbol != symbol:
raise PublicDataError(
f"{label} references a raw page for {descriptor.symbol}, not {symbol}"
)
if self._digest != digest:
# Re-hash at use time so verification and consumption are not split
# by an unnoticed local-file replacement.
_verify_file(descriptor.path, descriptor.sha256, f"raw aggregate page for {symbol}")
records = _aggregate_records(
descriptor.path,
f"raw aggregate page for {symbol}",
request_limit=self._request_limit,
requested_range=self._requested_range,
)
_verify_file(descriptor.path, descriptor.sha256, f"raw aggregate page for {symbol}")
ids = tuple(records)
if (
len(records) != descriptor.rows
or (ids[0] if ids else None) != descriptor.first_id
or (ids[-1] if ids else None) != descriptor.last_id
):
raise PublicDataError(f"raw aggregate page metadata changed for {symbol}")
self._digest = digest
self._records = records
raw_record = self._records.get(trade_id)
if raw_record is None:
raise PublicDataError(
f"{label} trade_id is absent from its exact raw aggregate-trade page"
)
return raw_record
class _ExpectedLineageIndex:
"""Disk-backed inverse lineage for every downloader-selected raw trade."""
def __init__(self, dataset: PublicTradeDataset) -> None:
self._connection = sqlite3.connect("")
self._connection.execute("PRAGMA cache_size = -2048")
self._connection.execute("PRAGMA temp_store = FILE")
self._connection.execute("PRAGMA journal_mode = OFF")
self._connection.execute("PRAGMA synchronous = OFF")
self._connection.execute(
"""
CREATE TABLE expected_lineage (
symbol TEXT NOT NULL,
source_sha256 TEXT NOT NULL,
trade_id INTEGER NOT NULL,
seen_count INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (symbol, source_sha256, trade_id)
) WITHOUT ROWID
"""
)
try:
self._populate(dataset)
except BaseException:
self._connection.close()
raise
def _populate(self, dataset: PublicTradeDataset) -> None:
row_cap = cast(int, dataset._config.data.max_events_per_symbol)
for symbol in dataset._config.data.symbols:
expected_count = 0
for page in dataset._ordered_raw_pages[symbol]:
if expected_count >= row_cap:
break
_verify_file(page.path, page.sha256, f"raw aggregate page for {symbol}")
records = _aggregate_records(
page.path,
f"raw aggregate page for {symbol}",
request_limit=dataset._config.data.request_limit,
requested_range=dataset._requested_range,
)
_verify_file(page.path, page.sha256, f"raw aggregate page for {symbol}")
ids = tuple(records)
if (
len(records) != page.rows
or (ids[0] if ids else None) != page.first_id
or (ids[-1] if ids else None) != page.last_id
):
raise PublicDataError(f"raw aggregate page metadata changed for {symbol}")
selected: list[tuple[str, str, int]] = []
for record in records.values():
if not (
dataset._requested_range[0]
<= record.event_ts_ns
< dataset._requested_range[1]
):
continue
if expected_count >= row_cap:
break
selected.append((symbol, page.sha256, record.aggregate_id))
expected_count += 1
try:
self._connection.executemany(
"""
INSERT INTO expected_lineage (symbol, source_sha256, trade_id)
VALUES (?, ?, ?)
""",
selected,
)
except sqlite3.IntegrityError as exc:
raise PublicDataError(
f"raw aggregate pages contain duplicate selected lineage for {symbol}"
) from exc
claimed = dataset._symbol_claims[symbol].rows
if expected_count != claimed:
raise PublicDataError(
f"raw selected rows for {symbol} ({expected_count}) do not match "
f"normalized coverage claim ({claimed})"
)
self._connection.commit()
def mark_seen(self, *, symbol: str, source_sha256: str, trade_id: int) -> None:
cursor = self._connection.execute(
"""
UPDATE expected_lineage
SET seen_count = seen_count + 1
WHERE symbol = ? AND source_sha256 = ? AND trade_id = ?
""",
(symbol, source_sha256, trade_id),
)
if cursor.rowcount != 1:
raise PublicDataError(
"normalized trade is outside the exact downloader-selected raw sequence"
)
def mismatch_counts(self) -> tuple[int, int]:
missing, repeated = self._connection.execute(
"""
SELECT
SUM(CASE WHEN seen_count = 0 THEN 1 ELSE 0 END),
SUM(CASE WHEN seen_count > 1 THEN 1 ELSE 0 END)
FROM expected_lineage
"""
).fetchone()
return int(missing or 0), int(repeated or 0)
def commit(self) -> None:
self._connection.commit()
def close(self) -> None:
self._connection.close()
def _validate_normalized_trade_row(
raw_row: object,
*,
row_index: int,
symbol_claims: Mapping[str, _SymbolClaim],
raw_cache: _RawPageCache,
) -> None:
label = f"normalized trade row {row_index}"
row = _object(raw_row, label)
symbol = _text(row.get("symbol"), f"{label}.symbol")
claim = symbol_claims.get(symbol)
if claim is None:
raise PublicDataError(f"{label} has an unmanifested symbol {symbol}")
source_id = _digest(row.get("source_artifact_id"), f"{label}.source_artifact_id")
trade_id = _integer(row.get("trade_id"), f"{label}.trade_id", minimum=0)
raw_record = raw_cache.record(
source_id,
symbol=symbol,
trade_id=trade_id,
label=label,
)
price = _positive_decimal_number(row.get("price"), f"{label}.price")
quantity = _positive_decimal_number(row.get("quantity"), f"{label}.quantity")
expected_price_ticks = _scaled_integer(raw_record.price, claim.tick_size, f"{label}.price")
expected_quantity_lots = _scaled_integer(
raw_record.quantity,
claim.lot_size,
f"{label}.quantity",
)
expected_aggressor = "sell" if raw_record.buyer_is_maker else "buy"
mismatches = {
"venue": row.get("venue") != "binance_spot",
"event_ts_ns": row.get("event_ts_ns") != raw_record.event_ts_ns,
"received_ts_ns": row.get("received_ts_ns") is not None,
"available_ts_ns": row.get("available_ts_ns") != raw_record.event_ts_ns,
"availability_basis": row.get("availability_basis") != "exchange_event_time_proxy",
"capture_seq": row.get("capture_seq") is not None,
"continuity_id": row.get("continuity_id") is not None,
"first_trade_id": row.get("first_trade_id") != raw_record.first_trade_id,
"last_trade_id": row.get("last_trade_id") != raw_record.last_trade_id,
"price_ticks": row.get("price_ticks") != expected_price_ticks,
"quantity_lots": row.get("quantity_lots") != expected_quantity_lots,
"price": price != raw_record.price,
"quantity": quantity != raw_record.quantity,
"quote_quantity": row.get("quote_quantity")
!= float(raw_record.price) * float(raw_record.quantity),
"aggressor_side": row.get("aggressor_side") != expected_aggressor,
"buyer_is_maker": row.get("buyer_is_maker") != raw_record.buyer_is_maker,
}
mismatched_fields = sorted(field for field, mismatched in mismatches.items() if mismatched)
if mismatched_fields:
raise PublicDataError(
f"{label} does not match its exact raw aggregate-trade record: "
+ ", ".join(mismatched_fields)
)
def _load_object(path: Path, label: str) -> Mapping[str, Any]:
_preflight_json_structure(path, label)
try:
return _object(read_json(path), label)
except PublicDataError:
raise
except (OSError, ValueError) as exc:
raise PublicDataError(f"cannot read {label} at {path}: {exc}") from exc
def _verify_file(path: Path, expected_sha256: str, label: str) -> None:
if not path.is_file():
raise PublicDataError(f"missing {label}: {path}")
try:
observed = sha256_file(path)
except OSError as exc:
raise PublicDataError(f"cannot hash {label} at {path}: {exc}") from exc
if observed != expected_sha256:
raise PublicDataError(
f"{label} SHA-256 mismatch: expected {expected_sha256}, got {observed}"
)
def _declared_file(root: Path, value: object, label: str) -> Path:
declared = Path(_text(value, label))
if declared.is_absolute():
raise PublicDataError(f"{label} must be relative to its manifest root")
resolved_root = root.resolve()
candidate = (resolved_root / declared).resolve()
if not candidate.is_relative_to(resolved_root):
raise PublicDataError(f"{label} escapes its manifest root: {declared}")
if not candidate.is_file():
raise PublicDataError(f"missing {label}: {candidate}")
return candidate
def _utc_iso_from_ns(timestamp_ns: int) -> str:
seconds, nanoseconds = divmod(timestamp_ns, _NS_PER_SECOND)
instant = datetime.fromtimestamp(seconds, tz=UTC)
return f"{instant:%Y-%m-%dT%H:%M:%S}.{nanoseconds:09d}Z"
def _utc_ns_from_iso(value: object, label: str) -> int:
raw = _text(value, label)
try:
parsed = datetime.fromisoformat(raw.replace("Z", "+00:00"))
except ValueError as exc:
raise PublicDataError(f"{label} is not a valid ISO-8601 timestamp") from exc
if parsed.tzinfo is None:
raise PublicDataError(f"{label} must include a UTC offset")
utc = parsed.astimezone(UTC)
epoch = datetime(1970, 1, 1, tzinfo=UTC)
delta = utc - epoch
return (
delta.days * 86_400 * _NS_PER_SECOND
+ delta.seconds * _NS_PER_SECOND
+ delta.microseconds * 1_000
)
def _coverage(start_ns: int, end_inclusive_ns: int) -> ObservedUtcCoverage:
if end_inclusive_ns < start_ns:
raise PublicDataError("observed coverage ends before it starts")
return ObservedUtcCoverage(
start_ns=start_ns,
end_inclusive_ns=end_inclusive_ns,
start_utc=_utc_iso_from_ns(start_ns),
end_inclusive_utc=_utc_iso_from_ns(end_inclusive_ns),
)
def _requested_range(manifest: Mapping[str, Any], label: str) -> tuple[int, int]:
requested = _object(manifest.get("requested_range_ns"), f"{label}.requested_range_ns")
start_ns = _integer(requested.get("start"), f"{label}.requested_range_ns.start")
end_ns = _integer(requested.get("end_exclusive"), f"{label}.requested_range_ns.end_exclusive")
if end_ns <= start_ns:
raise PublicDataError(f"{label} requested range must be non-empty")
return start_ns, end_ns
def _terminal_claim(
item: Mapping[str, Any],
*,
label: str,
rows: int,
complete_range: bool,
requested_range: tuple[int, int],
) -> _TerminalClaim | None:
top_fields = ("raw_page_count", "stop_reason", "last_raw_page_sha256")
presence = [item.get(field) is not None for field in top_fields]
summary_value = item.get("stream_summary")
if not any(presence) and summary_value is None:
return None
if not all(presence) or summary_value is None:
raise PublicDataError(f"{label} has an incomplete terminal stream claim")
raw_page_count = _integer(item.get("raw_page_count"), f"{label}.raw_page_count", minimum=1)
stop_reason = _text(item.get("stop_reason"), f"{label}.stop_reason")
if stop_reason not in {"event_cap", "range_end", "short_page", "empty_page"}:
raise PublicDataError(f"{label}.stop_reason is unsupported")
last_sha = _digest(item.get("last_raw_page_sha256"), f"{label}.last_raw_page_sha256")
summary = _object(summary_value, f"{label}.stream_summary")
if (
_integer(
summary.get("requested_start_ns"),
f"{label}.stream_summary.requested_start_ns",
)
!= requested_range[0]
or _integer(
summary.get("requested_end_ns"),
f"{label}.stream_summary.requested_end_ns",
)
!= requested_range[1]
or _integer(
summary.get("rows_yielded"),
f"{label}.stream_summary.rows_yielded",
minimum=1,
)
!= rows
or _integer(
summary.get("raw_page_count"),
f"{label}.stream_summary.raw_page_count",
minimum=1,
)
!= raw_page_count
or _text(summary.get("stop_reason"), f"{label}.stream_summary.stop_reason") != stop_reason
or _boolean(
summary.get("complete_range"),
f"{label}.stream_summary.complete_range",
)
!= complete_range
):
raise PublicDataError(f"{label}.stream_summary disagrees with its symbol claim")
expected_complete = stop_reason != "event_cap" and rows > 0
if complete_range != expected_complete:
raise PublicDataError(f"{label} completeness disagrees with terminal stop reason")
last = _object(summary.get("last_raw_page"), f"{label}.stream_summary.last_raw_page")
last_page_sha = _digest(last.get("sha256"), f"{label}.stream_summary.last_raw_page.sha256")
if last_page_sha != last_sha:
raise PublicDataError(f"{label} last raw page SHA-256 claims disagree")
return _TerminalClaim(
raw_page_count=raw_page_count,
stop_reason=stop_reason,
last_raw_page_sha256=last_sha,
last_path=_text(last.get("path"), f"{label}.stream_summary.last_raw_page.path"),
last_manifest_path=_text(
last.get("manifest_path"),
f"{label}.stream_summary.last_raw_page.manifest_path",
),
last_request_uri=_text(
last.get("request_uri"),
f"{label}.stream_summary.last_raw_page.request_uri",
),
last_row_count=_integer(
last.get("row_count"),
f"{label}.stream_summary.last_raw_page.row_count",
minimum=0,
),
)
def _validate_ingestion_manifest(
manifest: Mapping[str, Any],
config: ProjectConfig,
) -> tuple[
PublicEvidenceTier,
bool,
tuple[int, int],
dict[str, _SymbolClaim],
Mapping[str, Any],
]:
if manifest.get("manifest_version") != MANIFEST_VERSION:
raise PublicDataError("unsupported ingestion manifest version")
if manifest.get("artifact_kind") != "ingestion_run":
raise PublicDataError("manifest is not an ingestion_run artifact")
if manifest.get("mode") != "binance_rest":
raise PublicDataError("public trade reader requires a binance_rest ingestion manifest")
if (
manifest.get("schema_version") != SCHEMA_VERSION
or manifest.get("schema_version") != config.data.schema_version
):
raise PublicDataError("ingestion manifest has an unsupported schema version")
if _text(manifest.get("source"), "ingestion.source") != config.data.source:
raise PublicDataError("ingestion source does not match configured source")
effective = _text(manifest.get("evidence_tier"), "ingestion.evidence_tier")
requested = _text(manifest.get("requested_evidence_tier"), "ingestion.requested_evidence_tier")
if effective not in {"PUBLIC_SAMPLE_PARTIAL", "FULL_DATA"}:
raise PublicDataError(f"unsupported public evidence tier: {effective!r}")
if requested not in {"PUBLIC_SAMPLE_PARTIAL", "FULL_DATA"}:
raise PublicDataError(f"unsupported requested public evidence tier: {requested!r}")
if requested != config.run.evidence_tier:
raise PublicDataError("requested evidence tier does not match configuration")
all_complete = _boolean(
manifest.get("all_requested_ranges_complete"),
"ingestion.all_requested_ranges_complete",
)
row_cap = _integer(
manifest.get("row_cap_per_symbol"), "ingestion.row_cap_per_symbol", minimum=1
)
if row_cap != config.data.max_events_per_symbol:
raise PublicDataError("ingestion row cap does not match configuration")
requested_range = _requested_range(manifest, "ingestion")
if config.data.end is None:
raise PublicDataError("public input configuration requires a bounded end time")
configured_range = (datetime_to_ns(config.data.start), datetime_to_ns(config.data.end))
if requested_range != configured_range:
raise PublicDataError("ingestion requested range does not match configuration")
raw_symbols = _array(manifest.get("symbols"), "ingestion.symbols")
if not raw_symbols:
raise PublicDataError("ingestion.symbols must not be empty")
symbols: dict[str, _SymbolClaim] = {}
for index, raw_symbol in enumerate(raw_symbols):
item = _object(raw_symbol, f"ingestion.symbols[{index}]")
symbol = _text(item.get("symbol"), f"ingestion.symbols[{index}].symbol")
if symbol in symbols:
raise PublicDataError(f"duplicate symbol coverage entry: {symbol}")
rows = _integer(item.get("rows"), f"ingestion.symbols[{index}].rows", minimum=1)
if rows > row_cap:
raise PublicDataError(f"manifested rows for {symbol} exceed row_cap_per_symbol")
complete = _boolean(
item.get("complete_range"), f"ingestion.symbols[{index}].complete_range"
)
symbols[symbol] = _SymbolClaim(
rows=rows,
complete_range=complete,
tick_size=_positive_decimal(
item.get("tick_size"), f"ingestion.symbols[{index}].tick_size"
),
lot_size=_positive_decimal(
item.get("lot_size"), f"ingestion.symbols[{index}].lot_size"
),
terminal=_terminal_claim(
item,
label=f"ingestion.symbols[{index}]",
rows=rows,
complete_range=complete,
requested_range=requested_range,
),
)
if set(symbols) != set(config.data.symbols):
raise PublicDataError("ingestion symbols do not match configured symbols")
derived_complete = all(claim.complete_range for claim in symbols.values())
if all_complete != derived_complete:
raise PublicDataError("all_requested_ranges_complete disagrees with per-symbol coverage")
expected_effective = requested if all_complete else "PUBLIC_SAMPLE_PARTIAL"
if effective == "FULL_DATA" and expected_effective != "FULL_DATA":
raise PublicDataError("partial or lower-tier coverage cannot be promoted to FULL_DATA")
if effective != expected_effective:
raise PublicDataError(
"effective evidence tier does not match requested tier and manifested coverage"
)
legacy_complete = sorted(
symbol
for symbol, claim in symbols.items()
if claim.complete_range and claim.terminal is None
)
if legacy_complete:
raise PublicDataError(
"complete public coverage lacks terminal stream evidence for symbols: "
+ ", ".join(legacy_complete)
)
datasets = _array(manifest.get("normalized_datasets"), "ingestion.normalized_datasets")
if len(datasets) != 1:
raise PublicDataError("public trade ingestion must declare exactly one normalized dataset")
dataset = _object(datasets[0], "ingestion.normalized_datasets[0]")
if dataset.get("schema_name") != "trades":
raise PublicDataError("public ingestion normalized dataset must use the trades schema")
return (
cast(PublicEvidenceTier, effective),
all_complete,
requested_range,
symbols,
dataset,
)
def _validate_dataset_manifest(
manifest: Mapping[str, Any],
*,
ingestion_source: object,
requested_range: tuple[int, int],
) -> tuple[int, list[Any], str]:
if manifest.get("manifest_version") != MANIFEST_VERSION:
raise PublicDataError("unsupported normalized dataset manifest version")
if manifest.get("dataset") != "trades":
raise PublicDataError("referenced normalized manifest is not the trades dataset")
if manifest.get("schema_version") != SCHEMA_VERSION:
raise PublicDataError("normalized dataset manifest has an unsupported schema version")
source = _text(manifest.get("source"), "normalized dataset.source")
if source != ingestion_source:
raise PublicDataError("ingestion and normalized dataset sources do not match")
source_uri = _text(manifest.get("source_uri"), "normalized dataset.source_uri")
if _requested_range(manifest, "normalized dataset") != requested_range:
raise PublicDataError("ingestion and normalized dataset requested ranges do not match")
rows = _integer(manifest.get("rows"), "normalized dataset.rows", minimum=1)
artifacts = _array(manifest.get("artifacts"), "normalized dataset.artifacts")
if not artifacts:
raise PublicDataError("normalized dataset manifest declares no Parquet parts")
return rows, artifacts, source_uri
def _verify_raw_artifacts(
ingestion: Mapping[str, Any],
*,
bundle_root: Path,
requested_range: tuple[int, int],
config: ProjectConfig,
symbol_claims: Mapping[str, _SymbolClaim],
) -> _VerifiedRawArtifacts:
entries = _array(ingestion.get("raw_artifacts"), "ingestion.raw_artifacts")
if not entries:
raise PublicDataError("public ingestion manifest declares no raw artifacts")
raw_root = (bundle_root / "raw").resolve()
paths: list[Path] = []
manifest_paths: list[Path] = []
digests: set[str] = set()
pages_by_digest: dict[str, _RawPageDescriptor] = {}
ordered_page_descriptors: dict[str, tuple[_RawPageDescriptor, ...]] = {}
aggregate_pages: dict[str, list[_AggregatePage]] = {symbol: [] for symbol in symbol_claims}
exchange_info_symbols: set[str] = set()
seen_paths: dict[Path, bool] = {}
seen_manifest_paths: set[Path] = set()
try:
base_path = urlsplit(config.data.base_url).path.rstrip("/")
except ValueError as exc:
raise PublicDataError(f"configured data.base_url is not valid: {exc}") from exc
aggregate_endpoint = "/api/v3/aggTrades"
exchange_info_endpoint = "/api/v3/exchangeInfo"
for index, raw_entry in enumerate(entries):
label = f"ingestion.raw_artifacts[{index}]"
entry = _object(raw_entry, label)
path = _declared_file(bundle_root, entry.get("path"), f"{label}.path")
manifest_path = _declared_file(
bundle_root, entry.get("manifest_path"), f"{label}.manifest_path"
)
if not path.is_relative_to(raw_root) or not manifest_path.is_relative_to(raw_root):
raise PublicDataError(f"{label} is outside the ingestion bundle's raw root")
if manifest_path in seen_manifest_paths:
raise PublicDataError(f"duplicate declared raw sidecar at index {index}")
seen_manifest_paths.add(manifest_path)
digest = _digest(entry.get("sha256"), f"{label}.sha256")
manifest_digest = _digest(entry.get("manifest_sha256"), f"{label}.manifest_sha256")
_verify_file(path, digest, f"raw artifact {index}")
_verify_file(manifest_path, manifest_digest, f"raw artifact sidecar {index}")
if path.stat().st_size > _MAX_RAW_ARTIFACT_BYTES:
raise PublicDataError(
f"raw artifact {index} exceeds bounded JSON size {_MAX_RAW_ARTIFACT_BYTES} bytes"
)
sidecar = _load_object(manifest_path, f"raw artifact sidecar {index}")
if sidecar.get("manifest_version") != MANIFEST_VERSION:
raise PublicDataError(f"raw artifact sidecar {index} has an unsupported version")
if sidecar.get("artifact_kind") != "raw_source":
raise PublicDataError(f"raw artifact sidecar {index} has an unexpected kind")
if sidecar.get("source") != "binance_spot_public_api":
raise PublicDataError(f"raw artifact sidecar {index} is not a Binance public source")
source_uri = _text(sidecar.get("source_uri"), f"raw artifact sidecar {index}.source_uri")
downloaded_at_ns = _utc_ns_from_iso(
sidecar.get("downloaded_at_utc"),
f"raw artifact sidecar {index}.downloaded_at_utc",
)
if sidecar.get("path") != path.name or manifest_path.parent != path.parent:
raise PublicDataError(f"raw artifact sidecar {index} path does not match")
if (
_integer(sidecar.get("bytes"), f"raw artifact sidecar {index}.bytes", minimum=1)
!= path.stat().st_size
):
raise PublicDataError(f"raw artifact sidecar {index} byte count does not match")
checksum = _object(sidecar.get("checksum"), f"raw artifact sidecar {index}.checksum")
if (
checksum.get("algorithm") != "sha256"
or _digest(checksum.get("value"), f"raw artifact sidecar {index}.checksum.value")
!= digest
):
raise PublicDataError(f"raw artifact sidecar {index} checksum does not match")
raw_requested = _object(
sidecar.get("requested_range_ns"),
f"raw artifact sidecar {index}.requested_range_ns",
)
raw_start = raw_requested.get("start")
raw_end = raw_requested.get("end_exclusive")
requested_path = urlsplit(source_uri).path
raw_label = f"raw artifact sidecar {index}.source_uri"
is_empty_aggregate = False
if requested_path == f"{base_path}{aggregate_endpoint}":
query = _request_query(
source_uri,
base_url=config.data.base_url,
endpoint=aggregate_endpoint,
label=raw_label,
)
initial_keys = {"symbol", "startTime", "endTime", "limit"}
continuation_keys = {"symbol", "fromId", "limit"}
if set(query) == initial_keys:
expected_start_ms = requested_range[0] // 1_000_000
expected_end_ms = (requested_range[1] - 1) // 1_000_000
if (
_unsigned_integer_text(query["startTime"], f"{raw_label}.startTime")
!= expected_start_ms
or _unsigned_integer_text(query["endTime"], f"{raw_label}.endTime")
!= expected_end_ms
):
raise PublicDataError(
f"{raw_label} time query does not match the requested range"
)
from_id: int | None = None
elif set(query) == continuation_keys:
from_id = _unsigned_integer_text(query["fromId"], f"{raw_label}.fromId")
else:
raise PublicDataError(
f"{raw_label} must use exactly the initial-time or fromId query parameters"
)
symbol = query["symbol"]
if symbol not in symbol_claims:
raise PublicDataError(f"{raw_label} symbol is not a configured symbol")
limit = _unsigned_integer_text(query["limit"], f"{raw_label}.limit", minimum=1)
if limit != config.data.request_limit:
raise PublicDataError(f"{raw_label} limit does not match configuration")
if (
raw_start is None
or raw_end is None
or (
_integer(raw_start, f"raw artifact sidecar {index}.requested_range_ns.start"),
_integer(
raw_end,
f"raw artifact sidecar {index}.requested_range_ns.end_exclusive",
),
)
!= requested_range
):
raise PublicDataError(
f"raw artifact sidecar {index} requested range does not match"
)
aggregate_records = _aggregate_records(
path,
f"raw aggregate-trade artifact {index}",
request_limit=config.data.request_limit,
requested_range=requested_range,
)
aggregate_ids = tuple(aggregate_records)
if from_id is not None and aggregate_ids and aggregate_ids[0] != from_id:
raise PublicDataError(
f"raw aggregate-trade artifact {index} does not begin at requested fromId"
)
is_empty_aggregate = not aggregate_records
if aggregate_records:
descriptor = _RawPageDescriptor(
path=path,
sha256=digest,
symbol=symbol,
from_id=from_id,
rows=len(aggregate_records),
first_id=aggregate_ids[0],
last_id=aggregate_ids[-1],
)
existing = pages_by_digest.get(digest)
if existing is not None and (
existing.symbol != descriptor.symbol
or existing.from_id != descriptor.from_id
or existing.first_id != descriptor.first_id
or existing.last_id != descriptor.last_id
):
raise PublicDataError(
f"raw aggregate-trade digest has ambiguous nonempty semantics at index {index}"
)
if existing is None:
pages_by_digest[digest] = descriptor
aggregate_pages[symbol].append(
_AggregatePage(
path=path,
manifest_path=manifest_path,
sha256=digest,
request_uri=source_uri,
downloaded_at_ns=downloaded_at_ns,
from_id=from_id,
rows=len(aggregate_records),
first_id=aggregate_ids[0] if aggregate_ids else None,
last_id=aggregate_ids[-1] if aggregate_ids else None,
first_event_ts_ns=(
aggregate_records[aggregate_ids[0]].event_ts_ns if aggregate_ids else None
),
last_event_ts_ns=(
aggregate_records[aggregate_ids[-1]].event_ts_ns if aggregate_ids else None
),
)
)
elif requested_path == f"{base_path}{exchange_info_endpoint}":
query = _request_query(
source_uri,
base_url=config.data.base_url,
endpoint=exchange_info_endpoint,
label=raw_label,
)
if set(query) != {"symbol"}:
raise PublicDataError(f"{raw_label} must contain exactly the symbol parameter")
symbol = query["symbol"]
claim = symbol_claims.get(symbol)
if claim is None:
raise PublicDataError(f"{raw_label} symbol is not a configured symbol")
if raw_start is not None or raw_end is not None:
raise PublicDataError(
f"raw artifact sidecar {index} exchangeInfo range must be null"
)
_validate_exchange_info_payload(
path,
symbol=symbol,
claim=claim,
label=f"raw exchangeInfo artifact {index}",
)
exchange_info_symbols.add(symbol)
else:
raise PublicDataError(f"{raw_label} does not use an allowed Binance public endpoint")
if path in seen_paths and not (seen_paths[path] and is_empty_aggregate):
raise PublicDataError(
f"duplicate raw data path has nonempty or non-aggregate semantics at index {index}"
)
seen_paths[path] = is_empty_aggregate
paths.append(path)
manifest_paths.append(manifest_path)
digests.add(digest)
if exchange_info_symbols != set(symbol_claims):
missing = sorted(set(symbol_claims).difference(exchange_info_symbols))
raise PublicDataError(
"public ingestion lacks exchangeInfo raw artifacts for configured symbols: "
+ ", ".join(missing)
)
for symbol, pages in aggregate_pages.items():
initial_pages = [page for page in pages if page.from_id is None]
if len(initial_pages) != 1:
raise PublicDataError(
f"public ingestion must declare exactly one initial-time aggTrades page for {symbol}"
)
previous_last_id = initial_pages[0].last_id
previous_last_event_ts_ns = initial_pages[0].last_event_ts_ns
continuation_pages = sorted(
(page for page in pages if page.from_id is not None),
key=lambda page: cast(int, page.from_id),
)
seen_from_ids: set[int] = set()
terminal_empty_seen = initial_pages[0].rows == 0
for page in continuation_pages:
from_id = cast(int, page.from_id)
if from_id in seen_from_ids:
raise PublicDataError(f"raw aggregate-trade pagination repeats fromId for {symbol}")
seen_from_ids.add(from_id)
if terminal_empty_seen:
raise PublicDataError(
f"raw aggregate-trade pagination continues after an empty page for {symbol}"
)
if previous_last_id is None or from_id != previous_last_id + 1:
raise PublicDataError(
f"raw aggregate-trade pagination is not contiguous for {symbol}"
)
if (
previous_last_event_ts_ns is not None
and page.first_event_ts_ns is not None
and page.first_event_ts_ns < previous_last_event_ts_ns
):
raise PublicDataError(
f"raw aggregate-trade event time reverses across pages for {symbol}"
)
if page.rows == 0:
terminal_empty_seen = True
else:
previous_last_id = page.last_id
previous_last_event_ts_ns = page.last_event_ts_ns
ordered_pages = [initial_pages[0], *continuation_pages]
ordered_page_descriptors[symbol] = tuple(
_RawPageDescriptor(
path=page.path,
sha256=page.sha256,
symbol=symbol,
from_id=page.from_id,
rows=page.rows,
first_id=page.first_id,
last_id=page.last_id,
)
for page in ordered_pages
)
terminal_page = ordered_pages[-1]
symbol_claim = symbol_claims[symbol]
if symbol_claim.rows >= cast(int, config.data.max_events_per_symbol):
derived_stop_reason = "event_cap"
elif terminal_page.rows == 0:
derived_stop_reason = "empty_page"
elif (
terminal_page.last_event_ts_ns is not None
and terminal_page.last_event_ts_ns >= requested_range[1]
):
derived_stop_reason = "range_end"
elif terminal_page.rows < config.data.request_limit:
derived_stop_reason = "short_page"
else:
raise PublicDataError(
f"raw aggregate-trade chain ends without a terminal condition for {symbol}"
)
derived_complete = derived_stop_reason != "event_cap" and symbol_claim.rows > 0
if symbol_claim.complete_range != derived_complete:
raise PublicDataError(
f"manifested completeness disagrees with raw terminal page for {symbol}"
)
if derived_complete and terminal_page.downloaded_at_ns < requested_range[1]:
raise PublicDataError(
f"complete range for {symbol} ends after its terminal page was downloaded"
)
terminal_claim = symbol_claim.terminal
if terminal_claim is not None and (
terminal_claim.raw_page_count != len(ordered_pages)
or terminal_claim.stop_reason != derived_stop_reason
or terminal_claim.last_raw_page_sha256 != terminal_page.sha256
or terminal_claim.last_row_count != terminal_page.rows
or terminal_claim.last_request_uri != terminal_page.request_uri
or _declared_file(
bundle_root,
terminal_claim.last_path,
f"terminal raw page path for {symbol}",
)
!= terminal_page.path
or _declared_file(
bundle_root,
terminal_claim.last_manifest_path,
f"terminal raw page manifest path for {symbol}",
)
!= terminal_page.manifest_path
):
raise PublicDataError(
f"terminal stream claim does not match declared raw pages for {symbol}"
)
return _VerifiedRawArtifacts(
paths=tuple(sorted(set(paths))),
manifest_paths=tuple(sorted(manifest_paths)),
sha256s=frozenset(digests),
pages_by_digest=MappingProxyType(pages_by_digest),
ordered_pages=MappingProxyType(ordered_page_descriptors),
)
def _verify_part_descriptor(
raw_artifact: object,
*,
index: int,
write_ordinal: int,
normalized_root: Path,
dataset_source: str,
dataset_source_uri: str,
requested_range: tuple[int, int],
) -> _ParquetPartDescriptor:
label = f"normalized dataset.artifacts[{index}]"
artifact = _object(raw_artifact, label)
declared_rows = _integer(artifact.get("rows"), f"{label}.rows", minimum=1)
data_sha = _digest(artifact.get("data_sha256"), f"{label}.data_sha256")
sidecar_sha = _digest(artifact.get("manifest_sha256"), f"{label}.manifest_sha256")
data_path = _declared_file(normalized_root, artifact.get("data_path"), f"{label}.data_path")
sidecar_path = _declared_file(
normalized_root, artifact.get("manifest_path"), f"{label}.manifest_path"
)
_verify_file(data_path, data_sha, f"Parquet part {index}")
_verify_file(sidecar_path, sidecar_sha, f"Parquet sidecar {index}")
sidecar = _load_object(sidecar_path, f"Parquet sidecar {index}")
if sidecar.get("manifest_version") != MANIFEST_VERSION:
raise PublicDataError(f"Parquet sidecar {index} has an unsupported manifest version")
if sidecar.get("artifact_kind") != "normalized_parquet":
raise PublicDataError(f"Parquet sidecar {index} has an unexpected artifact kind")
if sidecar.get("dataset") != "trades" or sidecar.get("schema_name") != "trades":
raise PublicDataError(f"Parquet sidecar {index} declares an unexpected schema")
if sidecar.get("schema_version") != SCHEMA_VERSION:
raise PublicDataError(f"Parquet sidecar {index} has an unsupported schema version")
if sidecar.get("source") != dataset_source:
raise PublicDataError(f"Parquet sidecar {index} source does not match its dataset")
if sidecar.get("source_uri") != dataset_source_uri:
raise PublicDataError(f"Parquet sidecar {index} source URI does not match its dataset")
if _requested_range(sidecar, f"Parquet sidecar {index}") != requested_range:
raise PublicDataError(f"Parquet sidecar {index} requested range does not match")
if _integer(sidecar.get("rows"), f"Parquet sidecar {index}.rows", minimum=1) != declared_rows:
raise PublicDataError(f"Parquet sidecar {index} row count does not match")
checksum = _object(sidecar.get("checksum"), f"Parquet sidecar {index}.checksum")
if (
checksum.get("algorithm") != "sha256"
or _digest(checksum.get("value"), f"Parquet sidecar {index}.checksum.value") != data_sha
):
raise PublicDataError(f"Parquet sidecar {index} checksum does not match")
if sidecar.get("path") != artifact.get("data_path"):
raise PublicDataError(f"Parquet sidecar {index} data path does not match")
sidecar_ordinal = sidecar.get("write_ordinal")
if (
sidecar_ordinal is not None
and _integer(sidecar_ordinal, f"Parquet sidecar {index}.write_ordinal", minimum=0)
!= write_ordinal
):
raise PublicDataError(f"Parquet sidecar {index} write ordinal does not match")
if (
_integer(sidecar.get("bytes"), f"Parquet sidecar {index}.bytes", minimum=1)
!= data_path.stat().st_size
):
raise PublicDataError(f"Parquet sidecar {index} byte count does not match")
proportional_parquet_bound = _PARQUET_BASE_BYTES + (
declared_rows * _PARQUET_BYTES_PER_TRADE_ROW
)
encoded_parquet_bound = min(
proportional_parquet_bound,
_MAX_PARQUET_PART_ENCODED_BYTES,
)
if data_path.stat().st_size > encoded_parquet_bound:
raise PublicDataError(
f"Parquet part {index} exceeds bounded encoded bytes for its row count"
)
expected_schema = get_schema("trades")
try:
parquet = pq.ParquetFile(data_path)
if parquet.metadata.num_rows != declared_rows:
raise PublicDataError(f"Parquet part {index} metadata row count does not match")
uncompressed_bytes = sum(
parquet.metadata.row_group(row_group).total_byte_size
for row_group in range(parquet.metadata.num_row_groups)
)
decoded_parquet_bound = min(
proportional_parquet_bound,
_MAX_PARQUET_PART_DECODED_BYTES,
)
if uncompressed_bytes > decoded_parquet_bound:
raise PublicDataError(
f"Parquet part {index} exceeds bounded decoded bytes for its row count"
)
if not parquet.schema_arrow.equals(expected_schema, check_metadata=True):
raise PublicDataError(f"Parquet part {index} has an unexpected trades schema")
except PublicDataError:
raise
except (OSError, pa.ArrowException, ValueError) as exc:
raise PublicDataError(f"cannot inspect Parquet part {index} metadata: {exc}") from exc
observed = _object(sidecar.get("observed_range_ns"), f"Parquet sidecar {index}.observed")
observed_start = _integer(observed.get("start"), f"Parquet sidecar {index}.observed.start")
observed_end = _integer(
observed.get("end_inclusive"),
f"Parquet sidecar {index}.observed.end_inclusive",
)
if (
observed_end < observed_start
or observed_start < requested_range[0]
or observed_end >= requested_range[1]
):
raise PublicDataError(f"Parquet sidecar {index} observed range is invalid")
artifact_observed_value = artifact.get("observed_range_ns")
if artifact_observed_value is not None:
artifact_observed = _object(
artifact_observed_value,
f"normalized dataset.artifacts[{index}].observed_range_ns",
)
if (
_integer(
artifact_observed.get("start"),
f"normalized dataset.artifacts[{index}].observed_range_ns.start",
)
!= observed_start
or _integer(
artifact_observed.get("end_inclusive"),
f"normalized dataset.artifacts[{index}].observed_range_ns.end_inclusive",
)
!= observed_end
):
raise PublicDataError(
f"normalized dataset artifact {index} observed range does not match sidecar"
)
sidecar_symbol = _text(sidecar.get("symbol"), f"Parquet sidecar {index}.symbol")
sidecar_venue = _text(sidecar.get("venue"), f"Parquet sidecar {index}.venue")
partition_date = _text(sidecar.get("partition_date"), f"Parquet sidecar {index}.partition_date")
try:
parsed_date = datetime.strptime(partition_date, "%Y-%m-%d").date()
except ValueError as exc:
raise PublicDataError(f"Parquet sidecar {index} partition date is invalid") from exc
observed_dates = {
datetime.fromtimestamp(timestamp // _NS_PER_SECOND, tz=UTC).date()
for timestamp in (observed_start, observed_end)
}
if observed_dates != {parsed_date}:
raise PublicDataError(f"Parquet sidecar {index} observed range crosses its partition date")
return _ParquetPartDescriptor(
data_path=data_path,
data_sha256=data_sha,
sidecar_path=sidecar_path,
rows=declared_rows,
write_ordinal=write_ordinal,
venue=sidecar_venue,
symbol=sidecar_symbol,
partition_date=partition_date,
observed_start_ns=observed_start,
observed_end_inclusive_ns=observed_end,
)
def verify_public_trade_dataset(
config: ProjectConfig,
ingestion_manifest_path: str | Path,
*,
ingestion_manifest_sha256: str,
) -> PublicTradeDataset:
"""Verify a public-trade bundle without materializing normalized rows.
``ingestion_manifest_sha256`` is required so the path itself cannot silently
select a different ingestion run. This pass verifies every manifest,
sidecar, file digest, Parquet footer/schema, raw-page pagination claim, and
coverage claim. It retains only O(parts + raw pages + symbols) descriptors.
Normalized Parquet row data are read only by ``iter_verified_batches``.
"""
if config.data.mode != "binance_rest":
raise PublicDataError("public trade reader requires data.mode='binance_rest'")
configured_cap = config.data.max_events_per_symbol
if configured_cap is None or configured_cap < 1:
raise PublicDataError("public trade reader requires a configured positive row cap")
max_rows = configured_cap * len(config.data.symbols)
expected_ingestion_sha = _digest(ingestion_manifest_sha256, "ingestion_manifest_sha256")
manifest_path = Path(ingestion_manifest_path).resolve()
if manifest_path.parent.name != "_ingestion_manifests":
raise PublicDataError(
"ingestion manifest must remain under its bundle _ingestion_manifests directory"
)
_verify_file(manifest_path, expected_ingestion_sha, "ingestion manifest")
ingestion = _load_object(manifest_path, "ingestion manifest")
evidence_tier, all_complete, requested_range, symbol_claims, dataset_entry = (
_validate_ingestion_manifest(ingestion, config)
)
bundle_root = manifest_path.parent.parent.resolve()
verified_raw = _verify_raw_artifacts(
ingestion,
bundle_root=bundle_root,
requested_range=requested_range,
config=config,
symbol_claims=symbol_claims,
)
dataset_manifest_path = _declared_file(
bundle_root,
dataset_entry.get("manifest_path"),
"ingestion.normalized_datasets[0].manifest_path",
)
dataset_manifest_sha = _digest(
dataset_entry.get("manifest_sha256"),
"ingestion.normalized_datasets[0].manifest_sha256",
)
_verify_file(dataset_manifest_path, dataset_manifest_sha, "normalized dataset manifest")
if dataset_manifest_path.parent.name != "_manifests":
raise PublicDataError("normalized dataset manifest is outside its _manifests directory")
normalized_root = dataset_manifest_path.parent.parent.resolve()
if normalized_root != (bundle_root / "normalized").resolve():
raise PublicDataError(
"normalized dataset is not under the ingestion bundle's normalized root"
)
dataset_manifest = _load_object(dataset_manifest_path, "normalized dataset manifest")
dataset_rows, raw_artifacts, dataset_source_uri = _validate_dataset_manifest(
dataset_manifest,
ingestion_source=ingestion.get("source"),
requested_range=requested_range,
)
ingestion_rows = _integer(
dataset_entry.get("rows"), "ingestion.normalized_datasets[0].rows", minimum=1
)
if dataset_rows > max_rows:
raise PublicDataError(
f"manifested trades contain {dataset_rows} rows, above required bound {max_rows}"
)
if dataset_rows != ingestion_rows or dataset_rows != sum(
claim.rows for claim in symbol_claims.values()
):
raise PublicDataError("ingestion, symbol, and normalized dataset row counts do not match")
parts: list[_ParquetPartDescriptor] = []
declared_part_rows = 0
seen_data_paths: set[Path] = set()
seen_sidecar_paths: set[Path] = set()
declared_symbol_rows: dict[str, int] = {symbol: 0 for symbol in symbol_claims}
declared_symbol_starts: dict[str, int] = {}
declared_symbol_ends: dict[str, int] = {}
ordinal_presence: list[bool] = []
declared_ordinals: set[int] = set()
for index, raw_artifact in enumerate(raw_artifacts):
artifact = _object(raw_artifact, f"normalized dataset.artifacts[{index}]")
ordinal_value = artifact.get("write_ordinal")
ordinal_presence.append(ordinal_value is not None)
write_ordinal = (
_integer(
ordinal_value,
f"normalized dataset.artifacts[{index}].write_ordinal",
minimum=0,
)
if ordinal_value is not None
else index
)
if write_ordinal in declared_ordinals:
raise PublicDataError(f"duplicate Parquet write ordinal: {write_ordinal}")
declared_ordinals.add(write_ordinal)
next_rows = _integer(
artifact.get("rows"), f"normalized dataset.artifacts[{index}].rows", minimum=1
)
if declared_part_rows + next_rows > max_rows:
raise PublicDataError(f"declared Parquet parts exceed required row bound {max_rows}")
part = _verify_part_descriptor(
artifact,
index=index,
write_ordinal=write_ordinal,
normalized_root=normalized_root,
dataset_source=_text(dataset_manifest.get("source"), "normalized dataset.source"),
dataset_source_uri=dataset_source_uri,
requested_range=requested_range,
)
if part.data_path in seen_data_paths:
raise PublicDataError(f"duplicate declared Parquet part: {part.data_path}")
if part.sidecar_path in seen_sidecar_paths:
raise PublicDataError(f"duplicate declared Parquet sidecar: {part.sidecar_path}")
seen_data_paths.add(part.data_path)
seen_sidecar_paths.add(part.sidecar_path)
if part.venue != "binance_spot":
raise PublicDataError(f"Parquet part {index} venue is not binance_spot")
if part.symbol not in symbol_claims:
raise PublicDataError(f"Parquet part {index} symbol is not manifested")
declared_part_rows += part.rows
declared_symbol_rows[part.symbol] += part.rows
declared_symbol_starts[part.symbol] = min(
declared_symbol_starts.get(part.symbol, part.observed_start_ns),
part.observed_start_ns,
)
declared_symbol_ends[part.symbol] = max(
declared_symbol_ends.get(part.symbol, part.observed_end_inclusive_ns),
part.observed_end_inclusive_ns,
)
parts.append(part)
if declared_part_rows != dataset_rows:
raise PublicDataError("declared Parquet part rows do not match dataset rows")
if any(ordinal_presence) and not all(ordinal_presence):
raise PublicDataError("normalized dataset mixes present and missing write ordinals")
if all(ordinal_presence) and declared_ordinals != set(range(len(parts))):
raise PublicDataError("normalized dataset write ordinals must be contiguous from zero")
canonical_order = ("venue", "symbol", "available_ts_ns", "event_ts_ns", "trade_id")
symbol_coverage: list[SymbolObservedCoverage] = []
for symbol in sorted(symbol_claims):
claim = symbol_claims[symbol]
if declared_symbol_rows[symbol] != claim.rows:
raise PublicDataError(f"declared Parquet rows for {symbol} do not match coverage claim")
if symbol not in declared_symbol_starts:
raise PublicDataError(f"declared Parquet parts contain no rows for {symbol}")
symbol_coverage.append(
SymbolObservedCoverage(
symbol=symbol,
rows=claim.rows,
complete_range=claim.complete_range,
tick_size=claim.tick_size,
lot_size=claim.lot_size,
observed=_coverage(
declared_symbol_starts[symbol],
declared_symbol_ends[symbol],
),
)
)
return PublicTradeDataset(
rows=dataset_rows,
observed=_coverage(
min(part.observed_start_ns for part in parts),
max(part.observed_end_inclusive_ns for part in parts),
),
symbols=tuple(symbol_coverage),
evidence_tier=evidence_tier,
all_requested_ranges_complete=all_complete,
ingestion_manifest_path=manifest_path,
ingestion_manifest_sha256=expected_ingestion_sha,
dataset_manifest_path=dataset_manifest_path,
dataset_manifest_sha256=dataset_manifest_sha,
part_paths=tuple(part.data_path for part in parts),
raw_artifact_paths=verified_raw.paths,
raw_manifest_paths=verified_raw.manifest_paths,
raw_artifact_sha256s=tuple(sorted(verified_raw.sha256s)),
row_bound=max_rows,
canonical_order=canonical_order,
_config=config,
_requested_range=requested_range,
_symbol_claims=MappingProxyType(dict(symbol_claims)),
_parts=tuple(parts),
_raw_pages_by_digest=verified_raw.pages_by_digest,
_ordered_raw_pages=verified_raw.ordered_pages,
)
def _normalized_batch(
raw_batch: pa.RecordBatch,
*,
label: str,
) -> pa.RecordBatch:
expected = get_schema("trades")
try:
arrays: list[pa.Array] = []
for name in expected.names:
field_index = raw_batch.schema.get_field_index(name)
if field_index < 0:
raise PublicDataError(f"{label} is missing column {name!r}")
arrays.append(raw_batch.column(field_index))
normalized = pa.RecordBatch.from_arrays(arrays, schema=expected)
ensure_schema(normalized, "trades")
return normalized
except PublicDataError:
raise
except (pa.ArrowException, ValueError) as exc:
raise PublicDataError(f"{label} has an unexpected trades schema: {exc}") from exc
def _iter_audited_physical_batches(
dataset: PublicTradeDataset,
*,
batch_rows: int,
) -> Generator[pa.RecordBatch, None, _VerifiedStreamSummary]:
"""Yield each source batch once after immutable physical-order auditing."""
validator = IncrementalQualityValidator(
"trades",
max_spread_bps=dataset._config.quality.max_spread_bps,
max_silence_ns=dataset._config.quality.max_silence_ms * 1_000_000,
row_chunk_size=batch_rows,
)
validator_finished = False
lineage_index: _ExpectedLineageIndex | None = None
raw_cache = _RawPageCache(
dataset._raw_pages_by_digest,
request_limit=dataset._config.data.request_limit,
requested_range=dataset._requested_range,
)
actual_symbol_rows: dict[str, int] = {symbol: 0 for symbol in dataset._symbol_claims}
actual_symbol_starts: dict[str, int] = {}
actual_symbol_ends: dict[str, int] = {}
total_rows = 0
audited_schema = get_schema("trades").append(
pa.field("__physical_ordinal", pa.int64(), nullable=False)
)
try:
lineage_index = _ExpectedLineageIndex(dataset)
for part in sorted(dataset._parts, key=lambda item: item.write_ordinal):
_verify_file(part.data_path, part.data_sha256, "normalized Parquet part")
part_rows = 0
part_start: int | None = None
part_end: int | None = None
try:
parquet = pq.ParquetFile(part.data_path)
physical_batches = parquet.iter_batches(batch_size=batch_rows)
for raw_batch in physical_batches:
if raw_batch.num_rows < 1:
continue
if raw_batch.num_rows > batch_rows:
raise PublicDataError(
f"Parquet emitted {raw_batch.num_rows} rows above batch bound "
f"{batch_rows}"
)
normalized = _normalized_batch(
raw_batch,
label=f"Parquet part {part.data_path}",
)
rows = cast(list[dict[str, Any]], normalized.to_pylist())
for local_index, row in enumerate(rows):
row_index = total_rows + local_index
symbol = str(row["symbol"])
venue = str(row["venue"])
timestamp = int(row["event_ts_ns"])
if symbol != part.symbol or venue != part.venue:
raise PublicDataError(
f"normalized row {row_index} does not match its Parquet "
"sidecar partition"
)
actual_date = (
datetime.fromtimestamp(timestamp // _NS_PER_SECOND, tz=UTC)
.date()
.isoformat()
)
if actual_date != part.partition_date:
raise PublicDataError(
f"normalized row {row_index} does not match its partition date"
)
if (
not dataset._requested_range[0]
<= timestamp
< dataset._requested_range[1]
):
raise PublicDataError(
f"normalized row {row_index} is outside the requested range"
)
claim = dataset._symbol_claims.get(symbol)
if claim is None:
raise PublicDataError(
f"normalized row {row_index} has an unmanifested symbol {symbol}"
)
try:
tick_size = Decimal(str(row["tick_size"]))
lot_size = Decimal(str(row["lot_size"]))
except InvalidOperation as exc:
raise PublicDataError(
f"normalized row {row_index} has invalid scales for {symbol}"
) from exc
if tick_size != claim.tick_size or lot_size != claim.lot_size:
raise PublicDataError(
f"normalized row {row_index} scales do not match manifest for "
f"{symbol}"
)
_validate_normalized_trade_row(
row,
row_index=row_index,
symbol_claims=dataset._symbol_claims,
raw_cache=raw_cache,
)
lineage_index.mark_seen(
symbol=symbol,
source_sha256=str(row["source_artifact_id"]),
trade_id=int(row["trade_id"]),
)
actual_symbol_rows[symbol] += 1
actual_symbol_starts[symbol] = min(
actual_symbol_starts.get(symbol, timestamp), timestamp
)
actual_symbol_ends[symbol] = max(
actual_symbol_ends.get(symbol, timestamp), timestamp
)
part_start = timestamp if part_start is None else min(part_start, timestamp)
part_end = timestamp if part_end is None else max(part_end, timestamp)
del rows
validator.update(normalized)
lineage_index.commit()
batch_count = normalized.num_rows
physical_start = total_rows
total_rows += batch_count
part_rows += batch_count
yield pa.RecordBatch.from_arrays(
[
*normalized.columns,
pa.array(
range(physical_start, physical_start + batch_count),
type=pa.int64(),
),
],
schema=audited_schema,
)
except PublicDataError:
raise
except (OSError, pa.ArrowException, ValueError) as exc:
raise PublicDataError(
f"cannot stream normalized Parquet part {part.data_path}: {exc}"
) from exc
if part_rows != part.rows:
raise PublicDataError(
f"materialized rows for Parquet part {part.data_path} do not match sidecar"
)
if part_start != part.observed_start_ns or part_end != part.observed_end_inclusive_ns:
raise PublicDataError(
f"materialized coverage for Parquet part {part.data_path} does not match "
"sidecar"
)
_verify_file(part.data_path, part.data_sha256, "normalized Parquet part")
validation = validator.finish()
validator_finished = True
if total_rows != dataset.rows:
raise PublicDataError("streamed trades do not match manifested dataset rows")
symbol_coverage: list[SymbolObservedCoverage] = []
for symbol in sorted(dataset._symbol_claims):
claim = dataset._symbol_claims[symbol]
if actual_symbol_rows[symbol] != claim.rows:
raise PublicDataError(f"materialized rows for {symbol} do not match coverage claim")
if symbol not in actual_symbol_starts:
raise PublicDataError(f"materialized trades contain no rows for {symbol}")
symbol_coverage.append(
SymbolObservedCoverage(
symbol=symbol,
rows=claim.rows,
complete_range=claim.complete_range,
tick_size=claim.tick_size,
lot_size=claim.lot_size,
observed=_coverage(actual_symbol_starts[symbol], actual_symbol_ends[symbol]),
)
)
observed = _coverage(min(actual_symbol_starts.values()), max(actual_symbol_ends.values()))
if observed != dataset.observed or tuple(symbol_coverage) != dataset.symbols:
raise PublicDataError("materialized coverage does not match verified manifest metadata")
missing_lineage, repeated_lineage = lineage_index.mismatch_counts()
if (missing_lineage or repeated_lineage) and not (
dataset._config.quality.fail_on_error and validation.has_errors
):
raise PublicDataError(
"normalized rows do not exactly cover downloader-selected raw trades: "
f"missing={missing_lineage}, repeated={repeated_lineage}"
)
return _VerifiedStreamSummary(
validation=validation,
observed=observed,
symbols=tuple(symbol_coverage),
)
finally:
if not validator_finished:
validator.close()
if lineage_index is not None:
lineage_index.close()
def _stream_verified_batches(
dataset: PublicTradeDataset,
*,
batch_rows: int,
memory_limit: str,
temp_directory: str | Path | None,
) -> Generator[pa.RecordBatch, None, _VerifiedStreamSummary]:
"""Audit source order, then externally sort a fresh bounded research pass."""
owned_temp: tempfile.TemporaryDirectory[str] | None = None
if temp_directory is None:
owned_temp = tempfile.TemporaryDirectory(prefix="microstructure-public-sort-")
sort_temp = Path(owned_temp.name).resolve()
else:
sort_temp = Path(temp_directory).resolve()
if not sort_temp.is_dir():
raise PublicDataError(f"DuckDB temporary directory does not exist: {sort_temp}")
connection: duckdb.DuckDBPyConnection | None = None
reader: pa.RecordBatchReader | None = None
source_reader: pa.RecordBatchReader | None = None
physical_source: _PhysicalAuditBatchSource | None = None
field_names = get_schema("trades").names
previous_order_key: tuple[str, str, int, int, int, str, int] | None = None
emitted_rows = 0
quoted_fields = ", ".join(f'"{name}"' for name in field_names)
order_fields = ", ".join(f'"{name}" ASC' for name in dataset.canonical_order)
query = (
f"SELECT {quoted_fields}, __physical_ordinal "
"FROM verified_source "
f"ORDER BY {order_fields}, source_artifact_id ASC, __physical_ordinal ASC"
)
try:
connection = duckdb.connect(database=":memory:")
connection.execute("SET memory_limit = ?", [memory_limit])
connection.execute("SET temp_directory = ?", [str(sort_temp)])
connection.execute("SET threads = 1")
connection.execute("SET preserve_insertion_order = false")
audited_schema = get_schema("trades").append(
pa.field("__physical_ordinal", pa.int64(), nullable=False)
)
physical_source = _PhysicalAuditBatchSource(
_iter_audited_physical_batches(dataset, batch_rows=batch_rows)
)
source_reader = pa.RecordBatchReader.from_batches(audited_schema, physical_source)
connection.register("verified_source", source_reader)
reader = connection.execute(query).to_arrow_reader(batch_size=batch_rows)
summary: _VerifiedStreamSummary | None = None
for raw_batch in reader:
if summary is None:
summary = physical_source.summary
if dataset._config.quality.fail_on_error and summary.validation.has_errors:
raise PublicDataError(
"public normalized trades failed quality validation with "
f"{summary.validation.error_count} error findings"
)
if raw_batch.num_rows < 1:
continue
if raw_batch.num_rows > batch_rows:
raise PublicDataError(
f"DuckDB emitted {raw_batch.num_rows} rows above batch bound {batch_rows}"
)
normalized = _normalized_batch(raw_batch, label="DuckDB canonical output")
physical_ordinals = cast(
list[int],
raw_batch.column(
raw_batch.schema.get_field_index("__physical_ordinal")
).to_pylist(),
)
rows = cast(list[dict[str, Any]], normalized.to_pylist())
for row, physical_ordinal in zip(rows, physical_ordinals, strict=True):
order_key = (
str(row["venue"]),
str(row["symbol"]),
int(row["available_ts_ns"]),
int(row["event_ts_ns"]),
int(row["trade_id"]),
str(row["source_artifact_id"]),
int(physical_ordinal),
)
if previous_order_key is not None and order_key < previous_order_key:
raise PublicDataError("DuckDB output violated canonical trade order")
previous_order_key = order_key
del rows, physical_ordinals
emitted_rows += normalized.num_rows
yield normalized
if emitted_rows != dataset.rows:
raise PublicDataError("canonical stream rows do not match manifested dataset rows")
if summary is None:
summary = physical_source.summary
return summary
except duckdb.Error as exc:
raise PublicDataError(f"cannot externally sort public trades: {exc}") from exc
finally:
if reader is not None:
reader.close()
if source_reader is not None:
source_reader.close()
if physical_source is not None:
physical_source.close()
if connection is not None:
connection.close()
if owned_temp is not None:
owned_temp.cleanup()
def read_public_trades(
config: ProjectConfig,
ingestion_manifest_path: str | Path,
*,
ingestion_manifest_sha256: str,
materialization_max_rows: int = 100_000,
) -> PublicTrades:
"""Compatibility materializer built on the bounded verified stream.
``materialization_max_rows`` is a separate finite safety guard. It is
checked against already-verified manifest metadata before DuckDB or PyArrow
reads any normalized row, regardless of the configured ingestion cap.
"""
dataset = verify_public_trade_dataset(
config,
ingestion_manifest_path,
ingestion_manifest_sha256=ingestion_manifest_sha256,
)
if (
isinstance(materialization_max_rows, bool)
or not isinstance(materialization_max_rows, int)
or materialization_max_rows < 1
):
raise ValueError("materialization_max_rows must be a positive integer")
if dataset.rows > materialization_max_rows:
raise PublicDataError(
f"verified public data has {dataset.rows} rows, above materialization guard "
f"{materialization_max_rows}"
)
stream = dataset.iter_verified_batches(
batch_rows=min(65_536, materialization_max_rows),
memory_limit="256MB",
)
batches: list[pa.RecordBatch] = []
try:
for batch in stream:
batches.append(batch)
summary = stream.summary
finally:
stream.close()
try:
trades = pa.Table.from_batches(batches, schema=get_schema("trades"))
ensure_schema(trades, "trades")
except (pa.ArrowException, ValueError) as exc:
raise PublicDataError(f"cannot materialize verified public trades: {exc}") from exc
if trades.num_rows != dataset.rows:
raise PublicDataError("materialized trades do not match manifested dataset rows")
return PublicTrades(
arrow_trades=trades,
polars_trades=cast(pl.DataFrame, pl.from_arrow(trades)),
observed=summary.observed,
symbols=summary.symbols,
evidence_tier=dataset.evidence_tier,
all_requested_ranges_complete=dataset.all_requested_ranges_complete,
ingestion_manifest_path=dataset.ingestion_manifest_path,
ingestion_manifest_sha256=dataset.ingestion_manifest_sha256,
dataset_manifest_path=dataset.dataset_manifest_path,
dataset_manifest_sha256=dataset.dataset_manifest_sha256,
part_paths=dataset.part_paths,
raw_artifact_paths=dataset.raw_artifact_paths,
raw_manifest_paths=dataset.raw_manifest_paths,
raw_artifact_sha256s=dataset.raw_artifact_sha256s,
validation=summary.validation,
row_bound=dataset.row_bound,
canonical_order=dataset.canonical_order,
)
|