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
123 changes: 19 additions & 104 deletions crates/cost/src/measured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ use std::mem::discriminant;

use asap_types::ir::operator::AggIntent;
use asap_types::ir::properties::ExecutionTiming;
use asap_types::ir::{ASAPOp, NonASAPOp, Operator};
use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload as Payload;
use asap_types::summary_type::{ExactKind, SketchParams};
use serde::{Deserialize, Serialize};

use crate::node::analytical::AnalyticalCostError;
/// The operator vocabulary the table is keyed by; it lives with the IR.
pub use asap_types::ir::operator::OperatorKind;

/// The deployment's measured cost table
/// ([`CostInputs::measured_operation_costs`]). In JSON, an object
Expand Down Expand Up @@ -105,7 +105,22 @@ pub struct OperatorCost {
/// when each of its measures has an entry.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub measure: Option<String>,
/// CPU nanoseconds per unit the operator processes ([`OperatorKind`]).
/// CPU nanoseconds per unit the operator processes. The unit is the
/// row the operator reads unless listed here:
///
/// * `Scan`: per sample read. At ingestion time, an arriving sample; at
/// query time, a sample fetched and decoded from the database holding
/// raw data, part of the deployment's cost;
/// * `Aggregate`: per input row, for each measure;
/// * `SummaryAgg`: per input row inserted into its group's state;
/// * `SummaryEstimate`: per group read;
/// * `FinalizeExactAccumulator`: per accumulator finalized (one per
/// group);
/// * `SummaryMerge`: per input state merged (each tumbling window of a
/// chain is a state);
/// * `SummaryTumblingWindowChain`: per input row placed in its tumbling
/// window and inserted into that window's state.
///
/// It includes what the formulas charge apart, at the measured shape:
/// the output rows a merge, an aggregate or an estimate builds per
/// group and the label columns a grouping carries per row. Example: a
Expand All @@ -126,98 +141,6 @@ pub struct OperatorCost {
pub memory_bytes_per_state: Option<f64>,
}

/// Every physical operator a physical plan DAG can contain, by the name it
/// has there. The unit each is priced per is the row it reads unless said
/// otherwise.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OperatorKind {
/// Per sample read. At ingestion time, an arriving sample; at query
/// time, a sample fetched and decoded from the database holding raw
/// data, part of the deployment's cost.
Scan,
Values,
Filter,
Project,
/// Per input row, for each measure.
Aggregate,
Join,
SetOp,
Concat,
Dedup,
Sort,
Limit,
BinaryOp,
SQLWindowFunc,
TimeRange,
TimeShift,
PromqlVectorFromScalar,
PromqlRelabel,
PromqlInfoEnrich,
PromqlSeriesSample,
PromqlSubquery,
/// Per input row inserted into its group's state.
SummaryAgg,
/// Per group read.
SummaryEstimate,
/// Per accumulator finalized (one per group).
FinalizeExactAccumulator,
MaintainPopulation,
EvaluatePopulation,
/// Per input state merged (each tumbling window of a chain is a state).
SummaryMerge,
/// Per input row placed in its tumbling window and inserted into that tumbling window's state.
// Named `PaneChain` before #1163.
#[serde(alias = "PaneChain")]
SummaryTumblingWindowChain,
SummarySubtract,
SummaryDelete,
SummaryJoin,
Extension,
}

