Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions crates/executor/tests/parallel_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,279 @@ fn split_runs_charge_worker_cpu_and_span_waves() {
assert!(tasks > 0);
assert!(cpu > 0, "worker CPU not charged");
}

// Exact `Aggregate` split by group key over worker threads (spec
// `parallel-query-exec`, tests E1-E6): every measure, grouped, equals the
// one-thread run cell for cell and row for row.

use asap_executor::operators::Reduction;

/// Every exact measure over [`input`]'s columns.
fn every_measure() -> Vec<(String, Reduction)> {
vec![
("count".into(), Reduction::Count),
("sum_int".into(), Reduction::Sum(1)),
("sum_float".into(), Reduction::Sum(3)),
("avg".into(), Reduction::Avg(3)),
("min_float".into(), Reduction::Min(3)),
("max_text".into(), Reduction::Max(4)),
("distinct_item".into(), Reduction::Cardinality(vec![4])),
("distinct_pair".into(), Reduction::Cardinality(vec![4, 1])),
("l2".into(), Reduction::FrequencyL2(4)),
("entropy".into(), Reduction::FrequencyEntropy(4)),
(
"quantile".into(),
Reduction::Quantile { column: 3, q: 0.99 },
),
(
"percentile".into(),
Reduction::Percentile { column: 3, q: 0.5 },
),
(
"stddev".into(),
Reduction::StdDev {
column: 3,
population: false,
},
),
(
"variance".into(),
Reduction::Variance {
column: 3,
population: true,
},
),
("corr".into(), Reduction::PearsonCorr { left: 3, right: 5 }),
("group".into(), Reduction::Group),
]
}

// E1. Grouped by one key and by two (text and float), split 2, 3, 5 or 8
// ways: the same rows in the same order with the same bits, and the run
// ran partition tasks.
#[test]
fn grouped_aggregates_split_by_group_key_equal_the_sequential_aggregate() {
let inputs = rows(3, 8000, 300);
for groups in [vec![0], vec![0, 5], vec![5, 0]] {
let aggregate = || Operator::aggregate(input(), groups.clone(), every_measure()).unwrap();
let (sequential, tasks) = run(aggregate(), inputs.clone(), 1);
assert_eq!(tasks, 0);
let expected = fingerprint(&sequential);
assert!(expected.len() > 100, "{groups:?}");
for parallelism in [2, 3, 5, 8] {
let (parallel, tasks) = run(aggregate(), inputs.clone(), parallelism);
assert_eq!(
fingerprint(&parallel),
expected,
"{groups:?} x{parallelism}"
);
assert!(tasks > 0, "{groups:?} x{parallelism}: not split");
}
}
}

// E2. Measure filters (SQL FILTER), NULL values and a Float64 key with zero
// signs, NaN payloads and NULL: the same groups, first-row labels and
// measures as one thread.
#[test]
fn split_aggregates_keep_filters_nulls_and_first_labels() {
let schema = input();
let over_500 = Expression::planner(
CompiledExpression::compile(
&ScalarExpr::Compare {
semantics: planner_types::ir::ExprSemantics::Sql,
left: Box::new(ScalarExpr::Column(3)),
op: CompareOpKind::Gt,
right: Box::new(ScalarExpr::Literal(ScalarValue::Float64(500.0))),
},
&schema,
)
.unwrap(),
);
let aggregate = || {
Operator::aggregate(
schema.clone(),
vec![5],
vec![
("count".into(), Reduction::Count),
("sum".into(), Reduction::Sum(3)),
("distinct".into(), Reduction::Cardinality(vec![4])),
("p99".into(), Reduction::Percentile { column: 3, q: 0.99 }),
],
)
.unwrap()
.with_measure_filters(vec![
None,
Some(over_500.clone()),
Some(over_500.clone()),
None,
])
.unwrap()
};
let inputs = rows(4, 5000, 50);
let expected = fingerprint(&run(aggregate(), inputs.clone(), 1).0);
for parallelism in [2, 5] {
let (parallel, tasks) = run(aggregate(), inputs.clone(), parallelism);
assert_eq!(fingerprint(&parallel), expected, "x{parallelism}");
assert!(tasks > 0, "x{parallelism}: not split");
}
}

