T-102: M1.2t — tools/curate-etl: pure fx_1d curation stage (CAD.FOREX + teMarketData.USDCAD daily bars → Fx1dParquetRows with ts/known_at → Zstd Parquet) with tests and local benchmark (no new crates)
This commit is contained in:
parent
5442b9cd16
commit
f988094851
1 changed files with 127 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use std::borrow::Cow;
|
||||
use std::error::Error;
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
|
||||
|
||||
|
|
@ -570,3 +571,129 @@ fn writer_error_when_out_dir_is_regular_file_surfaces_as_parquet_error_with_sour
|
|||
"unexpected Display: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn curate_and_write_fx_1d_atomic_replace_preserves_open_handle_on_changed_input() {
|
||||
let tmp = TempDirGuard::new("atomic-replace");
|
||||
let eod_v1 = [
|
||||
bar(day(2025, 1, 2), 1.4301, 1.4402, 1.4253, 1.4354),
|
||||
bar(day(2025, 1, 3), 1.4341, 1.4392, 1.4313, 1.4374),
|
||||
];
|
||||
let eod_v2 = [
|
||||
bar(day(2025, 1, 2), 1.4301, 1.4402, 1.4253, 1.4354),
|
||||
bar(day(2025, 1, 3), 1.4341, 1.4392, 1.4313, 1.4374),
|
||||
bar(day(2025, 1, 6), 1.4410, 1.4490, 1.4380, 1.4455),
|
||||
];
|
||||
|
||||
curate_and_write_fx_1d(tmp.path(), &eod_v1, &[], Fx1dWriteOptions::default()).unwrap();
|
||||
let fx_dir = tmp.path().join("fx_1d");
|
||||
let part_path = fx_dir.join("part.parquet");
|
||||
let original_bytes = fs::read(&part_path).expect("read v1 part.parquet");
|
||||
|
||||
// Hold an open handle to v1 while publishing changed input v2.
|
||||
let mut old_handle = File::open(&part_path).expect("open handle to v1 part.parquet");
|
||||
|
||||
let expected_v2 = curate_fx_1d(&eod_v2, &[]).unwrap();
|
||||
let report_v2 =
|
||||
curate_and_write_fx_1d(tmp.path(), &eod_v2, &[], Fx1dWriteOptions::default()).unwrap();
|
||||
assert_eq!(report_v2.write_stats.rows_written, expected_v2.rows.len());
|
||||
|
||||
// An atomic rename replaces the directory entry without mutating the unlinked v1 inode.
|
||||
let mut old_handle_bytes = Vec::new();
|
||||
old_handle
|
||||
.read_to_end(&mut old_handle_bytes)
|
||||
.expect("read v1 bytes from open handle");
|
||||
assert_eq!(
|
||||
old_handle_bytes, original_bytes,
|
||||
"open handle to previous part.parquet must still read original bytes"
|
||||
);
|
||||
|
||||
// Opening the path now resolves to the newly published v2 rows.
|
||||
let (footer_rows, read_back_v2) = read_parquet_fx(&part_path);
|
||||
assert_eq!(footer_rows, expected_v2.rows.len());
|
||||
assert_eq!(read_back_v2.len(), expected_v2.rows.len());
|
||||
for (actual, expected) in read_back_v2.iter().zip(expected_v2.rows.iter()) {
|
||||
assert_row_bitwise_eq(actual, expected);
|
||||
}
|
||||
|
||||
for entry in fs::read_dir(&fx_dir).expect("read fx_1d dir after v2 publish") {
|
||||
let name = entry.unwrap().file_name().to_string_lossy().into_owned();
|
||||
assert_eq!(name, "part.parquet", "unexpected transient file '{name}'");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflowing_session_date_mixed_with_valid_rows_fails_whole_stage_without_writing() {
|
||||
let tmp = TempDirGuard::new("mixed-overflow");
|
||||
let eod = [
|
||||
bar(day(2025, 1, 2), 1.430, 1.440, 1.425, 1.435),
|
||||
flat(106_751_991, 1.35),
|
||||
];
|
||||
let te = [bar(day(2025, 1, 3), 1.434, 1.439, 1.431, 1.437)];
|
||||
|
||||
let err =
|
||||
curate_and_write_fx_1d(tmp.path(), &eod, &te, Fx1dWriteOptions::default()).unwrap_err();
|
||||
assert!(matches!(
|
||||
err,
|
||||
CurateFxWriteError::Curate(CurateFxError::SessionDateOutOfRange {
|
||||
session_date: 106_751_991
|
||||
})
|
||||
));
|
||||
assert!(!tmp.path().join("fx_1d").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_bar_with_overflowing_session_date_is_dropped_by_builder_before_timestamping() {
|
||||
let eod = [
|
||||
bar(day(2025, 1, 2), 1.430, 1.440, 1.425, 1.435),
|
||||
bar(i32::MAX, 1.35, 1.36, 1.34, 0.0),
|
||||
];
|
||||
let out = curate_fx_1d(&eod, &[]).expect("invalid overflowing bar should be dropped");
|
||||
assert_stage_invariants(&out);
|
||||
assert_eq!(out.stats.builder.eod.dropped_invalid_ohlc, 1);
|
||||
assert_eq!(out.rows.len(), 1);
|
||||
assert_eq!(out.rows[0].session_date, day(2025, 1, 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fx_1d_utc_day_end_micros_matches_i128_reference_across_seeded_sweep() {
|
||||
let mut state: u64 = 0x517c_c1b7_2722_0a95;
|
||||
let mut next_u64 = || -> u64 {
|
||||
state = state.wrapping_add(0x9e37_79b9_7f4a_7c15);
|
||||
let mut z = state;
|
||||
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
|
||||
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
|
||||
z ^ (z >> 31)
|
||||
};
|
||||
|
||||
let boundaries = [
|
||||
i32::MIN,
|
||||
-106_751_994,
|
||||
-106_751_993,
|
||||
-106_751_992,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
106_751_990,
|
||||
106_751_991,
|
||||
106_751_992,
|
||||
i32::MAX,
|
||||
];
|
||||
|
||||
let mut dates = Vec::with_capacity(boundaries.len() + 10_000);
|
||||
dates.extend_from_slice(&boundaries);
|
||||
for _ in 0..10_000 {
|
||||
dates.push(next_u64() as i32);
|
||||
}
|
||||
|
||||
for session_date in dates {
|
||||
let wide = (i128::from(session_date) + 1) * i128::from(FX_1D_MICROS_PER_DAY) - 1;
|
||||
let expected = i64::try_from(wide)
|
||||
.map_err(|_| CurateFxError::SessionDateOutOfRange { session_date });
|
||||
assert_eq!(
|
||||
fx_1d_utc_day_end_micros(session_date),
|
||||
expected,
|
||||
"mismatch for session_date={session_date}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue