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
85 changes: 10 additions & 75 deletions crates/asap_types/src/capability_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ pub enum AccuracyTarget {

/// Returns the aggregation types that can serve this statistic.
///
/// This list is the **superset of compatibility**: every `AggregationType`
/// that the planner's canonical map (`promql_utilities::query_logics::logics::
/// map_statistic_to_precompute_operator`) may legally produce for this
/// statistic — across both `Exact` and `Approximate` treatment types — must
/// appear here. The agreement is enforced by
/// `capability_canonical_map_agreement` in the test module: any future
/// divergence between this table and `map_statistic_to_precompute_operator`
/// will be caught at test-time.
/// This list is the **superset of compatibility** and, as of the
/// `promql_utilities` retirement, the **single source of truth** for it —
/// there used to be a second, independently-maintained table
/// (`promql_utilities::query_logics::logics::map_statistic_to_precompute_operator`,
/// the planner's own canonical map) that this one had to agree with,
/// checked by a `capability_canonical_map_agreement` test. That table was
/// dead code (a Python-planner relic — nothing in Rust ever called it
/// except that one test) and was deleted; this is now the only table.
///
/// The runtime caller (`find_compatible_aggregation`) has no
/// `QueryTreatmentType` to consult — `QueryRequirements` is treatment-agnostic
Expand All @@ -134,8 +134,7 @@ pub enum AccuracyTarget {
/// `aggregation_priority` (largest window size wins).
pub fn compatible_agg_types(stat: Statistic) -> &'static [AggregationType] {
match stat {
// Sum: exact via Sum / MultipleSum; approximate via CountMinSketch
// (the canonical approximator picked by `map_statistic_to_precompute_operator`).
// Sum: exact via Sum / MultipleSum; approximate via CountMinSketch.
// Pre-fix this list omitted CountMinSketch, so a `sum_over_time(...)`
// query against a CMS-only config fell through capability matching
// and onto the cold tier.
Expand Down Expand Up @@ -1057,69 +1056,6 @@ mod tests {
assert!(result.is_none());
}

// -----------------------------------------------------------------------
// Source-of-truth agreement check.
//
// `compatible_agg_types(Statistic)` (this file) and
// `promql_utilities::query_logics::logics::map_statistic_to_precompute_operator`
// are two views onto the same `(Statistic, AggregationType)` capability
// table. The planner emits configs from the canonical map; capability
// matching dispatches queries against the compat list. They MUST agree —
// every canonical map output for a given Statistic must be a member of
// `compatible_agg_types(Statistic)` — or queries the planner configured
// will silently fall through capability matching to the cold-tier
// fallback.
//
// This test enumerates every supported `(Statistic, QueryTreatmentType)`
// pair, calls the canonical map, and asserts membership. Any future edit
// on either side that breaks the agreement fails the build.
// -----------------------------------------------------------------------

#[test]
fn capability_canonical_map_agreement() {
use promql_utilities::query_logics::enums::QueryTreatmentType;
use promql_utilities::query_logics::logics::map_statistic_to_precompute_operator;

// Listed exhaustively so adding a new `Statistic` variant fails to
// compile here (forcing the author to decide its compat membership).
let stats = [
Statistic::Count,
Statistic::Sum,
Statistic::Cardinality,
Statistic::Increase,
Statistic::Rate,
Statistic::Min,
Statistic::Max,
Statistic::Quantile,
Statistic::Topk,
];
let treatments = [QueryTreatmentType::Exact, QueryTreatmentType::Approximate];

for &stat in &stats {
let compat = compatible_agg_types(stat);
for &treat in &treatments {
match map_statistic_to_precompute_operator(stat, treat) {
Ok((agg_type, _sub_type)) => {
assert!(
compat.contains(&agg_type),
"Divergence: map_statistic_to_precompute_operator({stat:?}, {treat:?}) \
returns {agg_type:?}, but compatible_agg_types({stat:?}) = {compat:?} \
does not list it. Either add {agg_type:?} to compatible_agg_types or \
change the canonical map. See the docstring on \
compatible_agg_types for the source-of-truth invariant.",
);
}
Err(_) => {
// The canonical map declines this pair (e.g.
// Quantile-Exact, Cardinality, etc.). That's fine —
// capability_matching never sees a planner-emitted
// config for that pair, so there's nothing to agree on.
}
}
}
}
}

/// Pin the canonical-approximator picks driving the ASAP-tier query path
/// (the "five sketch types" CMS / KLL / HLL / DDSketch / CountSketch
/// canonical statistic table from PROGRESS.md). HLL / DDSketch /
Expand Down Expand Up @@ -1345,8 +1281,7 @@ mod tests {
assert_eq!(parse_storage_backend_engine_id("not_an_engine"), None);
}

/// Source-of-truth agreement check, mirrors
/// `capability_canonical_map_agreement` for the storage axis.
/// Source-of-truth agreement check for the storage axis.
///
/// For every `(Statistic, AccuracyTarget, StorageBackend)` triple
/// the returned backend list must be non-empty and its head must
Expand Down
206 changes: 0 additions & 206 deletions crates/promql_utilities/src/query_logics/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,6 @@ use std::fmt;
use std::str::FromStr;
use tracing::debug;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum QueryPatternType {
OnlyTemporal,
OnlySpatial,
OneTemporalOneSpatial,
}

impl std::fmt::Display for QueryPatternType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug!("Formatting QueryPatternType: {:?}", self);
match self {
QueryPatternType::OnlyTemporal => write!(f, "only_temporal"),
QueryPatternType::OnlySpatial => write!(f, "only_spatial"),
QueryPatternType::OneTemporalOneSpatial => write!(f, "one_temporal_one_spatial"),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum QueryTreatmentType {
Exact,
Approximate,
}

impl std::fmt::Display for QueryTreatmentType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug!("Formatting QueryTreatmentType: {:?}", self);
match self {
QueryTreatmentType::Exact => write!(f, "exact"),
QueryTreatmentType::Approximate => write!(f, "approximate"),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Statistic {
Count,
Expand Down Expand Up @@ -113,148 +79,6 @@ impl std::fmt::Display for QueryResultType {
}
}

/// A PromQL function that produces a vector-valued result.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PromQLFunction {
Rate,
Increase,
SumOverTime,
CountOverTime,
AvgOverTime,
MinOverTime,
MaxOverTime,
QuantileOverTime,
}

impl PromQLFunction {
pub fn as_str(self) -> &'static str {
match self {
PromQLFunction::Rate => "rate",
PromQLFunction::Increase => "increase",
PromQLFunction::SumOverTime => "sum_over_time",
PromQLFunction::CountOverTime => "count_over_time",
PromQLFunction::AvgOverTime => "avg_over_time",
PromQLFunction::MinOverTime => "min_over_time",
PromQLFunction::MaxOverTime => "max_over_time",
PromQLFunction::QuantileOverTime => "quantile_over_time",
}
}

/// Returns `true` for functions whose result requires approximate pre-aggregation.
pub fn is_approximate(self) -> bool {
matches!(
self,
PromQLFunction::QuantileOverTime
| PromQLFunction::SumOverTime
| PromQLFunction::CountOverTime
| PromQLFunction::AvgOverTime
)
}
}

impl std::fmt::Display for PromQLFunction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}

impl std::str::FromStr for PromQLFunction {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"rate" => Ok(PromQLFunction::Rate),
"increase" => Ok(PromQLFunction::Increase),
"sum_over_time" => Ok(PromQLFunction::SumOverTime),
"count_over_time" => Ok(PromQLFunction::CountOverTime),
"avg_over_time" => Ok(PromQLFunction::AvgOverTime),
"min_over_time" => Ok(PromQLFunction::MinOverTime),
"max_over_time" => Ok(PromQLFunction::MaxOverTime),
"quantile_over_time" => Ok(PromQLFunction::QuantileOverTime),
other => Err(format!("Unknown PromQL function: '{other}'")),
}
}
}