impl OperatorKind {
/// The kind of `payload`.
pub fn of(payload: &Payload) -> Self {
match payload {
Operator::NonASAP(op) => match op {
NonASAPOp::Scan { .. } => Self::Scan,
NonASAPOp::Values { .. } => Self::Values,
NonASAPOp::Filter { .. } => Self::Filter,
NonASAPOp::Project { .. } => Self::Project,
NonASAPOp::Aggregate { .. } => Self::Aggregate,
NonASAPOp::Join { .. } => Self::Join,
NonASAPOp::SetOp { .. } => Self::SetOp,
NonASAPOp::Concat { .. } => Self::Concat,
NonASAPOp::Dedup { .. } => Self::Dedup,
NonASAPOp::Sort { .. } => Self::Sort,
NonASAPOp::Limit { .. } => Self::Limit,
NonASAPOp::BinaryOp { .. } => Self::BinaryOp,
NonASAPOp::SQLWindowFunc { .. } => Self::SQLWindowFunc,
NonASAPOp::TimeRange { .. } => Self::TimeRange,
NonASAPOp::TimeShift { .. } => Self::TimeShift,
NonASAPOp::PromqlVectorFromScalar(_) => Self::PromqlVectorFromScalar,
NonASAPOp::PromqlRelabel { .. } => Self::PromqlRelabel,
NonASAPOp::PromqlInfoEnrich { .. } => Self::PromqlInfoEnrich,
NonASAPOp::PromqlSeriesSample { .. } => Self::PromqlSeriesSample,
NonASAPOp::PromqlSubquery { .. } => Self::PromqlSubquery,
},
Operator::ASAP(op) => match op {
ASAPOp::SummaryAgg { .. } => Self::SummaryAgg,
ASAPOp::SummaryEstimate { .. } => Self::SummaryEstimate,
ASAPOp::FinalizeExactAccumulator { .. } => Self::FinalizeExactAccumulator,
ASAPOp::MaintainPopulation { .. } => Self::MaintainPopulation,
ASAPOp::EvaluatePopulation { .. } => Self::EvaluatePopulation,
ASAPOp::SummaryMerge { .. } => Self::SummaryMerge,
ASAPOp::SummaryTumblingWindowChain { .. } => Self::SummaryTumblingWindowChain,
ASAPOp::SummarySubtract { .. } => Self::SummarySubtract,
ASAPOp::SummaryDelete { .. } => Self::SummaryDelete,
ASAPOp::SummaryJoin { .. } => Self::SummaryJoin,
ASAPOp::Extension { .. } => Self::Extension,
},
}
}
}

