T-113: M1.2ac — tools/curate-etl: fourth runnable command curate-fx-1d (concatenated-BSON eodPrices CAD.FOREX + teMarketData USDCAD files → fx_1d/part.parquet) with a zero-copy teMarketData decoder, end-to-end tests and local benchmark (no new crates, no MongoDB connection)

This commit is contained in:
Studio 2026-11-29 03:15:14 -05:00
parent da730a5460
commit 823143905a
2 changed files with 323 additions and 3 deletions

View file

@ -1299,13 +1299,13 @@ Fixed-seed (`std`-only SplitMix64) end-to-end file→Parquet benchmark synthesiz
cargo run -p curate-etl --release --offline -- bench-curate-fx-cmd [rows]
```
Measured locally (`2026-11-29`, default `20,000` rows; two back-to-back runs with identical checksum `0x45ac34f4ddf4407c`):
Measured locally (`2026-11-29`, post-fix `curate-fx-1d` with full-document corruption scanning, default `20,000` rows; two back-to-back runs with identical checksum `0x45ac34f4ddf4407c`):
- Input: `rows=20000`, `eod_docs=18936` (`2,706,485 B`), `te_docs=3`, `te_points=5000` (`443,498 B`)
- Decode counts: `eod_kept=14918`, `eod_other=3936`, `eod_rejected=82`, `te_matched=1`, `te_other=2`, `te_decoded=4925`, `te_rejected=75`
- Output: `rows_out=17524` (`eod_out=14569`, `te_out=2955`, `te_superseded=1970`), `files=1`, `row_groups=1`, `bytes=733752`, `read_back_rows=17524`, `overlap: checked=1970 within_tol=1870 mismatched=100 max_rel=0.025000`
- Run 1: `decode=4.53 ms` (`4,413,780 rows/s`, `662.96 MB/s`), `curate=0.62 ms` (`31,847,808 rows/s`), `write=15.31 ms` (`1,144,502 rows/s`, `45.70 MB/s`), `total=20.47 ms` (`977,241 rows/s`), `checksum=0x45ac34f4ddf4407c`
- Run 2: `decode=4.73 ms` (`4,226,871 rows/s`, `634.89 MB/s`), `curate=0.76 ms` (`26,093,863 rows/s`), `write=15.22 ms` (`1,151,389 rows/s`, `45.98 MB/s`), `total=20.71 ms` (`965,626 rows/s`), `checksum=0x45ac34f4ddf4407c`
- Run 1: `decode=4.33 ms` (`4,614,470 rows/s`, `693.11 MB/s`), `curate=0.64 ms` (`30,806,515 rows/s`), `write=24.99 ms` (`701,274 rows/s`, `28.00 MB/s`), `total=29.97 ms` (`667,398 rows/s`), `checksum=0x45ac34f4ddf4407c`
- Run 2: `decode=4.26 ms` (`4,694,381 rows/s`, `705.11 MB/s`), `curate=0.67 ms` (`29,800,156 rows/s`), `write=15.27 ms` (`1,147,886 rows/s`, `45.84 MB/s`), `total=20.19 ms` (`990,462 rows/s`), `checksum=0x45ac34f4ddf4407c`
### Known Limits

View file

@ -664,3 +664,323 @@ fn failure_modes_return_typed_errors_and_preserve_existing_parquet() {
);
check_sentinel();
}
#[test]
fn single_source_runs_succeed_and_rerun_fully_replaces_previous_output() {
let tmp = TestTempDir::new("fx-single-source");
let out_dir = tmp.path.join("out");
let empty_file = tmp.path.join("empty.bson");
fs::write(&empty_file, []).unwrap();
// Run 1: EOD-only (TE file is zero bytes).
let eod_path = tmp.path.join("eod.bson");
let mut eod_bytes = Vec::new();
eod_bytes.extend_from_slice(&eod_doc(
"CAD.FOREX",
"2025-01-02",
1.430,
1.440,
1.425,
1.435,
0.0,
));
eod_bytes.extend_from_slice(&eod_doc(
"CAD.FOREX",
"2025-01-03",
1.435,
1.445,
1.430,
1.440,
0.0,
));
fs::write(&eod_path, &eod_bytes).unwrap();
let report1 = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: eod_path,
te_market_data: empty_file.clone(),
te_symbol: "USDCAD".to_string(),
out_dir: out_dir.clone(),
})
.expect("EOD-only run should succeed");
assert_report_invariants(&report1);
let parquet_path = out_dir.join("fx_1d").join("part.parquet");
let rows1 = read_fx_parquet(&parquet_path);
assert_eq!(rows1.len(), 2);
assert!(rows1
.iter()
.all(|r| r.source == FxSource::EodCadForex.as_str()));
// Run 2: TE-only (EOD file is zero bytes) into the same out_dir must fully replace output.
let te_path = tmp.path.join("te.bson");
let mut daily = RawArrayBuf::new();
daily.push(te_point_secs(day(2025, 2, 10), 1.420, 1.430, 1.415, 1.425));
let mut te_doc = RawDocumentBuf::new();
te_doc.append(cstr!("teSymbol"), "USDCAD");
te_doc.append(cstr!("daily"), daily);
fs::write(&te_path, te_doc.as_bytes()).unwrap();
let report2 = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: empty_file,
te_market_data: te_path,
te_symbol: "USDCAD".to_string(),
out_dir,
})
.expect("TE-only rerun should succeed");
assert_report_invariants(&report2);
let rows2 = read_fx_parquet(&parquet_path);
assert_eq!(rows2.len(), 1);
assert_eq!(rows2[0].session_date, day(2025, 2, 10));
assert_eq!(rows2[0].source, FxSource::TeUsdCad.as_str());
}
#[test]
fn truncated_dump_after_valid_documents_fails_and_preserves_existing_parquet() {
let tmp = TestTempDir::new("fx-trunc-tail");
let out_dir = tmp.path.join("out");
let fx_dir = out_dir.join("fx_1d");
fs::create_dir_all(&fx_dir).unwrap();
let parquet_path = fx_dir.join("part.parquet");
let sentinel = b"SENTINEL_PARQUET_TAIL_TRUNC";
fs::write(&parquet_path, sentinel).unwrap();
let valid_eod = tmp.path.join("valid_eod.bson");
fs::write(
&valid_eod,
eod_doc("CAD.FOREX", "2025-01-02", 1.43, 1.44, 1.42, 1.435, 0.0),
)
.unwrap();
let valid_te = tmp.path.join("valid_te.bson");
let mut daily = RawArrayBuf::new();
daily.push(te_point_secs(day(2025, 1, 3), 1.43, 1.44, 1.42, 1.435));
let mut te_doc = RawDocumentBuf::new();
te_doc.append(cstr!("teSymbol"), "USDCAD");
te_doc.append(cstr!("daily"), daily);
fs::write(&valid_te, te_doc.as_bytes()).unwrap();
// 1. EOD dump truncated mid-document after two valid documents.
let trunc_eod = tmp.path.join("trunc_after_valid_eod.bson");
let mut eod_bytes = Vec::new();
eod_bytes.extend_from_slice(&eod_doc(
"CAD.FOREX",
"2025-01-02",
1.43,
1.44,
1.42,
1.435,
0.0,
));
eod_bytes.extend_from_slice(&eod_doc(
"CAD.FOREX",
"2025-01-03",
1.435,
1.445,
1.43,
1.44,
0.0,
));
let third = eod_doc("CAD.FOREX", "2025-01-06", 1.44, 1.45, 1.435, 1.445, 0.0);
eod_bytes.extend_from_slice(&third[..third.len() / 2]);
fs::write(&trunc_eod, &eod_bytes).unwrap();
let err = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: trunc_eod,
te_market_data: valid_te.clone(),
te_symbol: "USDCAD".to_string(),
out_dir: out_dir.clone(),
})
.unwrap_err();
assert!(
matches!(err, RunCurateFxError::BsonStream { .. }),
"expected BsonStream for truncated EOD tail, got {err:?}"
);
assert_eq!(fs::read(&parquet_path).unwrap(), sentinel);
// 2. TE dump truncated mid-document after a valid document.
let trunc_te = tmp.path.join("trunc_after_valid_te.bson");
let mut te_bytes = te_doc.as_bytes().to_vec();
let half = te_doc.as_bytes().len() / 2;
te_bytes.extend_from_slice(&te_doc.as_bytes()[..half]);
fs::write(&trunc_te, &te_bytes).unwrap();
let err = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: valid_eod,
te_market_data: trunc_te,
te_symbol: "USDCAD".to_string(),
out_dir,
})
.unwrap_err();
assert!(
matches!(err, RunCurateFxError::BsonStream { .. }),
"expected BsonStream for truncated TE tail, got {err:?}"
);
assert_eq!(fs::read(&parquet_path).unwrap(), sentinel);
}
#[test]
fn corrupt_bytes_in_non_usdcad_te_document_abort_run_with_exact_offset() {
let tmp = TestTempDir::new("fx-corrupt-other-te");
let out_dir = tmp.path.join("out");
let fx_dir = out_dir.join("fx_1d");
fs::create_dir_all(&fx_dir).unwrap();
let parquet_path = fx_dir.join("part.parquet");
let sentinel = b"SENTINEL_PARQUET_OTHER_TE";
fs::write(&parquet_path, sentinel).unwrap();
let empty_eod = tmp.path.join("empty_eod.bson");
fs::write(&empty_eod, []).unwrap();
// First doc: valid USDCAD.
let mut daily1 = RawArrayBuf::new();
daily1.push(te_point_secs(day(2025, 1, 3), 1.43, 1.44, 1.42, 1.435));
let mut doc1 = RawDocumentBuf::new();
doc1.append(cstr!("teSymbol"), "USDCAD");
doc1.append(cstr!("daily"), daily1);
let doc1_len = doc1.as_bytes().len();
// Second doc: non-USDCAD (EURUSD) with a corrupted element tag after teSymbol.
let mut doc2 = RawDocumentBuf::new();
doc2.append(cstr!("teSymbol"), "EURUSD");
doc2.append(cstr!("extra"), 42_i32);
let mut doc2_bytes = doc2.as_bytes().to_vec();
let extra_pos = doc2_bytes
.windows(7)
.position(|w| w == b"\x10extra\0")
.expect("find int32 element 'extra'");
doc2_bytes[extra_pos] = 0xFF;
let mut te_bytes = doc1.as_bytes().to_vec();
te_bytes.extend_from_slice(&doc2_bytes);
let te_path = tmp.path.join("te.bson");
fs::write(&te_path, &te_bytes).unwrap();
let err = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: empty_eod,
te_market_data: te_path,
te_symbol: "USDCAD".to_string(),
out_dir,
})
.unwrap_err();
match err {
RunCurateFxError::CorruptTeBson {
source: BsonTeMarketDataError::CorruptBson { offset, .. },
..
} => assert_eq!(offset, doc1_len),
other => panic!("expected CorruptTeBson at offset {doc1_len}, got {other:?}"),
}
assert_eq!(fs::read(&parquet_path).unwrap(), sentinel);
}
#[test]
fn te_data_quality_doc_rejects_are_counted_and_run_continues() {
let tmp = TestTempDir::new("fx-te-doc-rejects");
let out_dir = tmp.path.join("out");
let empty_eod = tmp.path.join("empty_eod.bson");
fs::write(&empty_eod, []).unwrap();
let mut te_bytes = Vec::new();
// 1. Missing `daily` on matched symbol.
let mut missing_daily = RawDocumentBuf::new();
missing_daily.append(cstr!("teSymbol"), "USDCAD");
te_bytes.extend_from_slice(missing_daily.as_bytes());
// 2. Non-array `daily` on matched symbol.
let mut bad_daily = RawDocumentBuf::new();
bad_daily.append(cstr!("teSymbol"), "USDCAD");
bad_daily.append(cstr!("daily"), "not-an-array");
te_bytes.extend_from_slice(bad_daily.as_bytes());
// 3. Blank `teSymbol`.
let mut blank_sym = RawDocumentBuf::new();
blank_sym.append(cstr!("teSymbol"), " ");
blank_sym.append(cstr!("daily"), RawArrayBuf::new());
te_bytes.extend_from_slice(blank_sym.as_bytes());
// 4. Valid USDCAD document.
let mut valid_daily = RawArrayBuf::new();
valid_daily.push(te_point_secs(day(2025, 1, 6), 1.43, 1.44, 1.42, 1.435));
let mut valid_doc = RawDocumentBuf::new();
valid_doc.append(cstr!("teSymbol"), "USDCAD");
valid_doc.append(cstr!("daily"), valid_daily);
te_bytes.extend_from_slice(valid_doc.as_bytes());
let te_path = tmp.path.join("te.bson");
fs::write(&te_path, &te_bytes).unwrap();
let report = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: empty_eod,
te_market_data: te_path,
te_symbol: "USDCAD".to_string(),
out_dir,
})
.expect("data-quality rejects should be counted without aborting");
assert_report_invariants(&report);
assert_eq!(report.te_decode.docs_in, 4);
assert_eq!(report.te_decode.docs_matched, 1);
assert_eq!(report.te_decode.rejected_daily_missing, 1);
assert_eq!(report.te_decode.rejected_daily_not_array, 1);
assert_eq!(report.te_decode.rejected_empty_symbol, 1);
assert_eq!(report.write_stats.rows_written, 1);
}
#[test]
fn te_utc_day_edge_timestamps_never_have_known_at_before_raw_t() {
let tmp = TestTempDir::new("fx-no-lookahead");
let out_dir = tmp.path.join("out");
let empty_eod = tmp.path.join("empty_eod.bson");
fs::write(&empty_eod, []).unwrap();
// (raw_t, expected_session_date, raw_t_micros) sorted by expected_session_date across
// pre-1970 and post-1970 UTC day edges in both seconds and milliseconds.
let cases: &[(i64, i32, i64)] = &[
(-100_000_000_001, -1158, -100_000_000_001_000), // 1966-10-30 14:13:19.999 UTC (ms)
(-86_401, -2, -86_401_000_000), // 1969-12-30 23:59:59 UTC (s)
(-1, -1, -1_000_000), // 1969-12-31 23:59:59 UTC (s)
(0, 0, 0), // 1970-01-01 00:00:00 UTC (s)
(86_400, 1, 86_400_000_000), // 1970-01-02 00:00:00 UTC (s)
(1_735_689_599_999, day(2024, 12, 31), 1_735_689_599_999_000), // 2024-12-31 23:59:59.999 UTC (ms)
(1_735_689_600_000, day(2025, 1, 1), 1_735_689_600_000_000), // 2025-01-01 00:00:00.000 UTC (ms)
];
let mut daily = RawArrayBuf::new();
for &(raw_t, _, _) in cases {
let mut pt = RawDocumentBuf::new();
pt.append(cstr!("t"), raw_t);
pt.append(cstr!("o"), 1.35_f64);
pt.append(cstr!("h"), 1.36_f64);
pt.append(cstr!("l"), 1.34_f64);
pt.append(cstr!("c"), 1.355_f64);
daily.push(pt);
}
let mut te_doc = RawDocumentBuf::new();
te_doc.append(cstr!("teSymbol"), "USDCAD");
te_doc.append(cstr!("daily"), daily);
let te_path = tmp.path.join("te.bson");
fs::write(&te_path, te_doc.as_bytes()).unwrap();
let report = run_curate_fx_1d(&CurateFx1dArgs {
eod_prices: empty_eod,
te_market_data: te_path,
te_symbol: "USDCAD".to_string(),
out_dir: out_dir.clone(),
})
.expect("day-edge run should succeed");
assert_report_invariants(&report);
let rows = read_fx_parquet(&out_dir.join("fx_1d").join("part.parquet"));
assert_eq!(rows.len(), cases.len());
for (row, &(_, expected_day, raw_t_micros)) in rows.iter().zip(cases.iter()) {
assert_eq!(row.session_date, expected_day);
assert_eq!(
row.known_at_micros,
fx_1d_utc_day_end_micros(expected_day).expect("valid day-end micros")
);
assert!(
row.known_at_micros >= raw_t_micros,
"look-ahead violation: session_date={} known_at={} < raw_t={}",
row.session_date,
row.known_at_micros,
raw_t_micros
);
}
}