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:
Studio 2026-11-29 03:55:01 -05:00
parent d4671f39d5
commit 0de120e7c8
6 changed files with 493 additions and 73 deletions

View file

@ -9129,6 +9129,8 @@ impl std::error::Error for CurateShortsCmdBenchError {
pub struct CurateShortsCmdBenchReport {
pub rows: usize,
pub key_count: usize,
pub min_report_date: Option<i32>,
pub max_report_date: Option<i32>,
pub docs_framed: u64,
pub bytes_read: u64,
pub peak_chunk_bytes: usize,
@ -9184,6 +9186,8 @@ fn run_curate_shorts_command_benchmark_tracked(
CurateShortsCmdBenchReport {
rows: 0,
key_count: 0,
min_report_date: None,
max_report_date: None,
docs_framed: 0,
bytes_read: 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.
// 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| {
CurateShortsCmdBenchError::FixtureIo {
@ -9292,13 +9310,48 @@ fn run_curate_shorts_command_benchmark_tracked(
let mut w = BufWriter::with_capacity(1024 * 1024, f);
let mut rng = SplitMix64::new(CURATE_SHORTS_CMD_BENCH_SEED);
for i in 0..rows {
let r = rng.next_u64();
let key_idx = i % key_count;
let date_idx = (i / key_count) % date_count;
let report_date = all_dates[date_idx];
let dup_identical_target = rows / 200;
let dup_conflict_target = rows / 200;
let alias_identical_target = rows / 200;
let alias_conflict_target = rows / 200;
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_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();
@ -9326,34 +9379,19 @@ fn run_curate_shorts_command_benchmark_tracked(
doc.append(cstr!("sharesShort"), "bad_num");
}
}
w.write_all(doc.as_bytes()).map_err(|source| {
CurateShortsCmdBenchError::FixtureIo {
path: bson_path.clone(),
source,
}
})?;
write_doc(&mut w, &doc)?;
emitted += 1;
continue;
}
// Pick key: occasional alias collision on first 200 keys
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}"));
}
doc.append(cstr!("symbol"), format!("CEO{key_idx:05}"));
// Mix report_date BSON encodings: DateTime, Int32 secs, Int64 secs, Int64 millis, integral Double
match i % 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),
}
append_date(&mut doc, i, report_date);
// ~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
} else {
((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);
// Optional netChange: present, null, absent, or occasional wrong type
match i % 10 {
0 => doc.append(cstr!("netChange"), RawBson::Null),
1 => {} // absent
2 if i % 100 == 2 => doc.append(cstr!("netChange"), "n/a"),
let net_change_val = match i % 10 {
0 => {
doc.append(cstr!("netChange"), RawBson::Null);
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;
doc.append(cstr!("netChange"), delta);
Some(delta)
}
}
};
// Optional pctFloat: present, null, absent, or occasional wrong type
match i % 12 {
0 => doc.append(cstr!("pctFloat"), RawBson::Null),
1 => {} // absent
3 if i % 120 == 3 => doc.append(cstr!("pctFloat"), true),
let pct_float_val = match i % 12 {
0 => {
doc.append(cstr!("pctFloat"), RawBson::Null);
None
}
1 => None, // absent
3 if i % 120 == 3 => {
doc.append(cstr!("pctFloat"), true);
None
}
_ => {
let pct = ((r >> 32) % 2500) as f64 / 100.0;
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()
.map_err(|source| CurateShortsCmdBenchError::FixtureIo {
path: bson_path.clone(),
@ -9442,6 +9596,8 @@ fn run_curate_shorts_command_benchmark_tracked(
let report = CurateShortsCmdBenchReport {
rows,
key_count,
min_report_date,
max_report_date,
docs_framed: timed.report.docs_framed,
bytes_read: timed.report.bytes_read,
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() {
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 all_dates = semi_monthly_report_dates();
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_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_ne!(a.checksum, 0);
assert_eq!(a.rows_out, b.rows_out);
@ -11371,7 +11530,14 @@ mod tests {
assert_eq!(a.bytes_written, b.bytes_written);
assert!(a.peak_chunk_bytes > 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.listing_collision_collapsed > 0);
assert!(a.curate_stats.listing_collision_dropped > 0);
// Conservation invariants
assert_eq!(a.docs_framed as usize, a.decode.docs_in);