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:
parent
ac5506a4bf
commit
a8e63285a1
2 changed files with 429 additions and 42 deletions
|
|
@ -25,7 +25,9 @@ use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use parquet::basic::{Compression, LogicalType, Repetition, TimeUnit, Type as PhysicalType, ZstdLevel};
|
use parquet::basic::{
|
||||||
|
Compression, LogicalType, Repetition, TimeUnit, Type as PhysicalType, ZstdLevel,
|
||||||
|
};
|
||||||
use parquet::data_type::ByteArray;
|
use parquet::data_type::ByteArray;
|
||||||
use parquet::errors::ParquetError;
|
use parquet::errors::ParquetError;
|
||||||
use parquet::file::properties::{WriterProperties, WriterPropertiesPtr};
|
use parquet::file::properties::{WriterProperties, WriterPropertiesPtr};
|
||||||
|
|
@ -540,9 +542,7 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
check_forbidden_none(row, &row.ratio, "ratio", |&v| {
|
check_forbidden_none(row, &row.ratio, "ratio", |&v| ForbiddenFieldValue::Float(v))?;
|
||||||
ForbiddenFieldValue::Float(v)
|
|
||||||
})?;
|
|
||||||
check_forbidden_none(
|
check_forbidden_none(
|
||||||
row,
|
row,
|
||||||
&row.successor_listing_id,
|
&row.successor_listing_id,
|
||||||
|
|
@ -564,9 +564,7 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
check_forbidden_none(row, &row.ratio, "ratio", |&v| {
|
check_forbidden_none(row, &row.ratio, "ratio", |&v| ForbiddenFieldValue::Float(v))?;
|
||||||
ForbiddenFieldValue::Float(v)
|
|
||||||
})?;
|
|
||||||
check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| {
|
check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| {
|
||||||
ForbiddenFieldValue::Float(v)
|
ForbiddenFieldValue::Float(v)
|
||||||
})?;
|
})?;
|
||||||
|
|
@ -588,9 +586,7 @@ fn validate_row(row: &CorporateActionsParquetRow) -> Result<(), ParquetCorporate
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
check_forbidden_none(row, &row.ratio, "ratio", |&v| {
|
check_forbidden_none(row, &row.ratio, "ratio", |&v| ForbiddenFieldValue::Float(v))?;
|
||||||
ForbiddenFieldValue::Float(v)
|
|
||||||
})?;
|
|
||||||
check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| {
|
check_forbidden_none(row, &row.cash_amount, "cash_amount", |&v| {
|
||||||
ForbiddenFieldValue::Float(v)
|
ForbiddenFieldValue::Float(v)
|
||||||
})?;
|
})?;
|
||||||
|
|
@ -1098,11 +1094,13 @@ where
|
||||||
// `synced.final_path` was never moved during backup creation, so it remains intact;
|
// `synced.final_path` was never moved during backup creation, so it remains intact;
|
||||||
// remove the backup created for this run. `staging_guard` unlinks our staging file on drop.
|
// remove the backup created for this run. `staging_guard` unlinks our staging file on drop.
|
||||||
let backup_remove_failure = backup_path.and_then(|bak| {
|
let backup_remove_failure = backup_path.and_then(|bak| {
|
||||||
fs::remove_file(&bak).err().map(|remove_err| RollbackFailure {
|
fs::remove_file(&bak)
|
||||||
final_path: synced.final_path.clone(),
|
.err()
|
||||||
backup_path: Some(bak),
|
.map(|remove_err| RollbackFailure {
|
||||||
source: remove_err,
|
final_path: synced.final_path.clone(),
|
||||||
})
|
backup_path: Some(bak),
|
||||||
|
source: remove_err,
|
||||||
|
})
|
||||||
});
|
});
|
||||||
let cause = ParquetCorporateActionsError::Io {
|
let cause = ParquetCorporateActionsError::Io {
|
||||||
path: synced.final_path,
|
path: synced.final_path,
|
||||||
|
|
|
||||||
|
|
@ -41,12 +41,9 @@ fn single_row_and_row_group_boundary_counts_with_pre_1970_split() {
|
||||||
// 1 row: pre-1970 split with null declared_date
|
// 1 row: pre-1970 split with null declared_date
|
||||||
let out_1 = tmp.path().join("one");
|
let out_1 = tmp.path().join("one");
|
||||||
let single = vec![make_split_row(7, day(1969, 12, 31), None, 2.0)];
|
let single = vec![make_split_row(7, day(1969, 12, 31), None, 2.0)];
|
||||||
let stats_1 = write_corporate_actions_dataset(
|
let stats_1 =
|
||||||
&out_1,
|
write_corporate_actions_dataset(&out_1, &single, CorporateActionsWriteOptions::default())
|
||||||
&single,
|
.unwrap();
|
||||||
CorporateActionsWriteOptions::default(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(stats_1.rows_written, 1);
|
assert_eq!(stats_1.rows_written, 1);
|
||||||
assert_eq!(stats_1.files_written, 1);
|
assert_eq!(stats_1.files_written, 1);
|
||||||
assert_eq!(stats_1.row_groups_written, 1);
|
assert_eq!(stats_1.row_groups_written, 1);
|
||||||
|
|
@ -431,7 +428,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() {
|
||||||
ratio: None,
|
ratio: None,
|
||||||
..make_split_row(1, d1, None, 2.0)
|
..make_split_row(1, d1, None, 2.0)
|
||||||
}],
|
}],
|
||||||
|e| matches!(e, ParquetCorporateActionsError::InvalidRatio { value: None, .. }),
|
|e| {
|
||||||
|
matches!(
|
||||||
|
e,
|
||||||
|
ParquetCorporateActionsError::InvalidRatio { value: None, .. }
|
||||||
|
)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"split-ratio-zero",
|
"split-ratio-zero",
|
||||||
|
|
@ -465,7 +467,12 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() {
|
||||||
cash_amount: None,
|
cash_amount: None,
|
||||||
..make_div_row(1, d1, None, 0.25, "USD")
|
..make_div_row(1, d1, None, 0.25, "USD")
|
||||||
}],
|
}],
|
||||||
|e| matches!(e, ParquetCorporateActionsError::InvalidCashAmount { value: None, .. }),
|
|e| {
|
||||||
|
matches!(
|
||||||
|
e,
|
||||||
|
ParquetCorporateActionsError::InvalidCashAmount { value: None, .. }
|
||||||
|
)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"div-cash-zero",
|
"div-cash-zero",
|
||||||
|
|
@ -602,22 +609,42 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() {
|
||||||
(
|
(
|
||||||
"delist-return-nan",
|
"delist-return-nan",
|
||||||
vec![make_delisting_row(1, d1, f64::NAN)],
|
vec![make_delisting_row(1, d1, f64::NAN)],
|
||||||
|e| matches!(e, ParquetCorporateActionsError::InvalidTerminalReturn { .. }),
|
|e| {
|
||||||
|
matches!(
|
||||||
|
e,
|
||||||
|
ParquetCorporateActionsError::InvalidTerminalReturn { .. }
|
||||||
|
)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"delist-return-inf",
|
"delist-return-inf",
|
||||||
vec![make_delisting_row(1, d1, f64::NEG_INFINITY)],
|
vec![make_delisting_row(1, d1, f64::NEG_INFINITY)],
|
||||||
|e| matches!(e, ParquetCorporateActionsError::InvalidTerminalReturn { .. }),
|
|e| {
|
||||||
|
matches!(
|
||||||
|
e,
|
||||||
|
ParquetCorporateActionsError::InvalidTerminalReturn { .. }
|
||||||
|
)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"delist-return-below-minus-one",
|
"delist-return-below-minus-one",
|
||||||
vec![make_delisting_row(1, d1, -1.0001)],
|
vec![make_delisting_row(1, d1, -1.0001)],
|
||||||
|e| matches!(e, ParquetCorporateActionsError::InvalidTerminalReturn { .. }),
|
|e| {
|
||||||
|
matches!(
|
||||||
|
e,
|
||||||
|
ParquetCorporateActionsError::InvalidTerminalReturn { .. }
|
||||||
|
)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"delist-return-above-zero",
|
"delist-return-above-zero",
|
||||||
vec![make_delisting_row(1, d1, 0.0001)],
|
vec![make_delisting_row(1, d1, 0.0001)],
|
||||||
|e| matches!(e, ParquetCorporateActionsError::InvalidTerminalReturn { .. }),
|
|e| {
|
||||||
|
matches!(
|
||||||
|
e,
|
||||||
|
ParquetCorporateActionsError::InvalidTerminalReturn { .. }
|
||||||
|
)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
// Forbidden fields for Split
|
// Forbidden fields for Split
|
||||||
(
|
(
|
||||||
|
|
@ -1055,12 +1082,9 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() {
|
||||||
|
|
||||||
for (label, rows, check) in bad_cases {
|
for (label, rows, check) in bad_cases {
|
||||||
let fresh = tmp.path().join(format!("fresh-{label}"));
|
let fresh = tmp.path().join(format!("fresh-{label}"));
|
||||||
let err = write_corporate_actions_dataset(
|
let err =
|
||||||
&fresh,
|
write_corporate_actions_dataset(&fresh, &rows, CorporateActionsWriteOptions::default())
|
||||||
&rows,
|
.unwrap_err();
|
||||||
CorporateActionsWriteOptions::default(),
|
|
||||||
)
|
|
||||||
.unwrap_err();
|
|
||||||
assert!(check(&err), "fresh case '{label}' got unexpected {err:?}");
|
assert!(check(&err), "fresh case '{label}' got unexpected {err:?}");
|
||||||
assert!(
|
assert!(
|
||||||
!fresh.join("corporate_actions/part.parquet").exists(),
|
!fresh.join("corporate_actions/part.parquet").exists(),
|
||||||
|
|
@ -1076,7 +1100,10 @@ fn validation_errors_return_typed_variants_and_preserve_existing_state() {
|
||||||
CorporateActionsWriteOptions::default(),
|
CorporateActionsWriteOptions::default(),
|
||||||
)
|
)
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
assert!(check(&err_s), "seeded case '{label}' got unexpected {err_s:?}");
|
assert!(
|
||||||
|
check(&err_s),
|
||||||
|
"seeded case '{label}' got unexpected {err_s:?}"
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
fs::read(&part).unwrap(),
|
fs::read(&part).unwrap(),
|
||||||
before,
|
before,
|
||||||
|
|
@ -1231,17 +1258,17 @@ fn rename_failure_over_directory_blocker_cleans_up_staging_and_unowned_tmp_is_pr
|
||||||
fs::write(&unowned_tmp, b"keep-me").unwrap();
|
fs::write(&unowned_tmp, b"keep-me").unwrap();
|
||||||
|
|
||||||
let rows = vec![make_div_row(1, day(2024, 3, 15), None, 0.25, "USD")];
|
let rows = vec![make_div_row(1, day(2024, 3, 15), None, 0.25, "USD")];
|
||||||
let err = write_corporate_actions_dataset(
|
let err =
|
||||||
tmp.path(),
|
write_corporate_actions_dataset(tmp.path(), &rows, CorporateActionsWriteOptions::default())
|
||||||
&rows,
|
.unwrap_err();
|
||||||
CorporateActionsWriteOptions::default(),
|
|
||||||
)
|
|
||||||
.unwrap_err();
|
|
||||||
assert!(
|
assert!(
|
||||||
matches!(err, ParquetCorporateActionsError::Io { .. }),
|
matches!(err, ParquetCorporateActionsError::Io { .. }),
|
||||||
"got {err:?}"
|
"got {err:?}"
|
||||||
);
|
);
|
||||||
assert!(blocking_dir.is_dir(), "directory blocker must remain intact");
|
assert!(
|
||||||
|
blocking_dir.is_dir(),
|
||||||
|
"directory blocker must remain intact"
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
fs::read(&unowned_tmp).unwrap(),
|
fs::read(&unowned_tmp).unwrap(),
|
||||||
b"keep-me",
|
b"keep-me",
|
||||||
|
|
@ -1285,7 +1312,8 @@ fn concurrent_in_process_writers_serialize_cleanly() {
|
||||||
thread::sleep(Duration::from_millis(5));
|
thread::sleep(Duration::from_millis(5));
|
||||||
let out_2 = Arc::clone(&out);
|
let out_2 = Arc::clone(&out);
|
||||||
let handle_2 = thread::spawn(move || {
|
let handle_2 = thread::spawn(move || {
|
||||||
let iter = (0..2_i32).map(move |i| make_split_row(20, day(2024, 2, 15) + i * 30, None, 2.0));
|
let iter =
|
||||||
|
(0..2_i32).map(move |i| make_split_row(20, day(2024, 2, 15) + i * 30, None, 2.0));
|
||||||
write_corporate_actions_dataset(
|
write_corporate_actions_dataset(
|
||||||
out_2.as_path(),
|
out_2.as_path(),
|
||||||
iter,
|
iter,
|
||||||
|
|
@ -1447,3 +1475,364 @@ fn backup_unlink_and_restore_failure_reports_rollback_failed() {
|
||||||
}
|
}
|
||||||
assert_no_transient_files(&tmp.path().join("corporate_actions"));
|
assert_no_transient_files(&tmp.path().join("corporate_actions"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boundary_years_min_and_max_are_accepted() {
|
||||||
|
let tmp = TempDirGuard::new("boundary-years");
|
||||||
|
let min_day = day(MIN_PARQUET_YEAR, 1, 1);
|
||||||
|
let max_day = day(MAX_PARQUET_YEAR, 12, 31);
|
||||||
|
let rows = vec![
|
||||||
|
make_split_row(1, min_day, Some(min_day), 2.0),
|
||||||
|
make_div_row(1, max_day, Some(max_day), 0.25, "USD"),
|
||||||
|
];
|
||||||
|
let stats =
|
||||||
|
write_corporate_actions_dataset(tmp.path(), &rows, CorporateActionsWriteOptions::default())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stats.rows_written, 2);
|
||||||
|
let actual = read_parquet_rows(&tmp.path().join("corporate_actions/part.parquet"));
|
||||||
|
assert_eq!(actual.len(), 2);
|
||||||
|
for (a, e) in actual.iter().zip(&rows) {
|
||||||
|
assert_row_bitwise_eq(a, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extreme_values_and_non_ascii_currency_round_trip_bitwise() {
|
||||||
|
let tmp = TempDirGuard::new("extreme-values");
|
||||||
|
let d1 = day(2024, 3, 15);
|
||||||
|
let d2 = day(2024, 3, 16);
|
||||||
|
let rows = vec![
|
||||||
|
CorporateActionsParquetRow {
|
||||||
|
listing_id: i32::MIN,
|
||||||
|
ex_date: d1,
|
||||||
|
declared_date: None,
|
||||||
|
kind: CorporateActionKind::Split,
|
||||||
|
ratio: Some(f64::from_bits(1)), // smallest positive subnormal
|
||||||
|
cash_amount: None,
|
||||||
|
currency: None,
|
||||||
|
successor_listing_id: None,
|
||||||
|
terminal_return: None,
|
||||||
|
known_at_micros: i64::MIN,
|
||||||
|
known_at_source: KnownAtSource::ExDateFallback,
|
||||||
|
},
|
||||||
|
CorporateActionsParquetRow {
|
||||||
|
listing_id: i32::MIN,
|
||||||
|
ex_date: d2,
|
||||||
|
declared_date: Some(d1),
|
||||||
|
kind: CorporateActionKind::Dividend,
|
||||||
|
ratio: None,
|
||||||
|
cash_amount: Some(f64::MAX),
|
||||||
|
currency: Some(Cow::Borrowed("€¥£")),
|
||||||
|
successor_listing_id: None,
|
||||||
|
terminal_return: None,
|
||||||
|
known_at_micros: i64::MAX,
|
||||||
|
known_at_source: KnownAtSource::DeclaredDate,
|
||||||
|
},
|
||||||
|
CorporateActionsParquetRow {
|
||||||
|
listing_id: i32::MAX,
|
||||||
|
ex_date: d1,
|
||||||
|
declared_date: None,
|
||||||
|
kind: CorporateActionKind::OtcDelistTransfer,
|
||||||
|
ratio: None,
|
||||||
|
cash_amount: None,
|
||||||
|
currency: None,
|
||||||
|
successor_listing_id: Some(i32::MIN),
|
||||||
|
terminal_return: None,
|
||||||
|
known_at_micros: 0,
|
||||||
|
known_at_source: KnownAtSource::DelistDate,
|
||||||
|
},
|
||||||
|
CorporateActionsParquetRow {
|
||||||
|
listing_id: i32::MAX,
|
||||||
|
ex_date: d2,
|
||||||
|
declared_date: None,
|
||||||
|
kind: CorporateActionKind::Delisting,
|
||||||
|
ratio: None,
|
||||||
|
cash_amount: None,
|
||||||
|
currency: None,
|
||||||
|
successor_listing_id: None,
|
||||||
|
terminal_return: Some(-0.0),
|
||||||
|
known_at_micros: i64::MAX,
|
||||||
|
known_at_source: KnownAtSource::DelistDate,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let stats =
|
||||||
|
write_corporate_actions_dataset(tmp.path(), &rows, CorporateActionsWriteOptions::default())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stats.rows_written, 4);
|
||||||
|
let actual = read_parquet_rows(&tmp.path().join("corporate_actions/part.parquet"));
|
||||||
|
assert_eq!(actual.len(), rows.len());
|
||||||
|
for (a, e) in actual.iter().zip(&rows) {
|
||||||
|
assert_row_bitwise_eq(a, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn duplicate_key_rows_preserve_input_order_across_row_groups() {
|
||||||
|
let tmp = TempDirGuard::new("dup-key-order");
|
||||||
|
let d = day(2024, 6, 15);
|
||||||
|
let rows = vec![
|
||||||
|
make_div_row(42, d, Some(d - 10), 0.10, "USD"),
|
||||||
|
make_div_row(42, d, None, 0.20, "CAD"),
|
||||||
|
make_div_row(42, d, Some(d - 5), 0.30, "USD"),
|
||||||
|
make_div_row(42, d, None, 0.40, "CAD"),
|
||||||
|
make_div_row(42, d, Some(d - 1), 0.50, "EUR"),
|
||||||
|
];
|
||||||
|
let opts = CorporateActionsWriteOptions {
|
||||||
|
row_group_rows: 2,
|
||||||
|
..CorporateActionsWriteOptions::default()
|
||||||
|
};
|
||||||
|
let stats = write_corporate_actions_dataset(tmp.path(), &rows, opts).unwrap();
|
||||||
|
assert_eq!(stats.rows_written, 5);
|
||||||
|
assert_eq!(stats.row_groups_written, 3);
|
||||||
|
let actual = read_parquet_rows(&tmp.path().join("corporate_actions/part.parquet"));
|
||||||
|
assert_eq!(actual.len(), rows.len());
|
||||||
|
for (a, e) in actual.iter().zip(&rows) {
|
||||||
|
assert_row_bitwise_eq(a, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn late_declared_date_with_ex_date_fallback_is_accepted() {
|
||||||
|
let tmp = TempDirGuard::new("late-declared");
|
||||||
|
let ex = day(2024, 5, 1);
|
||||||
|
let late_decl = day(2024, 5, 10);
|
||||||
|
let rows = vec![
|
||||||
|
CorporateActionsParquetRow {
|
||||||
|
listing_id: 7,
|
||||||
|
ex_date: ex,
|
||||||
|
declared_date: Some(late_decl),
|
||||||
|
kind: CorporateActionKind::Dividend,
|
||||||
|
ratio: None,
|
||||||
|
cash_amount: Some(0.55),
|
||||||
|
currency: Some(Cow::Borrowed("USD")),
|
||||||
|
successor_listing_id: None,
|
||||||
|
terminal_return: None,
|
||||||
|
known_at_micros: known_at_from_day(ex),
|
||||||
|
known_at_source: KnownAtSource::ExDateFallback,
|
||||||
|
},
|
||||||
|
CorporateActionsParquetRow {
|
||||||
|
listing_id: 8,
|
||||||
|
ex_date: ex,
|
||||||
|
declared_date: Some(late_decl),
|
||||||
|
kind: CorporateActionKind::Split,
|
||||||
|
ratio: Some(3.0),
|
||||||
|
cash_amount: None,
|
||||||
|
currency: None,
|
||||||
|
successor_listing_id: None,
|
||||||
|
terminal_return: None,
|
||||||
|
known_at_micros: known_at_from_day(ex),
|
||||||
|
known_at_source: KnownAtSource::ExDateFallback,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let stats =
|
||||||
|
write_corporate_actions_dataset(tmp.path(), &rows, CorporateActionsWriteOptions::default())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stats.rows_written, 2);
|
||||||
|
let actual = read_parquet_rows(&tmp.path().join("corporate_actions/part.parquet"));
|
||||||
|
assert_eq!(actual.len(), 2);
|
||||||
|
for (a, e) in actual.iter().zip(&rows) {
|
||||||
|
assert_row_bitwise_eq(a, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unsorted_input_after_flushed_row_group_reports_both_keys_and_preserves_existing_file() {
|
||||||
|
let tmp = TempDirGuard::new("unsorted-after-rg");
|
||||||
|
let (part, before) = seed_existing(tmp.path());
|
||||||
|
let d1 = day(2024, 1, 10);
|
||||||
|
let d2 = day(2024, 2, 10);
|
||||||
|
let rows = vec![
|
||||||
|
make_split_row(10, d1, None, 2.0),
|
||||||
|
make_div_row(10, d2, None, 0.25, "USD"),
|
||||||
|
// Flushed row group 1 above (row_group_rows = 2); now regress ex_date.
|
||||||
|
make_split_row(10, d1, None, 1.5),
|
||||||
|
];
|
||||||
|
let opts = CorporateActionsWriteOptions {
|
||||||
|
row_group_rows: 2,
|
||||||
|
..CorporateActionsWriteOptions::default()
|
||||||
|
};
|
||||||
|
let err = write_corporate_actions_dataset(tmp.path(), &rows, opts).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
matches!(
|
||||||
|
err,
|
||||||
|
ParquetCorporateActionsError::UnsortedInput {
|
||||||
|
prev_listing_id: 10,
|
||||||
|
prev_ex_date,
|
||||||
|
prev_kind: CorporateActionKind::Dividend,
|
||||||
|
listing_id: 10,
|
||||||
|
ex_date,
|
||||||
|
kind: CorporateActionKind::Split,
|
||||||
|
} if prev_ex_date == d2 && ex_date == d1
|
||||||
|
),
|
||||||
|
"expected UnsortedInput with exact keys, got {err:?}"
|
||||||
|
);
|
||||||
|
assert_eq!(fs::read(&part).unwrap(), before);
|
||||||
|
assert_no_transient_files(&tmp.path().join("corporate_actions"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nan_in_forbidden_field_is_rejected_not_treated_as_empty() {
|
||||||
|
let tmp = TempDirGuard::new("nan-forbidden");
|
||||||
|
let d = day(2024, 4, 1);
|
||||||
|
let row = CorporateActionsParquetRow {
|
||||||
|
cash_amount: Some(f64::NAN),
|
||||||
|
..make_split_row(15, d, None, 2.0)
|
||||||
|
};
|
||||||
|
let err = write_corporate_actions_dataset(
|
||||||
|
tmp.path(),
|
||||||
|
&[row],
|
||||||
|
CorporateActionsWriteOptions::default(),
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(
|
||||||
|
matches!(
|
||||||
|
err,
|
||||||
|
ParquetCorporateActionsError::ForbiddenFieldForKind {
|
||||||
|
listing_id: 15,
|
||||||
|
ex_date,
|
||||||
|
kind: CorporateActionKind::Split,
|
||||||
|
field: "cash_amount",
|
||||||
|
value: ForbiddenFieldValue::Float(v),
|
||||||
|
} if ex_date == d && v.is_nan()
|
||||||
|
),
|
||||||
|
"expected ForbiddenFieldForKind with NaN float payload, got {err:?}"
|
||||||
|
);
|
||||||
|
assert!(!tmp.path().join("corporate_actions").exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_first_row_creates_no_output_directory() {
|
||||||
|
let tmp = TempDirGuard::new("invalid-first-row");
|
||||||
|
let out_dir = tmp.path().join("fresh-root");
|
||||||
|
let bad = CorporateActionsParquetRow {
|
||||||
|
ratio: Some(0.0),
|
||||||
|
..make_split_row(99, day(2024, 1, 2), None, 2.0)
|
||||||
|
};
|
||||||
|
let err =
|
||||||
|
write_corporate_actions_dataset(&out_dir, &[bad], CorporateActionsWriteOptions::default())
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(matches!(
|
||||||
|
err,
|
||||||
|
ParquetCorporateActionsError::InvalidRatio {
|
||||||
|
listing_id: 99,
|
||||||
|
value: Some(v),
|
||||||
|
..
|
||||||
|
} if v == 0.0
|
||||||
|
));
|
||||||
|
assert!(
|
||||||
|
!out_dir.exists(),
|
||||||
|
"invalid first row must not create out_dir"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bad_options_are_rejected_before_consuming_input_iterator() {
|
||||||
|
let tmp = TempDirGuard::new("bad-opts-no-poll");
|
||||||
|
let polled = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||||
|
let polled_clone = Arc::clone(&polled);
|
||||||
|
let iter = std::iter::from_fn(move || {
|
||||||
|
polled_clone.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
Some(make_split_row(1, day(2024, 1, 2), None, 2.0))
|
||||||
|
});
|
||||||
|
let err = write_corporate_actions_dataset(
|
||||||
|
tmp.path(),
|
||||||
|
iter,
|
||||||
|
CorporateActionsWriteOptions {
|
||||||
|
row_group_rows: 0,
|
||||||
|
..CorporateActionsWriteOptions::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(matches!(
|
||||||
|
err,
|
||||||
|
ParquetCorporateActionsError::InvalidRowGroupRows
|
||||||
|
));
|
||||||
|
assert!(
|
||||||
|
!polled.load(std::sync::atomic::Ordering::SeqCst),
|
||||||
|
"input iterator must not be polled when row_group_rows == 0"
|
||||||
|
);
|
||||||
|
|
||||||
|
let polled_z = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||||
|
let polled_z_clone = Arc::clone(&polled_z);
|
||||||
|
let iter_z = std::iter::from_fn(move || {
|
||||||
|
polled_z_clone.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
Some(make_split_row(1, day(2024, 1, 2), None, 2.0))
|
||||||
|
});
|
||||||
|
let err_z = write_corporate_actions_dataset(
|
||||||
|
tmp.path(),
|
||||||
|
iter_z,
|
||||||
|
CorporateActionsWriteOptions {
|
||||||
|
zstd_level: 99,
|
||||||
|
..CorporateActionsWriteOptions::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(matches!(
|
||||||
|
err_z,
|
||||||
|
ParquetCorporateActionsError::InvalidZstdLevel { level: 99, .. }
|
||||||
|
));
|
||||||
|
assert!(
|
||||||
|
!polled_z.load(std::sync::atomic::Ordering::SeqCst),
|
||||||
|
"input iterator must not be polled when zstd_level is invalid"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zstd_min_and_max_levels_1_and_22_both_succeed() {
|
||||||
|
let tmp = TempDirGuard::new("zstd-min-max");
|
||||||
|
let rows = vec![
|
||||||
|
make_split_row(1, day(2024, 1, 15), Some(day(2024, 1, 2)), 2.0),
|
||||||
|
make_div_row(1, day(2024, 3, 15), None, 0.50, "USD"),
|
||||||
|
make_delisting_row(2, day(2024, 6, 30), -0.45),
|
||||||
|
];
|
||||||
|
for level in [1, 22] {
|
||||||
|
let out = tmp.path().join(format!("zstd-{level}"));
|
||||||
|
let stats = write_corporate_actions_dataset(
|
||||||
|
&out,
|
||||||
|
&rows,
|
||||||
|
CorporateActionsWriteOptions {
|
||||||
|
zstd_level: level,
|
||||||
|
..CorporateActionsWriteOptions::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(stats.rows_written, rows.len());
|
||||||
|
let actual = read_parquet_rows(&out.join("corporate_actions/part.parquet"));
|
||||||
|
assert_eq!(actual.len(), rows.len());
|
||||||
|
for (a, e) in actual.iter().zip(&rows) {
|
||||||
|
assert_row_bitwise_eq(a, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn error_display_includes_listing_and_value_and_io_preserves_source() {
|
||||||
|
let d = day(2024, 7, 1);
|
||||||
|
let err_curr = ParquetCorporateActionsError::InvalidCurrency {
|
||||||
|
listing_id: 777,
|
||||||
|
ex_date: d,
|
||||||
|
value: Some(" ".to_owned()),
|
||||||
|
};
|
||||||
|
let msg_curr = err_curr.to_string();
|
||||||
|
assert!(msg_curr.contains("777"), "msg={msg_curr}");
|
||||||
|
assert!(msg_curr.contains(" "), "msg={msg_curr}");
|
||||||
|
|
||||||
|
let err_forbid = ParquetCorporateActionsError::ForbiddenFieldForKind {
|
||||||
|
listing_id: 888,
|
||||||
|
ex_date: d,
|
||||||
|
kind: CorporateActionKind::Split,
|
||||||
|
field: "cash_amount",
|
||||||
|
value: ForbiddenFieldValue::Float(1.25),
|
||||||
|
};
|
||||||
|
let msg_forbid = err_forbid.to_string();
|
||||||
|
assert!(msg_forbid.contains("888"), "msg={msg_forbid}");
|
||||||
|
assert!(msg_forbid.contains("cash_amount"), "msg={msg_forbid}");
|
||||||
|
assert!(msg_forbid.contains("1.25"), "msg={msg_forbid}");
|
||||||
|
|
||||||
|
let io_err = ParquetCorporateActionsError::Io {
|
||||||
|
path: PathBuf::from("/tmp/dummy.parquet"),
|
||||||
|
source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied"),
|
||||||
|
};
|
||||||
|
let src = io_err.source().expect("Io error must expose source()");
|
||||||
|
assert!(src.to_string().contains("denied"));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue