T-93: M1.2k — tools/curate-etl: Zstd Parquet writer for events/year=YYYY/part.parquet (contract schema, atomic year-partitioned publish) with round-trip tests and local benchmark (no new crates)

This commit is contained in:
Studio 2026-11-28 14:23:18 -05:00
parent f92b267e71
commit 4e344bcc2e
9 changed files with 3016 additions and 9 deletions

View file

@ -1,6 +1,6 @@
# `curate-etl`
Dedicated Rust crate (single direct dependency: `parquet` with `default-features = false, features = ["zstd"]`) for the `M1.2a` split-factor lookup table / bar un-adjustment core, the `M1.2b` pure `adj_factors` builder, the `M1.2c` pure `corporate_actions` event-log builder, the `M1.2d` pure `bars_1d` daily bar builder + `qmPrices.sharevolume` cross-check, the `M1.2e` pure `fx_1d` USD/CAD builder + carry-forward lookup, the `M1.2f` pure `shorts` (`ceoShorts`) point-in-time builder + Canadian TSX business-day / `America/Toronto` DST calendar, the `M1.2g` pure `events` (`qmFilings` + `qmEarnings`) point-in-time builder, the `M1.2h` pure in-RAM `provider_mapping` `(source, source_id, bar_date) -> listing_id` lookup table, the `M1.2i` streaming Zstd Parquet writer for `bars_1d`, and the `M1.2j` streaming Zstd Parquet writer for `shorts`.
Dedicated Rust crate (single direct dependency: `parquet` with `default-features = false, features = ["zstd"]`) for the `M1.2a` split-factor lookup table / bar un-adjustment core, the `M1.2b` pure `adj_factors` builder, the `M1.2c` pure `corporate_actions` event-log builder, the `M1.2d` pure `bars_1d` daily bar builder + `qmPrices.sharevolume` cross-check, the `M1.2e` pure `fx_1d` USD/CAD builder + carry-forward lookup, the `M1.2f` pure `shorts` (`ceoShorts`) point-in-time builder + Canadian TSX business-day / `America/Toronto` DST calendar, the `M1.2g` pure `events` (`qmFilings` + `qmEarnings`) point-in-time builder, the `M1.2h` pure in-RAM `provider_mapping` `(source, source_id, bar_date) -> listing_id` lookup table, the `M1.2i` streaming Zstd Parquet writer for `bars_1d`, the `M1.2j` streaming Zstd Parquet writer for `shorts`, and the `M1.2k` streaming Zstd Parquet writer for `events`.
## Scope & Sign Conventions
@ -254,6 +254,49 @@ Rules:
- Output: `files=1`, `row_groups=1`, `bytes=6741816`, `read_back_rows=1000000`
- Perf: `elapsed=106.79 ms`, `throughput=9364116 rows/s`, `write=60.21 MB/s`, `size=6.74 bytes/row`, `compression=7.42x`
## `events` Parquet Writer (`M1.2k`)
`write_events_dataset` (`src/parquet_events.rs`) streams strictly ascending `EventsParquetRow` records (with `From<&EventRow<i32>>` / `From<EventRow<i32>>` conversions from `build_events`) into Zstd-compressed, UTC-year-partitioned Parquet files matching `design/DATA-CONTRACTS.md` (§3A).
1. **Exact 7-column contract schema (`events_parquet_schema`):**
- `listing_id` (`INT32`, `REQUIRED`)
- `ts` (`INT64`, `REQUIRED`, `TIMESTAMP(MICROS, isAdjustedToUTC=true)`)
- `known_at` (`INT64`, `REQUIRED`, `TIMESTAMP(MICROS, isAdjustedToUTC=true)`)
- `known_at_source` (`BYTE_ARRAY`, `REQUIRED`, `STRING`, dictionary-encoded)
- `kind` (`BYTE_ARRAY`, `REQUIRED`, `STRING`, dictionary-encoded)
- `subtype` (`BYTE_ARRAY`, `REQUIRED`, `STRING`)
- `payload_json` (`BYTE_ARRAY`, `REQUIRED`, `STRING`, dictionary disabled)
2. **Partition layout, compression & streaming:**
- Writes `<out_dir>/events/year=YYYY/part.parquet` lazily per observed UTC year `partition_year_utc(ts_micros)` (`1900..=2200`).
- Empty input returns `Ok(EventsWriteStats::default())` without creating `<out_dir>/events` and without modifying or removing any pre-existing partitions.
- Zstd compression on every column chunk (`DEFAULT_ZSTD_LEVEL = 3`, configurable via `EventsWriteOptions::zstd_level`).
- Bounded streaming buffers (`DEFAULT_ROW_GROUP_ROWS = 1_000_000`, configurable via `EventsWriteOptions::row_group_rows`): buffers at most one row group per active year; low-cardinality `known_at_source`/`kind` use string interning while high-cardinality `subtype`/`payload_json` write directly as batched plain byte arrays without unbounded interning.
- Deterministic `created_by` metadata so identical inputs produce byte-identical `part.parquet` files.
3. **Validation, concurrency & atomic publish rules:**
- Input must be strictly ascending by the `build_events` sort key `(listing_id, ts_micros, kind, subtype, payload_json, known_at_micros, known_at_source)`; out-of-order and exact-duplicate rows are rejected with the offending `row_index`.
- `known_at_micros >= ts_micros` is enforced (`KnownAtBeforeTs` rejects look-ahead `known_at < ts`); partition year must fall in `MIN_PARQUET_YEAR..=MAX_PARQUET_YEAR`; `known_at_source`, `kind`, `subtype`, and `payload_json` must be non-empty after trimming.
- Reuses the shared `parquet_publish` locking, exclusive per-run staging (`part.parquet.<run>.tmp`), `File::sync_all()`, live-preserving backup (`part.parquet.<run>.bak`), atomic `rename`, and multi-partition rollback (`rollback_published` / `RollbackFailed`) path so partial or failing runs never leave corrupt partitions or transient files behind.
4. **Typed errors (`ParquetEventsError`):**
- `InvalidRowGroupRows` (`row_group_rows == 0`)
- `InvalidZstdLevel { level }`
- `UnsortedInput { row_index, prev_listing_id, prev_ts_micros, listing_id, ts_micros }`
- `DuplicateRow { row_index, listing_id, ts_micros }`
- `KnownAtBeforeTs { row_index, listing_id, ts_micros, known_at_micros }`
- `PartitionYearOutOfRange { row_index, listing_id, ts_micros, year, min_year, max_year }`
- `EmptyKnownAtSource { row_index, listing_id, ts_micros }`
- `EmptyKind { row_index, listing_id, ts_micros }`
- `EmptySubtype { row_index, listing_id, ts_micros }`
- `EmptyPayloadJson { row_index, listing_id, ts_micros }`
- `LockTimeout { path, waited }`
- `RollbackFailed { cause, failures }`
- `Io { path, source }`
- `Parquet { path, source }`
5. **Measured baseline (`2026-11-28`):**
- Command: `cargo run --release -p curate-etl --offline -- bench-parquet-events`
- Input: `rows=500000`, `listings=5000`, `avg_raw_row_bytes=243.6` (11 UTC years `2015..=2025`, ~98.5% filings / ~1.5% linked & lagged earnings)
- Output: `files=11`, `row_groups=11`, `bytes=13348791`, `read_back_rows=500000`
- Perf: `elapsed=549.54 ms`, `throughput=909847 rows/s`, `write=23.17 MB/s`, `size=26.70 bytes/row`, `compression=9.13x`
## Local Benchmarks
### `bench-split`
@ -396,13 +439,27 @@ Command (`2026-11-28`): `cargo run --release -p curate-etl --offline -- bench-pa
- Output: `files=1`, `row_groups=1`, `bytes=6741816`, `read_back_rows=1000000`
- Perf: `elapsed=106.79 ms`, `throughput=9364116 rows/s`, `write=60.21 MB/s`, `size=6.74 bytes/row`, `compression=7.42x`
### `bench-parquet-events`
Fixed-seed (`std`-only SplitMix64) streaming Zstd Parquet write benchmark over `500,000` sorted `EventsParquetRow` rows (~98.5% filings, ~1.5% linked/lagged earnings with ~150–300 byte `payload_json`) across `5,000` listings and 11 UTC years (`2015..=2025`), writing `events/year=YYYY/part.parquet` into a fresh temp directory under `std::env::temp_dir()`, verifying Parquet footer row counts across all 11 files, and deleting the temp directory:
```bash
cargo run --release -p curate-etl --offline -- bench-parquet-events [rows]
```
Command (`2026-11-28`): `cargo run --release -p curate-etl --offline -- bench-parquet-events`
- Input: `rows=500000`, `listings=5000`, `avg_raw_row_bytes=243.6`
- Output: `files=11`, `row_groups=11`, `bytes=13348791`, `read_back_rows=500000`
- Perf: `elapsed=549.54 ms`, `throughput=909847 rows/s`, `write=23.17 MB/s`, `size=26.70 bytes/row`, `compression=9.13x`
## Deferred Follow-Ups (`M1.3+` / `M2`)
This crate currently depends only on `parquet` (Zstd-only, no Arrow/async) so all tests and benchmarks run strictly offline once the Cargo cache is populated. Follow-up tasks will add:
- `bars_1d` and `fx_1d` schema metadata columns (`ts`, `known_at`, `currency`, `source`, `vwap`) requiring exchange session/timezone and listing metadata
- `CADUSD.FOREX` inversion, `fx_1m` intraday FX, and wiring `fx_1d` into `src/adj.rs` cross-currency dividends
- Zstd Parquet writers for the remaining curated tables (`adj_factors`, `corporate_actions`, `fx_1d`, `events`) and `qmPrices` OHLC cross-check
- Zstd Parquet writers for the remaining curated tables (`adj_factors`, `corporate_actions`, `fx_1d`) and `qmPrices` OHLC cross-check
- Read-only MongoDB `RawDocumentBuf` streaming cursors (`eodPrices`, `eodIntraday1M`, `qmSplits`)
- PostgreSQL I/O preload into `ProviderMappingTable` and rewiring the builders to resolve `(source, source_id, bar_date) -> listing_id: i32`
- `rayon` parallel sharding across symbol/date partitions