T-114: M1.2ad — tools/curate-etl: fifth runnable command curate-shorts (concatenated-BSON ceoShorts file + provider_mapping TSV → shorts/part.parquet) with a zero-copy ceoShorts decoder, end-to-end tests and local benchmark (no new crates, no MongoDB connection)
This commit is contained in:
parent
0de120e7c8
commit
6a4c15ffee
3 changed files with 195 additions and 2 deletions
|
|
@ -334,6 +334,26 @@ fn decode_optional_number(val: RawBsonRef<'_>) -> OptionalNumState {
|
|||
/// Maximum recursive nesting depth accepted while validating raw BSON documents and arrays.
|
||||
const MAX_BSON_VALIDATION_DEPTH: usize = 128;
|
||||
|
||||
#[inline]
|
||||
fn is_exact_array_index(key: &str, expected: usize) -> bool {
|
||||
let bytes = key.as_bytes();
|
||||
if bytes.is_empty() || (bytes.len() > 1 && bytes[0] == b'0') {
|
||||
return false;
|
||||
}
|
||||
let mut parsed: usize = 0;
|
||||
for &b in bytes {
|
||||
if !b.is_ascii_digit() {
|
||||
return false;
|
||||
}
|
||||
let digit = usize::from(b - b'0');
|
||||
match parsed.checked_mul(10).and_then(|n| n.checked_add(digit)) {
|
||||
Some(next) => parsed = next,
|
||||
None => return false,
|
||||
}
|
||||
}
|
||||
parsed == expected
|
||||
}
|
||||
|
||||
fn validate_raw_bson_value(
|
||||
val: RawBsonRef<'_>,
|
||||
doc_offset: u64,
|
||||
|
|
@ -353,8 +373,21 @@ fn validate_raw_bson_value(
|
|||
),
|
||||
});
|
||||
}
|
||||
for item_res in arr {
|
||||
let item = item_res.map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||
for (expected_idx, elem_res) in arr.iter_elements().enumerate() {
|
||||
let elem = elem_res.map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||
offset: doc_offset,
|
||||
detail: format!("field '{field_name}': {err}"),
|
||||
})?;
|
||||
let key = elem.key().as_str();
|
||||
if !is_exact_array_index(key, expected_idx) {
|
||||
return Err(BsonCeoShortsError::CorruptBson {
|
||||
offset: doc_offset,
|
||||
detail: format!(
|
||||
"field '{field_name}': invalid BSON array index '{key}' at position {expected_idx}"
|
||||
),
|
||||
});
|
||||
}
|
||||
let item = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||
offset: doc_offset,
|
||||
detail: format!("field '{field_name}': {err}"),
|
||||
})?;
|
||||
|
|
|
|||
|
|
@ -621,3 +621,112 @@ fn corrupt_unused_string_embedded_doc_and_array_fail_closed() {
|
|||
"expected CorruptBson for invalid UTF-8 in unused array, got {err:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_array_indexes_fail_closed() {
|
||||
use bson::raw::RawArrayBuf;
|
||||
|
||||
let fields = default_fields();
|
||||
let d_secs = i64::from(day(2024, 3, 15)) * 86_400;
|
||||
|
||||
// Valid multi-element unused array succeeds.
|
||||
let mut good_arr = RawArrayBuf::new();
|
||||
good_arr.push("first");
|
||||
good_arr.push("second");
|
||||
good_arr.push("third");
|
||||
let mut good_doc = RawDocumentBuf::new();
|
||||
good_doc.append(cstr!("symbol"), "RY");
|
||||
good_doc.append(cstr!("timestamp"), d_secs);
|
||||
good_doc.append(cstr!("sharesShort"), 100_i32);
|
||||
good_doc.append(cstr!("tags"), good_arr);
|
||||
let mut rows = 0usize;
|
||||
let stats = decode_ceo_shorts_bytes(good_doc.as_bytes(), &fields, |_| rows += 1).unwrap();
|
||||
assert_eq!(rows, 1);
|
||||
assert_conservation(&stats);
|
||||
|
||||
// Helper: build a doc with an unused 2-element array [10, 20] and mutate the array keys.
|
||||
let build_two_elem_array_doc = || {
|
||||
let mut arr = RawArrayBuf::new();
|
||||
arr.push(10_i32);
|
||||
arr.push(20_i32);
|
||||
let mut doc = RawDocumentBuf::new();
|
||||
doc.append(cstr!("symbol"), "RY");
|
||||
doc.append(cstr!("timestamp"), d_secs);
|
||||
doc.append(cstr!("sharesShort"), 100_i32);
|
||||
doc.append(cstr!("tags"), arr);
|
||||
doc.into_bytes()
|
||||
};
|
||||
|
||||
// 1. First index mutated from "0" to "8".
|
||||
let mut bytes = build_two_elem_array_doc();
|
||||
let idx0 = bytes
|
||||
.windows(3)
|
||||
.position(|w| w == [0x10, b'0', 0x00])
|
||||
.expect("locate index 0 element");
|
||||
bytes[idx0 + 1] = b'8';
|
||||
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||
"expected CorruptBson for non-zero starting array index, got {err:?}"
|
||||
);
|
||||
|
||||
// 2. Repeated index ("0", "0").
|
||||
let mut bytes = build_two_elem_array_doc();
|
||||
let idx1 = bytes
|
||||
.windows(3)
|
||||
.position(|w| w == [0x10, b'1', 0x00])
|
||||
.expect("locate index 1 element");
|
||||
bytes[idx1 + 1] = b'0';
|
||||
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||
"expected CorruptBson for repeated array index, got {err:?}"
|
||||
);
|
||||
|
||||
// 3. Skipped/out-of-order index ("0", "2").
|
||||
let mut bytes = build_two_elem_array_doc();
|
||||
let idx1 = bytes
|
||||
.windows(3)
|
||||
.position(|w| w == [0x10, b'1', 0x00])
|
||||
.expect("locate index 1 element");
|
||||
bytes[idx1 + 1] = b'2';
|
||||
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||
"expected CorruptBson for skipped array index, got {err:?}"
|
||||
);
|
||||
|
||||
// 4. Non-numeric index ("a", "1").
|
||||
let mut bytes = build_two_elem_array_doc();
|
||||
let idx0 = bytes
|
||||
.windows(3)
|
||||
.position(|w| w == [0x10, b'0', 0x00])
|
||||
.expect("locate index 0 element");
|
||||
bytes[idx0 + 1] = b'a';
|
||||
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||
"expected CorruptBson for non-numeric array index, got {err:?}"
|
||||
);
|
||||
|
||||
// 5. Leading-zero index ("00") constructed as raw document bytes placed in an array field.
|
||||
let mut fake_arr = RawDocumentBuf::new();
|
||||
fake_arr.append(cstr!("00"), 10_i32);
|
||||
let mut doc = RawDocumentBuf::new();
|
||||
doc.append(cstr!("symbol"), "RY");
|
||||
doc.append(cstr!("timestamp"), d_secs);
|
||||
doc.append(cstr!("sharesShort"), 100_i32);
|
||||
doc.append(cstr!("tags"), fake_arr);
|
||||
let mut bytes = doc.into_bytes();
|
||||
// Change field 'tags' element type from EmbeddedDocument (0x03) to Array (0x04).
|
||||
let tags_pos = bytes
|
||||
.windows(6)
|
||||
.position(|w| w == [0x03, b't', b'a', b'g', b's', 0x00])
|
||||
.expect("locate tags document element");
|
||||
bytes[tags_pos] = 0x04;
|
||||
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||
assert!(
|
||||
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||
"expected CorruptBson for leading-zero array index, got {err:?}"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -484,4 +484,55 @@ fn failure_modes_exit_nonzero_without_usage_and_preserve_preexisting_parquet() {
|
|||
sentinel,
|
||||
"existing shorts/part.parquet must remain byte-identical"
|
||||
);
|
||||
|
||||
// 7. Damaged BSON array index in an unused array field ("0" -> "8") ->
|
||||
// must fail closed with non-zero exit and preserve pre-existing shorts/part.parquet.
|
||||
let mut arr = bson::RawArrayBuf::new();
|
||||
arr.push(10_i32);
|
||||
arr.push(20_i32);
|
||||
let mut arr_corrupt_doc = RawDocumentBuf::new();
|
||||
arr_corrupt_doc.append(cstr!("symbol"), "RY");
|
||||
arr_corrupt_doc.append(
|
||||
cstr!("timestamp"),
|
||||
BsonDateTime::from_millis(i64::from(day(2024, 3, 15)) * 86_400_000),
|
||||
);
|
||||
arr_corrupt_doc.append(cstr!("sharesShort"), 100.0_f64);
|
||||
arr_corrupt_doc.append(cstr!("tags"), arr);
|
||||
let mut arr_corrupt_bytes = arr_corrupt_doc.into_bytes();
|
||||
let idx0_pos = arr_corrupt_bytes
|
||||
.windows(3)
|
||||
.position(|w| w == [0x10, b'0', 0x00])
|
||||
.expect("locate array index 0 int32 element");
|
||||
arr_corrupt_bytes[idx0_pos + 1] = b'8';
|
||||
|
||||
let arr_corrupt_bson = dir.path.join("array_index_corrupt.bson");
|
||||
write_bytes(&arr_corrupt_bson, &arr_corrupt_bytes);
|
||||
let res = run(&[
|
||||
"curate-shorts",
|
||||
"--ceo-shorts",
|
||||
arr_corrupt_bson.to_str().unwrap(),
|
||||
"--provider-mapping",
|
||||
valid_map.to_str().unwrap(),
|
||||
"--out",
|
||||
out_dir.to_str().unwrap(),
|
||||
"--shares-short-field",
|
||||
"sharesShort",
|
||||
]);
|
||||
assert!(
|
||||
!res.status.success(),
|
||||
"damaged BSON array index must fail closed"
|
||||
);
|
||||
assert!(stdout(&res).is_empty());
|
||||
let err = stderr(&res);
|
||||
assert_eq!(err.lines().count(), 1, "single-line stderr expected: {err}");
|
||||
assert!(!err.contains("Usage:"), "{err}");
|
||||
assert!(
|
||||
err.contains("array index"),
|
||||
"expected array index corruption detail in stderr, got: {err}"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(&sentinel_path).unwrap(),
|
||||
sentinel,
|
||||
"existing shorts/part.parquet must remain byte-identical"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue