Skip to content
Merged
Show file tree
Hide file tree
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
55 changes: 54 additions & 1 deletion crates/devtools/src/planner_fuzz/relations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,12 @@ fn with_operator(node: &OperatorNode, operator: Operator) -> Rc<OperatorNode> {
}

/// `q`'s estimate when it is `q`'s root or the only input of a root
/// `Project` of plain column references: the shapes in which another
/// `Project` of plain column references (or of a chain of them, folded by
/// [`column_projections_folded`]): the shapes in which another
/// estimate can stand in for it without changing what the nodes above
/// compute. Returns its state and statistic.
fn root_estimate(root: &Rc<OperatorNode>) -> Result<(Rc<OperatorNode>, SketchStatistic), String> {
let root = &column_projections_folded(root);
let estimate = match root.non_asap() {
Some(NonASAPOp::Project { cols, child, .. })
if cols
Expand All @@ -931,6 +933,56 @@ fn root_estimate(root: &Rc<OperatorNode>) -> Result<(Rc<OperatorNode>, SketchSta
}
}

/// `root` with a chain of `Project`s of plain column references folded
/// into one: SQL puts a grouped estimate under two (`Project [city, host,
/// v]` over `Project [city, host, quantile AS "percentile_cont(...)"]` over
/// the `SummaryEstimate`), and the outer one picks columns of the inner
/// one's output. A folded column keeps the outer alias, else the inner one. A
/// root that is not such a chain is returned as it is.
fn column_projections_folded(root: &Rc<OperatorNode>) -> Rc<OperatorNode> {
let plain_columns = |cols: &[ProjectItem]| {
cols.iter()
.map(|item| match item.expr {
ScalarExpr::Column(index) => Some(index),
_ => None,
})
.collect::<Option<Vec<usize>>>()
};
let Some(NonASAPOp::Project { cols, child }) = root.non_asap() else {
return root.clone();
};
let child = column_projections_folded(child);
let (
Some(outer),
Some(NonASAPOp::Project {
cols: inner_cols,
child: grandchild,
}),
) = (plain_columns(cols), child.non_asap())
else {
return root.clone();
};
let Some(inner) = plain_columns(inner_cols) else {
return root.clone();
};
let folded = outer
.iter()
.zip(cols)
.map(|(&index, item)| ProjectItem {
alias: item
.alias
.clone()
.or_else(|| inner_cols[index].alias.clone()),
expr: ScalarExpr::Column(inner[index]),
})
.collect();
OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Project {
cols: folded,
child: grandchild.clone(),
}))
.unwrap_or_else(|_| root.clone())
}

/// `root` with its root estimate ([`root_estimate`]) replaced by
/// `estimate`; a root `Project` keeps its columns by name, except the
/// `dropped` group labels (a rollup's), which it loses. Any other column
Expand All @@ -940,6 +992,7 @@ fn with_root_estimate(
estimate: Rc<OperatorNode>,
dropped: &[String],
) -> Result<Rc<OperatorNode>, String> {
let root = &column_projections_folded(root);
let Some(NonASAPOp::Project { cols, child }) = root.non_asap() else {
return Ok(estimate);
};
Expand Down
86 changes: 56 additions & 30 deletions crates/devtools/tests/planner_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ fn check(relation: Relation, workload: FuzzWorkload, target: usize) -> Outcome {
)
}

