T-103: M1.2u — tools/curate-etl: pure corporate_actions curation stage (source-keyed qmSplits + qmDividends + Pass 6 delisting outcomes → provider_mapping → CorporateActionsParquetRows → Zstd Parquet) with tests and local benchmark (no new crates)

This commit is contained in:
Studio 2026-11-28 19:48:40 -05:00
parent d191b9f46a
commit a2ab41e270
4 changed files with 89 additions and 36 deletions

View file

@ -811,12 +811,12 @@ cargo run --release -p curate-etl --offline -- bench-curate-fx [rows]
1. **Field & timestamp rules:**
- Calls `build_corporate_actions` unchanged (`1:1` split no-ops, `0.0` cash dividends, and delistings without an outcome drop inside the builder; same-key multi-dividends stay separate; late declarations clamp to `ex_date` with `KnownAtSource::ExDateFallback`; local `currency` is preserved).
- Resolves `(mapping_source, key, ex_date) -> listing_id` via `ProviderMappingTable::cursor_for` / `ProviderMappingCursor::resolve` (one cursor per contiguous `key` run) for `split`, `dividend`, and terminal `delisting` rows.
- For `otc_delist_transfer` rows, Pass 6 anchors `ex_date` (and `known_at`) to `effectiveDate` (the successor's first OTC trading date), which normally falls after the outgoing listing's primary-venue `valid_to` (e.g. `SIVB.US` ends `2023-03-10`, `SIVBQ.US` starts `2023-03-28`) and may coincide with ticker reuse. Callers pass Pass 6's outgoing listing identity via `CurateDelistingInput::from_listing_id`: when `Some(expected_id)`, curation selects only a `provider_mapping` range for `(mapping_source, key)` with `valid_from <= ex_date` and `listing_id == expected_id` (never attaching to an unrelated ticker occupant); when `None`, lookup falls back to strict point-in-time `ex_date` resolution (never guessing an ended range). `successor_key` is always resolved strictly on `ex_date` to `successor_listing_id`. Never emits an `otc_delist_transfer` with `successor_listing_id = None` or `successor_listing_id == listing_id`.
- For `otc_delist_transfer` rows, Pass 6 anchors `ex_date` (and `known_at`) to `effectiveDate` (the successor's first OTC trading date), which normally falls after the outgoing listing's primary-venue `valid_to` (e.g. `SIVB.US` ends `2023-03-10`, `SIVBQ.US` starts `2023-03-28`) and may coincide with ticker reuse. `otc_delist_transfer` therefore requires Pass 6's outgoing listing identity via `CurateDelistingInput::from_listing_id` (`Some(id)`): curation selects only a `provider_mapping` range for `(mapping_source, key)` with `valid_from <= ex_date` and `listing_id == id` (never attaching to an unrelated ticker occupant), and any identity-less `otc_delist_transfer` (`from_listing_id = None`) drops into `dropped_unmapped`. `successor_key` is always resolved strictly on `ex_date` to `successor_listing_id`. Never emits an `otc_delist_transfer` with `successor_listing_id = None` or `successor_listing_id == listing_id`.
- Stamps `known_at_micros = et_session_close_utc_micros(known_at_date)` (`16:00:00 ET` of `known_at_date`, matching `bars_1d`'s conservative end-of-session convention because source corporate-action records carry no intraday announcement time) and carries `known_at_source` through unchanged.
- Carries `ex_date`, `declared_date`, `kind`, `ratio`, `cash_amount`, `currency`, and `terminal_return` through bit-exactly (`"USD"`/`"CAD"` interned as `Cow::Borrowed`).
2. **Drop & collision policy (never panics or aborts on bad rows):**
- Strict drop priority per built row:
1. `dropped_unmapped`: no covering `provider_mapping` range for `(mapping_source, key, ex_date)` (or, for `otc_delist_transfer` with `from_listing_id = Some(id)`, no range with `valid_from <= ex_date` matching `id`; or, for terminal `delisting` with `from_listing_id = Some(id)`, active occupant does not match `id`).
1. `dropped_unmapped`: no covering `provider_mapping` range for `(mapping_source, key, ex_date)` (or, for `otc_delist_transfer`, missing `from_listing_id = None` or no range with `valid_from <= ex_date` matching `from_listing_id = Some(id)`; or, for terminal `delisting` with `from_listing_id = Some(id)`, active occupant does not match `id`).
2. `dropped_unmapped_successor`: `otc_delist_transfer` whose `successor_key` has no covering range on `ex_date`.
3. `dropped_self_successor`: `otc_delist_transfer` whose `successor_key` resolves to the row's own `listing_id`.
4. `dropped_known_at_out_of_range`: `known_at_date` falls outside `et_session_close_utc_micros`'s `1976-01-01..=2030-12-31` window.
@ -839,12 +839,12 @@ Command (`2026-11-28`, two back-to-back runs with identical checksum `0x0c22a1a1
- Input: `events=520000`, `splits=20262`, `dividends=494624`, `delistings=5114`, `keys=20000`
- Builder: `rows_out=514224`, `splits_out=19938`, `split_noops=324`, `divs_out=489671`, `zero_div=4953`, `otc_out=2856`, `delist_out=1759`, `delist_no_outcome=499`
- Output: `rows_out=494518` (`95.10%` retained), `listings_out=19996`, `files=1`, `row_groups=1`, `bytes=3802001`, `read_back_rows=494518`
- Curation stats: `unmapped=12887`, `unmapped_succ=510`, `self_succ=531`, `known_at_oor=2526`, `coll_collapsed=1398`, `coll_dropped=1854`
- Curation stats: `unmapped=12886`, `unmapped_succ=511`, `self_succ=531`, `known_at_oor=2526`, `coll_collapsed=1398`, `coll_dropped=1854`
- Run 1:
- Stage (in-memory): `elapsed=287.50 ms`, `throughput=1808724 events/s`, `latency=552.88 ns/event`, `checksum=0x0c22a1a1ab7caad4`
- Parquet write: `elapsed=85.31 ms`, `throughput=5797014 rows/s`
- Combined (stage → Parquet): `elapsed=372.80 ms`, `throughput=1394845 events/s`
- Stage (in-memory): `elapsed=278.66 ms`, `throughput=1866040 events/s`, `latency=535.89 ns/event`, `checksum=0x0c22a1a1ab7caad4`
- Parquet write: `elapsed=84.14 ms`, `throughput=5877527 rows/s`
- Combined (stage → Parquet): `elapsed=362.80 ms`, `throughput=1433288 events/s`
- Run 2:
- Stage (in-memory): `elapsed=281.40 ms`, `throughput=1847913 events/s`, `latency=541.15 ns/event`, `checksum=0x0c22a1a1ab7caad4`
- Parquet write: `elapsed=87.82 ms`, `throughput=5630801 rows/s`
- Combined (stage → Parquet): `elapsed=369.22 ms`, `throughput=1408366 events/s`
- Stage (in-memory): `elapsed=284.33 ms`, `throughput=1828877 events/s`, `latency=546.78 ns/event`, `checksum=0x0c22a1a1ab7caad4`
- Parquet write: `elapsed=87.57 ms`, `throughput=5647404 rows/s`
- Combined (stage → Parquet): `elapsed=371.89 ms`, `throughput=1398252 events/s`

View file

@ -5036,7 +5036,7 @@ pub struct CurateCorporateActionsBenchReport {
struct CurateCorporateActionsBenchFixture {
splits: Vec<(String, SplitActionEvent)>,
dividends: Vec<(String, DividendActionEvent)>,
delistings: Vec<(String, DelistingOutcomeEvent<String>)>,
delistings: Vec<crate::curate_corporate_actions::CurateDelistingInput<String>>,
provider_mapping: ProviderMappingTable,
key_count: usize,
}
@ -5115,7 +5115,7 @@ fn build_curate_corporate_actions_bench_fixture(
let mut rng = SplitMix64::new(CURATE_CORPORATE_ACTIONS_BENCH_SEED);
let mut splits: Vec<(String, SplitActionEvent)> = Vec::with_capacity(events / 25 + 64);
let mut dividends: Vec<(String, DividendActionEvent)> = Vec::with_capacity(events);
let mut delistings: Vec<(String, DelistingOutcomeEvent<String>)> =
let mut delistings: Vec<crate::curate_corporate_actions::CurateDelistingInput<String>> =
Vec::with_capacity(events / 90 + 32);
let mut generated = 0usize;
@ -5277,7 +5277,28 @@ fn build_curate_corporate_actions_bench_fixture(
terminal_return: None,
}
};
delistings.push((keys[key_idx].clone(), ev));
let from_listing_id = if outcome_roll < 55 {
if key_idx < SINGLE_RANGE_END {
Some((key_idx + 1) as i32)
} else if key_idx < REUSE_END {
if ex_date >= reuse_resume {
Some((key_idx + 100_001) as i32)
} else {
Some((key_idx + 1) as i32)
}
} else if key_idx < ALIAS_END {
Some((key_idx - REUSE_END + 1) as i32)
} else {
None
}
} else {
None
};
delistings.push(crate::curate_corporate_actions::CurateDelistingInput {
key: keys[key_idx].clone(),
event: ev,
from_listing_id,
});
generated += 1;
}
}

View file

@ -16,12 +16,12 @@
//! - For [`CorporateActionKind::OtcDelistTransfer`] rows, Pass 6 anchors `ex_date` (and `known_at`)
//! to `effectiveDate` (the successor's first OTC trading date), which normally falls after the
//! outgoing listing's primary-venue `valid_to` (e.g. `SIVB.US` ends `2023-03-10`, `SIVBQ.US`
//! starts `2023-03-28`) and may coincide with ticker reuse. Callers pass Pass 6's outgoing
//! listing identity via [`CurateDelistingInput::from_listing_id`]: when `Some(expected_id)`,
//! starts `2023-03-28`) and may coincide with ticker reuse. `OtcDelistTransfer` therefore requires
//! Pass 6's outgoing listing identity via [`CurateDelistingInput::from_listing_id`] (`Some(id)`):
//! curation selects the `(mapping_source, key)` range with `valid_from <= ex_date` whose
//! `listing_id == expected_id` (so an unrelated occupant of `key` is never linked to the OTC
//! successor); when `None`, lookup falls back to strict point-in-time `ex_date` resolution
//! (never guessing an ended range). `successor_key` is always resolved strictly on `ex_date` to
//! `listing_id == id` (so an unrelated occupant of `key` is never linked to the OTC successor),
//! and any identity-less `OtcDelistTransfer` (`from_listing_id = None`) drops into
//! `dropped_unmapped`. `successor_key` is always resolved strictly on `ex_date` to
//! `successor_listing_id`. Unmapped outgoing keys (`dropped_unmapped`), unmapped successors
//! (`dropped_unmapped_successor`), and successors that alias to the row's own `listing_id`
//! (`dropped_self_successor`) are dropped—never emitted with `successor_listing_id = None` or a
@ -171,9 +171,10 @@ impl From<ParquetCorporateActionsError> for CurateCorporateActionsWriteError {
/// [`DelistingOutcomeEvent`] with Pass 6's optional outgoing `listing_id` (`from_listing_id` in
/// `corporate_action_continuity`).
///
/// Implements `From<(K, DelistingOutcomeEvent<K>)>` (with `from_listing_id = None`) so existing
/// tuple iterators continue to work ergonomics-first while Pass 6 callers supply
/// `from_listing_id = Some(id)` to select/validate ended or reused outgoing ticker mappings.
/// Implements `From<(K, DelistingOutcomeEvent<K>)>` (with `from_listing_id = None`) for terminal
/// delisting callers; `OtcDelistTransfer` rows require `from_listing_id = Some(id)` (via
/// [`CurateDelistingInput::with_from_listing_id`]) so ended or reused outgoing tickers are never
/// attached to an unrelated occupant—identity-less OTC transfers drop into `dropped_unmapped`.
#[derive(Debug, Clone, PartialEq)]
pub struct CurateDelistingInput<K = String> {
pub key: K,
@ -386,9 +387,10 @@ where
// For `OtcDelistTransfer`, Pass 6 anchors `ex_date` to the successor's first OTC
// trading date (`effectiveDate`), after the outgoing listing's primary-venue mapping
// has typically ended (e.g. SIVB.US ends 2023-03-10, SIVBQ.US starts 2023-03-28) and
// possibly after ticker reuse. When Pass 6's `from_listing_id` is supplied, select
// only a range with `valid_from <= ex_date` matching `from_listing_id`; otherwise
// require an active range on `ex_date` (never guessing an ended occupant).
// possibly after ticker reuse. `OtcDelistTransfer` therefore requires Pass 6's
// `from_listing_id = Some(id)` (selecting only a range with `valid_from <= ex_date`
// matching `id`); identity-less OTC transfers (`None`) always drop into
// `dropped_unmapped` so they can never attach to an unrelated replacement occupant.
if cursor.ranges().is_empty() {
stats.dropped_unmapped += 1;
continue;
@ -397,6 +399,7 @@ where
(CorporateActionKind::OtcDelistTransfer, Some(expected_id)) => {
resolve_for_listing_on_or_before(cursor.ranges(), row.ex_date, expected_id)
}
(CorporateActionKind::OtcDelistTransfer, None) => None,
(CorporateActionKind::Delisting, Some(expected_id)) => {
cursor.resolve(row.ex_date).filter(|&id| id == expected_id)
}

View file

@ -550,25 +550,27 @@ fn drop_counters_and_priority_unmapped_successor_self_successor_and_known_at_oor
];
let delistings = vec![
// 1. Unmapped successor key (unknown) -> dropped_unmapped_successor
// 0. Identity-less OTC transfer drops into dropped_unmapped before successor checks
otc_ev("GOOD", d_valid, "NO_SUCH_SUCC"),
// 1. Unmapped successor key (unknown) -> dropped_unmapped_successor
otc_ev_from("GOOD", 10, d_valid, "NO_SUCH_SUCC"),
// 2. Successor only mapped starting d_valid + 1 (not on ex_date = d_valid) -> dropped_unmapped_successor
otc_ev("GOOD", d_valid, "SUCC_LATER"),
otc_ev_from("GOOD", 10, d_valid, "SUCC_LATER"),
// 3. Unmapped successor + out-of-range known_at_date -> successor check precedes known_at check
otc_ev("GOOD", d_pre_1976, "NO_SUCH_SUCC"),
otc_ev_from("GOOD", 10, d_pre_1976, "NO_SUCH_SUCC"),
// 4. Self-successor via alias key -> dropped_self_successor
otc_ev("GOOD", d_valid, "GOOD_ALIAS"),
otc_ev_from("GOOD", 10, d_valid, "GOOD_ALIAS"),
// 5. Self-successor via identical key + out-of-range -> self_successor precedes known_at check
otc_ev("GOOD", d_post_2030, "GOOD"),
otc_ev_from("GOOD", 10, d_post_2030, "GOOD"),
];
let out = curate_corporate_actions(splits, empty_divs(), delistings, "qm", &mapping).unwrap();
assert_conservation_and_order(&out);
assert_eq!(out.stats.rows_in, 10);
assert_eq!(out.stats.rows_in, 11);
assert_eq!(out.stats.rows_out, 1);
assert_eq!(out.stats.listings_out, 1);
assert_eq!(out.stats.dropped_unmapped, 1);
assert_eq!(out.stats.dropped_unmapped, 2);
assert_eq!(out.stats.dropped_unmapped_successor, 3);
assert_eq!(out.stats.dropped_self_successor, 2);
assert_eq!(out.stats.dropped_known_at_out_of_range, 3);
@ -601,8 +603,8 @@ fn cross_key_identical_collisions_collapse_across_all_kinds() {
div_ev("K2", d2, 0.50, "USD", Some(d2 - 14)),
];
let delistings = vec![
otc_ev("K1", d3, "SUCC"),
otc_ev("K2", d3, "SUCC"),
otc_ev_from("K1", 10, d3, "SUCC"),
otc_ev_from("K2", 10, d3, "SUCC"),
term_ev("K1", d4, -0.30),
term_ev("K2", d4, -0.30),
];
@ -738,7 +740,7 @@ fn permutation_invariance_shuffled_and_reversed_inputs_match_bit_for_bit() {
otc_ev_from("M_KEY", 30, d4, "M_SUCC"),
otc_ev_from("M_KEY", 999, d4, "M_SUCC"),
term_ev("Z_KEY", d4, -0.30),
otc_ev("A_KEY", d4, "A_ALIAS"),
otc_ev_from("A_KEY", 20, d4, "A_ALIAS"),
];
let baseline = curate_corporate_actions(
@ -1119,9 +1121,36 @@ fn otc_delist_transfer_never_attaches_to_unrelated_ticker_occupant() {
])
.unwrap();
// Tuple-input regression (From<(K, DelistingOutcomeEvent<K>)> with from_listing_id = None):
// REUSE.US is actively occupied by unrelated listing 99 on d_otc (2023-03-28), so an
// identity-less tuple OTC transfer must drop into dropped_unmapped and NEVER emit 99 -> 31.
let tuple_out = curate_corporate_actions(
empty_splits(),
empty_divs(),
vec![(
"REUSE.US".to_string(),
DelistingOutcomeEvent {
delist_date: d_otc,
successor_key: Some("REUSEQ.US".to_string()),
terminal_return: None,
},
)],
"eod",
&mapping,
)
.unwrap();
assert_conservation_and_order(&tuple_out);
assert_eq!(tuple_out.stats.rows_in, 1);
assert_eq!(tuple_out.stats.rows_out, 0);
assert_eq!(tuple_out.stats.listings_out, 0);
assert_eq!(tuple_out.stats.dropped_unmapped, 1);
assert!(tuple_out.rows.is_empty());
let delistings = vec![
// Case 1: Must select outgoing listing 30 (NOT unrelated new occupant 99).
// Case 1a: Must select outgoing listing 30 (NOT unrelated new occupant 99).
otc_ev_from("REUSE.US", 30, d_otc, "REUSEQ.US"),
// Case 1b: Identity-less OTC transfer on active reused ticker -> dropped_unmapped (NOT 99).
otc_ev("REUSE.US", d_otc, "REUSEQ.US"),
// Case 2a: Outgoing listing 40 missing -> must drop, NOT attach to older occupant 12.
otc_ev_from("OLD_ONLY.US", 40, d_otc, "OLD_ONLYQ.US"),
// Case 2b: Same ticker without from_listing_id -> must drop, NOT guess older occupant 12.
@ -1142,10 +1171,10 @@ fn otc_delist_transfer_never_attaches_to_unrelated_ticker_occupant() {
.unwrap();
assert_conservation_and_order(&out);
assert_eq!(out.stats.rows_in, 5);
assert_eq!(out.stats.rows_in, 6);
assert_eq!(out.stats.rows_out, 1);
assert_eq!(out.stats.listings_out, 1);
assert_eq!(out.stats.dropped_unmapped, 4);
assert_eq!(out.stats.dropped_unmapped, 5);
assert_eq!(out.stats.dropped_unmapped_successor, 0);
assert_eq!(out.stats.dropped_self_successor, 0);