From ac5506a4bfd5093918cdd8e852ea09bf48891ce9 Mon Sep 17 00:00:00 2001 From: Studio Date: Sat, 28 Nov 2026 15:58:08 -0500 Subject: [PATCH] =?UTF-8?q?T-96:=20M1.2n=20=E2=80=94=20`tools/curate-etl`:?= =?UTF-8?q?=20Zstd=20Parquet=20writer=20for=20`corporate=5Factions/part.pa?= =?UTF-8?q?rquet`=20(contract=20schema,=20atomic=20single-file=20publish)?= =?UTF-8?q?=20with=20round-trip=20tests=20and=20local=20benchmark=20(no=20?= =?UTF-8?q?new=20crates)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/curate-etl/src/lib.rs | 3 +- .../src/parquet_corporate_actions.rs | 102 +++++++++--- .../src/parquet_corporate_actions_tests.rs | 149 +++++++++++++----- 3 files changed, 197 insertions(+), 57 deletions(-) diff --git a/tools/curate-etl/src/lib.rs b/tools/curate-etl/src/lib.rs index 963fd60d..4b7ca850 100644 --- a/tools/curate-etl/src/lib.rs +++ b/tools/curate-etl/src/lib.rs @@ -54,7 +54,8 @@ pub use parquet_bars::{ }; pub use parquet_corporate_actions::{ corporate_actions_parquet_schema, write_corporate_actions_dataset, CorporateActionsParquetRow, - CorporateActionsWriteOptions, CorporateActionsWriteStats, ParquetCorporateActionsError, + CorporateActionsWriteOptions, CorporateActionsWriteStats, ForbiddenFieldValue, + ParquetCorporateActionsError, }; pub use parquet_events::{ events_parquet_schema, write_events_dataset, EventsParquetRow, EventsWriteOptions, diff --git a/tools/curate-etl/src/parquet_corporate_actions.rs b/tools/curate-etl/src/parquet_corporate_actions.rs index c1905b03..768e02d1 100644 --- a/tools/curate-etl/src/parquet_corporate_actions.rs +++ b/tools/curate-etl/src/parquet_corporate_actions.rs @@ -106,6 +106,24 @@ pub struct CorporateActionsWriteStats { pub bytes_written: u64, } +/// Typed offending value carried by [`ParquetCorporateActionsError::ForbiddenFieldForKind`]. +#[derive(Debug, Clone, PartialEq)] +pub enum ForbiddenFieldValue { + Float(f64), + Int(i32), + String(String), +} + +impl fmt::Display for ForbiddenFieldValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Float(v) => write!(f, "{v}"), + Self::Int(v) => write!(f, "{v}"), + Self::String(v) => write!(f, "{v:?}"), + } + } +} + /// Typed error returned by [`write_corporate_actions_dataset`]. #[derive(Debug)] pub enum ParquetCorporateActionsError { @@ -149,6 +167,7 @@ pub enum ParquetCorporateActionsError { InvalidCurrency { listing_id: i32, ex_date: i32, + value: Option, }, InvalidSuccessorListingId { listing_id: i32, @@ -165,6 +184,7 @@ pub enum ParquetCorporateActionsError { ex_date: i32, kind: CorporateActionKind, field: &'static str, + value: ForbiddenFieldValue, }, InconsistentKnownAtSource { listing_id: i32, @@ -251,9 +271,10 @@ impl fmt::Display for ParquetCorporateActionsError { Self::InvalidCurrency { listing_id, ex_date, + value, } => write!( f, - "invalid dividend currency for ({listing_id}, {ex_date}): must be Some(non-empty)" + "invalid dividend currency {value:?} for ({listing_id}, {ex_date}): must be Some(non-empty)" ), Self::InvalidSuccessorListingId { listing_id, @@ -276,9 +297,10 @@ impl fmt::Display for ParquetCorporateActionsError { ex_date, kind, field, + value, } => write!( f, - "field '{field}' must be None for kind '{kind}' on ({listing_id}, {ex_date})" + "field '{field}' (value {value}) must be None for kind '{kind}' on ({listing_id}, {ex_date})" ), Self::InconsistentKnownAtSource { listing_id, @@ -428,13 +450,15 @@ fn check_forbidden_none( row: &CorporateActionsParquetRow, value: &Option, field: &'static str, + to_value: impl FnOnce(&T) -> ForbiddenFieldValue, ) -> Result<(), ParquetCorporateActionsError> { - if value.is_some() { + if let Some(v) = value { return Err(ParquetCorporateActionsError::ForbiddenFieldForKind { listing_id: row.listing_id, ex_date: row.ex_date, kind: row.kind, field, + value: to_value(v), }); } Ok(()) @@ -479,10 +503,21 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate }); } } - check_forbidden_none(row, &row.cash_amount, "cash_amount")?; - check_forbidden_none(row, &row.currency, "currency")?; - check_forbidden_none(row, &row.successor_listing_id, "successor_listing_id")?; - check_forbidden_none(row, &row.terminal_return, "terminal_return")?; + check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| { + ForbiddenFieldValue::Float(v) + })?; + check_forbidden_none(row, &row.currency, "currency", |v| { + ForbiddenFieldValue::String(v.as_ref().to_owned()) + })?; + check_forbidden_none( + row, + &row.successor_listing_id, + "successor_listing_id", + |&v| ForbiddenFieldValue::Int(v), + )?; + check_forbidden_none(row, &row.terminal_return, "terminal_return", |&v| { + ForbiddenFieldValue::Float(v) + })?; } CorporateActionKind::Dividend => { match row.cash_amount { @@ -497,16 +532,26 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate } match row.currency.as_deref() { Some(curr) if !curr.trim().is_empty() => {} - _ => { + other => { return Err(ParquetCorporateActionsError::InvalidCurrency { listing_id: row.listing_id, ex_date: row.ex_date, + value: other.map(str::to_owned), }); } } - check_forbidden_none(row, &row.ratio, "ratio")?; - check_forbidden_none(row, &row.successor_listing_id, "successor_listing_id")?; - check_forbidden_none(row, &row.terminal_return, "terminal_return")?; + check_forbidden_none(row, &row.ratio, "ratio", |&v| { + ForbiddenFieldValue::Float(v) + })?; + check_forbidden_none( + row, + &row.successor_listing_id, + "successor_listing_id", + |&v| ForbiddenFieldValue::Int(v), + )?; + check_forbidden_none(row, &row.terminal_return, "terminal_return", |&v| { + ForbiddenFieldValue::Float(v) + })?; } CorporateActionKind::OtcDelistTransfer => { match row.successor_listing_id { @@ -519,10 +564,18 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate }); } } - check_forbidden_none(row, &row.ratio, "ratio")?; - check_forbidden_none(row, &row.cash_amount, "cash_amount")?; - check_forbidden_none(row, &row.currency, "currency")?; - check_forbidden_none(row, &row.terminal_return, "terminal_return")?; + check_forbidden_none(row, &row.ratio, "ratio", |&v| { + ForbiddenFieldValue::Float(v) + })?; + check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| { + ForbiddenFieldValue::Float(v) + })?; + check_forbidden_none(row, &row.currency, "currency", |v| { + ForbiddenFieldValue::String(v.as_ref().to_owned()) + })?; + check_forbidden_none(row, &row.terminal_return, "terminal_return", |&v| { + ForbiddenFieldValue::Float(v) + })?; } CorporateActionKind::Delisting => { match row.terminal_return { @@ -535,10 +588,21 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate }); } } - check_forbidden_none(row, &row.ratio, "ratio")?; - check_forbidden_none(row, &row.cash_amount, "cash_amount")?; - check_forbidden_none(row, &row.currency, "currency")?; - check_forbidden_none(row, &row.successor_listing_id, "successor_listing_id")?; + check_forbidden_none(row, &row.ratio, "ratio", |&v| { + ForbiddenFieldValue::Float(v) + })?; + check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| { + ForbiddenFieldValue::Float(v) + })?; + check_forbidden_none(row, &row.currency, "currency", |v| { + ForbiddenFieldValue::String(v.as_ref().to_owned()) + })?; + check_forbidden_none( + row, + &row.successor_listing_id, + "successor_listing_id", + |&v| ForbiddenFieldValue::Int(v), + )?; } } diff --git a/tools/curate-etl/src/parquet_corporate_actions_tests.rs b/tools/curate-etl/src/parquet_corporate_actions_tests.rs index d8bc0b27..cad2752f 100644 --- a/tools/curate-etl/src/parquet_corporate_actions_tests.rs +++ b/tools/curate-etl/src/parquet_corporate_actions_tests.rs @@ -15,7 +15,8 @@ use super::tests::{ }; use super::{ write_corporate_actions_dataset, write_corporate_actions_dataset_with_hooks, - CorporateActionsParquetRow, CorporateActionsWriteOptions, ParquetCorporateActionsError, + CorporateActionsParquetRow, CorporateActionsWriteOptions, ForbiddenFieldValue, + ParquetCorporateActionsError, }; use crate::corporate_actions::{CorporateActionKind, KnownAtSource}; use crate::parquet_bars::{MAX_PARQUET_YEAR, MIN_PARQUET_YEAR}; @@ -497,17 +498,61 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { currency: None, ..make_div_row(1, d1, None, 0.25, "USD") }], - |e| matches!(e, ParquetCorporateActionsError::InvalidCurrency { .. }), + |e| { + matches!( + e, + ParquetCorporateActionsError::InvalidCurrency { + listing_id: 1, + ex_date, + value: None, + } if *ex_date == day(2024, 3, 15) + ) + }, ), ( "div-currency-empty", vec![make_div_row(1, d1, None, 0.25, "")], - |e| matches!(e, ParquetCorporateActionsError::InvalidCurrency { .. }), + |e| { + matches!( + e, + ParquetCorporateActionsError::InvalidCurrency { + listing_id: 1, + ex_date, + value: Some(v), + } if *ex_date == day(2024, 3, 15) && v.is_empty() + ) + }, ), ( "div-currency-whitespace", vec![make_div_row(1, d1, None, 0.25, " \t ")], - |e| matches!(e, ParquetCorporateActionsError::InvalidCurrency { .. }), + |e| { + matches!( + e, + ParquetCorporateActionsError::InvalidCurrency { + listing_id: 1, + ex_date, + value: Some(v), + } if *ex_date == day(2024, 3, 15) && v == " \t " + ) + }, + ), + ( + "div-same-key-second-whitespace-currency", + vec![ + make_div_row(1, d1, None, 0.25, "USD"), + make_div_row(1, d1, None, 0.50, " "), + ], + |e| { + matches!( + e, + ParquetCorporateActionsError::InvalidCurrency { + listing_id: 1, + ex_date, + value: Some(v), + } if *ex_date == day(2024, 3, 15) && v == " " + ) && e.to_string().contains("\" \"") + }, ), // OTC delist transfer successor_listing_id violations ( @@ -585,11 +630,13 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Split, field: "cash_amount", - .. - } - ) + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == 0.25 + ) && e.to_string().contains("0.25") }, ), ( @@ -602,11 +649,13 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Split, field: "currency", - .. - } - ) + value: ForbiddenFieldValue::String(v), + } if *ex_date == day(2024, 3, 15) && v == "USD" + ) && e.to_string().contains("\"USD\"") }, ), ( @@ -619,11 +668,13 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Split, field: "successor_listing_id", - .. - } - ) + value: ForbiddenFieldValue::Int(2), + } if *ex_date == day(2024, 3, 15) + ) && e.to_string().contains("value 2") }, ), ( @@ -636,10 +687,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Split, field: "terminal_return", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == -0.3 ) }, ), @@ -654,10 +707,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Dividend, field: "ratio", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == 2.0 ) }, ), @@ -671,10 +726,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Dividend, field: "successor_listing_id", - .. - } + value: ForbiddenFieldValue::Int(2), + } if *ex_date == day(2024, 3, 15) ) }, ), @@ -688,10 +745,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Dividend, field: "terminal_return", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == -0.3 ) }, ), @@ -706,10 +765,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::OtcDelistTransfer, field: "ratio", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == 2.0 ) }, ), @@ -723,10 +784,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::OtcDelistTransfer, field: "cash_amount", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == 0.25 ) }, ), @@ -740,10 +803,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::OtcDelistTransfer, field: "currency", - .. - } + value: ForbiddenFieldValue::String(v), + } if *ex_date == day(2024, 3, 15) && v == "USD" ) }, ), @@ -757,10 +822,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::OtcDelistTransfer, field: "terminal_return", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == -0.3 ) }, ), @@ -775,10 +842,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Delisting, field: "ratio", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == 2.0 ) }, ), @@ -792,10 +861,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Delisting, field: "cash_amount", - .. - } + value: ForbiddenFieldValue::Float(v), + } if *ex_date == day(2024, 3, 15) && *v == 0.25 ) }, ), @@ -809,10 +880,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Delisting, field: "currency", - .. - } + value: ForbiddenFieldValue::String(v), + } if *ex_date == day(2024, 3, 15) && v == "USD" ) }, ), @@ -826,10 +899,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() { matches!( e, ParquetCorporateActionsError::ForbiddenFieldForKind { + listing_id: 1, + ex_date, kind: CorporateActionKind::Delisting, field: "successor_listing_id", - .. - } + value: ForbiddenFieldValue::Int(2), + } if *ex_date == day(2024, 3, 15) ) }, ),