T-110: M1.2z — tools/curate-etl: first runnable command curate-bars-1d (concatenated-BSON eodPrices + qmSplits files + provider_mapping / listing-currency TSV → bars_1d/year=YYYY/part.parquet) with end-to-end tests and local benchmark (no new crates, no MongoDB connection)
This commit is contained in:
parent
54b7276e59
commit
f9fa7de8bb
2 changed files with 359 additions and 0 deletions
|
|
@ -1037,3 +1037,4 @@ Measured locally (`2026-11-29`):
|
|||
- **No `rayon` sharding or direct MongoDB cursors yet:** single-threaded file-to-file execution only; hash/key-sharded `rayon` streaming and direct MongoDB cursors are deferred to a follow-up.
|
||||
- **`qmPrices` volume cross-check not wired yet:** `qm_volumes` is passed as empty because `qmPrices` does not yet have its own raw BSON decoder.
|
||||
- **Ticker-reuse split keying caveat:** because `curate_bars_1d` takes `SplitTable<K>` keyed by `eodSearchCode` rather than `listing_id`, attaching a split to every `"eod"` `source_id` pointing at a `listing_id` means a later split on a reused `eodSearchCode` could affect an earlier listing that previously held that same `eodSearchCode`.
|
||||
- **Partition-level replacement on `--out` reuse:** `write_bars_1d_dataset` atomically replaces only the `bars_1d/year=YYYY/part.parquet` partitions emitted by the current run. Reusing an existing `--out` directory for a narrower year range leaves older year partitions intact on disk; use a fresh `--out` directory for a complete rebuild.
|
||||
|
|
|
|||
|
|
@ -909,3 +909,361 @@ fn deterministic_parquet_and_report_across_runs_and_shuffled_inputs_and_timed_pa
|
|||
assert_eq!(b1, bt, "run 1 vs timed parity mismatch for {rel}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rerun_with_narrower_year_range_replaces_emitted_year_and_leaves_older_year_untouched() {
|
||||
let dir = TestTempDir::new("narrower-rerun");
|
||||
let eod_full = dir.path().join("eod_full.bson");
|
||||
let eod_2024 = dir.path().join("eod_2024.bson");
|
||||
let splits_path = dir.path().join("splits.bson");
|
||||
let map_path = dir.path().join("provider_mapping.tsv");
|
||||
let curr_path = dir.path().join("listing_currencies.tsv");
|
||||
let out_dir = dir.path().join("out");
|
||||
|
||||
write_bytes(&map_path, b"eod\tAAPL.US\t2020-01-01\t\t1\n");
|
||||
write_bytes(&curr_path, b"1\tUSD\n");
|
||||
write_bytes(&splits_path, b"");
|
||||
|
||||
let mut full_bytes = Vec::new();
|
||||
full_bytes.extend_from_slice(&eod_doc("AAPL.US", "2023-12-29", 190.0, 192.0, 189.0, 191.0, 1_000.0));
|
||||
full_bytes.extend_from_slice(&eod_doc("AAPL.US", "2024-01-02", 185.0, 188.0, 184.0, 186.0, 2_000.0));
|
||||
write_bytes(&eod_full, &full_bytes);
|
||||
|
||||
let r1 = run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: eod_full,
|
||||
qm_splits: splits_path.clone(),
|
||||
provider_mapping: map_path.clone(),
|
||||
listing_currencies: curr_path.clone(),
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.expect("initial 2023+2024 run");
|
||||
assert_eq!(r1.write_stats.rows_by_year.get(&2023), Some(&1));
|
||||
assert_eq!(r1.write_stats.rows_by_year.get(&2024), Some(&1));
|
||||
|
||||
let p2023 = out_dir.join("bars_1d/year=2023/part.parquet");
|
||||
let p2024 = out_dir.join("bars_1d/year=2024/part.parquet");
|
||||
let bytes_2023_before = fs::read(&p2023).expect("read initial 2023");
|
||||
|
||||
// Rerun into the same --out with only 2024 data (updated close/volume).
|
||||
write_bytes(
|
||||
&eod_2024,
|
||||
&eod_doc("AAPL.US", "2024-01-02", 185.0, 189.0, 184.0, 188.5, 2_500.0),
|
||||
);
|
||||
let r2 = run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: eod_2024,
|
||||
qm_splits: splits_path,
|
||||
provider_mapping: map_path,
|
||||
listing_currencies: curr_path,
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.expect("narrower 2024-only rerun");
|
||||
assert_eq!(r2.write_stats.rows_by_year.len(), 1);
|
||||
assert_eq!(r2.write_stats.rows_by_year.get(&2024), Some(&1));
|
||||
|
||||
// 2024 partition is replaced; 2023 partition remains byte-identical on disk.
|
||||
let rows_2024 = read_parquet_bars(&p2024);
|
||||
assert_eq!(rows_2024.len(), 1);
|
||||
assert_eq!(rows_2024[0].close.to_bits(), 188.5_f64.to_bits());
|
||||
assert_eq!(rows_2024[0].volume.to_bits(), 2_500.0_f64.to_bits());
|
||||
assert_eq!(fs::read(&p2023).expect("read 2023 after rerun"), bytes_2023_before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_rerun_over_existing_dataset_leaves_published_partitions_byte_identical() {
|
||||
let dir = TestTempDir::new("failed-rerun");
|
||||
let valid_eod = dir.path().join("valid_eod.bson");
|
||||
let valid_splits = dir.path().join("valid_splits.bson");
|
||||
let valid_map = dir.path().join("valid_map.tsv");
|
||||
let valid_curr = dir.path().join("valid_curr.tsv");
|
||||
let out_dir = dir.path().join("out");
|
||||
|
||||
write_bytes(
|
||||
&valid_map,
|
||||
b"eod\tNVDA.US\t2010-01-01\t\t10\n\
|
||||
qm\tNVDA:US\t2010-01-01\t\t10\n\
|
||||
qm\tNVDA_ALT:US\t2010-01-01\t\t10\n",
|
||||
);
|
||||
write_bytes(&valid_curr, b"10\tUSD\n");
|
||||
write_bytes(&valid_splits, &split_doc("NVDA:US", "2024-06-10", 10.0));
|
||||
write_bytes(
|
||||
&valid_eod,
|
||||
&eod_doc("NVDA.US", "2024-06-07", 1197.0, 1216.0, 1180.0, 1208.0, 400_000_000.0),
|
||||
);
|
||||
|
||||
run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: valid_eod.clone(),
|
||||
qm_splits: valid_splits.clone(),
|
||||
provider_mapping: valid_map.clone(),
|
||||
listing_currencies: valid_curr.clone(),
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.expect("seed existing dataset");
|
||||
|
||||
let published = out_dir.join("bars_1d/year=2024/part.parquet");
|
||||
let snapshot_before = fs::read(&published).expect("read published parquet");
|
||||
|
||||
let assert_unchanged_and_clean = || {
|
||||
assert_eq!(fs::read(&published).expect("re-read published"), snapshot_before);
|
||||
let year_dir = out_dir.join("bars_1d/year=2024");
|
||||
let entries: Vec<String> = fs::read_dir(&year_dir)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
||||
.collect();
|
||||
assert_eq!(entries, vec!["part.parquet".to_string()]);
|
||||
assert!(!out_dir.join("bars_1d/.write.lock").exists());
|
||||
};
|
||||
|
||||
// 1. Truncated BSON rerun
|
||||
let trunc_eod = dir.path().join("trunc.bson");
|
||||
let mut bytes = eod_doc("NVDA.US", "2024-06-07", 100.0, 101.0, 99.0, 100.5, 1000.0);
|
||||
bytes.truncate(bytes.len() - 4);
|
||||
write_bytes(&trunc_eod, &bytes);
|
||||
assert!(run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: trunc_eod,
|
||||
qm_splits: valid_splits.clone(),
|
||||
provider_mapping: valid_map.clone(),
|
||||
listing_currencies: valid_curr.clone(),
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.is_err());
|
||||
assert_unchanged_and_clean();
|
||||
|
||||
// 2. Conflicting QM alias split rerun
|
||||
let conflict_splits = dir.path().join("conflict_splits.bson");
|
||||
let mut split_bytes = Vec::new();
|
||||
split_bytes.extend_from_slice(&split_doc("NVDA:US", "2024-06-10", 10.0));
|
||||
split_bytes.extend_from_slice(&split_doc("NVDA_ALT:US", "2024-06-10", 5.0));
|
||||
write_bytes(&conflict_splits, &split_bytes);
|
||||
assert!(run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: valid_eod.clone(),
|
||||
qm_splits: conflict_splits,
|
||||
provider_mapping: valid_map.clone(),
|
||||
listing_currencies: valid_curr.clone(),
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.is_err());
|
||||
assert_unchanged_and_clean();
|
||||
|
||||
// 3. Malformed currencies TSV rerun
|
||||
let bad_curr = dir.path().join("bad_curr.tsv");
|
||||
write_bytes(&bad_curr, b"not_an_id\tUSD\n");
|
||||
assert!(run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: valid_eod,
|
||||
qm_splits: valid_splits,
|
||||
provider_mapping: valid_map,
|
||||
listing_currencies: bad_curr,
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.is_err());
|
||||
assert_unchanged_and_clean();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_and_all_unmapped_inputs_publish_nothing_and_account_cleanly() {
|
||||
let dir = TestTempDir::new("empty-unmapped");
|
||||
let empty_eod = dir.path().join("empty_eod.bson");
|
||||
let empty_splits = dir.path().join("empty_splits.bson");
|
||||
let empty_map = dir.path().join("empty_map.tsv");
|
||||
let empty_curr = dir.path().join("empty_curr.tsv");
|
||||
write_bytes(&empty_eod, b"");
|
||||
write_bytes(&empty_splits, b"");
|
||||
write_bytes(&empty_map, b"");
|
||||
write_bytes(&empty_curr, b"");
|
||||
|
||||
let out_empty = dir.path().join("out_empty");
|
||||
let report_empty = run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: empty_eod,
|
||||
qm_splits: empty_splits.clone(),
|
||||
provider_mapping: empty_map,
|
||||
listing_currencies: empty_curr,
|
||||
out_dir: out_empty.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.expect("zero-byte inputs succeed");
|
||||
assert_eq!(report_empty.eod_docs_framed, 0);
|
||||
assert_eq!(report_empty.write_stats.rows_written, 0);
|
||||
assert_eq!(report_empty.write_stats.files_written, 0);
|
||||
assert!(format_curate_bars_report(&report_empty).contains("years: none"));
|
||||
assert!(!out_empty.join("bars_1d").exists());
|
||||
|
||||
// Case-sensitive source ("EOD" != "eod") and lowercase ticker ("aapl.us" != "AAPL.US")
|
||||
// must all drop at early filter and publish nothing.
|
||||
let map_path = dir.path().join("case_map.tsv");
|
||||
let curr_path = dir.path().join("case_curr.tsv");
|
||||
let unmapped_eod = dir.path().join("unmapped_eod.bson");
|
||||
write_bytes(&map_path, b"EOD\tAAPL.US\t2020-01-01\t\t1\neod\tMSFT.US\t2020-01-01\t\t2\n");
|
||||
write_bytes(&curr_path, b"1\tUSD\n2\tUSD\n");
|
||||
let mut eod_bytes = Vec::new();
|
||||
eod_bytes.extend_from_slice(&eod_doc("AAPL.US", "2024-01-02", 100.0, 101.0, 99.0, 100.5, 1000.0));
|
||||
eod_bytes.extend_from_slice(&eod_doc("msft.us", "2024-01-02", 200.0, 202.0, 199.0, 201.0, 2000.0));
|
||||
write_bytes(&unmapped_eod, &eod_bytes);
|
||||
|
||||
let out_unmapped = dir.path().join("out_unmapped");
|
||||
let report_unmapped = run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: unmapped_eod,
|
||||
qm_splits: empty_splits,
|
||||
provider_mapping: map_path,
|
||||
listing_currencies: curr_path,
|
||||
out_dir: out_unmapped.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.expect("all-unmapped inputs succeed");
|
||||
assert_eq!(report_unmapped.eod_docs_framed, 2);
|
||||
assert_eq!(report_unmapped.eod_decode.decoded, 2);
|
||||
assert_eq!(report_unmapped.bars_unmapped_source_dropped, 2);
|
||||
assert_eq!(report_unmapped.buffered_bars, 0);
|
||||
assert_eq!(report_unmapped.write_stats.rows_written, 0);
|
||||
assert!(!out_unmapped.join("bars_1d").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_forward_and_reverse_split_math_preserves_ohlc() {
|
||||
let dir = TestTempDir::new("split-math");
|
||||
let eod_path = dir.path().join("eod.bson");
|
||||
let splits_path = dir.path().join("splits.bson");
|
||||
let map_path = dir.path().join("map.tsv");
|
||||
let curr_path = dir.path().join("curr.tsv");
|
||||
let out_dir = dir.path().join("out");
|
||||
|
||||
write_bytes(
|
||||
&map_path,
|
||||
b"eod\tFWD.US\t2020-01-01\t\t1\n\
|
||||
qm\tFWD:US\t2020-01-01\t\t1\n\
|
||||
eod\tREV.V\t2020-01-01\t\t2\n\
|
||||
qm\tREV:CA\t2020-01-01\t\t2\n",
|
||||
);
|
||||
write_bytes(&curr_path, b"1\tUSD\n2\tCAD\n");
|
||||
|
||||
// FWD: 2:1 on 2024-03-01 then 3:1 on 2024-06-01 (pre-March /6, March..May /3, June+ /1).
|
||||
// REV: 1-for-4 reverse split (ratio 0.25) on 2024-05-01 (pre-May *4, May+ *1).
|
||||
let mut split_bytes = Vec::new();
|
||||
split_bytes.extend_from_slice(&split_doc("FWD:US", "2024-03-01", 2.0));
|
||||
split_bytes.extend_from_slice(&split_doc("FWD:US", "2024-06-01", 3.0));
|
||||
split_bytes.extend_from_slice(&split_doc("REV:CA", "2024-05-01", 0.25));
|
||||
write_bytes(&splits_path, &split_bytes);
|
||||
|
||||
let mut eod_bytes = Vec::new();
|
||||
eod_bytes.extend_from_slice(&eod_doc("FWD.US", "2024-02-28", 60.0, 62.0, 59.0, 61.0, 600.0));
|
||||
eod_bytes.extend_from_slice(&eod_doc("FWD.US", "2024-03-01", 30.0, 31.0, 29.0, 30.5, 300.0));
|
||||
eod_bytes.extend_from_slice(&eod_doc("FWD.US", "2024-06-01", 10.0, 11.0, 9.5, 10.5, 100.0));
|
||||
eod_bytes.extend_from_slice(&eod_doc("REV.V", "2024-04-30", 0.50, 0.55, 0.45, 0.52, 250.0));
|
||||
eod_bytes.extend_from_slice(&eod_doc("REV.V", "2024-05-01", 2.00, 2.10, 1.90, 2.05, 500.0));
|
||||
write_bytes(&eod_path, &eod_bytes);
|
||||
|
||||
let report = run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: eod_path,
|
||||
qm_splits: splits_path,
|
||||
provider_mapping: map_path,
|
||||
listing_currencies: curr_path,
|
||||
out_dir: out_dir.clone(),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.expect("multi-split math");
|
||||
assert_eq!(report.write_stats.rows_written, 5);
|
||||
|
||||
let rows = read_parquet_bars(&out_dir.join("bars_1d/year=2024/part.parquet"));
|
||||
assert_eq!(rows.len(), 5);
|
||||
// FWD pre-March: 600 / 6 = 100, OHLC bit-identical.
|
||||
assert_eq!(rows[0].listing_id, 1);
|
||||
assert_eq!(rows[0].open.to_bits(), 60.0_f64.to_bits());
|
||||
assert_eq!(rows[0].close.to_bits(), 61.0_f64.to_bits());
|
||||
assert_eq!(rows[0].volume.to_bits(), 100.0_f64.to_bits());
|
||||
// FWD on 2024-03-01 (ex-date of 2:1, pre-3:1): 300 / 3 = 100.
|
||||
assert_eq!(rows[1].volume.to_bits(), 100.0_f64.to_bits());
|
||||
// FWD on 2024-06-01 (ex-date of 3:1): untouched = 100.
|
||||
assert_eq!(rows[2].volume.to_bits(), 100.0_f64.to_bits());
|
||||
// REV pre-May: 250 / 0.25 = 1000, OHLC bit-identical.
|
||||
assert_eq!(rows[3].listing_id, 2);
|
||||
assert_eq!(rows[3].open.to_bits(), 0.50_f64.to_bits());
|
||||
assert_eq!(rows[3].close.to_bits(), 0.52_f64.to_bits());
|
||||
assert_eq!(rows[3].volume.to_bits(), 1_000.0_f64.to_bits());
|
||||
// REV on ex-date 2024-05-01: untouched = 500.
|
||||
assert_eq!(rows[4].volume.to_bits(), 500.0_f64.to_bits());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tsv_loaders_reject_strict_edge_cases_and_accept_no_trailing_newline() {
|
||||
let dir = TestTempDir::new("tsv-strict");
|
||||
|
||||
// Last line without trailing newline is accepted.
|
||||
let no_nl_map = dir.path().join("no_nl_map.tsv");
|
||||
write_bytes(&no_nl_map, b"eod\tAAPL.US\t2020-01-01\t\t1");
|
||||
let rows = load_provider_mapping_tsv(&no_nl_map).expect("no trailing newline map");
|
||||
assert_eq!(rows.len(), 1);
|
||||
assert_eq!(rows[0].listing_id, 1);
|
||||
|
||||
let no_nl_curr = dir.path().join("no_nl_curr.tsv");
|
||||
write_bytes(&no_nl_curr, b"1\tUSD");
|
||||
let curr = load_listing_currencies_tsv(&no_nl_curr).expect("no trailing newline curr");
|
||||
assert_eq!(curr.get(&1).map(AsRef::as_ref), Some("USD"));
|
||||
|
||||
let bad_map_cases: &[(&str, &[u8])] = &[
|
||||
("four_cols", b"eod\tAAPL.US\t2020-01-01\t1\n"),
|
||||
("six_cols", b"eod\tAAPL.US\t2020-01-01\t\t1\textra\n"),
|
||||
("feb30", b"eod\tAAPL.US\t2024-02-30\t\t1\n"),
|
||||
("slash_date", b"eod\tAAPL.US\t2024/01/02\t\t1\n"),
|
||||
("blank_valid_to", b"eod\tAAPL.US\t2020-01-01\t \t1\n"),
|
||||
("i32_overflow", b"eod\tAAPL.US\t2020-01-01\t\t2147483648\n"),
|
||||
("empty_source", b"\tAAPL.US\t2020-01-01\t\t1\n"),
|
||||
("blank_source_id", b"eod\t \t2020-01-01\t\t1\n"),
|
||||
("interior_cr", b"eod\tAA\rPL.US\t2020-01-01\t\t1\n"),
|
||||
("comma_sep", b"eod,AAPL.US,2020-01-01,,1\n"),
|
||||
];
|
||||
for (name, content) in bad_map_cases {
|
||||
let p = dir.path().join(format!("bad_map_{name}.tsv"));
|
||||
write_bytes(&p, content);
|
||||
let err = load_provider_mapping_tsv(&p).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, RunCurateBarsError::TsvParse { line: 1, .. }),
|
||||
"case {name}: expected TsvParse line 1, got {err:?}"
|
||||
);
|
||||
assert!(err.to_string().contains(":1:"), "case {name}: {err}");
|
||||
}
|
||||
|
||||
let bad_curr_cases: &[(&str, &[u8])] = &[
|
||||
("empty_curr", b"1\t\n"),
|
||||
("blank_curr", b"1\t \n"),
|
||||
("extra_col", b"1\tUSD\textra\n"),
|
||||
("interior_cr", b"1\tUS\rD\n"),
|
||||
];
|
||||
for (name, content) in bad_curr_cases {
|
||||
let p = dir.path().join(format!("bad_curr_{name}.tsv"));
|
||||
write_bytes(&p, content);
|
||||
let err = load_listing_currencies_tsv(&p).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, RunCurateBarsError::TsvParse { line: 1, .. }),
|
||||
"case {name}: expected TsvParse line 1, got {err:?}"
|
||||
);
|
||||
assert!(err.to_string().contains(":1:"), "case {name}: {err}");
|
||||
}
|
||||
|
||||
// valid_to < valid_from fails with ProviderMappingError::InvalidRange.
|
||||
let inv_range_map = dir.path().join("inv_range_map.tsv");
|
||||
write_bytes(&inv_range_map, b"eod\tAAPL.US\t2024-06-01\t2024-05-01\t1\n");
|
||||
let empty_eod = dir.path().join("empty_eod.bson");
|
||||
let empty_splits = dir.path().join("empty_splits.bson");
|
||||
write_bytes(&empty_eod, b"");
|
||||
write_bytes(&empty_splits, b"");
|
||||
let err = run_curate_bars_1d(&CurateBars1dArgs {
|
||||
eod_prices: empty_eod,
|
||||
qm_splits: empty_splits,
|
||||
provider_mapping: inv_range_map,
|
||||
listing_currencies: no_nl_curr,
|
||||
out_dir: dir.path().join("out_inv_range"),
|
||||
rel_tolerance: DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||
})
|
||||
.unwrap_err();
|
||||
assert!(matches!(
|
||||
err,
|
||||
RunCurateBarsError::ProviderMapping {
|
||||
source: ProviderMappingError::InvalidRange { .. },
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue