T-132: M2.2a — tools/curate-etl: pure point-in-time fundamentals builder (qmFinancials × qmFilings known_at link + statutory-lag fallback → bitemporal rows → 2D PIT snapshots + TTM) with restatement fixture test and local benchmark (no new crates)

This commit is contained in:
Studio 2026-12-01 00:52:59 -05:00
parent be5bd1c38a
commit 948dbf560c
2 changed files with 106 additions and 32 deletions

View file

@ -637,6 +637,7 @@ where
});
let mut rows: Vec<FundamentalsRow<K>> = Vec::with_capacity(candidates.len());
let mut survivors: Vec<usize> = Vec::new();
let total = candidates.len();
let mut pe_start = 0usize;
@ -657,17 +658,41 @@ where
let mut g_start = pe_start;
while g_start < pe_end {
let first = &candidates[g_start].row;
let group_ts = candidates[g_start].row.known_at_utc_micros;
let mut g_end = g_start + 1;
while g_end < pe_end
&& candidates[g_end].row.known_at_utc_micros == first.known_at_utc_micros
{
while g_end < pe_end && candidates[g_end].row.known_at_utc_micros == group_ts {
g_end += 1;
}
let group_len = g_end - g_start;
let all_identical = candidates[g_start + 1..g_end].iter().all(|other_cand| {
let other = &other_cand.row;
// Filter out stale dated statutory-lag candidates before duplicate/conflict checks
// so a superseded estimate cannot cause an eligible estimate at the same fallback
// timestamp to be rejected as conflicting.
survivors.clear();
for idx in g_start..g_end {
let cand = &candidates[idx];
let prior_max_rd = match cand.row.fiscal_period {
FiscalPeriod::Quarterly => max_accepted_rd_q,
FiscalPeriod::Annual => max_accepted_rd_fy,
};
let is_stale_estimate = cand.row.known_at_source == KNOWN_AT_SOURCE_STATUTORY_LAG
&& cand
.report_date
.is_some_and(|rd_a| prior_max_rd.is_some_and(|rd_b| rd_b > rd_a));
if is_stale_estimate {
stats.observations_dropped_stale_estimate += 1;
} else {
survivors.push(idx);
}
}
if survivors.is_empty() {
g_start = g_end;
continue;
}
let first = &candidates[survivors[0]].row;
let all_identical = survivors[1..].iter().all(|&idx| {
let other = &candidates[idx].row;
first.fiscal_period == other.fiscal_period
&& first.currency == other.currency
&& first.known_at_source == other.known_at_source
@ -675,37 +700,17 @@ where
});
if !all_identical {
stats.duplicate_conflict_dropped += group_len;
stats.duplicate_conflict_dropped += survivors.len();
g_start = g_end;
continue;
}
let mut group_max_rd: Option<i32> = None;
let mut all_dated = true;
for cand in &candidates[g_start..g_end] {
match cand.report_date {
Some(rd) => {
group_max_rd = Some(group_max_rd.map_or(rd, |m| m.max(rd)));
}
None => all_dated = false,
for &idx in &survivors {
if let Some(rd) = candidates[idx].report_date {
group_max_rd = Some(group_max_rd.map_or(rd, |m| m.max(rd)));
}
}
let prior_max_rd = match first.fiscal_period {
FiscalPeriod::Quarterly => max_accepted_rd_q,
FiscalPeriod::Annual => max_accepted_rd_fy,
};
let is_stale_estimate = first.known_at_source == KNOWN_AT_SOURCE_STATUTORY_LAG
&& all_dated
&& group_max_rd
.is_some_and(|rd_a| prior_max_rd.is_some_and(|rd_b| rd_b > rd_a));
if is_stale_estimate {
stats.observations_dropped_stale_estimate += group_len;
g_start = g_end;
continue;
}
if let Some(rd) = group_max_rd {
let slot = match first.fiscal_period {
FiscalPeriod::Quarterly => &mut max_accepted_rd_q,
@ -714,7 +719,7 @@ where
*slot = Some(slot.map_or(rd, |m| m.max(rd)));
}
stats.duplicate_collapsed += group_len - 1;
stats.duplicate_collapsed += survivors.len() - 1;
if first.known_at_source == KNOWN_AT_SOURCE_FILING_TIMESTAMP {
stats.rows_linked_to_filing += 1;
} else {

View file

@ -880,6 +880,75 @@ fn conflicting_newer_filing_does_not_suppress_eligible_statutory_lag_estimate()
assert_eq!(snap.periods[0].fields.revenue, Some(100.0));
}
#[test]
fn stale_estimate_at_same_fallback_timestamp_does_not_conflict_with_eligible_estimate() {
let venues = venues_map(&[("ACME", StatutoryLagVenue::UsTsx)]);
let q1_end = day(2024, 3, 31);
// Apr 30 (rev 100) and May 12 (rev 115) have no same-day periodic filing -> both get the
// May 15 16:05 ET fallback timestamp. The accepted May 10 10-Q (rev 110) supersedes Apr 30,
// so Apr 30 must be dropped as a stale estimate before duplicate/conflict checks and must
// not cause the eligible May 12 estimate to be discarded as a conflict.
let filings = vec![filing("ACME", "2024-05-10T16:15:00 EDT", "10-Q")];
let observations = vec![
obs(
"ACME",
q1_end,
FiscalPeriod::Quarterly,
"USD",
Some(day(2024, 4, 30)),
fields_rev(100.0),
),
obs(
"ACME",
q1_end,
FiscalPeriod::Quarterly,
"USD",
Some(day(2024, 5, 10)),
fields_rev(110.0),
),
obs(
"ACME",
q1_end,
FiscalPeriod::Quarterly,
"USD",
Some(day(2024, 5, 12)),
fields_rev(115.0),
),
];
let built = build_fundamentals_rows(observations.clone(), filings.clone(), &venues);
assert_reconciliation(&built);
assert_eq!(built.stats.observations_dropped_stale_estimate, 1);
assert_eq!(built.stats.duplicate_conflict_dropped, 0);
assert_eq!(built.stats.rows_out, 2);
assert_eq!(built.stats.rows_linked_to_filing, 1);
assert_eq!(built.stats.rows_statutory_lag_estimate, 1);
// Input-order determinism
let mut obs_rev = observations;
obs_rev.reverse();
assert_eq!(built, build_fundamentals_rows(obs_rev, filings, &venues));
let snaps = build_fundamentals_snapshots(&built.rows, 4).unwrap();
let ts_may_15_fallback = et_to_utc_micros(day(2024, 5, 15), 16, 5, 0).unwrap();
let before_fallback = as_of(&snaps, "ACME", ts_may_15_fallback - 1)
.expect("May 10 10-Q must be visible before fallback");
assert_eq!(before_fallback.periods[0].period_end, q1_end);
assert_eq!(before_fallback.periods[0].fields.revenue, Some(110.0));
assert_eq!(before_fallback.known_at_source, KNOWN_AT_SOURCE_FILING_TIMESTAMP);
assert!(!before_fallback.has_estimated_lag);
let at_fallback = as_of(&snaps, "ACME", ts_may_15_fallback)
.expect("May 12 estimate must be visible at fallback cutoff");
assert_eq!(at_fallback.effective_known_at_utc_micros, ts_may_15_fallback);
assert_eq!(at_fallback.periods[0].period_end, q1_end);
assert_eq!(at_fallback.periods[0].fields.revenue, Some(115.0));
assert_eq!(at_fallback.known_at_source, KNOWN_AT_SOURCE_STATUTORY_LAG);
assert!(at_fallback.has_estimated_lag);
}
#[test]
fn annual_fy_sharing_q4_period_end_preserves_standalone_q4_for_ttm() {
let make_row = |pe: i32, kind: FiscalPeriod, ts: i64, src: &'static str, fields: FundamentalsFields| {