T-77: M1.2g — tools/curate-etl: pure events builder (qmFilings timefiled → UTC + form groups; qmEarnings linked to same-day filing or lagged to 16:05 ET) with local benchmark (no new crates)

This commit is contained in:
Studio 2026-11-27 22:45:41 -05:00
parent 3100ec65b1
commit 88a7e32db7
4 changed files with 189 additions and 19 deletions

View file

@ -19,10 +19,12 @@
//! whose local ET calendar date matches `report_date`, `known_at_micros` is the earliest such
//! filing's UTC timestamp (`known_at_source = "filing_timestamp"`).
//! 2. Otherwise `known_at_micros = report_date 16:05:00 America/Toronto` via
//! [`crate::shorts::et_to_utc_micros`] (`known_at_source = "statutory_lag_estimate"`,
//! tradable T+1 open). This conservative fallback applies to `N/A`, `BTO`, and `AMC` rows
//! alike whenever no linked filing provides a verified timestamp—unlinked `BTO`/`AMC` rows
//! are never stamped earlier than `16:05 ET`.
//! [`crate::shorts::et_to_utc_micros`] (`known_at_source = "statutory_lag_estimate"`).
//! This conservative fallback is intended for T+1 open use (not same-day intraday cutoffs,
//! since a later same-date `8-K` / `News release` filing supersedes the fallback with its
//! actual filing timestamp). It applies to `N/A`, `BTO`, and `AMC` rows alike whenever no
//! linked filing provides a verified timestamp—unlinked `BTO`/`AMC` rows are never stamped
//! earlier than `16:05 ET`.
//! 3. Unlinked `report_date` values outside `[MIN_SUPPORTED_DATE, MAX_SUPPORTED_DATE]`
//! (`2008-01-01..=2030-12-31`) are skipped and counted (`earnings_skipped_out_of_range`).
//! 4. Post-hoc overwritten `consensusEps` is deliberately omitted from [`QmEarning`] and
@ -256,11 +258,90 @@ const SEDAR_SUBSTRING_RULES: &[(&str, FilingFormGroup)] = &[
("NEWS RELEASE", FilingFormGroup::NewsRelease),
];
/// Returns `true` iff `upper` contains `"AMENDED"` as a standalone ASCII token (not embedded in a
/// longer word such as `"UNAMENDED"`).
fn contains_standalone_amended(upper: &str) -> bool {
let bytes = upper.as_bytes();
let needle = "AMENDED";
let mut offset = 0;
while let Some(rel) = upper[offset..].find(needle) {
let start = offset + rel;
let end = start + needle.len();
let left_ok = start == 0 || !bytes[start - 1].is_ascii_alphanumeric();
let right_ok = end == bytes.len() || !bytes[end].is_ascii_alphanumeric();
if left_ok && right_ok {
return true;
}
offset = start + 1;
}
false
}
/// Strips leading/trailing amendment markers (`/A`, `(AMENDED)`, or standalone `AMENDED` with
/// adjacent whitespace/punctuation separators) from an uppercased form label so exact EDGAR codes
/// such as `"10-K (Amended)"` or `"Amended 10-Q"` retain their underlying form group.
fn strip_amendment_markers(mut s: &str) -> (&str, bool) {
let mut stripped_any = false;
loop {
let trimmed = s.trim();
let next = if let Some(prefix) = trimmed.strip_suffix("/A") {
Some(prefix.trim_end())
} else if let Some(prefix) = trimmed.strip_suffix("(AMENDED)") {
Some(prefix.trim_end_matches(|c: char| {
c.is_ascii_whitespace() || matches!(c, '-' | ',' | ':')
}))
} else if let Some(suffix) = trimmed.strip_prefix("(AMENDED)") {
Some(suffix.trim_start_matches(|c: char| {
c.is_ascii_whitespace() || matches!(c, '-' | ',' | ':')
}))
} else if let Some(prefix) = trimmed.strip_suffix("AMENDED") {
if prefix
.as_bytes()
.last()
.is_some_and(|&b| b.is_ascii_whitespace() || matches!(b, b'-' | b',' | b':'))
{
Some(prefix.trim_end_matches(|c: char| {
c.is_ascii_whitespace() || matches!(c, '-' | ',' | ':')
}))
} else {
None
}
} else if let Some(suffix) = trimmed.strip_prefix("AMENDED") {
if suffix
.as_bytes()
.first()
.is_some_and(|&b| b.is_ascii_whitespace() || matches!(b, b'-' | b',' | b':'))
{
Some(suffix.trim_start_matches(|c: char| {
c.is_ascii_whitespace() || matches!(c, '-' | ',' | ':')
}))
} else {
None
}
} else {
None
};
match next {
Some(rest) => {
stripped_any = true;
s = rest;
}
None => {
s = trimmed;
break;
}
}
}
(s, stripped_any)
}
/// Classifies a raw `qmFilings.form_type` into `(FilingFormGroup, is_amendment)`.
///
/// - Trims ASCII whitespace and compares case-insensitively.
/// - Detects `/A` suffixes (e.g. `10-K/A`, `8-K/A`, `40-F/A`) and `"amended"` markers while
/// mapping amendments to the same underlying [`FilingFormGroup`].
/// - Detects and strips `/A` suffixes (e.g. `10-K/A`, `8-K/A`, `40-F/A`) and standalone
/// `"amended"` markers (e.g. `10-K (Amended)`, `10-Q - Amended`, `Amended 40-F`) before exact
/// EDGAR matching so amendments retain their underlying [`FilingFormGroup`].
/// - Checks exact EDGAR codes first, then SEDAR+ substring rules in fixed priority order,
/// defaulting to [`FilingFormGroup::Other`].
pub fn classify_form_type(form_type: &str) -> (FilingFormGroup, bool) {
@ -270,11 +351,8 @@ pub fn classify_form_type(form_type: &str) -> (FilingFormGroup, bool) {
}
let upper = trimmed.to_ascii_uppercase();
let (base, has_slash_a) = match upper.strip_suffix("/A") {
Some(prefix) => (prefix.trim_end(), true),
None => (upper.as_str(), false),
};
let is_amendment = has_slash_a || upper.contains("AMENDED");
let (base, stripped_amendment) = strip_amendment_markers(&upper);
let is_amendment = stripped_amendment || contains_standalone_amended(base);
for &(code, group) in EXACT_EDGAR_FORM_RULES {
if base == code {
@ -728,7 +806,9 @@ where
.then_with(|| a.report_date.cmp(&b.report_date))
});
rows.reserve(norm_earnings.len());
// Reserve only the exact upper bound for earnings rows so appending a small earnings batch
// onto a multi-million-row filings vector does not double the vector's capacity.
rows.reserve_exact(norm_earnings.len());
let len = norm_earnings.len();
let mut read_idx = 0usize;
while read_idx < len {
@ -953,15 +1033,21 @@ mod tests {
let cases = [
("10-K", FilingFormGroup::Form10K, false, true, false),
("10-K/A", FilingFormGroup::Form10K, true, true, false),
("10-K (Amended)", FilingFormGroup::Form10K, true, true, false),
("10-K/A (Amended)", FilingFormGroup::Form10K, true, true, false),
(" 10-q ", FilingFormGroup::Form10Q, false, true, false),
("10-Q/A", FilingFormGroup::Form10Q, true, true, false),
("10-Q - Amended", FilingFormGroup::Form10Q, true, true, false),
("8-K", FilingFormGroup::Form8K, false, false, true),
("8-K/A", FilingFormGroup::Form8K, true, false, true),
("8-K (Amended)", FilingFormGroup::Form8K, true, false, true),
("4", FilingFormGroup::Form4, false, false, false),
("4/A", FilingFormGroup::Form4, true, false, false),
("144", FilingFormGroup::Form144, false, false, false),
("40-F", FilingFormGroup::Form40F, false, true, false),
("40-F/A", FilingFormGroup::Form40F, true, true, false),
("Amended 40-F", FilingFormGroup::Form40F, true, true, false),
("UNAMENDED", FilingFormGroup::Other, false, false, false),
(
"Interim financial statements/report",
FilingFormGroup::InterimFinancials,