From 30c0e51813d36d77ec85486595b664ff628281ee Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 09:37:24 -0500 Subject: [PATCH] =?UTF-8?q?T-141:=20M2.3d=20=E2=80=94=20`tools/curate-etl`?= =?UTF-8?q?:=20zero-copy=20`eodIntraday1M`=20BSON=20decoder=20+=20chunked?= =?UTF-8?q?=20stream=20decode=20into=20`EodIntradayBar`=20(pure,=20no=20ne?= =?UTF-8?q?w=20crates,=20no=20live=20Mongo/QuestDB)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/curate-etl/src/bson_eod_intraday.rs | 14 +++- .../curate-etl/src/bson_eod_intraday_tests.rs | 79 +++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/tools/curate-etl/src/bson_eod_intraday.rs b/tools/curate-etl/src/bson_eod_intraday.rs index 0cd039e8..c8b9237b 100644 --- a/tools/curate-etl/src/bson_eod_intraday.rs +++ b/tools/curate-etl/src/bson_eod_intraday.rs @@ -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 { diff --git a/tools/curate-etl/src/bson_eod_intraday_tests.rs b/tools/curate-etl/src/bson_eod_intraday_tests.rs index 099f760b..6374d2b8 100644 --- a/tools/curate-etl/src/bson_eod_intraday_tests.rs +++ b/tools/curate-etl/src/bson_eod_intraday_tests.rs @@ -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(