T-168: M2.2k — tools/curate-etl: zero-copy raw BSON decoder for MongoDB qmFinancials (bson_qm_financials.rs → FinancialObservation<&str>, caller-supplied dotted field paths, fail-closed fiscal-period/date handling) with tests (no new crates, no CLI/bench/stream changes, no live Mongo)

This commit is contained in:
Studio 2026-12-02 12:08:23 -05:00
parent 49098cc195
commit d116bbf7a2
3 changed files with 56 additions and 38 deletions

View file

@ -90,7 +90,8 @@ pub const DEFAULT_QM_FINANCIALS_REPORT_PERIOD_FIELD: &str = "reportPeriod";
/// Intentionally has no `Default`: live inner field names under `BalanceSheet`, `CashFlow`, and
/// `IncomeStatement` and the currency path are unverified against a live dump. Use
/// [`Self::with_verified_defaults`] to populate only the four repo-evidenced top-level defaults
/// while explicitly supplying `currency` and any desired numeric value paths.
/// while explicitly supplying `currency` and an `Option<&str>` choice for each of the six numeric
/// value paths.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QmFinancialsFieldNames<'a> {
pub key: &'a str,
@ -108,22 +109,31 @@ pub struct QmFinancialsFieldNames<'a> {
impl<'a> QmFinancialsFieldNames<'a> {
/// Constructs a configuration pre-populated with the four repo-evidenced defaults
/// (`qmSearchCode`, `periodEndDate`, `reportDate`, `reportPeriod`), the caller-supplied
/// `currency` path, and all six numeric value paths left unconfigured (`None`).
/// (`qmSearchCode`, `periodEndDate`, `reportDate`, `reportPeriod`) while requiring the caller
/// to explicitly supply the `currency` path and an `Option<&str>` choice (`Some(path)` or
/// intentional `None` opt-out) for each of the six numeric value fields.
#[inline]
pub const fn with_verified_defaults(currency: &'a str) -> Self {
pub const fn with_verified_defaults(
currency: &'a str,
revenue: Option<&'a str>,
net_income: Option<&'a str>,
operating_cash_flow: Option<&'a str>,
total_assets: Option<&'a str>,
total_equity: Option<&'a str>,
shares_outstanding: Option<&'a str>,
) -> Self {
Self {
key: DEFAULT_QM_FINANCIALS_KEY_FIELD,
period_end: DEFAULT_QM_FINANCIALS_PERIOD_END_FIELD,
report_date: DEFAULT_QM_FINANCIALS_REPORT_DATE_FIELD,
report_period: DEFAULT_QM_FINANCIALS_REPORT_PERIOD_FIELD,
currency,
revenue: None,
net_income: None,
operating_cash_flow: None,
total_assets: None,
total_equity: None,
shares_outstanding: None,
revenue,
net_income,
operating_cash_flow,
total_assets,
total_equity,
shares_outstanding,
}
}

View file

@ -4,7 +4,7 @@ use bson::raw::{cstr, RawArrayBuf, RawBson, RawDocument, RawDocumentBuf};
use bson::DateTime as BsonDateTime;
use super::*;
use crate::events::{parse_filing_timefiled, QmFiling, KNOWN_AT_SOURCE_FILING_TIMESTAMP};
use crate::events::{QmFiling, KNOWN_AT_SOURCE_FILING_TIMESTAMP};
use crate::fundamentals::{build_fundamentals_rows, StatutoryLagVenue};
use crate::shorts::epoch_days_to_ymd;
use crate::split::ymd_to_epoch_days;
@ -18,15 +18,15 @@ fn assert_conservation(stats: &BsonQmFinancialsDecodeStats) {
}
fn full_test_fields() -> QmFinancialsFieldNames<'static> {
QmFinancialsFieldNames {
revenue: Some("IncomeStatement.totalRevenue"),
net_income: Some("IncomeStatement.netIncome"),
operating_cash_flow: Some("CashFlow.operatingCashFlow"),
total_assets: Some("BalanceSheet.totalAssets"),
total_equity: Some("BalanceSheet.totalEquity"),
shares_outstanding: Some("BalanceSheet.commonSharesOutstanding"),
..QmFinancialsFieldNames::with_verified_defaults("currency")
}
QmFinancialsFieldNames::with_verified_defaults(
"currency",
Some("IncomeStatement.totalRevenue"),
Some("IncomeStatement.netIncome"),
Some("CashFlow.operatingCashFlow"),
Some("BalanceSheet.totalAssets"),
Some("BalanceSheet.totalEquity"),
Some("BalanceSheet.commonSharesOutstanding"),
)
}
const QUARTERLY_TOKENS: &[&str] = &["Q", "Q1", "Q2", "Q3", "Q4"];
@ -499,7 +499,8 @@ fn covers_each_skip_counter_and_inclusive_date_bounds() {
max_bound_null_rd.append(cstr!("reportDate"), RawBson::Null);
max_bound_null_rd.append(cstr!("currency"), "USD");
let minimal_fields = QmFinancialsFieldNames::with_verified_defaults("currency");
let minimal_fields =
QmFinancialsFieldNames::with_verified_defaults("currency", None, None, None, None, None, None);
let mut emitted = Vec::new();
let mut buf = Vec::new();
buf.extend_from_slice(min_bound_absent_rd.as_bytes());
@ -518,7 +519,8 @@ fn covers_each_skip_counter_and_inclusive_date_bounds() {
#[test]
fn fixed_fiscal_period_override_ignores_report_period_field() {
let fields = QmFinancialsFieldNames::with_verified_defaults("currency");
let fields =
QmFinancialsFieldNames::with_verified_defaults("currency", None, None, None, None, None, None);
let mut doc = RawDocumentBuf::new();
doc.append(cstr!("qmSearchCode"), "MSFT:US");
@ -542,16 +544,16 @@ fn fixed_fiscal_period_override_ignores_report_period_field() {
#[test]
fn numeric_null_counters_and_unconfigured_paths() {
// Configure 5 of 6 paths; leave `shares_outstanding` unconfigured (`None`).
let fields = QmFinancialsFieldNames {
revenue: Some("IncomeStatement.totalRevenue"), // missing sub-document
net_income: Some("BalanceSheet.netIncome"), // wrong type (boolean)
operating_cash_flow: Some("CashFlow.ocf"), // unparsable string
total_assets: Some("BalanceSheet.assets"), // non-finite double (NaN)
total_equity: Some("BalanceSheet.equity"), // non-finite string ("inf")
shares_outstanding: None, // unconfigured -> no null counter
..QmFinancialsFieldNames::with_verified_defaults("meta.currency")
};
// Configure 5 of 6 paths; leave `shares_outstanding` explicitly unconfigured (`None`).
let fields = QmFinancialsFieldNames::with_verified_defaults(
"meta.currency",
Some("IncomeStatement.totalRevenue"), // missing sub-document
Some("BalanceSheet.netIncome"), // wrong type (boolean)
Some("CashFlow.ocf"), // unparsable string
Some("BalanceSheet.assets"), // non-finite double (NaN)
Some("BalanceSheet.equity"), // non-finite string ("inf")
None, // unconfigured -> no null counter
);
let mut meta = RawDocumentBuf::new();
meta.append(cstr!("currency"), "CAD");
@ -597,10 +599,15 @@ fn numeric_null_counters_and_unconfigured_paths() {
doc_null.append(cstr!("currency"), "CAD");
doc_null.append(cstr!("IncomeStatement"), income_null);
let single_field = QmFinancialsFieldNames {
revenue: Some("IncomeStatement.totalRevenue"),
..QmFinancialsFieldNames::with_verified_defaults("currency")
};
let single_field = QmFinancialsFieldNames::with_verified_defaults(
"currency",
Some("IncomeStatement.totalRevenue"),
None,
None,
None,
None,
None,
);
let mut rows_null = Vec::new();
let stats_null =
decode_qm_financials_doc(doc_null.as_ref(), &single_field, token_period_cfg(), |obs| {
@ -769,7 +776,8 @@ fn decoded_observation_feeds_build_fundamentals_rows_with_filing_timestamp() {
assert_eq!(observations.len(), 1);
let timefiled = "2024-08-02T16:30:15 EDT";
let expected_micros = parse_filing_timefiled(timefiled).unwrap().utc_micros;
// 2024-08-02T16:30:15 EDT == 2024-08-02T20:30:15Z (1,722,630,615 seconds since Unix epoch).
let expected_micros: i64 = 1_722_630_615_000_000;
let filings = vec![QmFiling {
key: "AAPL:US",