/// #902: Q3 = p90 per city; the same query for `city = 'city1'` built its
/// own KLL instead of selecting the group from Q3's result. #943 fixed the
/// issue's ungrouped case and closed #902; this grouped variant
/// (`… AND city = 'city1' GROUP BY city`) holds only since #952's
/// finer-aggregate reuse (#950), and is now its regression test.
/// After the current cost refits it fails again: the planner's plan costs
/// about 0.080/s more than the witness's (which adds about 0.00001/s), past
/// the tolerance. That gap is the one #891 tracks.
/// #902: Q3 = p90 per city; the same query for `city = 'city1'` should
/// select its group from Q3's finished result. The witness does (one
/// `Filter` over Q3's estimates, about 0.00001/s). The planner has no such
/// candidate (#891): its plan filters Q3's KLL states, merges them by city
/// and reads out a quantile again, about 0.080/s more than W, past the
/// tolerance (1% of the larger side, 0.0197/s). Before the measured unit
/// costs, that path fit the tolerance.
#[test]
#[ignore = "planner bug: #891"]
fn finds_issue_902_filter_on_group_key_not_reading_the_grouped_result() {
let mut workload = seed("issue902.json");
let per_city = SqlSpec {
Expand All @@ -63,18 +63,17 @@ fn finds_issue_902_filter_on_group_key_not_reading_the_grouped_result() {
);
}

/// #891: the per-pod CPU rate query filtered on a group key now reads the
/// grouped rate: the related plan filters the grouped sum's exact states,
/// then merges and finalizes them (`Filter`, `SummaryMerge`,
/// `FinalizeExactAccumulator`), adding 0.000100/s to W's 0.008500/s. The
/// witness filters the finished result instead, adding 0.000035/s. The
/// gap that remains, open in #891, is pinned here: the planner's plan
/// (0.008600/s) costs 0.000065/s more than the witness's (0.008535/s), at
/// most the tolerance (1% of W, 0.000086/s). After the current cost refits
/// it fails: the planner's plan costs about 0.0925/s more than the
/// witness's (which adds about 0.00004/s), past the tolerance. Tracked in
/// #891.
/// #891: the per-container CPU rate filtered on `cluster` should read the
/// grouped rate's finished result through one `Filter`. The witness does:
/// it adds 0.000039/s to W's 0.421706/s. The planner's closest candidate is
/// the rollup over that `Filter`, a `Filter` and then a sum by the same
/// four labels, which regroups 1,000 rows into the same 1,000 groups. At
/// the measured unit costs that sum is charged 104 operations per row plus
/// one visit per group, 0.092468/s at one run every 30 s, so the planner's
/// plan adds 0.092502/s, past the tolerance (1% of 0.514207/s, 0.005142/s).
/// A candidate that is the `Filter` alone is missing (#891).
#[test]
#[ignore = "planner bug: #891"]
fn issue_891_filtered_rate_reads_the_grouped_rate() {
let mut workload = seed("issue891.workload.json");
let grouped = PromqlSpec {
Expand Down Expand Up @@ -219,20 +218,47 @@ fn witness_of(relation: Relation, workload: FuzzWorkload) -> Witness {
}

/// The sketch rollup witness builds and costs for a grouped quantile:
/// a `SummaryMerge` with `group_by` over the finer per-group KLL.
/// a `SummaryMerge` with `group_by` over the finer per-group KLL. The
/// query must select a KLL, or there is no sketch to roll up. The
/// generated PromQL `quantile by (job, instance)` reads only each series'
/// latest samples (a 15 s range), too few rows for a sketch, and plans an
/// exact quantile. p90 per (city, host) over 1 h at 100,000 rows/s
/// (4 cities x 5 hosts, ε 0.05, δ 0.01) reads a KLL tumbling-window
/// chain, and p90 per city rolls up from it.
#[test]
fn sketch_rollup_witness_costs_for_a_grouped_quantile() {
let q = promql(
"m_gauge",
None,
Some((OuterOp::Quantile(0.9), &["job", "instance"])),
);
assert_eq!(
witness_of(Relation::CoarserGrouping, generated(q, true)),
Witness::SketchRollup {
labels: vec!["job".into()]
use asap_types::accuracy::AccuracyTarget;
let mut workload = seed("issue902.json");
let per_city_and_host = SqlSpec {
table: "metrics".into(),
aggregate: SqlAggregate::Quantile(0.9),
group_by: vec!["city".into(), "host".into()],
filters: vec![],
window_ms: 3_600_000,
};
workload.queries = vec![workload.queries[2]
.with_spec(QuerySpec::Sql(per_city_and_host))
.with_accuracy(AccuracyTarget::max_error(0.05).failure_probability(0.01))];
workload.data = serde_json::from_value(serde_json::json!({
"arrival": {"ingesting": {"rows_per_second": 100_000.0}},
"sources": {"metrics": {
"series_count": 100,
"distinct_values_per_label": {"city": 4, "host": 5},
}},
}))
.expect("the data facts deserialize");
match check(Relation::CoarserGrouping, workload, 0) {
Outcome::Holds(m) | Outcome::Violated(m) => {
assert!(m.base_plan.contains("Kll"), "{}", m.base_plan);
assert_eq!(
m.witness.and_then(|w| w.derivation),
Some(Witness::SketchRollup {
labels: vec!["city".into()]
})
);
}
);
Outcome::Inconclusive { reason } => panic!("CoarserGrouping is inconclusive: {reason}"),
}
}

/// The exact rollup witness builds and costs for a grouped count (a sum
Expand Down
Loading