T-70: M1.2c — tools/curate-etl: pure corporate_actions builder (splits + dividends + OTC continuation / Shumway terminal delisting rows) with local benchmark (no new crates)

This commit is contained in:
Studio 2026-11-27 19:14:15 -05:00
parent 88625d6b70
commit 722fdea615
4 changed files with 85 additions and 6 deletions

View file

@ -48,7 +48,7 @@ Rules:
2. **Dividends (`kind = "dividend"`):** Local `currency` and `cash` are preserved unchanged (deliberately omitting `usdFxRate`). `cash == 0.0` events are dropped and counted in `stats.dividend_zero_cash_dropped`. Multiple dividends on the same `(key, ex_date)` remain separate event-log rows. Non-finite or `< 0.0` cash returns `InvalidDividendCash`; empty/whitespace currency on positive cash returns `EmptyDividendCurrency`.
3. **Point-in-time `known_at` clamping:** Uses `declared_date` when present and `<= ex_date` (`known_at_source = "declared_date"`). Otherwise falls back to `ex_date` (`known_at_source = "ex_date_fallback"`), preserving the raw `declared_date` on the row and counting `declared_date > ex_date` in `stats.declared_after_ex_date` so `known_at_date <= ex_date` always holds.
4. **Delistings (Pass 6):**
- `successor_key = Some(s), terminal_return = None` → one `otc_delist_transfer` row (`ex_date = known_at_date = delist_date`, `known_at_source = "delist_date"`).
- `successor_key = Some(s), terminal_return = None` → one `otc_delist_transfer` row (`ex_date = known_at_date = delist_date`, `known_at_source = "delist_date"`). Callers must pass the Pass 6 continuity `effectiveDate` (the successor's first observed OTC trading date) as `delist_date`, not the primary listing's last exchange print / halt date.
- `successor_key = None, terminal_return = Some(r)` → one `delisting` row (`r` must be finite and in `[-1.0, 0.0]`, normalizing `-0.0` to `0.0`, else `InvalidTerminalReturn`).
- Both `None` → dropped and counted in `stats.delist_without_outcome`.
- Both `Some` → `CorporateActionError::ConflictingDelistingOutcome`.

View file

@ -158,7 +158,7 @@ pub fn run_adj_benchmark(events: usize) -> Result<AdjBenchReport, AdjError> {
let mut dividends: Vec<(i32, DividendEvent)> = Vec::with_capacity(events);
for _ in 0..events {
let key = ((rng.next_u64() as usize) % BENCH_KEY_COUNT) as i32 + 1;
let key = (rng.next_u64() % (BENCH_KEY_COUNT as u64)) as i32 + 1;
let ex_date = min_date + (rng.next_u64() % date_span) as i32;
let kind_roll = rng.next_u64() % 100;
@ -167,7 +167,7 @@ pub fn run_adj_benchmark(events: usize) -> Result<AdjBenchReport, AdjError> {
let ratio = if ratio_roll == 0 {
1.0
} else {
SAMPLE_RATIOS[(rng.next_u64() as usize) % SAMPLE_RATIOS.len()]
SAMPLE_RATIOS[(rng.next_u64() % (SAMPLE_RATIOS.len() as u64)) as usize]
};
splits.push((key, SplitEvent { ex_date, ratio }));
} else {
@ -283,7 +283,7 @@ pub fn run_corporate_actions_benchmark(
let mut delistings: Vec<(i32, DelistingOutcomeEvent<i32>)> = Vec::with_capacity(events / 20);
for _ in 0..events {
let key = ((rng.next_u64() as usize) % BENCH_KEY_COUNT) as i32 + 1;
let key = (rng.next_u64() % (BENCH_KEY_COUNT as u64)) as i32 + 1;
let ex_date = min_date + (rng.next_u64() % date_span) as i32;
let kind_roll = rng.next_u64() % 100;
@ -292,7 +292,7 @@ pub fn run_corporate_actions_benchmark(
let ratio = if ratio_roll == 0 {
1.0
} else {
SAMPLE_RATIOS[(rng.next_u64() as usize) % SAMPLE_RATIOS.len()]
SAMPLE_RATIOS[(rng.next_u64() % (SAMPLE_RATIOS.len() as u64)) as usize]
};
let decl_roll = rng.next_u64() % 100;
let declared_date = if decl_roll < 30 {

View file

@ -107,7 +107,11 @@ pub struct DividendActionEvent {
/// Input delisting outcome event (mirrors Pass 6 output) for [`build_corporate_actions`].
#[derive(Debug, Clone, PartialEq)]
pub struct DelistingOutcomeEvent<K = String> {
/// Effective delisting date as days since 1970-01-01 (UTC Gregorian).
/// Effective date as days since 1970-01-01 (UTC Gregorian).
///
/// For OTC continuation transfers (`successor_key = Some(_)`), callers must pass the Pass 6
/// continuity `effectiveDate` (the successor's first observed OTC trading date), not the
/// primary listing's last exchange print / halt date.
pub delist_date: i32,
/// OTC continuation successor key (e.g. `SIVB` -> `SIVBQ`), mutually exclusive with `terminal_return`.
pub successor_key: Option<K>,
@ -944,6 +948,41 @@ mod tests {
assert!(!err.to_string().is_empty());
}
#[test]
fn otc_transfer_carries_pass6_effective_date_not_primary_end_date() {
// SIVB halted on Nasdaq on 2023-03-10 (primary listing end), then began quoting OTC as
// SIVBQ on 2023-03-28 (Pass 6 continuity `effectiveDate`). The transfer row must carry
// the successor's first OTC date so SIVBQ is never published as active before Mar 28.
let primary_end = ymd_to_epoch_days(2023, 3, 10).unwrap();
let otc_start = ymd_to_epoch_days(2023, 3, 28).unwrap();
assert!(primary_end < otc_start);
let built = build_corporate_actions(
empty_splits(),
empty_dividends(),
[(
"SIVB".to_string(),
DelistingOutcomeEvent {
delist_date: otc_start,
successor_key: Some("SIVBQ".to_string()),
terminal_return: None,
},
)],
)
.unwrap();
assert_stats_conservation(&built);
assert_eq!(built.rows.len(), 1);
let row = &built.rows[0];
assert_eq!(row.kind, CorporateActionKind::OtcDelistTransfer);
assert_eq!(row.successor_key.as_deref(), Some("SIVBQ"));
assert_eq!(row.ex_date, otc_start);
assert_eq!(row.known_at_date, otc_start);
assert_ne!(row.ex_date, primary_end);
assert_ne!(row.known_at_date, primary_end);
assert_eq!(row.known_at_source, KnownAtSource::DelistDate);
}
#[test]
fn shuffled_mixed_fixture_is_bit_deterministic_and_conserves_stats() {
let d1 = ymd_to_epoch_days(2023, 3, 28).unwrap();

View file

@ -25,6 +25,7 @@ fn no_args_and_help_print_usage_and_succeed() {
let text = stdout(&out);
assert!(text.contains("Usage:"), "{args:?}: {text}");
assert!(text.contains("bench-split"), "{args:?}: {text}");
assert!(text.contains("bench-corporate-actions"), "{args:?}: {text}");
}
}
@ -49,6 +50,10 @@ fn invalid_invocations_exit_nonzero_with_usage_on_stderr() {
&["bench-split", "0"][..],
&["bench-split", "-5"][..],
&["bench-split", "10", "20"][..],
&["bench-corporate-actions", "abc"][..],
&["bench-corporate-actions", "0"][..],
&["bench-corporate-actions", "-5"][..],
&["bench-corporate-actions", "10", "20"][..],
&["--unknown"][..],
&["export"][..],
] {
@ -86,3 +91,38 @@ fn bench_split_small_run_is_headless_deterministic_and_reports_rates() {
};
assert_eq!(checksum(&text), checksum(&stdout(&b)));
}
#[test]
fn bench_corporate_actions_small_run_is_headless_deterministic_and_reports_rates() {
let a = run(&["bench-corporate-actions", "1000"]);
let b = run(&["bench-corporate-actions", "1000"]);
assert!(a.status.success(), "{}", stderr(&a));
assert!(b.status.success(), "{}", stderr(&b));
assert!(stderr(&a).is_empty(), "unexpected stderr: {}", stderr(&a));
assert!(stderr(&b).is_empty(), "unexpected stderr: {}", stderr(&b));
let text_a = stdout(&a);
let text_b = stdout(&b);
assert!(
text_a.contains("curate-etl bench-corporate-actions"),
"{text_a}"
);
assert!(text_a.contains("events=1000 "), "{text_a}");
assert!(text_a.contains("keys=20000"), "{text_a}");
assert!(text_a.contains("rows_out="), "{text_a}");
assert!(text_a.contains("events/s"), "{text_a}");
assert!(text_a.contains("ns/event"), "{text_a}");
assert!(text_a.contains("checksum=0x"), "{text_a}");
assert!(text_a.contains("stats: split_noops="), "{text_a}");
let field = |s: &str, prefix: &str| {
s.split_whitespace()
.find(|t| t.starts_with(prefix))
.map(str::to_owned)
};
let checksum_a = field(&text_a, "checksum=");
let checksum_b = field(&text_b, "checksum=");
assert!(checksum_a.is_some(), "{text_a}");
assert_eq!(checksum_a, checksum_b);
assert_eq!(field(&text_a, "rows_out="), field(&text_b, "rows_out="));
}