Datasets:
Tasks:
Tabular Classification
Formats:
parquet
Languages:
English
Size:
< 1K
Tags:
economics
quantitative-finance
causal-inference
macroeconomics
housing-economics
market-microstructure
License:
File size: 22,481 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 | """Streaming, content-addressed Parquet storage and immutable data manifests."""
from __future__ import annotations
import hashlib
import json
import os
import re
import tempfile
from collections import defaultdict
from collections.abc import Iterable, Mapping, Sequence
from contextlib import nullcontext, suppress
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import pyarrow as pa # type: ignore[import-untyped]
import pyarrow.compute as pc # type: ignore[import-untyped]
import pyarrow.parquet as pq # type: ignore[import-untyped]
from microstructure.data.evidence_budget import RetainedEvidenceBudget
from microstructure.data.schemas import SCHEMA_VERSION, ensure_schema, get_schema
from microstructure.provenance import read_json, sha256_file, utc_now_iso, write_json
MANIFEST_VERSION = "1.0.0"
_SAFE_COMPONENT = re.compile(r"^[A-Za-z0-9_.-]+$")
_NS_PER_SECOND = 1_000_000_000
class StorageError(RuntimeError):
"""Raised for an unsafe path or inconsistent immutable artifact."""
@dataclass(frozen=True, slots=True)
class PartitionArtifact:
dataset: str
venue: str
symbol: str
partition_date: str
rows: int
write_ordinal: int
observed_start_ns: int
observed_end_inclusive_ns: int
data_path: Path
manifest_path: Path
data_sha256: str
manifest_sha256: str
@dataclass(frozen=True, slots=True)
class DatasetWriteResult:
dataset: str
schema_version: str
rows: int
artifacts: tuple[PartitionArtifact, ...]
manifest_path: Path
manifest_sha256: str
@dataclass(frozen=True, slots=True)
class CaptureDatasetWriteResult:
"""Constant-descriptor result for one bounded-memory live capture."""
dataset: str
schema_version: str
rows: int
data_path: Path | None
data_sha256: str | None
manifest_path: Path
manifest_sha256: str
def _safe(value: str, label: str) -> str:
if not value or _SAFE_COMPONENT.fullmatch(value) is None:
raise StorageError(f"unsafe {label} path component: {value!r}")
return value
def _stable_sha(payload: Mapping[str, Any]) -> str:
encoded = json.dumps(payload, sort_keys=True, separators=(",", ":"), allow_nan=False)
return hashlib.sha256(encoded.encode()).hexdigest()
def _partition_date(timestamp_ns: int) -> str:
seconds = timestamp_ns // _NS_PER_SECOND
return datetime.fromtimestamp(seconds, tz=UTC).date().isoformat()
def _immutable_json(
directory: Path,
stem: str,
payload: Mapping[str, Any],
*,
retained_evidence_budget: RetainedEvidenceBudget | None = None,
) -> tuple[Path, str]:
identity = _stable_sha(payload)
destination = directory / f"{stem}-{identity[:20]}.json"
if retained_evidence_budget is not None:
retained_evidence_budget.assert_contains(destination)
transaction = (
retained_evidence_budget.write_transaction()
if retained_evidence_budget is not None
else nullcontext()
)
with transaction:
if destination.exists():
existing = read_json(destination)
if existing != dict(payload):
raise StorageError(f"immutable manifest collision at {destination}")
else:
encoded = (
json.dumps(payload, indent=2, sort_keys=True, allow_nan=False) + "\n"
).encode()
reservation = (
retained_evidence_budget.reserve(
len(encoded),
label=f"raw source manifest {destination.name}",
)
if retained_evidence_budget is not None
else None
)
try:
write_json(destination, payload)
if destination.stat().st_size != len(encoded):
raise StorageError(
f"source manifest byte count changed while writing {destination}"
)
if reservation is not None:
reservation.commit()
except BaseException:
destination.unlink(missing_ok=True)
if reservation is not None and reservation.active:
reservation.release()
raise
return destination, sha256_file(destination)
def write_source_manifest(
raw_path: str | Path,
*,
source: str,
source_uri: str,
downloaded_at_utc: str,
requested_start_ns: int | None,
requested_end_ns: int | None,
upstream_checksum_sha256: str | None = None,
response_headers: Mapping[str, str] | None = None,
retained_evidence_budget: RetainedEvidenceBudget | None = None,
) -> tuple[Path, str]:
"""Write an immutable sidecar for an untouched raw response or archive."""
path = Path(raw_path)
if not path.is_file():
raise StorageError(f"raw artifact does not exist: {path}")
checksum = sha256_file(path)
payload: dict[str, Any] = {
"manifest_version": MANIFEST_VERSION,
"artifact_kind": "raw_source",
"source": source,
"source_uri": source_uri,
"downloaded_at_utc": downloaded_at_utc,
"requested_range_ns": {"start": requested_start_ns, "end_exclusive": requested_end_ns},
"checksum": {"algorithm": "sha256", "value": checksum},
"upstream_checksum_sha256": upstream_checksum_sha256,
"bytes": path.stat().st_size,
"path": path.name,
"response_headers": dict(sorted((response_headers or {}).items())),
}
manifest_path, manifest_sha = _immutable_json(
path.parent,
f"{path.name}.manifest",
payload,
retained_evidence_budget=retained_evidence_budget,
)
return manifest_path, manifest_sha
def _write_parquet_part(
*,
table: pa.Table,
root: Path,
dataset: str,
schema_name: str,
venue: str,
symbol: str,
date: str,
source: str,
source_uri: str,
downloaded_at_utc: str,
source_checksum_sha256: str | None,
requested_start_ns: int | None,
requested_end_ns: int | None,
time_column: str,
compression: str,
write_ordinal: int,
) -> PartitionArtifact:
partition = (
root
/ _safe(dataset, "dataset")
/ f"schema-{_safe(SCHEMA_VERSION, 'schema version')}"
/ f"venue-{_safe(venue, 'venue')}"
/ f"symbol-{_safe(symbol, 'symbol')}"
/ f"date-{_safe(date, 'date')}"
)
partition.mkdir(parents=True, exist_ok=True)
handle, temporary_name = tempfile.mkstemp(dir=partition, prefix=".part-", suffix=".parquet.tmp")
os.close(handle)
temporary = Path(temporary_name)
try:
table = table.replace_schema_metadata(get_schema(schema_name).metadata)
pq.write_table(
table,
temporary,
compression=compression,
use_dictionary=True,
write_statistics=True,
)
checksum = sha256_file(temporary)
destination = partition / f"part-{checksum[:20]}.parquet"
if destination.exists():
if sha256_file(destination) != checksum:
raise StorageError(f"content-address collision at {destination}")
temporary.unlink()
else:
os.replace(temporary, destination)
except BaseException:
temporary.unlink(missing_ok=True)
raise
time_bounds = pc.min_max(table.column(time_column)).as_py()
if time_bounds is None or time_bounds["min"] is None or time_bounds["max"] is None:
raise StorageError("cannot manifest a Parquet part without a timestamp range")
manifest_payload: dict[str, Any] = {
"manifest_version": MANIFEST_VERSION,
"artifact_kind": "normalized_parquet",
"dataset": dataset,
"schema_name": schema_name,
"schema_version": SCHEMA_VERSION,
"venue": venue,
"symbol": symbol,
"partition_date": date,
"write_ordinal": write_ordinal,
"source": source,
"source_uri": source_uri,
"downloaded_at_utc": downloaded_at_utc,
"requested_range_ns": {"start": requested_start_ns, "end_exclusive": requested_end_ns},
"observed_range_ns": {
"start": int(time_bounds["min"]),
"end_inclusive": int(time_bounds["max"]),
},
"source_checksum_sha256": source_checksum_sha256,
"checksum": {"algorithm": "sha256", "value": checksum},
"rows": table.num_rows,
"bytes": destination.stat().st_size,
"path": str(destination.relative_to(root)),
"transformations": [
"normalized field names and types",
"UTC epoch-nanosecond timestamp conversion",
"exact integer tick/lot conversion where scale supplied",
],
}
manifest_path, manifest_sha = _immutable_json(
partition, f"part-{checksum[:20]}.manifest", manifest_payload
)
return PartitionArtifact(
dataset=dataset,
venue=venue,
symbol=symbol,
partition_date=date,
rows=table.num_rows,
write_ordinal=write_ordinal,
observed_start_ns=int(time_bounds["min"]),
observed_end_inclusive_ns=int(time_bounds["max"]),
data_path=destination,
manifest_path=manifest_path,
data_sha256=checksum,
manifest_sha256=manifest_sha,
)
def _as_table(batch: pa.RecordBatch | pa.Table) -> pa.Table:
return batch if isinstance(batch, pa.Table) else pa.Table.from_batches([batch])
def write_partitioned_parquet(
batches: Iterable[pa.RecordBatch | pa.Table],
*,
root: str | Path,
dataset: str,
schema_name: str,
source: str,
source_uri: str = "synthetic://local",
downloaded_at_utc: str | None = None,
source_checksum_sha256: str | None = None,
requested_start_ns: int | None = None,
requested_end_ns: int | None = None,
time_column: str = "event_ts_ns",
max_rows_per_file: int = 250_000,
max_input_batch_rows: int = 250_000,
compression: str = "zstd",
) -> DatasetWriteResult:
"""Stream batches into immutable Parquet parts partitioned by venue/symbol/day.
Each input batch is split only within that bounded batch, so this function
never requires the complete data set in memory. Existing content-addressed
parts are reused rather than overwritten.
"""
if max_rows_per_file < 1:
raise ValueError("max_rows_per_file must be positive")
if max_input_batch_rows < 1:
raise ValueError("max_input_batch_rows must be positive")
destination_root = Path(root)
destination_root.mkdir(parents=True, exist_ok=True)
download_time = downloaded_at_utc or utc_now_iso()
artifacts: list[PartitionArtifact] = []
for raw_batch in batches:
table = _as_table(raw_batch)
if table.num_rows > max_input_batch_rows:
raise StorageError(
f"input batch has {table.num_rows} rows, above the bounded-memory limit "
f"{max_input_batch_rows}"
)
ensure_schema(table, schema_name)
if time_column not in table.column_names:
raise StorageError(f"partition time column is missing: {time_column}")
groups: dict[tuple[str, str, str], list[int]] = defaultdict(list)
venues = table.column("venue").to_pylist()
symbols = table.column("symbol").to_pylist()
timestamps = table.column(time_column).to_pylist()
for row_index, (venue, symbol, timestamp_ns) in enumerate(
zip(venues, symbols, timestamps, strict=True)
):
groups[(str(venue), str(symbol), _partition_date(int(timestamp_ns)))].append(row_index)
for (venue, symbol, date), indices in groups.items():
for offset in range(0, len(indices), max_rows_per_file):
selected = indices[offset : offset + max_rows_per_file]
part = table.take(pa.array(selected, type=pa.int64()))
artifacts.append(
_write_parquet_part(
table=part,
root=destination_root,
dataset=dataset,
schema_name=schema_name,
venue=venue,
symbol=symbol,
date=date,
source=source,
source_uri=source_uri,
downloaded_at_utc=download_time,
source_checksum_sha256=source_checksum_sha256,
requested_start_ns=requested_start_ns,
requested_end_ns=requested_end_ns,
time_column=time_column,
compression=compression,
write_ordinal=len(artifacts),
)
)
artifact_entries = [
{
"data_path": str(item.data_path.relative_to(destination_root)),
"manifest_path": str(item.manifest_path.relative_to(destination_root)),
"data_sha256": item.data_sha256,
"manifest_sha256": item.manifest_sha256,
"rows": item.rows,
"write_ordinal": item.write_ordinal,
"observed_range_ns": {
"start": item.observed_start_ns,
"end_inclusive": item.observed_end_inclusive_ns,
},
}
for item in artifacts
]
stable_identity: dict[str, Any] = {
"manifest_version": MANIFEST_VERSION,
"dataset": dataset,
"schema_version": SCHEMA_VERSION,
"source": source,
"source_uri": source_uri,
"downloaded_at_utc": download_time,
"requested_range_ns": {"start": requested_start_ns, "end_exclusive": requested_end_ns},
"artifacts": artifact_entries,
"rows": sum(item.rows for item in artifacts),
}
manifest_directory = destination_root / "_manifests"
manifest_directory.mkdir(parents=True, exist_ok=True)
manifest_path, manifest_sha = _immutable_json(
manifest_directory, f"{_safe(dataset, 'dataset')}.manifest", stable_identity
)
return DatasetWriteResult(
dataset=dataset,
schema_version=SCHEMA_VERSION,
rows=sum(item.rows for item in artifacts),
artifacts=tuple(artifacts),
manifest_path=manifest_path,
manifest_sha256=manifest_sha,
)
def write_capture_parquet(
batches: Iterable[pa.RecordBatch | pa.Table],
*,
root: str | Path,
dataset: str,
schema_name: str,
venue: str,
symbol: str,
capture_id: str,
source: str,
source_uri: str,
downloaded_at_utc: str | None = None,
source_checksum_sha256: str | None = None,
requested_start_ns: int | None = None,
requested_end_ns: int | None = None,
time_column: str = "event_ts_ns",
max_input_batch_rows: int = 16_384,
compression: str = "zstd",
) -> CaptureDatasetWriteResult:
"""Write one live-capture Parquet artifact from a bounded batch iterator.
The Parquet writer emits one bounded row group per input batch and retains
exactly one output descriptor, independent of capture length. Live capture
data is partitioned by immutable ``capture_id`` rather than UTC day because
capture-order quality evidence must not be reordered to satisfy a partition.
"""
if max_input_batch_rows < 1:
raise ValueError("max_input_batch_rows must be positive")
safe_dataset = _safe(dataset, "dataset")
safe_schema = _safe(SCHEMA_VERSION, "schema version")
safe_venue = _safe(venue, "venue")
safe_symbol = _safe(symbol, "symbol")
safe_capture_id = _safe(capture_id, "capture ID")
schema = get_schema(schema_name)
if time_column not in schema.names:
raise StorageError(f"partition time column is missing: {time_column}")
destination_root = Path(root)
partition = (
destination_root
/ safe_dataset
/ f"schema-{safe_schema}"
/ f"venue-{safe_venue}"
/ f"symbol-{safe_symbol}"
/ f"capture-{safe_capture_id}"
)
partition.mkdir(parents=True, exist_ok=True)
descriptor, temporary_name = tempfile.mkstemp(
dir=partition,
prefix=".capture-",
suffix=".parquet.tmp",
)
os.close(descriptor)
temporary = Path(temporary_name)
writer: pq.ParquetWriter | None = None
rows = 0
observed_start_ns: int | None = None
observed_end_ns: int | None = None
destination: Path | None = None
checksum: str | None = None
try:
writer = pq.ParquetWriter(
temporary,
schema,
compression=compression,
use_dictionary=True,
write_statistics=True,
)
for raw_batch in batches:
table = _as_table(raw_batch)
if table.num_rows > max_input_batch_rows:
raise StorageError(
f"input batch has {table.num_rows} rows, above the bounded-memory "
f"limit {max_input_batch_rows}"
)
ensure_schema(table, schema_name)
if table.num_rows == 0:
continue
if set(table.column("venue").to_pylist()) != {venue}:
raise StorageError("live capture batch contains an unexpected venue")
if set(table.column("symbol").to_pylist()) != {symbol}:
raise StorageError("live capture batch contains an unexpected symbol")
bounds = pc.min_max(table.column(time_column)).as_py()
if bounds is None or bounds["min"] is None or bounds["max"] is None:
raise StorageError("cannot write a live capture batch without timestamps")
batch_start = int(bounds["min"])
batch_end = int(bounds["max"])
observed_start_ns = (
batch_start if observed_start_ns is None else min(observed_start_ns, batch_start)
)
observed_end_ns = (
batch_end if observed_end_ns is None else max(observed_end_ns, batch_end)
)
writer.write_table(table, row_group_size=max_input_batch_rows)
rows += table.num_rows
writer.close()
writer = None
if rows == 0:
temporary.unlink()
else:
with temporary.open("rb") as handle:
os.fsync(handle.fileno())
checksum = sha256_file(temporary)
destination = partition / f"capture-{checksum[:20]}.parquet"
if destination.exists():
if sha256_file(destination) != checksum:
raise StorageError(f"content-address collision at {destination}")
temporary.unlink()
else:
os.replace(temporary, destination)
except BaseException:
if writer is not None:
with suppress(BaseException):
writer.close()
temporary.unlink(missing_ok=True)
raise
download_time = downloaded_at_utc or utc_now_iso()
data_path = destination
data_sha256 = checksum
artifact_entry: dict[str, Any] | None = None
if data_path is not None and data_sha256 is not None:
artifact_payload: dict[str, Any] = {
"manifest_version": MANIFEST_VERSION,
"artifact_kind": "normalized_live_capture_parquet",
"dataset": dataset,
"schema_name": schema_name,
"schema_version": SCHEMA_VERSION,
"venue": venue,
"symbol": symbol,
"capture_id": capture_id,
"source": source,
"source_uri": source_uri,
"downloaded_at_utc": download_time,
"requested_range_ns": {
"start": requested_start_ns,
"end_exclusive": requested_end_ns,
},
"observed_range_ns": {
"start": observed_start_ns,
"end_inclusive": observed_end_ns,
},
"source_checksum_sha256": source_checksum_sha256,
"checksum": {"algorithm": "sha256", "value": data_sha256},
"rows": rows,
"bytes": data_path.stat().st_size,
"path": str(data_path.relative_to(destination_root)),
"transformations": [
"normalized field names and types",
"UTC epoch-nanosecond timestamp conversion",
"exact integer tick/lot conversion where scale supplied",
],
}
artifact_manifest_path, artifact_manifest_sha = _immutable_json(
partition,
f"capture-{data_sha256[:20]}.manifest",
artifact_payload,
)
artifact_entry = {
"data_path": str(data_path.relative_to(destination_root)),
"manifest_path": str(artifact_manifest_path.relative_to(destination_root)),
"data_sha256": data_sha256,
"manifest_sha256": artifact_manifest_sha,
"rows": rows,
"write_ordinal": 0,
"observed_range_ns": artifact_payload["observed_range_ns"],
}
dataset_payload: dict[str, Any] = {
"manifest_version": MANIFEST_VERSION,
"dataset": dataset,
"schema_version": SCHEMA_VERSION,
"source": source,
"source_uri": source_uri,
"downloaded_at_utc": download_time,
"requested_range_ns": {
"start": requested_start_ns,
"end_exclusive": requested_end_ns,
},
"partitioning": {"kind": "capture_id", "value": capture_id},
"artifacts": [artifact_entry] if artifact_entry is not None else [],
"rows": rows,
}
manifest_directory = destination_root / "_manifests"
manifest_directory.mkdir(parents=True, exist_ok=True)
manifest_path, manifest_sha = _immutable_json(
manifest_directory,
f"{safe_dataset}.capture-{safe_capture_id}.manifest",
dataset_payload,
)
return CaptureDatasetWriteResult(
dataset=dataset,
schema_version=SCHEMA_VERSION,
rows=rows,
data_path=data_path,
data_sha256=data_sha256,
manifest_path=manifest_path,
manifest_sha256=manifest_sha,
)
def parquet_paths(result: DatasetWriteResult) -> Sequence[Path]:
"""Return concrete parts in manifest order for Polars/DuckDB consumers."""
return tuple(item.data_path for item in result.artifacts)
|