T-90: M1.2i — tools/curate-etl: Zstd Parquet writer for bars_1d (year=YYYY/part.parquet, contract schema) with round-trip tests and local benchmark (adds the parquet crate)

This commit is contained in:
Studio 2026-11-28 11:56:40 -05:00
parent 64cb0cb725
commit 441a98cd05

View file

@ -678,3 +678,209 @@ fn error_after_30_flushed_row_groups_leaves_no_partial_files() {
assert!(!final_path.exists(), "partial part.parquet must not be published");
assert_no_transient_files(&tmp.path().join("bars_1d"));
}
#[test]
fn bars_1d_path_occupied_by_regular_file_returns_io_error_and_leaves_file_untouched() {
let tmp = TempDirGuard::new("bars-1d-is-file");
let bars_path = tmp.path().join("bars_1d");
let sentinel = b"not-a-directory";
fs::write(&bars_path, sentinel).unwrap();
let rows = vec![make_row(
1,
2024,
1,
2,
10.0,
100.0,
Some(10.0),
"USD",
"eod_unadj",
)];
let err =
write_bars_1d_dataset(tmp.path(), &rows, Bars1dWriteOptions::default()).unwrap_err();
assert!(
matches!(err, ParquetBarsError::Io { ref path, .. } if path == &bars_path),
"expected Io error for bars_1d path, got {err:?}"
);
assert!(bars_path.is_file());
assert_eq!(fs::read(&bars_path).unwrap(), sentinel);
let entries: Vec<_> = fs::read_dir(tmp.path())
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(entries, vec![std::ffi::OsString::from("bars_1d")]);
}
#[test]
fn blocked_year_dir_partway_through_preserves_prior_year_cleans_staging_and_releases_lock() {
let tmp = TempDirGuard::new("blocked-year-dir");
let initial = vec![make_row(
1,
2024,
6,
15,
100.0,
1_000.0,
Some(99.5),
"USD",
"eod_unadj",
)];
write_bars_1d_dataset(tmp.path(), &initial, Bars1dWriteOptions::default()).unwrap();
let path_2024 = tmp.path().join("bars_1d/year=2024/part.parquet");
let orig_2024_bytes = fs::read(&path_2024).unwrap();
// Block year=2025 with a regular file so staging fails partway through after 2024 opens.
let blocked_2025 = tmp.path().join("bars_1d/year=2025");
fs::write(&blocked_2025, b"blocker").unwrap();
let refresh = vec![
make_row(1, 2024, 6, 15, 110.0, 2_000.0, Some(109.5), "USD", "eod_unadj"),
make_row(1, 2025, 1, 2, 120.0, 3_000.0, Some(119.5), "USD", "eod_unadj"),
];
let err =
write_bars_1d_dataset(tmp.path(), &refresh, Bars1dWriteOptions::default()).unwrap_err();
assert!(
matches!(err, ParquetBarsError::Io { ref path, .. } if path == &blocked_2025),
"expected Io error naming year=2025, got {err:?}"
);
assert_eq!(fs::read(&path_2024).unwrap(), orig_2024_bytes);
assert_no_transient_files(&tmp.path().join("bars_1d"));
// Lock must be released so a subsequent valid write succeeds immediately.
let follow_up = vec![make_row(
1,
2024,
6,
16,
111.0,
2_100.0,
None,
"USD",
"eod_unadj",
)];
let stats = write_bars_1d_dataset_with_hooks(
tmp.path(),
&follow_up,
Bars1dWriteOptions::default(),
Duration::from_millis(50),
|_, _, _| {},
)
.unwrap();
assert_eq!(stats.rows_written, 1);
assert_no_transient_files(&tmp.path().join("bars_1d"));
}
#[test]
fn multi_year_refresh_replaces_existing_adds_new_preserves_untouched_and_leaves_only_part_parquet() {
let tmp = TempDirGuard::new("multi-year-refresh");
let initial = vec![
make_row(1, 2022, 5, 1, 10.0, 100.0, None, "USD", "eod_unadj"),
make_row(1, 2023, 5, 1, 20.0, 200.0, Some(19.9), "USD", "eod_unadj"),
make_row(1, 2024, 5, 1, 30.0, 300.0, None, "USD", "eod_unadj"),
];
write_bars_1d_dataset(tmp.path(), &initial, Bars1dWriteOptions::default()).unwrap();
let path_2022 = tmp.path().join("bars_1d/year=2022/part.parquet");
let orig_2022_bytes = fs::read(&path_2022).unwrap();
let refresh = vec![
make_row(1, 2023, 5, 1, 25.0, 250.0, Some(24.5), "USD", "eod_unadj"),
make_row(1, 2023, 5, 2, 26.0, 260.0, Some(25.5), "USD", "eod_unadj"),
make_row(1, 2024, 5, 1, 35.0, 350.0, Some(34.5), "CAD", "qm_raw"),
make_row(1, 2025, 1, 2, 40.0, 400.0, None, "USD", "eod_unadj"),
make_row(2, 2025, 1, 2, 41.0, 410.0, Some(40.5), "USD", "eod_unadj"),
make_row(3, 2025, 1, 2, 42.0, 420.0, None, "CAD", "qm_raw"),
];
let stats =
write_bars_1d_dataset(tmp.path(), &refresh, Bars1dWriteOptions::default()).unwrap();
assert_eq!(stats.files_written, 3);
assert_eq!(stats.rows_written, 6);
assert_eq!(stats.rows_by_year.get(&2023), Some(&2));
assert_eq!(stats.rows_by_year.get(&2024), Some(&1));
assert_eq!(stats.rows_by_year.get(&2025), Some(&3));
assert_eq!(stats.rows_by_year.get(&2022), None);
assert_eq!(fs::read(&path_2022).unwrap(), orig_2022_bytes);
assert_eq!(
read_parquet_rows(&tmp.path().join("bars_1d/year=2023/part.parquet")).len(),
2
);
assert_eq!(
read_parquet_rows(&tmp.path().join("bars_1d/year=2024/part.parquet")).len(),
1
);
assert_eq!(
read_parquet_rows(&tmp.path().join("bars_1d/year=2025/part.parquet")).len(),
3
);
let bars_dir = tmp.path().join("bars_1d");
assert_no_transient_files(&bars_dir);
for year in [2022, 2023, 2024, 2025] {
let year_dir = bars_dir.join(format!("year={year}"));
let entries: Vec<_> = fs::read_dir(&year_dir)
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(
entries,
vec![std::ffi::OsString::from("part.parquet")],
"year={year} should contain only part.parquet"
);
}
}
#[test]
fn active_external_write_lock_times_out_without_deleting_lock_or_modifying_existing_partition() {
let tmp = TempDirGuard::new("external-lock-held");
let initial = vec![make_row(
1,
2024,
3,
10,
50.0,
500.0,
Some(49.5),
"USD",
"eod_unadj",
)];
write_bars_1d_dataset(tmp.path(), &initial, Bars1dWriteOptions::default()).unwrap();
let path_2024 = tmp.path().join("bars_1d/year=2024/part.parquet");
let orig_2024_bytes = fs::read(&path_2024).unwrap();
let lock_path = tmp.path().join("bars_1d/.write.lock");
let lock_contents = b"pid=999999\n";
fs::write(&lock_path, lock_contents).unwrap();
let updated = vec![make_row(
1,
2024,
3,
10,
99.0,
999.0,
Some(98.5),
"USD",
"eod_unadj",
)];
let err = write_bars_1d_dataset_with_hooks(
tmp.path(),
&updated,
Bars1dWriteOptions::default(),
Duration::from_millis(60),
|_, _, _| {},
)
.unwrap_err();
assert!(
matches!(err, ParquetBarsError::LockTimeout { ref path, .. } if path == &lock_path),
"expected LockTimeout naming lock path, got {err:?}"
);
assert!(lock_path.exists(), "must not delete another writer's lock");
assert_eq!(fs::read(&lock_path).unwrap(), lock_contents);
assert_eq!(fs::read(&path_2024).unwrap(), orig_2024_bytes);
}