From 948dbf560c8398f08dfad58ed6b7f40c6ba9f940 Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 00:52:59 -0500 Subject: [PATCH] =?UTF-8?q?T-132:=20M2.2a=20=E2=80=94=20`tools/curate-etl`?= =?UTF-8?q?:=20pure=20point-in-time=20fundamentals=20builder=20(`qmFinanci?= =?UTF-8?q?als`=20=C3=97=20`qmFilings`=20`known=5Fat`=20link=20+=20statuto?= =?UTF-8?q?ry-lag=20fallback=20=E2=86=92=20bitemporal=20rows=20=E2=86=92?= =?UTF-8?q?=202D=20PIT=20snapshots=20+=20TTM)=20with=20restatement=20fixtu?= =?UTF-8?q?re=20test=20and=20local=20benchmark=20(no=20new=20crates)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/curate-etl/src/fundamentals.rs | 69 ++++++++++++---------- tools/curate-etl/src/fundamentals_tests.rs | 69 ++++++++++++++++++++++ 2 files changed, 106 insertions(+), 32 deletions(-) diff --git a/tools/curate-etl/src/fundamentals.rs b/tools/curate-etl/src/fundamentals.rs index 98b12b1c..7ac7cae6 100644 --- a/tools/curate-etl/src/fundamentals.rs +++ b/tools/curate-etl/src/fundamentals.rs @@ -637,6 +637,7 @@ where }); let mut rows: Vec> = Vec::with_capacity(candidates.len()); + let mut survivors: Vec = 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 = 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 { diff --git a/tools/curate-etl/src/fundamentals_tests.rs b/tools/curate-etl/src/fundamentals_tests.rs index 9f9dd370..a3919caf 100644 --- a/tools/curate-etl/src/fundamentals_tests.rs +++ b/tools/curate-etl/src/fundamentals_tests.rs @@ -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| {