From a384c9e314493b19b702720b47c2cf71d8f309d2 Mon Sep 17 00:00:00 2001 From: Studio Date: Sat, 28 Nov 2026 18:01:52 -0500 Subject: [PATCH] =?UTF-8?q?T-97:=20M1.2o=20=E2=80=94=20`tools/curate-etl`:?= =?UTF-8?q?=20zero-copy=20raw=20BSON=20decoder=20for=20`eodPrices`=20docum?= =?UTF-8?q?ents=20=E2=86=92=20`EodDailyBar`=20(adds=20the=20`bson`=20crate?= =?UTF-8?q?=20only;=20no=20MongoDB=20connection)=20with=20fixture=20tests?= =?UTF-8?q?=20and=20local=20benchmark?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/curate-etl/src/bson_eod_prices.rs | 17 +-- tools/curate-etl/src/bson_eod_prices_tests.rs | 101 ++++++++++++++++++ 2 files changed, 110 insertions(+), 8 deletions(-) diff --git a/tools/curate-etl/src/bson_eod_prices.rs b/tools/curate-etl/src/bson_eod_prices.rs index 23c9c5f3..9547f250 100644 --- a/tools/curate-etl/src/bson_eod_prices.rs +++ b/tools/curate-etl/src/bson_eod_prices.rs @@ -270,12 +270,13 @@ fn parse_ymd_strict(s: &str) -> Result { #[inline] fn decode_element_value<'a>( + field: &'static str, elem: RawElement<'a>, offset: usize, ) -> Result, BsonEodPriceError> { elem.value().map_err(|err| BsonEodPriceError::CorruptBson { offset, - detail: err.to_string(), + detail: format!("field '{field}': {err}"), }) } @@ -340,7 +341,7 @@ fn decode_eod_price_doc_at<'a>( let key = elem.key(); if key == cstr!("eodSearchCode") { - match decode_element_value(elem, offset)? { + match decode_element_value("eodSearchCode", elem, offset)? { RawBsonRef::String(s) => { if s.trim().is_empty() { return Err(BsonEodPriceError::EmptyField { @@ -357,7 +358,7 @@ fn decode_eod_price_doc_at<'a>( } } } else if key == cstr!("date") { - match decode_element_value(elem, offset)? { + match decode_element_value("date", elem, offset)? { RawBsonRef::String(s) => { session_date = Some(parse_ymd_strict(s)?); } @@ -375,19 +376,19 @@ fn decode_eod_price_doc_at<'a>( } } } else if key == cstr!("open") { - let val = decode_element_value(elem, offset)?; + let val = decode_element_value("open", elem, offset)?; open = Some(decode_non_negative_number("open", val)?); } else if key == cstr!("high") { - let val = decode_element_value(elem, offset)?; + let val = decode_element_value("high", elem, offset)?; high = Some(decode_non_negative_number("high", val)?); } else if key == cstr!("low") { - let val = decode_element_value(elem, offset)?; + let val = decode_element_value("low", elem, offset)?; low = Some(decode_non_negative_number("low", val)?); } else if key == cstr!("close") { - let val = decode_element_value(elem, offset)?; + let val = decode_element_value("close", elem, offset)?; close = Some(decode_non_negative_number("close", val)?); } else if key == cstr!("volume") { - let val = decode_element_value(elem, offset)?; + let val = decode_element_value("volume", elem, offset)?; adj_volume = Some(decode_non_negative_number("volume", val)?); } // Unknown extra fields are skipped without calling `elem.value()`. diff --git a/tools/curate-etl/src/bson_eod_prices_tests.rs b/tools/curate-etl/src/bson_eod_prices_tests.rs index b0f5bc10..e5a614b5 100644 --- a/tools/curate-etl/src/bson_eod_prices_tests.rs +++ b/tools/curate-etl/src/bson_eod_prices_tests.rs @@ -682,3 +682,104 @@ fn decoded_nvda_split_buffer_feeds_build_bars_1d_with_non_string_key_mapper() { assert_eq!(r2.close.to_bits(), 120.91_f64.to_bits()); assert_eq!(r2.volume.to_bits(), 222_551_200.0_f64.to_bits()); } + +#[test] +fn invalid_utf8_in_known_field_names_field_and_stream_continues() { + let good_1 = make_valid_doc("AAPL.US", "2024-06-05", 194.0, 196.9, 193.5, 195.87, 54_000_000.0); + let good_2 = make_valid_doc("MSFT.US", "2024-06-06", 423.0, 425.3, 420.5, 424.52, 14_800_000.0); + + // Build a length-valid document and corrupt a byte inside the `eodSearchCode` string value + // to invalid UTF-8 (`0xFF`) while leaving all framing and null terminators intact. + let mut bad_utf8 = make_valid_doc("BADX.US", "2024-06-05", 10.0, 11.0, 9.0, 10.5, 100.0) + .as_bytes() + .to_vec(); + let needle = b"BADX.US"; + let pos = bad_utf8 + .windows(needle.len()) + .position(|w| w == needle) + .expect("fixture should contain sentinel eodSearchCode bytes"); + bad_utf8[pos] = 0xFF; + + // Single-document decode reports CorruptBson naming the field. + let err = decode_eod_price_bytes(&bad_utf8).unwrap_err(); + match &err { + BsonEodPriceError::CorruptBson { offset, detail } => { + assert_eq!(*offset, 0); + assert!( + detail.contains("eodSearchCode"), + "CorruptBson detail should name 'eodSearchCode', got: {detail}" + ); + } + other => panic!("expected CorruptBson for invalid UTF-8, got {other:?}"), + } + assert!( + err.to_string().contains("eodSearchCode"), + "Display output should name 'eodSearchCode', got: {err}" + ); + + // Stream continues past the invalid-UTF-8 document and decodes the following document. + let mut buffer = Vec::new(); + buffer.extend_from_slice(good_1.as_bytes()); + let bad_offset = buffer.len(); + buffer.extend_from_slice(&bad_utf8); + buffer.extend_from_slice(good_2.as_bytes()); + + let mut decoded_codes: Vec = Vec::new(); + let mut errors: Vec<(usize, BsonEodPriceError)> = Vec::new(); + let stats = decode_eod_price_buffer( + &buffer, + |bar| decoded_codes.push(bar.eod_search_code.to_string()), + |offset, err| errors.push((offset, err)), + ); + + assert_eq!(decoded_codes, vec!["AAPL.US", "MSFT.US"]); + assert_eq!(stats.docs_in, 3); + assert_eq!(stats.decoded, 2); + assert_eq!(stats.rejected_corrupt_document, 1); + assert_eq!(stats.rejected_total(), 1); + assert_eq!(stats.decoded + stats.rejected_total(), stats.docs_in); + assert_eq!(errors.len(), 1); + assert_eq!(errors[0].0, bad_offset); + assert!( + errors[0].1.to_string().contains("eodSearchCode"), + "stream error should name 'eodSearchCode', got: {}", + errors[0].1 + ); +} + +#[test] +fn corrupt_length_prefix_in_middle_stops_stream_cleanly() { + let good_1 = make_valid_doc("AAPL.US", "2024-06-05", 194.0, 196.9, 193.5, 195.87, 54_000_000.0); + let good_2 = make_valid_doc("MSFT.US", "2024-06-06", 423.0, 425.3, 420.5, 424.52, 14_800_000.0); + + let mut buffer = Vec::new(); + buffer.extend_from_slice(good_1.as_bytes()); + let bad_offset = buffer.len(); + buffer.extend_from_slice(&2_i32.to_le_bytes()); + buffer.extend_from_slice(good_2.as_bytes()); + + let mut decoded_codes: Vec = Vec::new(); + let mut errors: Vec<(usize, BsonEodPriceError)> = Vec::new(); + let stats = decode_eod_price_buffer( + &buffer, + |bar| decoded_codes.push(bar.eod_search_code.to_string()), + |offset, err| errors.push((offset, err)), + ); + + assert_eq!(decoded_codes, vec!["AAPL.US"]); + assert_eq!(stats.docs_in, 2); + assert_eq!(stats.decoded, 1); + assert_eq!(stats.rejected_corrupt_document, 1); + assert_eq!(stats.rejected_total(), 1); + assert_eq!(stats.decoded + stats.rejected_total(), stats.docs_in); + assert_eq!( + errors, + vec![( + bad_offset, + BsonEodPriceError::CorruptDocumentLength { + offset: bad_offset, + declared_len: 2, + } + )] + ); +}