|
| 1 | +//! Frontend-to-Pass-1 acceptance: candidate discovery precedes empirical selection. |
| 2 | +use asap_aware_mapping::{ |
| 3 | + logical_candidates::{ |
| 4 | + enumerate_local_logical_candidates, local_realizations_for_intent, LogicalCandidateError, |
| 5 | + }, |
| 6 | + Realization, |
| 7 | +}; |
| 8 | +use asap_types::{ |
| 9 | + ir::operator_properties::{Reduction, Source}, |
| 10 | + ir::{NonASAPOp, Operator, OperatorNode, QueryRoot, ScalarExpr}, |
| 11 | + post_asap::{ExactKind, SketchAlgorithm}, |
| 12 | + pre_asap::{AggIntent, DataType, Field, Schema}, |
| 13 | + types::AccuracyTarget, |
| 14 | +}; |
| 15 | +use std::rc::Rc; |
| 16 | + |
| 17 | +fn approximate() -> AccuracyTarget { |
| 18 | + AccuracyTarget::EpsilonDelta { |
| 19 | + epsilon: 0.05, |
| 20 | + delta: 0.01, |
| 21 | + } |
| 22 | +} |
| 23 | +fn algorithms(choices: &[Realization]) -> Vec<SketchAlgorithm> { |
| 24 | + choices |
| 25 | + .iter() |
| 26 | + .filter_map(|choice| match choice { |
| 27 | + Realization::Sketch(kind) => Some(kind.algorithm().clone()), |
| 28 | + _ => None, |
| 29 | + }) |
| 30 | + .collect() |
| 31 | +} |
| 32 | +fn aggregate(intent: AggIntent) -> Rc<OperatorNode> { |
| 33 | + let child = OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Scan { |
| 34 | + source: Source::Table { |
| 35 | + table_ref: "flows".into(), |
| 36 | + }, |
| 37 | + predicates: vec![], |
| 38 | + schema: Schema::lifted(vec![Field::plain("src_ip", DataType::Utf8, false)], None), |
| 39 | + })) |
| 40 | + .unwrap(); |
| 41 | + OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Aggregate { |
| 42 | + child, |
| 43 | + reduction: Reduction::by(vec![]), |
| 44 | + measures: vec![intent], |
| 45 | + output_names: vec![], |
| 46 | + filters: vec![], |
| 47 | + having: None, |
| 48 | + })) |
| 49 | + .unwrap() |
| 50 | +} |
| 51 | + |
| 52 | +/// Example 2 preserves specialized distinct summaries and the universal alternative. |
| 53 | +#[test] |
| 54 | +fn cardinality_keeps_exact_specialized_and_universal_alternatives() { |
| 55 | + let choices = local_realizations_for_intent(&AggIntent::Cardinality { |
| 56 | + cols: vec![0], |
| 57 | + accuracy: approximate(), |
| 58 | + }) |
| 59 | + .unwrap(); |
| 60 | + assert!(matches!(choices[0], Realization::PassThrough)); |
| 61 | + assert_eq!( |
| 62 | + algorithms(&choices), |
| 63 | + vec![ |
| 64 | + SketchAlgorithm::Hll, |
| 65 | + SketchAlgorithm::Theta, |
| 66 | + SketchAlgorithm::Kmv, |
| 67 | + SketchAlgorithm::UnivMon |
| 68 | + ] |
| 69 | + ); |
| 70 | + let tuple = local_realizations_for_intent(&AggIntent::Cardinality { |
| 71 | + cols: vec![0, 1], |
| 72 | + accuracy: approximate(), |
| 73 | + }) |
| 74 | + .unwrap(); |
| 75 | + assert!(!algorithms(&tuple).contains(&SketchAlgorithm::UnivMon)); |
| 76 | +} |
| 77 | + |
| 78 | +/// Frequency moments retain exact execution and a universal sketch without certification. |
| 79 | +#[test] |
| 80 | +fn frequency_statistics_keep_universal_choices() { |
| 81 | + for intent in [ |
| 82 | + AggIntent::FrequencyL2 { |
| 83 | + col: Some(0), |
| 84 | + accuracy: approximate(), |
| 85 | + }, |
| 86 | + AggIntent::FrequencyEntropy { |
| 87 | + col: Some(0), |
| 88 | + accuracy: approximate(), |
| 89 | + }, |
| 90 | + ] { |
| 91 | + let choices = local_realizations_for_intent(&intent).unwrap(); |
| 92 | + assert!(matches!(choices[0], Realization::PassThrough)); |
| 93 | + assert_eq!(algorithms(&choices), vec![SketchAlgorithm::UnivMon]); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// An exact request cannot acquire an approximate sketch merely because one is available. |
| 98 | +#[test] |
| 99 | +fn exact_quantile_stays_exact_and_approximate_keeps_both_families() { |
| 100 | + let choices = local_realizations_for_intent(&AggIntent::Quantile { |
| 101 | + col: Some(0), |
| 102 | + q: 0.99, |
| 103 | + accuracy: approximate(), |
| 104 | + }) |
| 105 | + .unwrap(); |
| 106 | + assert_eq!( |
| 107 | + algorithms(&choices), |
| 108 | + vec![SketchAlgorithm::Kll, SketchAlgorithm::DDSketch] |
| 109 | + ); |
| 110 | + let exact = local_realizations_for_intent(&AggIntent::Quantile { |
| 111 | + col: Some(0), |
| 112 | + q: 0.99, |
| 113 | + accuracy: AccuracyTarget::Exact, |
| 114 | + }) |
| 115 | + .unwrap(); |
| 116 | + assert_eq!(exact, vec![Realization::PassThrough]); |
| 117 | +} |
| 118 | + |
| 119 | +/// Scalar roots expose their producer targets; repeated references retain one target identity. |
| 120 | +#[test] |
| 121 | +fn scalar_root_producers_are_discovered_once() { |
| 122 | + let producer = aggregate(AggIntent::Cardinality { |
| 123 | + cols: vec![0], |
| 124 | + accuracy: approximate(), |
| 125 | + }); |
| 126 | + let roots = vec![ |
| 127 | + ( |
| 128 | + "scalar", |
| 129 | + QueryRoot::Scalar(ScalarExpr::ScalarSubquery(producer.clone())), |
| 130 | + ), |
| 131 | + ("relation", QueryRoot::Operator(producer.clone())), |
| 132 | + ]; |
| 133 | + let candidates = enumerate_local_logical_candidates(roots).unwrap(); |
| 134 | + assert_eq!(candidates.roots.len(), 2); |
| 135 | + for (_, root) in &candidates.roots { |
| 136 | + asap_types::ir::export::compile_logical_asap_query(root) |
| 137 | + .unwrap() |
| 138 | + .validate() |
| 139 | + .unwrap(); |
| 140 | + } |
| 141 | + assert_eq!(candidates.targets.len(), 1); |
| 142 | + assert!(Rc::ptr_eq(&candidates.targets[0].target, &producer)); |
| 143 | + assert!(producer.timing.is_none()); |
| 144 | + assert!(producer.guarantee.is_none()); |
| 145 | + asap_types::ir::export::compile_logical_asap_dag(&producer) |
| 146 | + .unwrap() |
| 147 | + .validate() |
| 148 | + .unwrap(); |
| 149 | +} |
| 150 | + |
| 151 | +/// Example 1 rate lowering reaches the exact accumulator choice without a cost model. |
| 152 | +#[test] |
| 153 | +fn promql_lowering_reaches_phase_free_local_candidates() { |
| 154 | + use asap_types::workload::{ |
| 155 | + AccuracyRequirement, BatchEntry, PlanningWorkload, Query, QueryLanguage, QueryRequirements, |
| 156 | + QueryWorkload, |
| 157 | + }; |
| 158 | + let workload = PlanningWorkload { |
| 159 | + query_workload: QueryWorkload { |
| 160 | + language: QueryLanguage::PromQL, |
| 161 | + query_batch: Some(vec![BatchEntry { |
| 162 | + query: Query("sum by (job) (rate(http_requests_total[1m]))".into()), |
| 163 | + requirements: QueryRequirements { |
| 164 | + accuracy: AccuracyRequirement::Explicit(approximate()), |
| 165 | + ..Default::default() |
| 166 | + }, |
| 167 | + predictability: Default::default(), |
| 168 | + invocations: 1, |
| 169 | + execute_at: None, |
| 170 | + time_selection: Default::default(), |
| 171 | + }]), |
| 172 | + repeating_queries: None, |
| 173 | + }, |
| 174 | + data_workload: Some(asap_types::workload::DataWorkload { |
| 175 | + data_ingestion_interval: asap_types::workload::Evidence { |
| 176 | + value: Some(asap_types::workload::DurationMs(1000)), |
| 177 | + ..Default::default() |
| 178 | + }, |
| 179 | + ..Default::default() |
| 180 | + }), |
| 181 | + }; |
| 182 | + let roots = asap_frontend_promql::unified::lower_promql_query_workload(&workload, 0).unwrap(); |
| 183 | + let candidates = |
| 184 | + enumerate_local_logical_candidates(roots.into_iter().enumerate().collect()).unwrap(); |
| 185 | + assert!(candidates |
| 186 | + .targets |
| 187 | + .iter() |
| 188 | + .any(|target| target.alternatives.iter().any(|choice| matches!( |
| 189 | + choice, |
| 190 | + Realization::ExactAggregate { |
| 191 | + kind: ExactKind::Rate, |
| 192 | + .. |
| 193 | + } |
| 194 | + )))); |
| 195 | + assert!(candidates |
| 196 | + .targets |
| 197 | + .iter() |
| 198 | + .all(|target| target.target.timing.is_none())); |
| 199 | +} |
| 200 | + |
| 201 | +/// Physical annotations and invalid probability requirements fail at the stage boundary. |
| 202 | +#[test] |
| 203 | +fn assigned_timing_and_invalid_accuracy_are_rejected() { |
| 204 | + let mut producer = (*aggregate(AggIntent::Count { |
| 205 | + accuracy: approximate(), |
| 206 | + })) |
| 207 | + .clone(); |
| 208 | + producer.timing = Some(asap_types::post_asap::ExecutionTiming::QueryTime); |
| 209 | + assert!(matches!( |
| 210 | + enumerate_local_logical_candidates(vec![(0, QueryRoot::Operator(Rc::new(producer)))]), |
| 211 | + Err(LogicalCandidateError::AssignedTiming) |
| 212 | + )); |
| 213 | + for target in [ |
| 214 | + AccuracyTarget::Epsilon(f64::NAN), |
| 215 | + AccuracyTarget::EpsilonDelta { |
| 216 | + epsilon: 0.1, |
| 217 | + delta: 0.0, |
| 218 | + }, |
| 219 | + ] { |
| 220 | + assert!(matches!( |
| 221 | + local_realizations_for_intent(&AggIntent::Count { accuracy: target }), |
| 222 | + Err(LogicalCandidateError::InvalidAccuracy) |
| 223 | + )); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +/// Local TopK keeps both declared heap substrates without choosing an implementation. |
| 228 | +#[test] |
| 229 | +fn topk_keeps_both_specialized_heap_choices() { |
| 230 | + let choices = local_realizations_for_intent(&AggIntent::TopK { |
| 231 | + k: 10, |
| 232 | + accuracy: approximate(), |
| 233 | + }) |
| 234 | + .unwrap(); |
| 235 | + assert!(matches!(choices[0], Realization::PassThrough)); |
| 236 | + assert_eq!( |
| 237 | + algorithms(&choices), |
| 238 | + vec![ |
| 239 | + SketchAlgorithm::CmsWithHeap, |
| 240 | + SketchAlgorithm::CountSketchWithHeap |
| 241 | + ] |
| 242 | + ); |
| 243 | +} |
0 commit comments