/// The state family a summary node works on, which selects its entries.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StateFamily<'a> {
Expand Down Expand Up @@ -326,7 +249,7 @@ pub(crate) fn aggregate_lookup(
memory_bytes_per_state: None,
};
for measure in measures {
let name = measure_name(measure);
let name = measure.kind_name();
let find = |for_phase: Option<ExecutionTiming>| {
table.operators.iter().find(|e| {
e.operator == OperatorKind::Aggregate
Expand All @@ -341,14 +264,6 @@ pub(crate) fn aggregate_lookup(
(!measures.is_empty()).then_some(total)
}

/// An aggregate measure's name as an entry states it: its JSON `kind`.
fn measure_name(measure: &AggIntent) -> String {
serde_json::to_value(measure)
.ok()
.and_then(|value| value.get("kind")?.as_str().map(str::to_owned))
.unwrap_or_else(|| format!("{measure:?}"))
}

fn price_of(entry: &OperatorCost) -> UnitPrice {
UnitPrice {
ns_per_unit: entry.ns_per_unit,
Expand Down
4 changes: 3 additions & 1 deletion crates/devtools/src/bin/asap_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
// field takes its default).
// - `--deployment-capabilities <file>`: the `DeploymentCapabilities`
// (`supported_summaries`, `raw_data_retained`,
// `query_time_results_retained`; an omitted field takes its default).
// `query_time_results_retained`, `counter_semantics`,
// `state_not_mergeable_across_windows`, `excluded_candidates`; an
// omitted field takes its default).
// Without `supported_summaries`, the executor's summary families are
// supported, as `plan_installable` defaults it.
// - `--ignore-accuracy-guarantees`: Stage 3 accepts every query output
Expand Down
2 changes: 1 addition & 1 deletion crates/devtools/src/bin/sketch_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn analyze_corpus(
rejected += 1;
continue;
};
let Ok(pass1) = pass1_memo(memo, None) else {
let Ok(pass1) = pass1_memo(memo, &[]) else {
rejected += 1;
continue;
};
Expand Down
1 change: 1 addition & 0 deletions crates/devtools/src/planner_fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ impl FuzzWorkload {
),
raw_data_retained: self.raw_data_retained,
query_time_results_retained: self.query_time_results_retained,
..DeploymentCapabilities::default()
}
}

Expand Down
20 changes: 8 additions & 12 deletions crates/devtools/src/planning_stages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,13 @@ pub fn run_stages_until(
return Ok(stages);
}
let run = &mut stages.run;
let pass1 = pass1_memo(
stage0_dag,
models
.deployment_capabilities
.supported_summaries
.as_deref(),
)
// As `run_planning`: a query Pass 1 cannot plan is reported before
// contradictory deployment capabilities.
.map_err(|error| PlanningStagesError::Pass1(error.to_string()))?;
// As `run_planning`: the exclusions are expanded once, for Pass 1 and
// Stage 2.
let exclusions = models.deployment_capabilities.exclusions();
let pass1 = pass1_memo(stage0_dag, &exclusions)
// As `run_planning`: a query Pass 1 cannot plan is reported before
// contradictory deployment capabilities.
.map_err(|error| PlanningStagesError::Pass1(error.to_string()))?;
if let Err(error) = models.deployment_capabilities.validate(data) {
run.selection = Err(SelectionError::InvalidDeploymentCapabilities(error));
return Ok(stages);
Expand All @@ -288,8 +285,7 @@ pub fn run_stages_until(
return Ok(stages);
}
let logical = run.logical_asap_candidates.as_ref().expect("set above");
let physical =
generate_physical_asap_memo_candidates(logical, data, models.deployment_capabilities);
let physical = generate_physical_asap_memo_candidates(logical, data, &exclusions);
if until == RunUntil::Stage2 {
run.physical_asap_candidates = Some(physical);
return Ok(stages);
Expand Down
25 changes: 19 additions & 6 deletions crates/devtools/src/planning_stages_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use asap_types::stages::{
AggregateCoverage, CandidateForMemoGroup, CandidateForMemoGroupId, ColumnLineage,
EvaluationGrid, LogicalASAPMemoCandidates, LogicalEdge, Memo, MemoGroup, MemoGroupId,
PhysicalASAPMemoCandidates, PhysicalCandidate, PhysicalCandidateId, PhysicalEdge,
PhysicalProperties, PrunedCandidate, RequiredAccuracy, RootPruneReason,
PhysicalProperties, PruneReason, PrunedCandidate, RequiredAccuracy, RootPruneReason,
};
use asap_types::summary_type::SketchParams;

Expand Down Expand Up @@ -1281,13 +1281,14 @@ fn write_physical_pruned(
if !verbose {
let reasons: Vec<String> = pruned
.iter()
.map(|pruned| {
let reason = format!("{:?}", pruned.reason);
reason
.map(|pruned| match &pruned.reason {
// Counted per capability it comes from.
PruneReason::ExcludedByDeployment(_) => prune_reason_text(&pruned.reason),
reason => format!("{reason:?}")
.split(['(', ' ', '{'])
.next()
.unwrap_or_default()
.to_owned()
.to_owned(),
})
.collect();
if !reasons.is_empty() {
Expand All @@ -1305,7 +1306,7 @@ fn write_physical_pruned(
vec![
candidate_id_text(pruned.candidate.logical_candidate_id),
physical_properties_text(&pruned.candidate.physical_properties),
format!("{:?}", pruned.reason),
prune_reason_text(&pruned.reason),
]
})
.collect();
Expand Down Expand Up @@ -1394,6 +1395,18 @@ fn write_physical_by_logical_candidate(
}
}

/// A Stage 2 or Stage 3 prune reason: a deployment exclusion names the
/// capability it comes from as the capabilities' JSON does, e.g.
/// `ExcludedByDeployment(raw_data_not_retained)` or
/// `ExcludedByDeployment(excluded_candidates[0])`; any other reason is its
/// debug form, e.g. `DominatedBy(PhysicalCandidateId(3))`.
fn prune_reason_text(reason: &PruneReason) -> String {
match reason {
PruneReason::ExcludedByDeployment(source) => format!("ExcludedByDeployment({source})"),
reason => format!("{reason:?}"),
}
}

/// Stage 3's plan, each node with its physical candidate, operator and
/// input nodes; the plan's cost (`plan_cost`) with each node's charge; its
/// materialized views (`materialized_views`), one line each with its node,
Expand Down
9 changes: 8 additions & 1 deletion crates/devtools/tests/asap_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ fn aws_fargate_prices_example_4_in_dollars() {
/// the document's `deployment_capabilities` shows (defaults filled in;
/// without the flag, every summary supported, raw data retained, no
/// query-time result retained). For Pattern B with KLL only and query-time
/// results retained: Pass 1 offers no DDSketch candidate. Without raw data, a plan is still selected
/// results retained: Pass 1 offers no DDSketch candidate (the deployment
/// excludes every DDSketch state outright). Without raw data, a plan is still selected
/// with guarantees enforced, from materialized views. A deployment with an
/// unknown field is refused.
#[test]
Expand All @@ -794,6 +795,9 @@ fn deployment_capabilities_flag_sets_the_capabilities_and_shows_them() {
"supported_summaries": asap_executor::supported_summary_families(),
"raw_data_retained": true,
"query_time_results_retained": false,
"counter_semantics": { "reset_handling": true, "extrapolation": true },
"state_not_mergeable_across_windows": [],
"excluded_candidates": [],
})
);
let kll_kept = with_capabilities(
Expand All @@ -807,6 +811,9 @@ fn deployment_capabilities_flag_sets_the_capabilities_and_shows_them() {
"supported_summaries": [{ "summary_type": "Kll", "readouts": ["Quantile"] }],
"raw_data_retained": true,
"query_time_results_retained": true,
"counter_semantics": { "reset_handling": true, "extrapolation": true },
"state_not_mergeable_across_windows": [],
"excluded_candidates": [],
})
);
let has_ddsketch = |document: &Value| {
Expand Down
44 changes: 44 additions & 0 deletions crates/integration-tests/tests/crate_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,50 @@ fn workspace_crate_graph_follows_the_target_graph() {
assert!(violations.is_empty(), "{}", violations.join("\n"));
}

/// Planning reads its inputs only from its arguments: no planning stage or
/// the facade reads the process environment, so what a deployment
/// excludes travels in `DeploymentCapabilities` and cannot come back as an
/// environment-variable hook patched into a stage (such as a variable
/// naming candidates Stage 3 must not select). Every `.rs` file under the
/// stage crates', the shared types' and the facade's `src`.
#[test]
fn planning_crates_read_no_environment_variables() {
let workspace = concat!(env!("CARGO_MANIFEST_DIR"), "/../..");
let mut pending: Vec<std::path::PathBuf> = [
"types",
"stage0-memo",
"stage1-pass1-per-computation-candidates",
"stage1-pass2-reuse-candidates",
"stage2-physical-optimization",
"stage3-plan-selection",
"planner",
]
.iter()
.map(|name| {
std::path::Path::new(workspace)
.join("crates")
.join(name)
.join("src")
})
.collect();
let mut readers = Vec::new();
while let Some(path) = pending.pop() {
for entry in std::fs::read_dir(&path).expect("reads the source directory") {
let path = entry.expect("reads an entry").path();
if path.is_dir() {
pending.push(path);
} else if path.extension().is_some_and(|extension| extension == "rs") {
let text = std::fs::read_to_string(&path).expect("reads a source file");
// `env::var` also finds `env::var_os`.
if text.contains("env::var") {
readers.push(format!("{}: env::var", path.display()));
}
}
}
}
assert!(readers.is_empty(), "{}", readers.join("\n"));
}

/// A package of synthetic `cargo metadata` JSON with `[dependencies]` on
/// `deps` and `[dev-dependencies]` on `dev_deps`.
fn package(name: &str, deps: &[&str], dev_deps: &[&str]) -> Value {
Expand Down
Loading
Loading