T-118: M1.2ah — tools/curate-etl: wire the qmPrices.sharevolume cross-check into curate-bars-1d (zero-copy qmPrices decoder + optional --qm-prices file, re-keyed via provider_mapping) with end-to-end tests and local benchmark (no new crates, no MongoDB connection)

This commit is contained in:
Studio 2026-11-29 06:58:28 -05:00
parent 87fad608b0
commit 3279c50ae3
3 changed files with 82 additions and 23 deletions

View file

@ -1034,9 +1034,10 @@ Both `--flag value` and `--flag=value` forms are accepted (in the separated `--f
### `qmPrices` Raw BSON Decoder (`src/bson_qm_prices.rs`)
- **Empirical field contract** (`prices.action.ts:97-109`, `bar-spans.ts:19-24`, `sample-closes.ts:300-303`, `design/DATA-CONTRACTS.md` §1):
- **Documented field names & raw-volume semantics** (`prices.action.ts:97-109`, `bar-spans.ts:19-24`, `sample-closes.ts:300-303`, `design/DATA-CONTRACTS.md` §1): `qmPrices` stores QuoteMedia daily bars keyed by `qmSearchCode` with provider date field `date` (and a separate BSON `Date` in `dateTime`) and unadjusted share volume in `sharevolume`.
- **Accepted BSON types (live `qmPrices` date format and numeric type distribution remain unverified against QA MongoDB):**
- `qmSearchCode`: BSON UTF-8 `String` borrowed verbatim (never trimmed; empty/whitespace-only rejected as `EmptyField`).
- `date`: strict 10-byte ASCII `YYYY-MM-DD` BSON `String` validated via `parse_ymd_strict_days` (BSON `Date` lives separately in `dateTime`; non-string `date` rejected as `WrongType`, bad format/calendar strings as `MalformedDate`).
- `date`: strict 10-byte ASCII `YYYY-MM-DD` BSON `String` validated via `parse_ymd_strict_days` (non-string `date` — including BSON `DateTime` — is rejected as `WrongType` and counted in `qm_bson.rejected`; bad format/calendar strings are rejected as `MalformedDate`).
- `sharevolume`: BSON `Double`, `Int32`, or `Int64` converted to `f64`. Numeric domain validity (negative / non-finite) is passed through to `build_bars_1d` (`qm_invalid_volume_dropped`); missing, `Null`, or non-numeric types are rejected with counted reasons.
- All extra fields (`_id`, `dateTime`, `unadjustedopen..unadjustedclose`, nested docs/arrays) are skipped without calling `elem.value()`.
- **Accounting invariant:** `docs_in == decoded + rejected_total()` (`rejected_missing_field + rejected_empty_field + rejected_wrong_type + rejected_malformed_date + rejected_corrupt_document`).
@ -1082,19 +1083,19 @@ cargo run -p curate-etl --release --offline -- bench-curate-bars-cmd [rows]
Measured locally (`2026-11-29`):
1. **1,000,000 rows** (`cargo run -p curate-etl --release --offline -- bench-curate-bars-cmd 1000000`):
- Input: `rows=1000000`, `eod_bytes=161690000` (154.2 MiB), `splits_docs=2040`, `splits_bytes=188080`, `qm_docs=222624`, `qm_bytes=20374624` (19.4 MiB), `keys=2000`
- Decode (all inputs): `elapsed=234.52 ms`, `throughput=4264112 rows/s`, `scan=741.14 MB/s`, `unmapped_dropped=20000`, `buffered_bars=980000`, `buffered_bytes=67161784` (`68.5 bytes/bar`)
- QM decode + rekey: `elapsed=31.49 ms`, `throughput=7070243 docs/s`, `scan=617.10 MB/s`, `attached=220124`, `unmapped=1000`, `no_eod_partner=1000`, `rejected=500`, `checked=220124`, `within_tol=217922`, `mismatched=2202 (injected=2202)`, `unchecked=759836`, `samples=100`
- Curate: `elapsed=209.14 ms`, `throughput=4685933 rows/s`, `rows_out=938023` (`93.80%` retained), `listings_out=1941`, `unmapped=595`, `missing_curr=9500`, `ts_oor=1842`, `coll_collapsed=10000`, `coll_dropped=20000`
- Write: `elapsed=462.20 ms`, `throughput=2029479 rows/s`, `write=53.03 MB/s`, `files=36`, `row_groups=36`, `bytes=25699104`, `read_back_rows=938023`
- Total: `elapsed=905.85 ms`, `throughput=1103934 rows/s`, `throughput_mb=191.87 MB/s`, `splits_applied=1699`, `splits_coll_collapsed=40`, `splits_unmapped_qm=101`, `splits_no_eod_partner=100`, `splits_noop=100`
- Input: `rows=1000000`, `eod_bytes=161690000` (154.2 MiB), `splits_docs=2040`, `splits_bytes=188080`, `qm_docs=227439`, `qm_bytes=20815907` (19.9 MiB), `keys=2000`
- Decode (all inputs): `elapsed=234.63 ms`, `throughput=4262081 rows/s`, `scan=742.58 MB/s`, `unmapped_dropped=20000`, `buffered_bars=980000`, `buffered_bytes=67161784` (`68.5 bytes/bar`)
- QM decode + rekey: `elapsed=31.93 ms`, `throughput=7123258 docs/s`, `scan=621.74 MB/s`, `attached=224939`, `unmapped=1000`, `no_eod_partner=1000`, `rejected=500`, `checked=224939`, `within_tol=222689`, `mismatched=2250 (injected=2250)`, `unchecked=755021`, `samples=100`
- Curate: `elapsed=208.07 ms`, `throughput=4709887 rows/s`, `rows_out=938023` (`93.80%` retained), `listings_out=1941`, `unmapped=595`, `missing_curr=9500`, `ts_oor=1842`, `coll_collapsed=10000`, `coll_dropped=20000`
- Write: `elapsed=456.39 ms`, `throughput=2055293 rows/s`, `write=53.70 MB/s`, `files=36`, `row_groups=36`, `bytes=25699104`, `read_back_rows=938023`
- Total: `elapsed=899.09 ms`, `throughput=1112231 rows/s`, `throughput_mb=193.78 MB/s`, `splits_applied=1699`, `splits_coll_collapsed=40`, `splits_unmapped_qm=101`, `splits_no_eod_partner=100`, `splits_noop=100`
2. **5,000,000 rows** (`cargo run -p curate-etl --release --offline -- bench-curate-bars-cmd 5000000`):
- Input: `rows=5000000`, `eod_bytes=808449920` (771.0 MiB), `splits_docs=4080`, `splits_bytes=376160`, `qm_docs=1114364`, `qm_bytes=101988156` (97.3 MiB), `keys=6000`
- Decode (all inputs): `elapsed=1130.24 ms`, `throughput=4423839 rows/s`, `scan=768.53 MB/s`, `unmapped_dropped=99960`, `buffered_bars=4900040`, `buffered_bytes=537029672` (`109.6 bytes/bar`)
- QM decode + rekey: `elapsed=149.87 ms`, `throughput=7435584 docs/s`, `scan=648.99 MB/s`, `attached=1101864`, `unmapped=5000`, `no_eod_partner=5000`, `rejected=2500`, `checked=1101864`, `within_tol=1090845`, `mismatched=11019 (injected=11019)`, `unchecked=3797802`, `samples=100`
- Curate: `elapsed=1096.29 ms`, `throughput=4469640 rows/s`, `rows_out=4691865` (`93.84%` retained), `listings_out=5822`, `unmapped=2879`, `missing_curr=48328`, `ts_oor=9132`, `coll_collapsed=49206`, `coll_dropped=98256`
- Write: `elapsed=1248.49 ms`, `throughput=3758021 rows/s`, `write=79.10 MB/s`, `files=36`, `row_groups=36`, `bytes=103552480`, `read_back_rows=4691865`
- Total: `elapsed=3475.03 ms`, `throughput=1438838 rows/s`, `throughput_mb=249.96 MB/s`, `splits_applied=3399`, `splits_coll_collapsed=80`, `splits_unmapped_qm=201`, `splits_no_eod_partner=200`, `splits_noop=200`
- Input: `rows=5000000`, `eod_bytes=808449920` (771.0 MiB), `splits_docs=4080`, `splits_bytes=376160`, `qm_docs=1138361`, `qm_bytes=104186437` (99.4 MiB), `keys=6000`
- Decode (all inputs): `elapsed=1167.06 ms`, `throughput=4284272 rows/s`, `scan=746.08 MB/s`, `unmapped_dropped=99960`, `buffered_bars=4900040`, `buffered_bytes=537029672` (`109.6 bytes/bar`)
- QM decode + rekey: `elapsed=171.72 ms`, `throughput=6629277 docs/s`, `scan=578.63 MB/s`, `attached=1125861`, `unmapped=5000`, `no_eod_partner=5000`, `rejected=2500`, `checked=1125861`, `within_tol=1114602`, `mismatched=11259 (injected=11259)`, `unchecked=3773805`, `samples=100`
- Curate: `elapsed=1119.70 ms`, `throughput=4376207 rows/s`, `rows_out=4691865` (`93.84%` retained), `listings_out=5822`, `unmapped=2879`, `missing_curr=48328`, `ts_oor=9132`, `coll_collapsed=49206`, `coll_dropped=98256`
- Write: `elapsed=1238.26 ms`, `throughput=3789066 rows/s`, `write=79.75 MB/s`, `files=36`, `row_groups=36`, `bytes=103552480`, `read_back_rows=4691865`
- Total: `elapsed=3525.02 ms`, `throughput=1418430 rows/s`, `throughput_mb=247.01 MB/s`, `splits_applied=3399`, `splits_coll_collapsed=80`, `splits_unmapped_qm=201`, `splits_no_eod_partner=200`, `splits_noop=200`
### Known Limits

View file

@ -4975,6 +4975,8 @@ pub struct CurateBarsCmdBenchReport {
pub splits_decode: BsonQmActionDecodeStats,
pub qm_prices_decode: crate::bson_qm_prices::BsonQmPriceDecodeStats,
pub qm_rekey: crate::run_curate_bars::QmVolumeRekeyStats,
pub qm_matched_us: usize,
pub qm_matched_ca: usize,
pub injected_mismatches: usize,
pub mismatch_samples_out: usize,
pub curate_stats: crate::curate_bars::CurateBars1dStats,
@ -5313,6 +5315,8 @@ fn run_curate_bars_cmd_benchmark_tracked(
// 3. Stream-write `eodPrices.bson` and `qmPrices.bson` directly to disk.
let mut injected_mismatches = 0usize;
let mut qm_matched_emitted = 0usize;
let mut qm_matched_us = 0usize;
let mut qm_matched_ca = 0usize;
{
let mut eod_w = BufWriter::with_capacity(
512 * 1024,
@ -5441,14 +5445,21 @@ fn run_curate_bars_cmd_benchmark_tracked(
source,
})?;
// Emit a synthetic `qmPrices` partner for 25% of unique non-alias, non-gap mapped bars.
// Emit a synthetic `qmPrices` partner for 25% of unique non-alias, non-gap mapped bars
// using a deterministic selector independent of `k % 4` so both US (`k % 4 != 0`) and
// Canadian (`k % 4 == 0`) tickers are sampled across every pass.
let is_clean_unique_main_bar = k < mapped_keys
&& k % 50 != 0
&& k % 50 != 1
&& k % 50 != 2
&& roll >= 22;
if is_clean_unique_main_bar && i % 4 == 0 {
if is_clean_unique_main_bar && ((i / 4) ^ i ^ pass) % 4 == 0 {
qm_matched_emitted += 1;
if k % 4 == 0 {
qm_matched_ca += 1;
} else {
qm_matched_us += 1;
}
let factor = ref_splits.cum_factor_after(&k, epoch_day);
let raw_vol = crate::split::unadjust_volume(volume_i64 as f64, factor);
let mut qdoc = RawDocumentBuf::new();
@ -5466,9 +5477,9 @@ fn run_curate_bars_cmd_benchmark_tracked(
let rounded = raw_vol.round();
let safe_int = rounded >= 1.0
&& (rounded - raw_vol).abs() / raw_vol <= 0.005;
if i % 12 == 4 && safe_int && rounded <= i32::MAX as f64 {
if qm_matched_emitted % 3 == 1 && safe_int && rounded <= i32::MAX as f64 {
qdoc.append(cstr!("sharevolume"), rounded as i32);
} else if i % 12 == 8 && safe_int && rounded <= i64::MAX as f64 {
} else if qm_matched_emitted % 3 == 2 && safe_int && rounded <= i64::MAX as f64 {
qdoc.append(cstr!("sharevolume"), rounded as i64);
} else {
qdoc.append(cstr!("sharevolume"), raw_vol);
@ -5641,6 +5652,8 @@ fn run_curate_bars_cmd_benchmark_tracked(
splits_decode: timed.report.splits_decode,
qm_prices_decode: timed.report.qm_prices_decode,
qm_rekey: timed.report.qm_rekey,
qm_matched_us,
qm_matched_ca,
injected_mismatches,
mismatch_samples_out: timed.report.mismatch_samples.len(),
curate_stats: timed.report.curate_stats,
@ -11909,6 +11922,12 @@ mod tests {
+ report.qm_prices_decode.rejected_total()
);
assert!(report.qm_rekey.qm_attached > 0);
assert!(report.qm_matched_us > 0);
assert!(report.qm_matched_ca > 0);
assert_eq!(
report.qm_matched_us + report.qm_matched_ca,
report.qm_rekey.qm_attached
);
assert!(report.qm_rekey.qm_unmapped_qm > 0);
assert!(report.qm_rekey.qm_no_eod_partner > 0);
assert!(report.qm_prices_decode.rejected_wrong_type > 0);
@ -11944,6 +11963,8 @@ mod tests {
assert_eq!(report.rows, 0);
assert_eq!(report.eod_docs_framed, 0);
assert_eq!(report.qm_docs_framed, 0);
assert_eq!(report.qm_matched_us, 0);
assert_eq!(report.qm_matched_ca, 0);
assert_eq!(report.injected_mismatches, 0);
assert_eq!(report.curate_stats.builder.volume_mismatched, 0);
assert_eq!(report.mismatch_samples_out, 0);

View file

@ -542,7 +542,32 @@ fn curate_bars_1d_cli_qm_prices_cross_check_and_framing_failures() {
let pq_with_qm = fs::read(out_with_qm.join("bars_1d/year=2024/part.parquet")).unwrap();
assert_eq!(pq_no_qm, pq_with_qm);
// Equals syntax (`--qm-prices=<path>`) also succeeds.
// Equals syntax (`--qm-prices=<path>`) via the documented `--curate-inputs <dir>`
// directory + `current` symlink also succeeds, prints identical check counters/mismatches,
// and emits byte-identical Parquet.
let curate_inputs_dir = dir.path.join("curate_inputs");
let gen_dir = curate_inputs_dir.join("gen-001");
fs::create_dir_all(&gen_dir).unwrap();
write_file(
&gen_dir.join("provider_mapping.tsv"),
b"eod\tNVDA.US\t2020-01-01\t\t1\nqm\tNVDA:US\t2020-01-01\t\t1\n",
);
write_file(&gen_dir.join("listing_currencies.tsv"), b"1\tUSD\n");
#[cfg(unix)]
std::os::unix::fs::symlink(&gen_dir, curate_inputs_dir.join("current")).unwrap();
#[cfg(not(unix))]
{
fs::create_dir_all(curate_inputs_dir.join("current")).unwrap();
write_file(
&curate_inputs_dir.join("current/provider_mapping.tsv"),
b"eod\tNVDA.US\t2020-01-01\t\t1\nqm\tNVDA:US\t2020-01-01\t\t1\n",
);
write_file(
&curate_inputs_dir.join("current/listing_currencies.tsv"),
b"1\tUSD\n",
);
}
let eq_flag = format!("--qm-prices={}", qm_path.to_str().unwrap());
let run_eq = run(&[
"curate-bars-1d",
@ -551,14 +576,26 @@ fn curate_bars_1d_cli_qm_prices_cross_check_and_framing_failures() {
"--qm-splits",
splits_path.to_str().unwrap(),
&eq_flag,
"--provider-mapping",
map_path.to_str().unwrap(),
"--listing-currencies",
curr_path.to_str().unwrap(),
"--curate-inputs",
curate_inputs_dir.to_str().unwrap(),
"--out",
out_eq_qm.to_str().unwrap(),
]);
assert!(run_eq.status.success(), "{}", stderr(&run_eq));
assert!(stderr(&run_eq).is_empty(), "{}", stderr(&run_eq));
let eq_out_text = stdout(&run_eq);
assert!(eq_out_text.contains("qm_bson:"), "{eq_out_text}");
assert!(eq_out_text.contains("qm_rekey:"), "{eq_out_text}");
assert!(
eq_out_text.contains(
"volume_checked=2 volume_within_tolerance=1 volume_mismatched=1 volume_unchecked=0"
),
"{eq_out_text}"
);
assert!(
eq_out_text.contains("qm_mismatch: key=NVDA.US session_date=2024-06-10"),
"{eq_out_text}"
);
assert_eq!(
fs::read(out_eq_qm.join("bars_1d/year=2024/part.parquet")).unwrap(),
pq_no_qm