T-141: M2.3d — tools/curate-etl: zero-copy eodIntraday1M BSON decoder + chunked stream decode into EodIntradayBar (pure, no new crates, no live Mongo/QuestDB)

This commit is contained in:
Studio 2026-12-01 09:37:24 -05:00
parent d43d987416
commit 30c0e51813
2 changed files with 90 additions and 3 deletions

View file

@ -41,7 +41,9 @@
//! [`BsonEodIntradayError::WrongType`]; `NaN`, `±inf`, and `< 0.0` are rejected as
//! [`BsonEodIntradayError::InvalidNumber`].
//! - Unknown extra fields (`_id`, `symbol`, `exchange`, `datetime`, `gmtoffset`, `source`, nested
//! objects/arrays, etc.) are skipped without calling `elem.value()`.
//! objects/arrays, etc.) are validated as borrowed `RawBsonRef` views (zero allocation) and
//! otherwise ignored, so malformed payloads (such as invalid UTF-8 in `datetime`) still surface
//! as [`BsonEodIntradayError::CorruptBson`].
//! - Error precedence: retains the first semantic error while continuing to scan remaining
//! top-level elements so any malformed BSON bytes surface immediately as `CorruptBson`.
@ -547,9 +549,15 @@ fn decode_eod_intraday_doc_at<'a>(
}
}
}
} else {
// Validate skipped top-level payloads via borrowed `RawBsonRef` views (zero
// allocation) so malformed bytes (e.g. invalid UTF-8 in `datetime`) still reject the
// document as `CorruptBson` at its frame offset.
elem.value().map_err(|err| BsonEodIntradayError::CorruptBson {
offset,
detail: format!("field '{}': {err}", key.as_str()),
})?;
}
// Unknown extra fields (`_id`, `symbol`, `exchange`, `datetime`, `gmtoffset`, `source`,
// etc.) are skipped without calling `elem.value()`.
}
if let Some(err) = first_semantic_err {

View file

@ -580,6 +580,84 @@ fn rejects_truncated_and_corrupt_single_doc_bytes_without_panicking() {
}
}
#[test]
fn rejects_corrupt_utf8_in_skipped_top_level_field_and_recovers_to_next_frame() {
// Build a valid document with a skipped `datetime` string field, then corrupt one payload byte
// inside `datetime` to invalid UTF-8 (0xFF) while leaving all lengths and NUL terminators
// intact.
const DATETIME_MARKER: &[u8] = b"2024-06-07 13:30:00";
let mut doc_with_datetime = make_valid_doc(
"NVDA.US",
1_717_767_000,
120.50,
121.25,
120.10,
120.90,
1_000.0,
);
doc_with_datetime.append(cstr!("datetime"), "2024-06-07 13:30:00");
let mut corrupt_bytes = doc_with_datetime.as_bytes().to_vec();
let marker_pos = corrupt_bytes
.windows(DATETIME_MARKER.len())
.position(|w| w == DATETIME_MARKER)
.expect("datetime payload must exist in serialized BSON");
corrupt_bytes[marker_pos + 4] = 0xFF;
// Single-document decode must reject as CorruptBson at offset 0.
match decode_eod_intraday_bytes(&corrupt_bytes) {
Err(BsonEodIntradayError::CorruptBson { offset: 0, detail }) => {
assert!(
detail.contains("datetime"),
"expected offending field name in detail, got: {detail}"
);
}
other => panic!("expected CorruptBson at offset 0, got {other:?}"),
}
// Buffer decode with a preceding valid frame and a following valid frame: rejects the
// damaged frame at its exact byte offset, increments `rejected_corrupt_document`, and
// recovers cleanly to decode the next frame.
let valid_before =
make_valid_doc("AAPL.US", 1_717_766_940, 195.0, 196.0, 194.5, 195.5, 500.0);
let valid_after =
make_valid_doc("MSFT.US", 1_717_767_060, 420.0, 421.0, 419.5, 420.5, 750.0);
let mut buf = Vec::new();
buf.extend_from_slice(valid_before.as_bytes());
let corrupt_offset = buf.len();
buf.extend_from_slice(&corrupt_bytes);
buf.extend_from_slice(valid_after.as_bytes());
let mut rows = Vec::new();
let mut errors = Vec::new();
let stats = decode_eod_intraday_buffer(
&buf,
|bar| rows.push((bar.eod_search_code.to_owned(), bar.ts, bar.close)),
|off, err| errors.push((off, err)),
);
assert_eq!(
rows,
vec![
("AAPL.US".to_owned(), 1_717_766_940_000_000, 195.5),
("MSFT.US".to_owned(), 1_717_767_060_000_000, 420.5),
]
);
assert_eq!(stats.docs_seen, 3);
assert_eq!(stats.rows_decoded, 2);
assert_eq!(stats.rejected_corrupt_document, 1);
assert_eq!(stats.rejected_total(), 1);
assert_eq!(stats.docs_seen, stats.rows_decoded + stats.rejected_total());
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].0, corrupt_offset);
assert!(matches!(
&errors[0].1,
BsonEodIntradayError::CorruptBson { offset, detail }
if *offset == corrupt_offset && detail.contains("datetime")
));
}
#[test]
fn decodes_mixed_buffer_preserving_order_recovering_past_corrupt_frame_and_merging_stats() {
let valid1 = make_valid_doc("NVDA.US", 1_717_767_000, 120.0, 121.0, 119.5, 120.5, 1000.0);
@ -795,6 +873,7 @@ fn decoded_nvda_bars_feed_build_bars_1m_end_to_end_with_10_for_1_split() {
assert_rel_eq(pre_split.open, pre_empty.open * 10.0, 1e-12);
assert_rel_eq(pre_split.high, pre_empty.high * 10.0, 1e-12);
assert_rel_eq(pre_split.low, pre_empty.low * 10.0, 1e-12);
assert_rel_eq(pre_split.close, 1_208.88, 1e-12);
assert_rel_eq(pre_split.close, pre_empty.close * 10.0, 1e-12);
assert_rel_eq(pre_split.volume, pre_empty.volume / 10.0, 1e-12);
assert_rel_eq(