Skip to content
Draft
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
2 changes: 1 addition & 1 deletion crates/frontend-promql/tests/univmon_candidates.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use asap_logical_optimizer::accuracy::{AccuracyModel, DefaultAccuracyModel};
use asap_plan_selection::{AccuracyModel, DefaultAccuracyModel};
mod support;
use asap_types::ir::cse::share_common_sub_dags;
use asap_types::ir::properties::ErrorMetric;
Expand Down
53 changes: 25 additions & 28 deletions crates/logical-optimizer/src/accuracy/estimators/cms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ mod tests {
use asap_types::ir::schema::{GroupingStrategy, SketchKind};
let c = asap_types::ir::operator::agg_intent::default_cardinality();
let params = default_size_params(SketchAlgorithm::Cms, &c, 0.01, 0.001);
let g = DefaultAccuracyModel
.local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::Cms, params),
GroupingStrategy::default(),
),
&SketchStatistic::Cardinality,
)
.unwrap();
let g = local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::Cms, params),
GroupingStrategy::default(),
),
&SketchStatistic::Cardinality,
)
.unwrap();
assert_eq!(g.metric, ErrorMetric::Frequency);
assert!(DefaultAccuracyModel.satisfies(
assert!(satisfies(
&g,
&AccuracyTarget::EpsilonDelta {
epsilon: 0.01,
Expand Down Expand Up @@ -96,16 +95,15 @@ mod tests {
},
false => GroupingStrategy::default(),
};
DefaultAccuracyModel
.local_guarantee(
&FieldDataType::Sketch(SketchKind::new(SketchAlgorithm::Cms, params), grouping),
&group_count,
)
.unwrap()
local_guarantee(
&FieldDataType::Sketch(SketchKind::new(SketchAlgorithm::Cms, params), grouping),
&group_count,
)
.unwrap()
};
assert!(DefaultAccuracyModel.satisfies(&guarantee(0.01, 0.01, false), &target));
assert!(!DefaultAccuracyModel.satisfies(&guarantee(0.01, 0.01, true), &target));
assert!(DefaultAccuracyModel.satisfies(&guarantee(0.005, 0.005, true), &target));
assert!(satisfies(&guarantee(0.01, 0.01, false), &target));
assert!(!satisfies(&guarantee(0.01, 0.01, true), &target));
assert!(satisfies(&guarantee(0.005, 0.005, true), &target));
}

#[test]
Expand All @@ -116,15 +114,14 @@ mod tests {
depth: 5,
heap_size: 10,
};
let topk_frequency = DefaultAccuracyModel
.local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::CmsWithHeap, cms_heap),
GroupingStrategy::default(),
),
&SketchStatistic::TopK { k: 10 },
)
.expect("heap sketch still provides per-key frequency intervals");
let topk_frequency = local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::CmsWithHeap, cms_heap),
GroupingStrategy::default(),
),
&SketchStatistic::TopK { k: 10 },
)
.expect("heap sketch still provides per-key frequency intervals");
assert_eq!(topk_frequency.metric, ErrorMetric::Frequency);
}
}
25 changes: 12 additions & 13 deletions crates/logical-optimizer/src/accuracy/estimators/count_sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,19 @@ mod tests {
use asap_types::ir::schema::{GroupingStrategy, SketchKind};
let intent = default_cardinality();
let count_sketch = default_size_params(SketchAlgorithm::CountSketch, &intent, 0.01, 0.01);
let guarantee = DefaultAccuracyModel
.local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::CountSketch, count_sketch),
GroupingStrategy::default(),
),
&SketchStatistic::PointCount {
key: asap_types::ir::scalar::ColumnRef::SampleValue,
value: None,
},
)
.expect("CountSketch has a parameter-derived L2 guarantee");
let guarantee = local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::CountSketch, count_sketch),
GroupingStrategy::default(),
),
&SketchStatistic::PointCount {
key: asap_types::ir::scalar::ColumnRef::SampleValue,
value: None,
},
)
.expect("CountSketch has a parameter-derived L2 guarantee");
assert_eq!(guarantee.metric, ErrorMetric::L2Frequency);
assert!(DefaultAccuracyModel.satisfies(
assert!(satisfies(
&guarantee,
&AccuracyTarget::EpsilonDelta {
epsilon: 0.01,
Expand Down
21 changes: 10 additions & 11 deletions crates/logical-optimizer/src/accuracy/estimators/hll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,18 @@ mod tests {
use asap_types::ir::schema::{GroupingStrategy, SketchKind};
let c = default_cardinality();
let params = default_size_params(SketchAlgorithm::Hll, &c, 0.01, 0.01);
let g = DefaultAccuracyModel
.local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::Hll, params),
GroupingStrategy::default(),
),
&SketchStatistic::Cardinality,
)
.unwrap();
let g = local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::Hll, params),
GroupingStrategy::default(),
),
&SketchStatistic::Cardinality,
)
.unwrap();
assert_eq!(g.metric, ErrorMetric::Cardinality);
assert_eq!(g.failure_probability.evaluate(), None);
assert!(DefaultAccuracyModel.satisfies(&g, &AccuracyTarget::Epsilon(0.01)));
assert!(!DefaultAccuracyModel.satisfies(
assert!(satisfies(&g, &AccuracyTarget::Epsilon(0.01)));
assert!(!satisfies(
&g,
&AccuracyTarget::EpsilonDelta {
epsilon: 0.01,
Expand Down
21 changes: 10 additions & 11 deletions crates/logical-optimizer/src/accuracy/estimators/kll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,18 @@ mod tests {
use asap_types::ir::schema::{GroupingStrategy, SketchKind};
let q = default_quantile(0.99);
let params = default_size_params(SketchAlgorithm::Kll, &q, 0.01, 0.01);
let g = DefaultAccuracyModel
.local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::Kll, params),
GroupingStrategy::default(),
),
&SketchStatistic::Quantile { q: 0.99 },
)
.unwrap();
let g = local_guarantee(
&FieldDataType::Sketch(
SketchKind::new(SketchAlgorithm::Kll, params),
GroupingStrategy::default(),
),
&SketchStatistic::Quantile { q: 0.99 },
)
.unwrap();
assert_eq!(g.metric, ErrorMetric::Rank);
assert!(DefaultAccuracyModel.satisfies(&g, &AccuracyTarget::Epsilon(0.01)));
assert!(satisfies(&g, &AccuracyTarget::Epsilon(0.01)));
assert_eq!(g.failure_probability.evaluate(), Some(0.01));
assert!(DefaultAccuracyModel.satisfies(
assert!(satisfies(
&g,
&AccuracyTarget::EpsilonDelta {
epsilon: 0.01,
Expand Down
15 changes: 10 additions & 5 deletions crates/logical-optimizer/src/accuracy/estimators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ pub mod hll;
pub mod kll;
pub mod univmon;

pub(super) fn sketch_guarantee(
/// The guarantee of reading `query` out of a sketch with these committed
/// parameters, built over an exact input; `None` when no error model covers
/// the pair.
pub fn sketch_guarantee(
algorithm: &SketchAlgorithm,
params: &SketchParams,
query: &SketchStatistic,
Expand Down Expand Up @@ -55,10 +58,12 @@ fn bounded_guarantee(
}
}

pub(super) fn local_guarantee(
family: &FieldDataType,
query: &SketchStatistic,
) -> Option<ResultGuarantee> {
/// The guarantee of reading `query` out of a summary of family `family` built
/// over an **exact** input — derived from the family's committed parameters by
/// inverting the same sizing formulas
/// [`crate::pass1::realization::default_size_params`] uses. `None` when no
/// error model covers the family (none does for `Sample`/`Wavelet`/`StatModel`).
pub fn local_guarantee(family: &FieldDataType, query: &SketchStatistic) -> Option<ResultGuarantee> {
match family {
FieldDataType::Plain(_) => Some(ResultGuarantee::exact("Plain value")),
FieldDataType::ExactAggregate(kind, _) => {
Expand Down
12 changes: 4 additions & 8 deletions crates/logical-optimizer/src/accuracy/estimators/univmon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod tests {
}

fn l2(params: SketchParams) -> Option<ResultGuarantee> {
DefaultAccuracyModel.local_guarantee(&family(params), &SketchStatistic::FrequencyL2)
local_guarantee(&family(params), &SketchStatistic::FrequencyL2)
}

fn shape(params: &SketchParams) -> (u32, u32) {
Expand Down Expand Up @@ -191,12 +191,10 @@ mod tests {
key: ColumnRef::SampleValue,
value: None,
};
assert!(DefaultAccuracyModel
.local_guarantee(&family(params.clone()), &total)
.is_some_and(|g| g.is_exact()));
assert!(local_guarantee(&family(params.clone()), &total).is_some_and(|g| g.is_exact()));
let guarantee = l2(params.clone()).expect("L2 is certified");
assert_eq!(guarantee.metric, ErrorMetric::RelativeValue);
assert!(DefaultAccuracyModel.satisfies(
assert!(crate::accuracy::satisfies(
&guarantee,
&AccuracyTarget::EpsilonDelta {
epsilon: 0.01,
Expand All @@ -208,9 +206,7 @@ mod tests {
SketchStatistic::FrequencyEntropy,
] {
assert!(
DefaultAccuracyModel
.local_guarantee(&family(params.clone()), &query)
.is_none(),
local_guarantee(&family(params.clone()), &query).is_none(),
"{query:?}"
);
}
Expand Down
Loading