/// A PromQL aggregation operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AggregationOperator {
Sum,
Count,
Avg,
Quantile,
Min,
Max,
Topk,
}

impl AggregationOperator {
pub fn as_str(self) -> &'static str {
match self {
AggregationOperator::Sum => "sum",
AggregationOperator::Count => "count",
AggregationOperator::Avg => "avg",
AggregationOperator::Quantile => "quantile",
AggregationOperator::Min => "min",
AggregationOperator::Max => "max",
AggregationOperator::Topk => "topk",
}
}

/// Returns the `Statistic` values required to answer this operator.
/// `Avg` needs both `Sum` and `Count`.
pub fn to_statistics(self) -> Vec<Statistic> {
match self {
AggregationOperator::Avg => vec![Statistic::Sum, Statistic::Count],
AggregationOperator::Sum => vec![Statistic::Sum],
AggregationOperator::Count => vec![Statistic::Count],
AggregationOperator::Quantile => vec![Statistic::Quantile],
AggregationOperator::Min => vec![Statistic::Min],
AggregationOperator::Max => vec![Statistic::Max],
AggregationOperator::Topk => vec![Statistic::Topk],
}
}

pub fn as_str_slice() -> &'static [&'static str] {
&["sum", "count", "avg", "quantile", "min", "max", "topk"]
}
}

impl fmt::Display for AggregationOperator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl FromStr for AggregationOperator {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"sum" => Ok(AggregationOperator::Sum),
"count" => Ok(AggregationOperator::Count),
"avg" => Ok(AggregationOperator::Avg),
"quantile" => Ok(AggregationOperator::Quantile),
"min" => Ok(AggregationOperator::Min),
"max" => Ok(AggregationOperator::Max),
"topk" => Ok(AggregationOperator::Topk),
other => Err(format!("Unknown aggregation operator: '{other}'")),
}
}
}

impl AggregationOperator {
/// Returns `true` for operators whose result requires approximate pre-aggregation.
pub fn is_approximate(self) -> bool {
matches!(
self,
AggregationOperator::Quantile
| AggregationOperator::Sum
| AggregationOperator::Count
| AggregationOperator::Avg
| AggregationOperator::Topk
)
}
}

/// Concrete aggregation/sketch type used in precompute configs and accumulator dispatch.
///
/// `Display` outputs the canonical PascalCase name used in YAML/JSON configs.
Expand Down Expand Up @@ -413,33 +237,3 @@ impl<'de> Deserialize<'de> for AggregationType {
s.parse().map_err(serde::de::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_query_treatment_type_display() {
assert_eq!(QueryTreatmentType::Exact.to_string(), "exact");
assert_eq!(QueryTreatmentType::Approximate.to_string(), "approximate");
}

#[test]
fn test_query_treatment_type_serialization() {
let exact = QueryTreatmentType::Exact;
let approximate = QueryTreatmentType::Approximate;

// Test that they can be serialized/deserialized
let exact_str = serde_json::to_string(&exact).unwrap();
let approximate_str = serde_json::to_string(&approximate).unwrap();

assert_eq!(exact_str, "\"Exact\"");
assert_eq!(approximate_str, "\"Approximate\"");

let exact_back: QueryTreatmentType = serde_json::from_str(&exact_str).unwrap();
let approximate_back: QueryTreatmentType = serde_json::from_str(&approximate_str).unwrap();

assert_eq!(exact_back, QueryTreatmentType::Exact);
assert_eq!(approximate_back, QueryTreatmentType::Approximate);
}
}
Loading