// E3. Aggregates that stay on one thread: no group column (a global
// aggregate, also over no row with `emit_empty_global`), and a small input.
// Same answer, no partition task.
#[test]
fn global_and_small_aggregates_stay_sequential() {
let global = || Operator::aggregate(input(), vec![], every_measure()).unwrap();
let inputs = rows(3, 8000, 300);
let expected = fingerprint(&run(global(), inputs.clone(), 1).0);
let (parallel, tasks) = run(global(), inputs, 8);
assert_eq!(fingerprint(&parallel), expected);
assert_eq!(tasks, 0);

let empty = vec![Batch::try_new(input(), vec![]).unwrap()];
let global_empty = || {
Operator::aggregate(input(), vec![], vec![("sum".into(), Reduction::Sum(3))])
.unwrap()
.with_emit_empty_global(true, None)
.unwrap()
};
let expected = fingerprint(&run(global_empty(), empty.clone(), 1).0);
assert_eq!(expected.len(), 1);
let (parallel, tasks) = run(global_empty(), empty, 8);
assert_eq!(fingerprint(&parallel), expected);
assert_eq!(tasks, 0);

let small = || Operator::aggregate(input(), vec![0, 1], every_measure()).unwrap();
let inputs = rows(2, 1000, 30);
let expected = fingerprint(&run(small(), inputs.clone(), 1).0);
let (parallel, tasks) = run(small(), inputs, 8);
assert_eq!(fingerprint(&parallel), expected);
assert_eq!(tasks, 0);
}

// E4. A split aggregate is charged its parts' groups and states: over a
// limit the one-thread run exceeds, it fails too.
#[test]
fn a_split_aggregate_over_the_memory_limit_fails() {
let aggregate = || {
Operator::aggregate(
input(),
vec![0, 1],
vec![
("distinct".into(), Reduction::Cardinality(vec![4])),
("q".into(), Reduction::Quantile { column: 3, q: 0.5 }),
],
)
.unwrap()
};
let limits = || Limits {
max_bytes: 256 << 10,
};
for parallelism in [1, 4] {
let result = run_with(aggregate(), rows(3, 8000, 300), parallelism, limits());
assert!(
matches!(result, Err(Error::MemoryLimit)),
"x{parallelism}: {:?}",
result.err()
);
}
}

// E5. 300,000 rows (two waves of the split): equal to one thread, with
// partition tasks and their worker-thread CPU charged to the run.
#[test]
fn split_aggregates_charge_worker_cpu_and_span_waves() {
let aggregate = || {
Operator::aggregate(
input(),
vec![0, 1],
vec![
("sum".into(), Reduction::Sum(3)),
("distinct".into(), Reduction::Cardinality(vec![4])),
],
)
.unwrap()
};
let inputs = rows(3, 100_000, 3000);
let (sequential, _, sequential_cpu) = run_with(
aggregate(),
inputs.clone(),
1,
Limits { max_bytes: 1 << 30 },
)
.unwrap();
assert_eq!(sequential_cpu, 0);
let (parallel, tasks, cpu) =
run_with(aggregate(), inputs, 4, Limits { max_bytes: 1 << 30 }).unwrap();
assert_eq!(fingerprint(&parallel), fingerprint(&sequential));
assert!(tasks >= 2, "{tasks} partition tasks over two waves");
assert!(cpu > 0, "worker CPU not charged");
}

/// `(target Utf8, user Utf8)`: `rows` rows, `hot` of them (a share) in one
/// target, users drawn from `users` values (the SB q3 shape: count of
/// distinct users per target, one target holding most rows).
fn skewed_rows(rows: usize, hot: f64, users: u64) -> Vec<Batch> {
let schema = Arc::new(Schema {
fields: vec![
field("target", DataType::Utf8, false),
field("user", DataType::Utf8, false),
],
time_index: None,
unique_keys: vec![],
closed: false,
});
let mut seed = 0x9e37_79b9_7f4a_7c15u64;
let mut next = move || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
seed
};
(0..rows)
.collect::<Vec<_>>()
.chunks(20_000)
.map(|chunk| {
let rows = chunk
.iter()
.map(|_| {
let r = next();
let target = if (r % 1000) as f64 / 1000.0 < hot {
0
} else {
1 + (r >> 10) % 2000
};
vec![
Value::Utf8(format!("t{target}").into()),
Value::Utf8(format!("u{}", (r >> 24) % users).into()),
]
})
.collect();
Batch::try_new(schema.clone(), rows).unwrap()
})
.collect()
}

// E6. Count distinct per target with one target holding 60% of 200,000
// rows over 50,000 users, split 4 or 16 ways: equal to one thread.
#[test]
fn a_split_count_distinct_equals_sequential_on_skewed_keys() {
let inputs = skewed_rows(200_000, 0.6, 50_000);
let schema = inputs[0].schema().clone();
let aggregate = || {
Operator::aggregate(
schema.clone(),
vec![0],
vec![("v".into(), Reduction::Cardinality(vec![1]))],
)
.unwrap()
};
let expected = fingerprint(&run(aggregate(), inputs.clone(), 1).0);
assert!(expected.len() > 1000);
for parallelism in [4, 16] {
let (parallel, tasks) = run(aggregate(), inputs.clone(), parallelism);
assert_eq!(fingerprint(&parallel), expected, "x{parallelism}");
assert!(tasks > 0, "x{parallelism}: not split");
}
}