T-96: M1.2n — tools/curate-etl: Zstd Parquet writer for corporate_actions/part.parquet (contract schema, atomic single-file publish) with round-trip tests and local benchmark (no new crates)

This commit is contained in:
Studio 2026-11-28 15:58:08 -05:00
parent 6215df190f
commit ac5506a4bf
3 changed files with 197 additions and 57 deletions

View file

@ -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,

View file

@ -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<String>,
},
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<T>(
row: &CorporateActionsParquetRow,
value: &Option<T>,
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),
)?;
}
}

View file

@ -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)
)
},
),