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
d4671f39d5
commit
0de120e7c8
6 changed files with 493 additions and 73 deletions
|
|
@ -1347,7 +1347,7 @@ Both `--flag value` and `--flag=value` forms are accepted (in the separated `--f
|
||||||
|
|
||||||
### `ceoShorts` Decoder Rules
|
### `ceoShorts` Decoder Rules
|
||||||
|
|
||||||
- Scans every element of each `RawDocument` to completion before applying semantic skip priority (`key` → `report_date` → `shares_short`) so structural BSON corruption always fails closed with a typed error carrying the absolute byte offset.
|
- Scans and validates every element and nested raw value (including unused strings, embedded documents, and arrays) of each `RawDocument` to completion before applying semantic skip priority (`key` → `report_date` → `shares_short`) so structural BSON corruption always fails closed with a typed error carrying the absolute byte offset.
|
||||||
- `key`: BSON UTF-8 `String` passed through verbatim (empty/whitespace-only, missing, or wrong type skipped and counted).
|
- `key`: BSON UTF-8 `String` passed through verbatim (empty/whitespace-only, missing, or wrong type skipped and counted).
|
||||||
- `report_date`: BSON `DateTime` (UTC calendar day via `timestamp_millis().div_euclid(86_400_000)` checked to `i32`), or `Int32` / `Int64` / integral finite `Double` via `te_epoch_i64_to_session_date` (seconds vs. milliseconds UTC). Non-finite or fractional `Double`s skip under `invalid_date`; values overflowing `i64`/`i32` skip under `date_oor`.
|
- `report_date`: BSON `DateTime` (UTC calendar day via `timestamp_millis().div_euclid(86_400_000)` checked to `i32`), or `Int32` / `Int64` / integral finite `Double` via `te_epoch_i64_to_session_date` (seconds vs. milliseconds UTC). Non-finite or fractional `Double`s skip under `invalid_date`; values overflowing `i64`/`i32` skip under `date_oor`.
|
||||||
- `shares_short`: strict BSON `Double`, `Int32`, or `Int64` (numeric strings are rejected as `wrong_shares_type`). Sign and finiteness validation remains in `build_shorts`.
|
- `shares_short`: strict BSON `Double`, `Int32`, or `Int64` (numeric strings are rejected as `wrong_shares_type`). Sign and finiteness validation remains in `build_shorts`.
|
||||||
|
|
@ -1364,19 +1364,19 @@ On success, `curate-shorts` prints deterministic `key=value` lines to stdout (`c
|
||||||
|
|
||||||
### `bench-curate-shorts-command`
|
### `bench-curate-shorts-command`
|
||||||
|
|
||||||
Fixed-seed (`std`-only SplitMix64) end-to-end file→Parquet benchmark synthesizing a concatenated-BSON `ceoShorts` file (default `700,000` docs, `12,500` keys across the 218 semi-monthly dates `2017-04-15..=2026-04-30`, mixing `DateTime`/`Int32`/`Int64`/`Double` dates, optional fields with nulls and wrong types, ~0.5% semantically skipped docs, invalid `shares_short`, alias keys, and unmapped keys) plus `provider_mapping.tsv` into a temp dir, timing `decode` / `curate` / `write` separately, verifying Parquet footer row count, and cleaning up on drop:
|
Fixed-seed (`std`-only SplitMix64) end-to-end file→Parquet benchmark synthesizing a concatenated-BSON `ceoShorts` file (default `700,000` docs, `12,500` keys distributed across all 218 semi-monthly dates `2017-04-15..=2026-04-30`, mixing `DateTime`/`Int32`/`Int64`/`Double` dates, optional fields with nulls and wrong types, ~0.5% semantically skipped docs, invalid `shares_short`, explicit identical/conflicting same-key duplicates, explicit identical/conflicting primary+alias collisions, ticker-reuse gap dates, and unmapped keys) plus `provider_mapping.tsv` into a temp dir, timing `decode` / `curate` / `write` separately, verifying Parquet footer row count, and cleaning up on drop:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo run -p curate-etl --release --offline -- bench-curate-shorts-command [rows]
|
cargo run -p curate-etl --release --offline -- bench-curate-shorts-command [rows]
|
||||||
```
|
```
|
||||||
|
|
||||||
Measured locally (`2026-11-29`, Linux `6.8.0-151-generic`, AMD Ryzen 9 9950X3D 16-Core Processor, default `700,000` rows; two back-to-back runs with identical checksum `0xc83dd4cd79c6de64`):
|
Measured locally (`2026-11-29`, Linux `6.8.0-151-generic`, AMD Ryzen 9 9950X3D 16-Core Processor, default `700,000` rows; two back-to-back runs with identical checksum `0xa759372dd4476f50`):
|
||||||
|
|
||||||
- Input: `rows=700000`, `docs_framed=700000`, `bytes_read=67955103` (`64.81 MiB`), `keys=12500`, `peak_chunk_bytes=8388608` (`8 MiB`)
|
- Input: `rows=700000`, `docs_framed=700000`, `bytes_read=67925127` (`64.78 MiB`), `keys=12500`, `peak_chunk_bytes=8388608` (`8 MiB`)
|
||||||
- Decode counts: `rows_out=696500`, `skipped=3500`, `nulled_net_change=7000`, `nulled_pct_float=5834`
|
- Decode counts: `rows_out=696570`, `skipped=3430`, `nulled_net_change=6860`, `nulled_pct_float=5717`
|
||||||
- Output: `rows_out=682500` (`97.50%` retained), `listings_out=12250`, `dropped_unmapped=10500`, `invalid_shares=3500`, `files=1`, `row_groups=1`, `bytes=5891286`, `read_back_rows=682500`
|
- Output: `rows_out=656911` (`93.84%` retained), `listings_out=12345`, `dropped_unmapped=15229`, `dup_collapsed=3500`, `dup_conflict=7000`, `coll_collapsed=3500`, `coll_dropped=7000`, `invalid_shares=3430`, `files=1`, `row_groups=1`, `bytes=5791000`, `read_back_rows=656911`
|
||||||
- Run 1: `decode=108.96 ms` (`6,424,653 rows/s`, `594.80 MB/s`), `curate=129.29 ms` (`5,387,095 rows/s`), `write=86.75 ms` (`7,867,006 rows/s`, `64.76 MB/s`), `total=325.00 ms` (`2,153,843 rows/s`), `peak_chunk_bytes=8388608`, `checksum=0xc83dd4cd79c6de64`
|
- Run 1: `decode=141.29 ms` (`4,954,433 rows/s`, `458.49 MB/s`), `curate=130.95 ms` (`5,319,222 rows/s`), `write=93.49 ms` (`7,026,896 rows/s`, `59.08 MB/s`), `total=365.73 ms` (`1,914,000 rows/s`), `peak_chunk_bytes=8388608`, `checksum=0xa759372dd4476f50`
|
||||||
- Run 2: `decode=108.20 ms` (`6,469,586 rows/s`, `598.96 MB/s`), `curate=129.44 ms` (`5,380,992 rows/s`), `write=83.96 ms` (`8,129,295 rows/s`, `66.92 MB/s`), `total=321.59 ms` (`2,176,676 rows/s`), `peak_chunk_bytes=8388608`, `checksum=0xc83dd4cd79c6de64`
|
- Run 2: `decode=152.15 ms` (`4,600,763 rows/s`, `425.76 MB/s`), `curate=130.95 ms` (`5,319,464 rows/s`), `write=86.38 ms` (`7,604,711 rows/s`, `63.93 MB/s`), `total=369.48 ms` (`1,894,564 rows/s`), `peak_chunk_bytes=8388608`, `checksum=0xa759372dd4476f50`
|
||||||
|
|
||||||
### Known Limitations
|
### Known Limitations
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9129,6 +9129,8 @@ impl std::error::Error for CurateShortsCmdBenchError {
|
||||||
pub struct CurateShortsCmdBenchReport {
|
pub struct CurateShortsCmdBenchReport {
|
||||||
pub rows: usize,
|
pub rows: usize,
|
||||||
pub key_count: usize,
|
pub key_count: usize,
|
||||||
|
pub min_report_date: Option<i32>,
|
||||||
|
pub max_report_date: Option<i32>,
|
||||||
pub docs_framed: u64,
|
pub docs_framed: u64,
|
||||||
pub bytes_read: u64,
|
pub bytes_read: u64,
|
||||||
pub peak_chunk_bytes: usize,
|
pub peak_chunk_bytes: usize,
|
||||||
|
|
@ -9184,6 +9186,8 @@ fn run_curate_shorts_command_benchmark_tracked(
|
||||||
CurateShortsCmdBenchReport {
|
CurateShortsCmdBenchReport {
|
||||||
rows: 0,
|
rows: 0,
|
||||||
key_count: 0,
|
key_count: 0,
|
||||||
|
min_report_date: None,
|
||||||
|
max_report_date: None,
|
||||||
docs_framed: 0,
|
docs_framed: 0,
|
||||||
bytes_read: 0,
|
bytes_read: 0,
|
||||||
peak_chunk_bytes: 0,
|
peak_chunk_bytes: 0,
|
||||||
|
|
@ -9282,6 +9286,20 @@ fn run_curate_shorts_command_benchmark_tracked(
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Write synthetic concatenated-BSON ceoShorts file with fixed-seed SplitMix64.
|
// 2. Write synthetic concatenated-BSON ceoShorts file with fixed-seed SplitMix64.
|
||||||
|
// Distribute base rows across all 218 semi-monthly dates (2017-04-15..=2026-04-30) and
|
||||||
|
// reserve explicit rows for same-key identical/conflicting duplicates and primary/alias
|
||||||
|
// identical/conflicting collisions.
|
||||||
|
struct SeedRow {
|
||||||
|
key_idx: usize,
|
||||||
|
report_date: i32,
|
||||||
|
shares_short: f64,
|
||||||
|
net_change: Option<f64>,
|
||||||
|
pct_float: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut min_report_date: Option<i32> = None;
|
||||||
|
let mut max_report_date: Option<i32> = None;
|
||||||
|
|
||||||
{
|
{
|
||||||
let f = File::create(&bson_path).map_err(|source| {
|
let f = File::create(&bson_path).map_err(|source| {
|
||||||
CurateShortsCmdBenchError::FixtureIo {
|
CurateShortsCmdBenchError::FixtureIo {
|
||||||
|
|
@ -9292,13 +9310,48 @@ fn run_curate_shorts_command_benchmark_tracked(
|
||||||
let mut w = BufWriter::with_capacity(1024 * 1024, f);
|
let mut w = BufWriter::with_capacity(1024 * 1024, f);
|
||||||
let mut rng = SplitMix64::new(CURATE_SHORTS_CMD_BENCH_SEED);
|
let mut rng = SplitMix64::new(CURATE_SHORTS_CMD_BENCH_SEED);
|
||||||
|
|
||||||
for i in 0..rows {
|
let dup_identical_target = rows / 200;
|
||||||
let r = rng.next_u64();
|
let dup_conflict_target = rows / 200;
|
||||||
let key_idx = i % key_count;
|
let alias_identical_target = rows / 200;
|
||||||
let date_idx = (i / key_count) % date_count;
|
let alias_conflict_target = rows / 200;
|
||||||
let report_date = all_dates[date_idx];
|
let reserved_extras = dup_identical_target
|
||||||
|
+ dup_conflict_target
|
||||||
|
+ alias_identical_target
|
||||||
|
+ alias_conflict_target;
|
||||||
|
let base_rows = rows.saturating_sub(reserved_extras);
|
||||||
|
|
||||||
|
let mut dup_identical_seeds: Vec<SeedRow> = Vec::with_capacity(dup_identical_target);
|
||||||
|
let mut dup_conflict_seeds: Vec<SeedRow> = Vec::with_capacity(dup_conflict_target);
|
||||||
|
let mut alias_identical_seeds: Vec<SeedRow> = Vec::with_capacity(alias_identical_target);
|
||||||
|
let mut alias_conflict_seeds: Vec<SeedRow> = Vec::with_capacity(alias_conflict_target);
|
||||||
|
|
||||||
|
let write_doc = |w: &mut BufWriter<File>, doc: &RawDocumentBuf| {
|
||||||
|
w.write_all(doc.as_bytes())
|
||||||
|
.map_err(|source| CurateShortsCmdBenchError::FixtureIo {
|
||||||
|
path: bson_path.clone(),
|
||||||
|
source,
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let append_date = |doc: &mut RawDocumentBuf, encoding: usize, report_date: i32| {
|
||||||
let epoch_secs = i64::from(report_date) * 86_400;
|
let epoch_secs = i64::from(report_date) * 86_400;
|
||||||
let epoch_millis = epoch_secs * 1_000;
|
let epoch_millis = epoch_secs * 1_000;
|
||||||
|
match encoding % 5 {
|
||||||
|
0 => doc.append(cstr!("timestamp"), BsonDateTime::from_millis(epoch_millis)),
|
||||||
|
1 => doc.append(cstr!("timestamp"), epoch_secs as i32),
|
||||||
|
2 => doc.append(cstr!("timestamp"), epoch_secs),
|
||||||
|
3 => doc.append(cstr!("timestamp"), epoch_millis),
|
||||||
|
_ => doc.append(cstr!("timestamp"), epoch_secs as f64),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut emitted = 0usize;
|
||||||
|
for i in 0..base_rows {
|
||||||
|
let r = rng.next_u64();
|
||||||
|
let date_idx = i % date_count;
|
||||||
|
let key_idx = ((i / date_count) + date_idx * 57) % key_count;
|
||||||
|
let report_date = all_dates[date_idx];
|
||||||
|
let epoch_secs = i64::from(report_date) * 86_400;
|
||||||
|
|
||||||
let mut doc = RawDocumentBuf::new();
|
let mut doc = RawDocumentBuf::new();
|
||||||
|
|
||||||
|
|
@ -9326,34 +9379,19 @@ fn run_curate_shorts_command_benchmark_tracked(
|
||||||
doc.append(cstr!("sharesShort"), "bad_num");
|
doc.append(cstr!("sharesShort"), "bad_num");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.write_all(doc.as_bytes()).map_err(|source| {
|
write_doc(&mut w, &doc)?;
|
||||||
CurateShortsCmdBenchError::FixtureIo {
|
emitted += 1;
|
||||||
path: bson_path.clone(),
|
|
||||||
source,
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pick key: occasional alias collision on first 200 keys
|
doc.append(cstr!("symbol"), format!("CEO{key_idx:05}"));
|
||||||
let use_alias = key_idx < 200 && i % 40 == 7;
|
|
||||||
if use_alias {
|
|
||||||
doc.append(cstr!("symbol"), format!("ALIAS{key_idx:03}"));
|
|
||||||
} else {
|
|
||||||
doc.append(cstr!("symbol"), format!("CEO{key_idx:05}"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mix report_date BSON encodings: DateTime, Int32 secs, Int64 secs, Int64 millis, integral Double
|
// Mix report_date BSON encodings: DateTime, Int32 secs, Int64 secs, Int64 millis, integral Double
|
||||||
match i % 5 {
|
append_date(&mut doc, i, report_date);
|
||||||
0 => doc.append(cstr!("timestamp"), BsonDateTime::from_millis(epoch_millis)),
|
|
||||||
1 => doc.append(cstr!("timestamp"), epoch_secs as i32),
|
|
||||||
2 => doc.append(cstr!("timestamp"), epoch_secs),
|
|
||||||
3 => doc.append(cstr!("timestamp"), epoch_millis),
|
|
||||||
_ => doc.append(cstr!("timestamp"), epoch_secs as f64),
|
|
||||||
}
|
|
||||||
|
|
||||||
// ~0.5% invalid shares_short (negative), otherwise deterministic positive count
|
// ~0.5% invalid shares_short (negative), otherwise deterministic positive count
|
||||||
let shares_short = if i % 200 == 100 {
|
let invalid_shares = i % 200 == 100;
|
||||||
|
let shares_short = if invalid_shares {
|
||||||
-50.0
|
-50.0
|
||||||
} else {
|
} else {
|
||||||
((r % 5_000_000) as f64) * 1.0
|
((r % 5_000_000) as f64) * 1.0
|
||||||
|
|
@ -9361,34 +9399,150 @@ fn run_curate_shorts_command_benchmark_tracked(
|
||||||
doc.append(cstr!("sharesShort"), shares_short);
|
doc.append(cstr!("sharesShort"), shares_short);
|
||||||
|
|
||||||
// Optional netChange: present, null, absent, or occasional wrong type
|
// Optional netChange: present, null, absent, or occasional wrong type
|
||||||
match i % 10 {
|
let net_change_val = match i % 10 {
|
||||||
0 => doc.append(cstr!("netChange"), RawBson::Null),
|
0 => {
|
||||||
1 => {} // absent
|
doc.append(cstr!("netChange"), RawBson::Null);
|
||||||
2 if i % 100 == 2 => doc.append(cstr!("netChange"), "n/a"),
|
None
|
||||||
|
}
|
||||||
|
1 => None, // absent
|
||||||
|
2 if i % 100 == 2 => {
|
||||||
|
doc.append(cstr!("netChange"), "n/a");
|
||||||
|
None
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let delta = ((r >> 16) % 200_000) as f64 - 100_000.0;
|
let delta = ((r >> 16) % 200_000) as f64 - 100_000.0;
|
||||||
doc.append(cstr!("netChange"), delta);
|
doc.append(cstr!("netChange"), delta);
|
||||||
|
Some(delta)
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Optional pctFloat: present, null, absent, or occasional wrong type
|
// Optional pctFloat: present, null, absent, or occasional wrong type
|
||||||
match i % 12 {
|
let pct_float_val = match i % 12 {
|
||||||
0 => doc.append(cstr!("pctFloat"), RawBson::Null),
|
0 => {
|
||||||
1 => {} // absent
|
doc.append(cstr!("pctFloat"), RawBson::Null);
|
||||||
3 if i % 120 == 3 => doc.append(cstr!("pctFloat"), true),
|
None
|
||||||
|
}
|
||||||
|
1 => None, // absent
|
||||||
|
3 if i % 120 == 3 => {
|
||||||
|
doc.append(cstr!("pctFloat"), true);
|
||||||
|
None
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let pct = ((r >> 32) % 2500) as f64 / 100.0;
|
let pct = ((r >> 32) % 2500) as f64 / 100.0;
|
||||||
doc.append(cstr!("pctFloat"), pct);
|
doc.append(cstr!("pctFloat"), pct);
|
||||||
|
Some(pct)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
write_doc(&mut w, &doc)?;
|
||||||
|
emitted += 1;
|
||||||
|
|
||||||
|
// Track full date span and collect clean mapped single-range seeds for explicit
|
||||||
|
// duplicate and alias-collision pairs.
|
||||||
|
if !invalid_shares && key_idx % 50 < 48 {
|
||||||
|
min_report_date = Some(min_report_date.map_or(report_date, |m| m.min(report_date)));
|
||||||
|
max_report_date = Some(max_report_date.map_or(report_date, |m| m.max(report_date)));
|
||||||
|
if key_idx < 200 {
|
||||||
|
if alias_identical_seeds.len() < alias_identical_target {
|
||||||
|
alias_identical_seeds.push(SeedRow {
|
||||||
|
key_idx,
|
||||||
|
report_date,
|
||||||
|
shares_short,
|
||||||
|
net_change: net_change_val,
|
||||||
|
pct_float: pct_float_val,
|
||||||
|
});
|
||||||
|
} else if alias_conflict_seeds.len() < alias_conflict_target {
|
||||||
|
alias_conflict_seeds.push(SeedRow {
|
||||||
|
key_idx,
|
||||||
|
report_date,
|
||||||
|
shares_short,
|
||||||
|
net_change: net_change_val,
|
||||||
|
pct_float: pct_float_val,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if dup_identical_seeds.len() < dup_identical_target {
|
||||||
|
dup_identical_seeds.push(SeedRow {
|
||||||
|
key_idx,
|
||||||
|
report_date,
|
||||||
|
shares_short,
|
||||||
|
net_change: net_change_val,
|
||||||
|
pct_float: pct_float_val,
|
||||||
|
});
|
||||||
|
} else if dup_conflict_seeds.len() < dup_conflict_target {
|
||||||
|
dup_conflict_seeds.push(SeedRow {
|
||||||
|
key_idx,
|
||||||
|
report_date,
|
||||||
|
shares_short,
|
||||||
|
net_change: net_change_val,
|
||||||
|
pct_float: pct_float_val,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.write_all(doc.as_bytes()).map_err(|source| {
|
|
||||||
CurateShortsCmdBenchError::FixtureIo {
|
|
||||||
path: bson_path.clone(),
|
|
||||||
source,
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut emit_seed = |sym: String,
|
||||||
|
encoding: usize,
|
||||||
|
seed: &SeedRow,
|
||||||
|
shares_override: Option<f64>|
|
||||||
|
-> Result<(), CurateShortsCmdBenchError> {
|
||||||
|
let mut doc = RawDocumentBuf::new();
|
||||||
|
doc.append(cstr!("symbol"), sym);
|
||||||
|
append_date(&mut doc, encoding, seed.report_date);
|
||||||
|
doc.append(
|
||||||
|
cstr!("sharesShort"),
|
||||||
|
shares_override.unwrap_or(seed.shares_short),
|
||||||
|
);
|
||||||
|
if let Some(v) = seed.net_change {
|
||||||
|
doc.append(cstr!("netChange"), v);
|
||||||
|
}
|
||||||
|
if let Some(v) = seed.pct_float {
|
||||||
|
doc.append(cstr!("pctFloat"), v);
|
||||||
|
}
|
||||||
|
write_doc(&mut w, &doc)?;
|
||||||
|
emitted += 1;
|
||||||
|
Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
for (idx, seed) in dup_identical_seeds.iter().enumerate() {
|
||||||
|
emit_seed(format!("CEO{:05}", seed.key_idx), idx, seed, None)?;
|
||||||
|
}
|
||||||
|
for (idx, seed) in dup_conflict_seeds.iter().enumerate() {
|
||||||
|
emit_seed(
|
||||||
|
format!("CEO{:05}", seed.key_idx),
|
||||||
|
idx + 1,
|
||||||
|
seed,
|
||||||
|
Some(seed.shares_short + 1_000.0),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
for (idx, seed) in alias_identical_seeds.iter().enumerate() {
|
||||||
|
emit_seed(format!("ALIAS{:03}", seed.key_idx), idx + 2, seed, None)?;
|
||||||
|
}
|
||||||
|
for (idx, seed) in alias_conflict_seeds.iter().enumerate() {
|
||||||
|
emit_seed(
|
||||||
|
format!("ALIAS{:03}", seed.key_idx),
|
||||||
|
idx + 3,
|
||||||
|
seed,
|
||||||
|
Some(seed.shares_short + 1_000.0),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If `rows` is tiny and we didn't collect all reserved seeds, top up with clean base docs.
|
||||||
|
let mut fill_idx = base_rows;
|
||||||
|
while emitted < rows {
|
||||||
|
let r = rng.next_u64();
|
||||||
|
let date_idx = fill_idx % date_count;
|
||||||
|
let key_idx = ((fill_idx / date_count) + date_idx * 57) % key_count;
|
||||||
|
let report_date = all_dates[date_idx];
|
||||||
|
let mut doc = RawDocumentBuf::new();
|
||||||
|
doc.append(cstr!("symbol"), format!("CEO{key_idx:05}"));
|
||||||
|
append_date(&mut doc, fill_idx, report_date);
|
||||||
|
doc.append(cstr!("sharesShort"), ((r % 5_000_000) as f64) * 1.0);
|
||||||
|
write_doc(&mut w, &doc)?;
|
||||||
|
emitted += 1;
|
||||||
|
fill_idx += 1;
|
||||||
|
}
|
||||||
|
debug_assert_eq!(emitted, rows);
|
||||||
|
|
||||||
w.flush()
|
w.flush()
|
||||||
.map_err(|source| CurateShortsCmdBenchError::FixtureIo {
|
.map_err(|source| CurateShortsCmdBenchError::FixtureIo {
|
||||||
path: bson_path.clone(),
|
path: bson_path.clone(),
|
||||||
|
|
@ -9442,6 +9596,8 @@ fn run_curate_shorts_command_benchmark_tracked(
|
||||||
let report = CurateShortsCmdBenchReport {
|
let report = CurateShortsCmdBenchReport {
|
||||||
rows,
|
rows,
|
||||||
key_count,
|
key_count,
|
||||||
|
min_report_date,
|
||||||
|
max_report_date,
|
||||||
docs_framed: timed.report.docs_framed,
|
docs_framed: timed.report.docs_framed,
|
||||||
bytes_read: timed.report.bytes_read,
|
bytes_read: timed.report.bytes_read,
|
||||||
peak_chunk_bytes: timed.report.peak_chunk_bytes,
|
peak_chunk_bytes: timed.report.peak_chunk_bytes,
|
||||||
|
|
@ -11358,10 +11514,13 @@ mod tests {
|
||||||
fn curate_shorts_cmd_benchmark_small_run_is_deterministic_cleans_up_and_conserves_counts() {
|
fn curate_shorts_cmd_benchmark_small_run_is_deterministic_cleans_up_and_conserves_counts() {
|
||||||
let (a, dir_a) = run_curate_shorts_command_benchmark_tracked(5_000).unwrap();
|
let (a, dir_a) = run_curate_shorts_command_benchmark_tracked(5_000).unwrap();
|
||||||
let (b, dir_b) = run_curate_shorts_command_benchmark_tracked(5_000).unwrap();
|
let (b, dir_b) = run_curate_shorts_command_benchmark_tracked(5_000).unwrap();
|
||||||
|
let all_dates = semi_monthly_report_dates();
|
||||||
|
|
||||||
assert!(!dir_a.exists(), "temp dir {} should be removed", dir_a.display());
|
assert!(!dir_a.exists(), "temp dir {} should be removed", dir_a.display());
|
||||||
assert!(!dir_b.exists(), "temp dir {} should be removed", dir_b.display());
|
assert!(!dir_b.exists(), "temp dir {} should be removed", dir_b.display());
|
||||||
assert_eq!(a.rows, 5_000);
|
assert_eq!(a.rows, 5_000);
|
||||||
|
assert_eq!(a.min_report_date, Some(all_dates[0]));
|
||||||
|
assert_eq!(a.max_report_date, Some(*all_dates.last().unwrap()));
|
||||||
assert_eq!(a.checksum, b.checksum);
|
assert_eq!(a.checksum, b.checksum);
|
||||||
assert_ne!(a.checksum, 0);
|
assert_ne!(a.checksum, 0);
|
||||||
assert_eq!(a.rows_out, b.rows_out);
|
assert_eq!(a.rows_out, b.rows_out);
|
||||||
|
|
@ -11371,7 +11530,14 @@ mod tests {
|
||||||
assert_eq!(a.bytes_written, b.bytes_written);
|
assert_eq!(a.bytes_written, b.bytes_written);
|
||||||
assert!(a.peak_chunk_bytes > 0);
|
assert!(a.peak_chunk_bytes > 0);
|
||||||
assert!(a.decode.skipped_total() > 0);
|
assert!(a.decode.skipped_total() > 0);
|
||||||
|
assert!(a.decode.nulled_wrong_type_net_change > 0);
|
||||||
|
assert!(a.decode.nulled_wrong_type_pct_float > 0);
|
||||||
|
assert!(a.curate_stats.builder.dropped_invalid_shares_short > 0);
|
||||||
|
assert!(a.curate_stats.builder.duplicate_collapsed > 0);
|
||||||
|
assert!(a.curate_stats.builder.duplicate_conflict_dropped > 0);
|
||||||
assert!(a.curate_stats.dropped_unmapped > 0);
|
assert!(a.curate_stats.dropped_unmapped > 0);
|
||||||
|
assert!(a.curate_stats.listing_collision_collapsed > 0);
|
||||||
|
assert!(a.curate_stats.listing_collision_dropped > 0);
|
||||||
|
|
||||||
// Conservation invariants
|
// Conservation invariants
|
||||||
assert_eq!(a.docs_framed as usize, a.decode.docs_in);
|
assert_eq!(a.docs_framed as usize, a.decode.docs_in);
|
||||||
|
|
|
||||||
|
|
@ -331,6 +331,72 @@ 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;
|
||||||
|
|
||||||
|
fn validate_raw_bson_value(
|
||||||
|
val: RawBsonRef<'_>,
|
||||||
|
doc_offset: u64,
|
||||||
|
field_name: &str,
|
||||||
|
depth: usize,
|
||||||
|
) -> Result<(), BsonCeoShortsError> {
|
||||||
|
match val {
|
||||||
|
RawBsonRef::Document(subdoc) => {
|
||||||
|
validate_raw_document(subdoc, doc_offset, field_name, depth + 1)
|
||||||
|
}
|
||||||
|
RawBsonRef::Array(arr) => {
|
||||||
|
if depth >= MAX_BSON_VALIDATION_DEPTH {
|
||||||
|
return Err(BsonCeoShortsError::CorruptBson {
|
||||||
|
offset: doc_offset,
|
||||||
|
detail: format!(
|
||||||
|
"field '{field_name}': nested BSON depth exceeds limit {MAX_BSON_VALIDATION_DEPTH}"
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for item_res in arr {
|
||||||
|
let item = item_res.map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||||
|
offset: doc_offset,
|
||||||
|
detail: format!("field '{field_name}': {err}"),
|
||||||
|
})?;
|
||||||
|
validate_raw_bson_value(item, doc_offset, field_name, depth + 1)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
RawBsonRef::JavaScriptCodeWithScope(code_with_scope) => {
|
||||||
|
validate_raw_document(code_with_scope.scope, doc_offset, field_name, depth + 1)
|
||||||
|
}
|
||||||
|
_ => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_raw_document(
|
||||||
|
doc: &RawDocument,
|
||||||
|
doc_offset: u64,
|
||||||
|
field_name: &str,
|
||||||
|
depth: usize,
|
||||||
|
) -> Result<(), BsonCeoShortsError> {
|
||||||
|
if depth > MAX_BSON_VALIDATION_DEPTH {
|
||||||
|
return Err(BsonCeoShortsError::CorruptBson {
|
||||||
|
offset: doc_offset,
|
||||||
|
detail: format!(
|
||||||
|
"field '{field_name}': nested BSON depth exceeds limit {MAX_BSON_VALIDATION_DEPTH}"
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for elem_res in doc.iter_elements() {
|
||||||
|
let elem = elem_res.map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||||
|
offset: doc_offset,
|
||||||
|
detail: format!("field '{field_name}': {err}"),
|
||||||
|
})?;
|
||||||
|
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||||
|
offset: doc_offset,
|
||||||
|
detail: format!("field '{field_name}': {err}"),
|
||||||
|
})?;
|
||||||
|
validate_raw_bson_value(val, doc_offset, field_name, depth)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn decode_ceo_shorts_doc_at<'a, F>(
|
fn decode_ceo_shorts_doc_at<'a, F>(
|
||||||
doc: &'a RawDocument,
|
doc: &'a RawDocument,
|
||||||
doc_offset: u64,
|
doc_offset: u64,
|
||||||
|
|
@ -351,20 +417,22 @@ where
|
||||||
let mut net_change_state = OptionalNumState::None;
|
let mut net_change_state = OptionalNumState::None;
|
||||||
let mut pct_float_state = OptionalNumState::None;
|
let mut pct_float_state = OptionalNumState::None;
|
||||||
|
|
||||||
// Scan all elements to completion so structural BSON corruption is never masked by an earlier
|
// Scan and validate every element and nested raw value to completion so structural BSON
|
||||||
// semantic skip.
|
// corruption (including invalid UTF-8 in unused fields or malformed nested documents/arrays)
|
||||||
|
// always fails closed and is never masked by an earlier semantic skip.
|
||||||
for elem_res in doc.iter_elements() {
|
for elem_res in doc.iter_elements() {
|
||||||
let elem = elem_res.map_err(|err| BsonCeoShortsError::CorruptBson {
|
let elem = elem_res.map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||||
offset: doc_offset,
|
offset: doc_offset,
|
||||||
detail: err.to_string(),
|
detail: err.to_string(),
|
||||||
})?;
|
})?;
|
||||||
let name = elem.key().as_str();
|
let name = elem.key().as_str();
|
||||||
|
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
||||||
|
offset: doc_offset,
|
||||||
|
detail: format!("field '{name}': {err}"),
|
||||||
|
})?;
|
||||||
|
validate_raw_bson_value(val, doc_offset, name, 0)?;
|
||||||
|
|
||||||
if name == fields.key {
|
if name == fields.key {
|
||||||
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
|
||||||
offset: doc_offset,
|
|
||||||
detail: format!("field '{}': {err}", fields.key),
|
|
||||||
})?;
|
|
||||||
key_state = match val {
|
key_state = match val {
|
||||||
RawBsonRef::String(s) => {
|
RawBsonRef::String(s) => {
|
||||||
if s.trim().is_empty() {
|
if s.trim().is_empty() {
|
||||||
|
|
@ -376,28 +444,12 @@ where
|
||||||
_ => KeyState::WrongType,
|
_ => KeyState::WrongType,
|
||||||
};
|
};
|
||||||
} else if name == fields.report_date {
|
} else if name == fields.report_date {
|
||||||
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
|
||||||
offset: doc_offset,
|
|
||||||
detail: format!("field '{}': {err}", fields.report_date),
|
|
||||||
})?;
|
|
||||||
date_state = decode_report_date_val(val);
|
date_state = decode_report_date_val(val);
|
||||||
} else if name == fields.shares_short {
|
} else if name == fields.shares_short {
|
||||||
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
|
||||||
offset: doc_offset,
|
|
||||||
detail: format!("field '{}': {err}", fields.shares_short),
|
|
||||||
})?;
|
|
||||||
shares_state = decode_required_number(val);
|
shares_state = decode_required_number(val);
|
||||||
} else if fields.net_change == Some(name) {
|
} else if fields.net_change == Some(name) {
|
||||||
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
|
||||||
offset: doc_offset,
|
|
||||||
detail: format!("field '{name}': {err}"),
|
|
||||||
})?;
|
|
||||||
net_change_state = decode_optional_number(val);
|
net_change_state = decode_optional_number(val);
|
||||||
} else if fields.pct_float == Some(name) {
|
} else if fields.pct_float == Some(name) {
|
||||||
let val = elem.value().map_err(|err| BsonCeoShortsError::CorruptBson {
|
|
||||||
offset: doc_offset,
|
|
||||||
detail: format!("field '{name}': {err}"),
|
|
||||||
})?;
|
|
||||||
pct_float_state = decode_optional_number(val);
|
pct_float_state = decode_optional_number(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -505,3 +505,119 @@ fn framing_errors_return_typed_errors_with_offsets() {
|
||||||
let err = decode_ceo_shorts_bytes(&extra, &fields, |_| {}).unwrap_err();
|
let err = decode_ceo_shorts_bytes(&extra, &fields, |_| {}).unwrap_err();
|
||||||
assert!(matches!(err, BsonCeoShortsError::CorruptDocumentLength { .. }));
|
assert!(matches!(err, BsonCeoShortsError::CorruptDocumentLength { .. }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn corrupt_unused_string_embedded_doc_and_array_fail_closed() {
|
||||||
|
use bson::raw::RawArrayBuf;
|
||||||
|
|
||||||
|
let fields = default_fields();
|
||||||
|
let d_secs = i64::from(day(2024, 3, 15)) * 86_400;
|
||||||
|
|
||||||
|
// 1. Valid unused nested doc & array still decode cleanly.
|
||||||
|
let mut inner = RawDocumentBuf::new();
|
||||||
|
inner.append(cstr!("tag"), "clean");
|
||||||
|
let mut arr = RawArrayBuf::new();
|
||||||
|
arr.push("ok");
|
||||||
|
arr.push(inner.clone());
|
||||||
|
|
||||||
|
let mut valid_doc = RawDocumentBuf::new();
|
||||||
|
valid_doc.append(cstr!("symbol"), "RY");
|
||||||
|
valid_doc.append(cstr!("timestamp"), d_secs);
|
||||||
|
valid_doc.append(cstr!("sharesShort"), 100_i32);
|
||||||
|
valid_doc.append(cstr!("note"), "hello");
|
||||||
|
valid_doc.append(cstr!("meta"), inner.clone());
|
||||||
|
valid_doc.append(cstr!("tags"), arr);
|
||||||
|
|
||||||
|
let mut emitted = 0usize;
|
||||||
|
let stats = decode_ceo_shorts_bytes(valid_doc.as_bytes(), &fields, |_| emitted += 1)
|
||||||
|
.expect("valid nested doc/array should decode");
|
||||||
|
assert_conservation(&stats);
|
||||||
|
assert_eq!(emitted, 1);
|
||||||
|
|
||||||
|
// 2. Invalid UTF-8 in an unused string field fails closed (0 rows emitted).
|
||||||
|
let mut bad_utf8_doc = RawDocumentBuf::new();
|
||||||
|
bad_utf8_doc.append(cstr!("symbol"), "RY");
|
||||||
|
bad_utf8_doc.append(cstr!("timestamp"), d_secs);
|
||||||
|
bad_utf8_doc.append(cstr!("sharesShort"), 100_i32);
|
||||||
|
bad_utf8_doc.append(cstr!("unusedNote"), "MARKER");
|
||||||
|
let mut bytes = bad_utf8_doc.into_bytes();
|
||||||
|
let pos = bytes
|
||||||
|
.windows(6)
|
||||||
|
.position(|w| w == b"MARKER")
|
||||||
|
.expect("locate MARKER");
|
||||||
|
bytes[pos] = 0xff;
|
||||||
|
bytes[pos + 1] = 0xfe;
|
||||||
|
|
||||||
|
let mut emitted = 0usize;
|
||||||
|
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| emitted += 1).unwrap_err();
|
||||||
|
assert_eq!(emitted, 0);
|
||||||
|
assert!(
|
||||||
|
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||||
|
"expected CorruptBson for invalid UTF-8 in unused string, got {err:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. Malformed element inside an unused embedded document fails closed.
|
||||||
|
let mut bad_inner = RawDocumentBuf::new();
|
||||||
|
bad_inner.append(cstr!("nestedText"), "INNER!");
|
||||||
|
let mut bad_nested_doc = RawDocumentBuf::new();
|
||||||
|
bad_nested_doc.append(cstr!("symbol"), "RY");
|
||||||
|
bad_nested_doc.append(cstr!("timestamp"), d_secs);
|
||||||
|
bad_nested_doc.append(cstr!("sharesShort"), 100_i32);
|
||||||
|
bad_nested_doc.append(cstr!("meta"), bad_inner);
|
||||||
|
let mut bytes = bad_nested_doc.into_bytes();
|
||||||
|
let pos = bytes
|
||||||
|
.windows(6)
|
||||||
|
.position(|w| w == b"INNER!")
|
||||||
|
.expect("locate INNER!");
|
||||||
|
bytes[pos] = 0xff;
|
||||||
|
|
||||||
|
let mut emitted = 0usize;
|
||||||
|
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| emitted += 1).unwrap_err();
|
||||||
|
assert_eq!(emitted, 0);
|
||||||
|
assert!(
|
||||||
|
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||||
|
"expected CorruptBson for invalid UTF-8 in embedded doc, got {err:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// 4. Corrupt element type byte inside an unused embedded document fails closed.
|
||||||
|
let mut bad_type_inner = RawDocumentBuf::new();
|
||||||
|
bad_type_inner.append(cstr!("k"), 42_i32);
|
||||||
|
let mut bad_type_doc = RawDocumentBuf::new();
|
||||||
|
bad_type_doc.append(cstr!("symbol"), "RY");
|
||||||
|
bad_type_doc.append(cstr!("timestamp"), d_secs);
|
||||||
|
bad_type_doc.append(cstr!("sharesShort"), 100_i32);
|
||||||
|
bad_type_doc.append(cstr!("meta"), bad_type_inner);
|
||||||
|
let mut bytes = bad_type_doc.into_bytes();
|
||||||
|
let k_pos = bytes
|
||||||
|
.windows(3)
|
||||||
|
.position(|w| w == [0x10, b'k', 0x00])
|
||||||
|
.expect("locate nested int32 element");
|
||||||
|
bytes[k_pos] = 0x7e;
|
||||||
|
|
||||||
|
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||||
|
"expected CorruptBson for corrupt element type in embedded doc, got {err:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// 5. Invalid UTF-8 inside an unused array element fails closed.
|
||||||
|
let mut bad_arr = RawArrayBuf::new();
|
||||||
|
bad_arr.push("ARRVAL");
|
||||||
|
let mut bad_arr_doc = RawDocumentBuf::new();
|
||||||
|
bad_arr_doc.append(cstr!("symbol"), "RY");
|
||||||
|
bad_arr_doc.append(cstr!("timestamp"), d_secs);
|
||||||
|
bad_arr_doc.append(cstr!("sharesShort"), 100_i32);
|
||||||
|
bad_arr_doc.append(cstr!("tags"), bad_arr);
|
||||||
|
let mut bytes = bad_arr_doc.into_bytes();
|
||||||
|
let pos = bytes
|
||||||
|
.windows(6)
|
||||||
|
.position(|w| w == b"ARRVAL")
|
||||||
|
.expect("locate ARRVAL");
|
||||||
|
bytes[pos] = 0xff;
|
||||||
|
|
||||||
|
let err = decode_ceo_shorts_bytes(&bytes, &fields, |_| {}).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
matches!(err, BsonCeoShortsError::CorruptBson { offset: 0, .. }),
|
||||||
|
"expected CorruptBson for invalid UTF-8 in unused array, got {err:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1336,13 +1336,15 @@ fn main() -> ExitCode {
|
||||||
report.decode.nulled_wrong_type_pct_float
|
report.decode.nulled_wrong_type_pct_float
|
||||||
);
|
);
|
||||||
println!(
|
println!(
|
||||||
" curate: elapsed={:.2} ms throughput={:.0} rows/s rows_out={} ({:.2}% retained) listings_out={} dropped_unmapped={} coll_collapsed={} coll_dropped={} invalid_shares={} checksum=0x{:016x}",
|
" curate: elapsed={:.2} ms throughput={:.0} rows/s rows_out={} ({:.2}% retained) listings_out={} dropped_unmapped={} dup_collapsed={} dup_conflict={} coll_collapsed={} coll_dropped={} invalid_shares={} checksum=0x{:016x}",
|
||||||
report.curate_elapsed_ms,
|
report.curate_elapsed_ms,
|
||||||
report.curate_rows_per_sec,
|
report.curate_rows_per_sec,
|
||||||
report.rows_out,
|
report.rows_out,
|
||||||
retention_pct,
|
retention_pct,
|
||||||
report.listings_out,
|
report.listings_out,
|
||||||
report.curate_stats.dropped_unmapped,
|
report.curate_stats.dropped_unmapped,
|
||||||
|
report.curate_stats.builder.duplicate_collapsed,
|
||||||
|
report.curate_stats.builder.duplicate_conflict_dropped,
|
||||||
report.curate_stats.listing_collision_collapsed,
|
report.curate_stats.listing_collision_collapsed,
|
||||||
report.curate_stats.listing_collision_dropped,
|
report.curate_stats.listing_collision_dropped,
|
||||||
report.curate_stats.builder.dropped_invalid_shares_short,
|
report.curate_stats.builder.dropped_invalid_shares_short,
|
||||||
|
|
|
||||||
|
|
@ -400,4 +400,88 @@ fn failure_modes_exit_nonzero_without_usage_and_preserve_preexisting_parquet() {
|
||||||
assert!(out.contains("dropped_unmapped=1"), "{out}");
|
assert!(out.contains("dropped_unmapped=1"), "{out}");
|
||||||
assert!(out.contains("rows_written=0"), "{out}");
|
assert!(out.contains("rows_written=0"), "{out}");
|
||||||
assert_eq!(fs::read(&sentinel_path).unwrap(), sentinel);
|
assert_eq!(fs::read(&sentinel_path).unwrap(), sentinel);
|
||||||
|
|
||||||
|
// 5. Corrupt UTF-8 in an unused text field of an otherwise valid, mapped report ->
|
||||||
|
// must fail closed with non-zero exit and preserve pre-existing shorts/part.parquet.
|
||||||
|
let mut unused_corrupt_doc = RawDocumentBuf::new();
|
||||||
|
unused_corrupt_doc.append(cstr!("symbol"), "RY");
|
||||||
|
unused_corrupt_doc.append(
|
||||||
|
cstr!("timestamp"),
|
||||||
|
BsonDateTime::from_millis(i64::from(day(2024, 3, 15)) * 86_400_000),
|
||||||
|
);
|
||||||
|
unused_corrupt_doc.append(cstr!("sharesShort"), 100.0_f64);
|
||||||
|
unused_corrupt_doc.append(cstr!("unusedNote"), "CORRUPT");
|
||||||
|
let mut unused_corrupt_bytes = unused_corrupt_doc.into_bytes();
|
||||||
|
let pos = unused_corrupt_bytes
|
||||||
|
.windows(7)
|
||||||
|
.position(|w| w == b"CORRUPT")
|
||||||
|
.expect("locate CORRUPT marker");
|
||||||
|
unused_corrupt_bytes[pos] = 0xff;
|
||||||
|
unused_corrupt_bytes[pos + 1] = 0xfe;
|
||||||
|
|
||||||
|
let unused_corrupt_bson = dir.path.join("unused_corrupt.bson");
|
||||||
|
write_bytes(&unused_corrupt_bson, &unused_corrupt_bytes);
|
||||||
|
let res = run(&[
|
||||||
|
"curate-shorts",
|
||||||
|
"--ceo-shorts",
|
||||||
|
unused_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(), "corrupt unused string 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_eq!(
|
||||||
|
fs::read(&sentinel_path).unwrap(),
|
||||||
|
sentinel,
|
||||||
|
"existing shorts/part.parquet must remain byte-identical"
|
||||||
|
);
|
||||||
|
|
||||||
|
// 6. Malformed element inside an unused embedded document -> must also fail closed.
|
||||||
|
let mut inner = RawDocumentBuf::new();
|
||||||
|
inner.append(cstr!("k"), 7_i32);
|
||||||
|
let mut nested_corrupt_doc = RawDocumentBuf::new();
|
||||||
|
nested_corrupt_doc.append(cstr!("symbol"), "RY");
|
||||||
|
nested_corrupt_doc.append(
|
||||||
|
cstr!("timestamp"),
|
||||||
|
BsonDateTime::from_millis(i64::from(day(2024, 3, 15)) * 86_400_000),
|
||||||
|
);
|
||||||
|
nested_corrupt_doc.append(cstr!("sharesShort"), 100.0_f64);
|
||||||
|
nested_corrupt_doc.append(cstr!("meta"), inner);
|
||||||
|
let mut nested_corrupt_bytes = nested_corrupt_doc.into_bytes();
|
||||||
|
let k_pos = nested_corrupt_bytes
|
||||||
|
.windows(3)
|
||||||
|
.position(|w| w == [0x10, b'k', 0x00])
|
||||||
|
.expect("locate nested int32 element");
|
||||||
|
nested_corrupt_bytes[k_pos] = 0x7e;
|
||||||
|
|
||||||
|
let nested_corrupt_bson = dir.path.join("nested_corrupt.bson");
|
||||||
|
write_bytes(&nested_corrupt_bson, &nested_corrupt_bytes);
|
||||||
|
let res = run(&[
|
||||||
|
"curate-shorts",
|
||||||
|
"--ceo-shorts",
|
||||||
|
nested_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(), "corrupt embedded doc 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_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