From f5d364f33bb33f2e0dc548187262fee2c0aa08dd Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:29:12 +0000 Subject: [PATCH] test: sign derivation oracle and rule tables (tests only) Tests for sign derivation by construction, written before the implementation. They fail today where the rules or the executor are unsound or incomplete. - types/tests/value_sign_rules.rs: one row per transfer rule (Scan, row operators, measures, arithmetic for Float64 and Int64, math and date functions, scalar expressions, binary operators, summary states, readouts and finalize), plus the regression rows and a source check for the module lint. - integration-tests value_sign_oracle.rs: every subtree of the fuzz and adversarial queries is planned and executed exactly, and every known sign is checked against the evaluated values. - integration-tests value_sign_function_keys.rs: every PromQL math or date function name from the parser parses to one typed key. - integration-tests value_sign_executor_zeros.rs: signed zeros through sum, sgn and clamp. - Consumer tests: error contract, Count-Min in Stage 2, the DDSketch relative guarantee, and the selected-sketch check. S2 cost tests are ignored. Non-test code only declares the functions the tests call, with todo!() bodies: ValueSign::is_one_sign, GroupProperties::state_column_sign, math_function_of_call, time_function_of_call and the test-only column_signs_of_candidate_expression. The test-support fixture memo_fixtures::binary_op is public and takes vector_match and return_bool, so the rule tables build every BinaryOp through it instead of a local copy. --- Cargo.lock | 2 + crates/accuracy/src/error_contract/tests.rs | 392 +++ crates/accuracy/src/logical_edge/tests.rs | 165 ++ crates/cost/src/node/usage/tests.rs | 251 ++ .../tests/histogram_metadata.rs | 30 + crates/integration-tests/Cargo.toml | 4 + crates/integration-tests/tests/main.rs | 4 + .../tests/value_sign_consumers.rs | 279 ++ .../tests/value_sign_executor_zeros.rs | 200 ++ .../tests/value_sign_function_keys.rs | 273 ++ .../tests/value_sign_oracle.rs | 1751 ++++++++++++ crates/planner/src/installable/tests.rs | 67 + .../src/tests/candidates.rs | 210 ++ .../types/src/ir/scalar/scalar_type_rules.rs | 19 + .../stages/logical/group/column_properties.rs | 9 + crates/types/src/stages/logical/group/mod.rs | 8 + crates/types/src/test_support/forwarders.rs | 13 + .../types/src/test_support/memo_fixtures.rs | 21 +- crates/types/tests/main.rs | 2 + .../tests/value_sign_derivation_sites.rs | 253 ++ crates/types/tests/value_sign_rules.rs | 2364 +++++++++++++++++ 21 files changed, 6312 insertions(+), 5 deletions(-) create mode 100644 crates/integration-tests/tests/value_sign_consumers.rs create mode 100644 crates/integration-tests/tests/value_sign_executor_zeros.rs create mode 100644 crates/integration-tests/tests/value_sign_function_keys.rs create mode 100644 crates/integration-tests/tests/value_sign_oracle.rs create mode 100644 crates/types/tests/value_sign_derivation_sites.rs create mode 100644 crates/types/tests/value_sign_rules.rs diff --git a/Cargo.lock b/Cargo.lock index 84b535f2d..77bb4190f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -405,7 +405,9 @@ dependencies = [ "asap-types", "asap_sketchlib", "futures", + "metricsql_parser", "serde_json", + "strum", "tokio", ] diff --git a/crates/accuracy/src/error_contract/tests.rs b/crates/accuracy/src/error_contract/tests.rs index 4bbdca982..f2f841947 100644 --- a/crates/accuracy/src/error_contract/tests.rs +++ b/crates/accuracy/src/error_contract/tests.rs @@ -471,3 +471,395 @@ fn set_operators_and_relabels_copy_their_inputs_error() { ); } } + +/// The consumers of `column_signs` in the error contract (sign test plan +/// T5.1 and T5.4): a relative contract of a `sum` or `avg` reads whether +/// its input's values have one sign, and a `Project` of a math function +/// keeps an error by the function's typed key, the one the sign +/// derivation also reads. +mod sign_consumers { + use super::*; + use asap_types::ir::operator::MathFunc; + use asap_types::ir::scalar::scalar_type_rules::math_function_of_call; + use asap_types::test_support::memo_fixtures::range; + + const FIVE_MINUTES_MS: i64 = 300_000; + + /// Sources `pos` (`value` declared non-negative), `neg` (declared + /// non-positive), `mixed` (nothing declared) and `c_total` (a counter, + /// its value non-negative without a declaration). + fn signed_sources() -> DataWorkload { + let declared = |sign| SourceFacts { + column_signs: BTreeMap::from([("value".to_string(), sign)]), + ..SourceFacts::default() + }; + DataWorkload { + sources: BTreeMap::from([ + ("pos".to_string(), declared(ValueSign::NonNegative)), + ("neg".to_string(), declared(ValueSign::NonPositive)), + ("mixed".to_string(), SourceFacts::default()), + ( + "c_total".to_string(), + SourceFacts { + metric_type: Some(asap_types::workload::PrometheusMetricType::Counter), + ..SourceFacts::default() + }, + ), + ]), + ..DataWorkload::default() + } + } + + /// The sample-value column of `group`'s output. + fn value_column(memo: &Memo, group: MemoGroupId) -> usize { + resolve_column_ref( + &ColumnRef::SampleValue, + &memo.groups()[group.0 as usize].group_properties.schema, + ) + .expect("the group has a sample value") + } + + /// `quantile_over_time(0.9, metric[5m])` per series, computed exactly. + fn quantile_over_time(memo: &mut Memo, metric: &str) -> MemoGroupId { + let input = memo + .find_or_add_group(scan(metric, &[("job", DataType::Utf8)], vec![])) + .unwrap(); + let window = memo + .find_or_add_group(range(FIVE_MINUTES_MS, input)) + .unwrap(); + memo.find_or_add_group(aggregate( + Reduction::PerEntity, + AggIntent::Quantile { col: None, q: 0.9 }, + window, + )) + .unwrap() + } + + /// `-child`: every column of `child` copied, its sample value negated. + fn negated(memo: &mut Memo, child: MemoGroupId) -> MemoGroupId { + let value = value_column(memo, child); + let fields = memo.groups()[child.0 as usize] + .group_properties + .schema + .fields + .clone(); + let project = Operator::NonASAP(NonASAPOp::Project { + cols: fields + .iter() + .enumerate() + .map(|(column, field)| ProjectItem { + alias: Some(field.name.clone()), + expr: if column == value { + ScalarExpr::Negative { + expr: Box::new(ScalarExpr::Column(column)), + semantics: ExprSemantics::Promql, + } + } else { + ScalarExpr::Column(column) + }, + }) + .collect(), + child, + }); + memo.find_or_add_group(project).unwrap() + } + + /// A relative error ε on each input of a `sum` or `avg` is at most ε + /// on the result exactly when the inputs' exact values have one sign + /// (`|Σ(x̂ - x)| ≤ ε Σ|x| = ε |Σx|`): β = 1 for a child whose value + /// column is `NonNegative` or `NonPositive`, no contract for + /// `Unknown`, whose values may cancel (1 + (-1) = 0, while 1.05 + + /// (-0.95) = 0.1). In absolute error an `avg` keeps the error (β = 1) + /// whatever the sign, a `sum` adds the errors up (no contract). + /// + /// The children's signs are declared (a `Scan`'s `SourceFacts`) or + /// derived, each hand-derived under IEEE 754 with its sign bit: + /// + /// | Child | Sign | Why | + /// |---|---|---| + /// | `pos` | N | declared | + /// | `neg` | P | declared | + /// | `mixed` | U | undeclared | + /// | `rate(c_total[5m])` | N | a counter's rate is ≥ +0 | + /// | `-quantile_over_time(0.9, pos[5m])` | P | a quantile of N values is one of them or between two, `-x` flips the sign bit | + /// | `q(pos) - q(neg)` | N | `x - y = x + (-y)`, N + N = N, and `+0 - (-0) = +0` | + /// + /// Every mismatch is listed, then the test fails once with all of + /// them. Today the last row fails: `-` has no sign rule (B6). + #[test] + fn relative_sum_and_avg_contracts_need_one_signed_inputs() { + use ValueSign::{NonNegative, NonPositive, Unknown}; + let mut memo = Memo::new(&signed_sources()); + let declared = |memo: &mut Memo, metric: &str| { + memo.find_or_add_group(scan(metric, &[("job", DataType::Utf8)], vec![])) + .unwrap() + }; + let pos = declared(&mut memo, "pos"); + let neg = declared(&mut memo, "neg"); + let mixed = declared(&mut memo, "mixed"); + let rate_of_counter = { + let input = declared(&mut memo, "c_total"); + let window = memo + .find_or_add_group(range(FIVE_MINUTES_MS, input)) + .unwrap(); + memo.find_or_add_group(aggregate(Reduction::PerEntity, AggIntent::Rate, window)) + .unwrap() + }; + let quantile_of_pos = quantile_over_time(&mut memo, "pos"); + let quantile_of_neg = quantile_over_time(&mut memo, "neg"); + let negated_quantile = negated(&mut memo, quantile_of_pos); + let difference = memo + .find_or_add_group(arithmetic( + ArithmeticOpKind::Sub, + quantile_of_pos, + quantile_of_neg, + )) + .unwrap(); + let children = [ + ("pos", pos, NonNegative), + ("neg", neg, NonPositive), + ("mixed", mixed, Unknown), + ("rate(c_total[5m])", rate_of_counter, NonNegative), + ("-q(pos)", negated_quantile, NonPositive), + ("q(pos) - q(neg)", difference, NonNegative), + ]; + let mut mismatches = Vec::new(); + for (name, child, sign) in children { + let derived = memo.groups()[child.0 as usize] + .group_properties + .column_signs[value_column(&memo, child)]; + if derived != sign { + mismatches.push(format!( + "{name}: value sign {derived:?}, hand-derived {sign:?}" + )); + } + // Over all rows (`sum(x)`), so no grouping key reads an + // estimate. + let total = || Reduction::by(vec![]); + let sum = aggregate(total(), AggIntent::Sum { col: None }, child); + let avg = aggregate(total(), AggIntent::Avg { col: None }, child); + let exact_sum = Operator::ASAP(ASAPOp::SummaryAgg { + child, + summary_type: FieldDataType::exact(ExactKind::Sum), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: total(), + filter: None, + }); + let one_sign = matches!(sign, NonNegative | NonPositive); + for (consumer_name, consumer, absolute) in [ + ("sum", &sum, None), + ("avg", &avg, Some(vec![1.0])), + ("exact sum", &exact_sum, None), + ] { + for (metric, expected) in [ + (AccuracyMetric::Relative, one_sign.then(|| vec![1.0])), + (AccuracyMetric::ABSOLUTE, absolute.clone()), + ] { + let factors = error_contract(&memo, consumer, metric) + .map(|contract| contract.error_factor_per_input); + if factors != expected { + mismatches.push(format!( + "{consumer_name}({name}), {metric:?}: {factors:?}, expected {expected:?}" + )); + } + } + } + } + assert!(mismatches.is_empty(), "{mismatches:#?}"); + } + + /// Every `MathFunc` variant, each with representative literal + /// arguments. The `match` in [`promql_call`] lists every variant with + /// no wildcard, so a new variant fails to compile until it is added + /// there and here. + fn all_math_funcs() -> Vec { + vec![ + MathFunc::Abs, + MathFunc::Ceil, + MathFunc::Floor, + MathFunc::Exp, + MathFunc::Ln, + MathFunc::Log2, + MathFunc::Log10, + MathFunc::Sqrt, + MathFunc::Sgn, + MathFunc::Sin, + MathFunc::Cos, + MathFunc::Tan, + MathFunc::Asin, + MathFunc::Acos, + MathFunc::Atan, + MathFunc::Sinh, + MathFunc::Cosh, + MathFunc::Tanh, + MathFunc::Asinh, + MathFunc::Acosh, + MathFunc::Atanh, + MathFunc::Deg, + MathFunc::Rad, + MathFunc::Round { to_nearest: 1.0 }, + MathFunc::Clamp { + min: 0.0, + max: 100.0, + }, + MathFunc::ClampMin { min: 0.0 }, + MathFunc::ClampMax { max: 5.0 }, + ] + } + + /// The PromQL frontend's call of `f` on `value`: its name and its + /// literal arguments after the value (`clamp_min(v, 0)` → + /// `promql_clamp_min`, `[0]`), and whether it keeps an error with + /// β = 1 in absolute and in relative error. A function moving a value + /// by at most its error is 1-Lipschitz: `|f(x̂) - f(x)| ≤ |x̂ - x|` + /// (abs, clamp, clamp_min, clamp_max). Only `abs` also keeps the + /// magnitude, `||x̂| - |x|| ≤ |x̂ - x|` with `||x|| = |x|`, so a + /// relative error ε on x is at most ε on |x|; `clamp_min(x, 0)` of + /// x = -10 and x̂ = -9.5 is 0 for both, but of x = 0.1, x̂ = -0.1 it + /// is 0 against 0.1: not relative. + fn promql_call(f: &MathFunc) -> (&'static str, Vec, bool, bool) { + match f { + MathFunc::Abs => ("promql_abs", vec![], true, true), + MathFunc::Ceil => ("promql_ceil", vec![], false, false), + MathFunc::Floor => ("promql_floor", vec![], false, false), + MathFunc::Exp => ("promql_exp", vec![], false, false), + MathFunc::Ln => ("promql_ln", vec![], false, false), + MathFunc::Log2 => ("promql_log2", vec![], false, false), + MathFunc::Log10 => ("promql_log10", vec![], false, false), + MathFunc::Sqrt => ("promql_sqrt", vec![], false, false), + MathFunc::Sgn => ("promql_sgn", vec![], false, false), + MathFunc::Sin => ("promql_sin", vec![], false, false), + MathFunc::Cos => ("promql_cos", vec![], false, false), + MathFunc::Tan => ("promql_tan", vec![], false, false), + MathFunc::Asin => ("promql_asin", vec![], false, false), + MathFunc::Acos => ("promql_acos", vec![], false, false), + MathFunc::Atan => ("promql_atan", vec![], false, false), + MathFunc::Sinh => ("promql_sinh", vec![], false, false), + MathFunc::Cosh => ("promql_cosh", vec![], false, false), + MathFunc::Tanh => ("promql_tanh", vec![], false, false), + MathFunc::Asinh => ("promql_asinh", vec![], false, false), + MathFunc::Acosh => ("promql_acosh", vec![], false, false), + MathFunc::Atanh => ("promql_atanh", vec![], false, false), + MathFunc::Deg => ("promql_deg", vec![], false, false), + MathFunc::Rad => ("promql_rad", vec![], false, false), + MathFunc::Round { to_nearest } => ("promql_round", vec![*to_nearest], false, false), + MathFunc::Clamp { min, max } => ("promql_clamp", vec![*min, *max], true, false), + MathFunc::ClampMin { min } => ("promql_clamp_min", vec![*min], true, false), + MathFunc::ClampMax { max } => ("promql_clamp_max", vec![*max], true, false), + } + } + + /// The error contract and the sign derivation recognize a function by + /// one typed key, [`math_function_of_call`], not by two string lists + /// that can drift apart (B10). For every `MathFunc`, a `Project` over + /// a KLL quantile readout replacing the estimate with + /// `promql_(value, literals…)`: + /// - keeps the absolute error iff f ∈ {Abs, Clamp, ClampMin, ClampMax}; + /// - keeps the relative error iff f = Abs; + /// - and `math_function_of_call` returns f for that call. + /// + /// SQL's `abs` and `ABS` are the same key as `promql_abs`; a clamp + /// whose bound is a column, not a literal, has no key and no contract. + #[test] + fn error_contract_and_sign_read_one_function_key() { + let mut memo = Memo::new(&DataWorkload::default()); + let input = memo + .find_or_add_group(scan("latency", &[("job", DataType::Utf8)], vec![])) + .unwrap(); + let state = memo + .find_or_add_group(Operator::ASAP(ASAPOp::SummaryAgg { + child: input, + summary_type: FieldDataType::Sketch(SketchAlgorithm::Kll), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![2]), + filter: None, + })) + .unwrap(); + let readout = memo + .find_or_add_group(readout(SketchStatistic::Quantile { q: 0.9 }, state)) + .unwrap(); + let estimate = value_column(&memo, readout); + let fields = memo.groups()[readout.0 as usize] + .group_properties + .schema + .fields + .clone(); + let project_value = |expr: ScalarExpr| { + Operator::NonASAP(NonASAPOp::Project { + cols: fields + .iter() + .enumerate() + .map(|(column, field)| ProjectItem { + alias: Some(field.name.clone()), + expr: if column == estimate { + expr.clone() + } else { + ScalarExpr::Column(column) + }, + }) + .collect(), + child: readout, + }) + }; + let call = |name: &str, literals: &[f64]| ScalarExpr::FunctionCall { + name: name.to_string(), + args: std::iter::once(ScalarExpr::Column(estimate)) + .chain( + literals + .iter() + .map(|x| ScalarExpr::Literal(ScalarValue::Float64(*x))), + ) + .collect(), + }; + let keeps = |expr: &ScalarExpr, metric| { + error_contract(&memo, &project_value(expr.clone()), metric) + .map(|contract| contract.error_factor_per_input) + == Some(vec![1.0]) + }; + // The contracts, first: they hold today through the string list. + let mut calls = Vec::new(); + for f in all_math_funcs() { + let (name, literals, absolute, relative) = promql_call(&f); + let expr = call(name, &literals); + assert_eq!( + keeps(&expr, AccuracyMetric::ABSOLUTE), + absolute, + "{name}: absolute" + ); + assert_eq!( + keeps(&expr, AccuracyMetric::Relative), + relative, + "{name}: relative" + ); + calls.push((f, name, expr)); + } + for name in ["abs", "ABS"] { + let expr = call(name, &[]); + assert!(keeps(&expr, AccuracyMetric::ABSOLUTE), "{name}"); + assert!(keeps(&expr, AccuracyMetric::Relative), "{name}"); + } + let bound_by_a_column = ScalarExpr::FunctionCall { + name: "promql_clamp_min".to_string(), + args: vec![ScalarExpr::Column(estimate), ScalarExpr::Column(estimate)], + }; + assert!(!keeps(&bound_by_a_column, AccuracyMetric::ABSOLUTE)); + // The key both read: one parse function for every spelling. + for (f, name, expr) in &calls { + let ScalarExpr::FunctionCall { args, .. } = expr else { + unreachable!("built as a call") + }; + assert_eq!(math_function_of_call(name, args), Some(f.clone()), "{name}"); + } + for name in ["abs", "ABS"] { + assert_eq!( + math_function_of_call(name, &[ScalarExpr::::Column(estimate)]), + Some(MathFunc::Abs), + "{name}" + ); + } + let ScalarExpr::FunctionCall { name, args } = &bound_by_a_column else { + unreachable!("built as a call") + }; + assert_eq!(math_function_of_call(name, args), None); + } +} diff --git a/crates/accuracy/src/logical_edge/tests.rs b/crates/accuracy/src/logical_edge/tests.rs index 40d51baf9..031f0aa18 100644 --- a/crates/accuracy/src/logical_edge/tests.rs +++ b/crates/accuracy/src/logical_edge/tests.rs @@ -145,3 +145,168 @@ fn a_heap_smaller_than_k_fails_the_topk_readouts_edge() { } assert_eq!(small, 2 * 28); } + +/// The DDSketch relative guarantee and the sign of the values it is +/// built over (sign test plan T5.3 and T4 b12). +mod ddsketch_sign_gate { + use super::*; + use asap_types::ir::scalar::ScalarExpr; + use asap_types::ir::{NonASAPOp, ProjectItem}; + use asap_types::stages::{LogicalRequirements, MemoGroupId, RequiredAccuracy, ValueSign}; + use asap_types::test_support::memo_builders::{ + estimate, group, own_candidate, scan_group, sketch_state, + }; + use asap_types::test_support::memo_fixtures::range; + use asap_types::workload::SourceFacts; + use std::collections::BTreeMap; + + /// A relative error of 5%, as `AccuracyTarget::max_error(0.05)` in + /// relative error with no failure probability set (1). + const RELATIVE_5_PERCENT: RequiredAccuracy = RequiredAccuracy { + metric: AccuracyMetric::Relative, + max_error: 0.05, + failure_probability: 1.0, + }; + + /// Sources `pos` (`value` declared non-negative), `neg` (declared + /// non-positive) and `mixed` (nothing declared). + fn signed_sources() -> DataWorkload { + let declared = |sign| SourceFacts { + column_signs: BTreeMap::from([("value".to_string(), sign)]), + ..SourceFacts::default() + }; + DataWorkload { + sources: BTreeMap::from([ + ("pos".to_string(), declared(ValueSign::NonNegative)), + ("neg".to_string(), declared(ValueSign::NonPositive)), + ("mixed".to_string(), SourceFacts::default()), + ]), + ..DataWorkload::default() + } + } + + /// `abs(value)` of `child`: every other column copied. + fn abs_of_value(memo: &mut Memo, child: MemoGroupId) -> MemoGroupId { + let schema = &memo.groups()[child.0 as usize].group_properties.schema; + let value = asap_types::ir::scalar::resolve_column_ref(&ColumnRef::SampleValue, schema) + .expect("a sample value"); + let cols = schema + .fields + .iter() + .enumerate() + .map(|(column, field)| ProjectItem { + alias: Some(field.name.clone()), + expr: if column == value { + ScalarExpr::FunctionCall { + name: "promql_abs".into(), + args: vec![ScalarExpr::Column(column)], + } + } else { + ScalarExpr::Column(column) + }, + }) + .collect(); + group(memo, Operator::NonASAP(NonASAPOp::Project { cols, child })) + } + + /// The outcome of the edge from each DDSketch build of + /// `quantile_over_time(q, metric[5m])`'s state whose α is within 5% + /// into its readout, the readout required to be within 5% relative + /// error (as Pass 2 writes it), under a query with that target whose + /// root is the readout, or `abs` of it when `abs_root`. + fn ddsketch_edges_within_five_percent( + metric: &str, + q: f64, + abs_root: bool, + ) -> Vec<(f64, Result<(), RootPruneReason>)> { + let mut memo = Memo::new(&signed_sources()); + let input = scan_group(&mut memo, metric, &["job"], vec![]); + let window = group(&mut memo, range(300_000, input)); + let state = sketch_state( + &mut memo, + window, + SketchAlgorithm::DDSketch, + Reduction::PerEntity, + ); + let readout = estimate(&mut memo, state, SketchStatistic::Quantile { q }); + let root = match abs_root { + true => abs_of_value(&mut memo, readout), + false => readout, + }; + query( + &mut memo, + root, + Some(AccuracyTarget::max_error(0.05).accuracy_metric(AccuracyMetric::Relative)), + ); + memo.set_candidate_input_requirements( + own_candidate(readout), + vec![LogicalRequirements { + required_accuracy: Some(RELATIVE_5_PERCENT), + }], + ); + let error_metrics = + ErrorMetricsOfMemo::of(&memo, &memo.topological_order().expect("acyclic")); + let edges: Vec<_> = (0..memo.groups()[state.0 as usize].candidates.len()) + .filter_map(|index_in_group| { + let build = CandidateForMemoGroupId { + group: state, + index_in_group, + }; + let Some(SketchParams::DDSketch { alpha }) = + memo.candidate(build).logical_properties.parameters() + else { + panic!("a DDSketch build"); + }; + (*alpha <= 0.05).then(|| { + ( + *alpha, + error_metrics.edge_meets_logical_requirements( + &memo, + build, + own_candidate(readout), + 0, + &NoMeasuredGuarantees, + ), + ) + }) + }) + .collect(); + assert!(!edges.is_empty(), "a listed α within 5%"); + edges + } + + /// DDSketch's relative error α holds for values of one sign: it maps + /// |v| to a log-spaced bucket in a positive or a negative store. Over + /// values of both signs a quantile can fall between the stores, where + /// the relative bound does not hold: of 1,000 values of -1 and + /// 1,000 of +1, the true median may be -1 or +1, an estimate near 0 is + /// off by ~100%. So the relative guarantee needs the state column (the + /// values the sketch is updated with) of one sign: granted over `pos` + /// (N) and `neg` (P) for every α within 5%, refused over `mixed` (U). + /// The gate reads the state group's `state_column_sign` at the + /// logical edge. Today the `mixed` rows are granted. + #[test] + fn ddsketch_relative_guarantee_needs_a_one_signed_state_column() { + // provisional answer Q7: the gate is at the logical edge. + for (metric, granted) in [("pos", true), ("neg", true), ("mixed", false)] { + for (alpha, edge) in ddsketch_edges_within_five_percent(metric, 0.9, false) { + assert_eq!(edge.is_ok(), granted, "{metric}, α = {alpha}: {edge:?}"); + } + } + } + + /// `abs(quantile_over_time(0.3, mixed[5m]))` under a 5% relative + /// target (B12): `abs` keeps the relative error of the quantile + /// below it, so the root's target reaches the DDSketch readout, which + /// may not grant it over `mixed`, whose values may have both signs. + /// The same query over `pos` and `neg` is granted. + #[test] + fn b12_ddsketch_relative_guarantee_needs_one_signed_weights() { + // provisional answer Q7: the gate is at the logical edge. + for (metric, granted) in [("pos", true), ("neg", true), ("mixed", false)] { + for (alpha, edge) in ddsketch_edges_within_five_percent(metric, 0.3, true) { + assert_eq!(edge.is_ok(), granted, "{metric}, α = {alpha}: {edge:?}"); + } + } + } +} diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index efd664bf0..f40c28c51 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -735,3 +735,254 @@ fn a_ddsketch_over_a_counter_declaring_its_value_unknown_keeps_both_stores() { ddsketch_costed_bytes(true, None) ); } + +/// A DDSketch keeps a store for positive values, one for negative values +/// and a zero bucket (both +0.0 and -0.0 land in it). Values all ≥ 0 or +/// all ≤ 0 fill one store, values of both signs two; a sign-bit slip +/// (-0.0 where +0.0 was expected) still lands in the zero bucket, so the +/// store count follows the real-number sign of the values the sketch is +/// updated with: one store over a column of known sign, two over an +/// `Unknown` one. Today the cost decides it from its own lookup of a raw +/// producer over sources declared non-negative instead of the producer's +/// column signs. +mod ddsketch_stores_by_sign { + use super::*; + use asap_types::ir::operator::AggIntent; + use asap_types::stages::ValueSign; + use asap_types::summary_type::SketchAlgorithm; + use asap_types::workload::{PrometheusMetricType, SourceFacts}; + use std::collections::BTreeMap; + + const ALPHA: f64 = 0.01; + const INPUT_ROWS: u64 = 100_000; + + /// Sources `pos` (`value` declared non-negative), `neg` (declared + /// non-positive), `mixed` (nothing declared), `c_total` (a counter) + /// and the histogram family `h` (its `h_sum` undeclared). + fn signed_sources() -> DataWorkload { + let declared = |sign| SourceFacts { + column_signs: BTreeMap::from([("value".to_string(), sign)]), + ..SourceFacts::default() + }; + let typed = |metric_type| SourceFacts { + metric_type: Some(metric_type), + ..SourceFacts::default() + }; + DataWorkload { + sources: BTreeMap::from([ + ("pos".to_string(), declared(ValueSign::NonNegative)), + ("neg".to_string(), declared(ValueSign::NonPositive)), + ("mixed".to_string(), SourceFacts::default()), + ("c_total".to_string(), typed(PrometheusMetricType::Counter)), + ("h".to_string(), typed(PrometheusMetricType::Histogram)), + ]), + ..DataWorkload::default() + } + } + + /// The bytes of one DDSketch state of α = 0.01 over all + /// [`INPUT_ROWS`] rows (one group) holding one store (`one_store`) or + /// two: the state-bytes formula with the store count chosen by hand. + fn state_bytes_with(one_store: bool) -> u64 { + sketch_shape( + &SketchParams::DDSketch { alpha: ALPHA }, + StateInputRows { + rows_per_state: Some(INPUT_ROWS), + non_negative: one_store, + value_magnitude_range: None, + }, + ) + .1 + } + + /// The bytes the cost prices for a query-time DDSketch build over all + /// rows of `producer`, which reads `source` and outputs `[ts, value]`: + /// a time range passes both on unchanged from the scan, any other + /// producer (the rate) computes `value`. + fn priced_state_bytes(producer: Payload, source: &str) -> u64 { + use asap_types::ir::schema::{DataType, Field}; + let settings = CostInputs::default(); + let data = signed_sources(); + let unchanged_columns = match &producer { + Payload::NonASAP(NonASAPOp::TimeRange { .. }) => BTreeSet::from(["ts", "value"]), + _ => BTreeSet::new(), + }; + let producer_node = PhysicalASAPDAGNode { + output_schema: Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ], + 0, + vec![], + ), + ..node(producer) + }; + let input = ProducerOutput { + payload: &producer_node.payload, + output_schema: &producer_node.output_schema, + output: EdgeStatistics { + rows: INPUT_ROWS, + bytes: INPUT_ROWS * 16, + }, + sources: BTreeSet::from([source]), + family: StateFamily::None, + unchanged_columns, + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + }; + let build = PhysicalASAPDAGNode { + parameters: Some(SketchParams::DDSketch { alpha: ALPHA }), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: FieldDataType::Sketch(SketchAlgorithm::DDSketch), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![]), + filter: None, + })) + }; + node_resource_usage_and_cost(&build, &[input], (1, 1.0), None, &data, &settings) + .unwrap() + .output + .bytes + } + + /// The last five minutes of a scan: raw samples. + fn last_five_minutes() -> Payload { + Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Range, + child: 0, + }) + } + + /// `rate(x[1m])` per series: not raw samples. + fn rate_per_series() -> Payload { + Payload::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::PerEntity, + measures: vec![AggIntent::Rate], + output_names: vec!["rate".into()], + filters: vec![], + having: None, + child: 0, + }) + } + + /// The two store counts price differently, so each test below can + /// tell them apart. + fn assert_store_counts_differ() { + assert!(state_bytes_with(true) < state_bytes_with(false)); + } + + /// A DDSketch updated with `rate(c_total[1m])` per series (its + /// producer the rate, as in `quantile_over_time(0.9, + /// rate(c_total[1m])[5m:1m])`): a counter's rates are each ≥ +0, so + /// one store, though its producer is not a raw scan. + #[test] + #[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] + fn b08_ddsketch_over_a_rate_of_a_counter_prices_one_store() { + assert_store_counts_differ(); + assert_eq!( + priced_state_bytes(rate_per_series(), "c_total"), + state_bytes_with(true) + ); + } + + /// Over `neg`, declared non-positive: every value ≤ 0, one store. + #[test] + #[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] + fn ddsketch_over_non_positive_values_prices_one_store() { + assert_store_counts_differ(); + assert_eq!( + priced_state_bytes(last_five_minutes(), "neg"), + state_bytes_with(true) + ); + } + + /// Over `mixed`, undeclared: values of both signs, two stores. + #[test] + #[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] + fn ddsketch_over_mixed_values_prices_two_stores() { + assert_store_counts_differ(); + assert_eq!( + priced_state_bytes(last_five_minutes(), "mixed"), + state_bytes_with(false) + ); + } + + /// Over `pos`, declared non-negative and read raw: one store, as + /// today. + #[test] + #[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] + fn ddsketch_over_a_declared_raw_scan_is_unchanged() { + assert_store_counts_differ(); + assert_eq!( + priced_state_bytes(last_five_minutes(), "pos"), + state_bytes_with(true) + ); + } + + /// Over `h_sum`, the sum of an undeclared histogram family: a + /// histogram may observe negative values (a temperature delta), so + /// its sum may fall and have either sign: two stores. Today the cost + /// reads `_sum` as a counter. + #[test] + #[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] + fn an_undeclared_histogram_sum_prices_two_stores() { + // provisional answer Q8: only a declaration on the family's + // `value` makes `h_sum` one-signed. + assert_store_counts_differ(); + assert_eq!( + priced_state_bytes(last_five_minutes(), "h_sum"), + state_bytes_with(false) + ); + } +} + +/// The cost derives no sign of its own (sign test plan T6): the source +/// lookups `declared_non_negative`, `declared_counter` and +/// `raw_data_samples_non_negative` are gone from the cost crate and from +/// `asap_types::workload::bounds`, and the cost reads the producer's +/// `column_signs`. A source-text check of the non-test files. +#[test] +#[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] +fn cost_derives_no_sign() { + use std::path::{Path, PathBuf}; + fn non_test_files(directory: &Path, files: &mut Vec) { + for entry in std::fs::read_dir(directory).unwrap() { + let path = entry.unwrap().path(); + let name = path.file_name().unwrap().to_string_lossy().into_owned(); + if path.is_dir() { + if name != "tests" { + non_test_files(&path, files); + } + } else if name.ends_with(".rs") && name != "tests.rs" { + files.push(path); + } + } + } + let crate_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let mut files = Vec::new(); + non_test_files(&crate_dir.join("src"), &mut files); + files.push(crate_dir.join("../types/src/workload/bounds.rs")); + let mut violations = Vec::new(); + let mut reads_column_signs = false; + for path in &files { + let source = std::fs::read_to_string(path).unwrap(); + let in_cost = path.starts_with(crate_dir.join("src")); + reads_column_signs |= in_cost && source.contains("column_signs"); + for derivation in [ + "declared_non_negative", + "declared_counter", + "raw_data_samples_non_negative", + ] { + if source.contains(derivation) { + violations.push(format!("{}: {derivation}", path.display())); + } + } + } + if !reads_column_signs { + violations.push("the cost crate reads no column_signs".into()); + } + assert!(violations.is_empty(), "{violations:#?}"); +} diff --git a/crates/frontend-promql/tests/histogram_metadata.rs b/crates/frontend-promql/tests/histogram_metadata.rs index 8b44fbcea..979a7bc16 100644 --- a/crates/frontend-promql/tests/histogram_metadata.rs +++ b/crates/frontend-promql/tests/histogram_metadata.rs @@ -206,3 +206,33 @@ fn declared_histogram_family() { assert!(declared_non_negative(&data, "foo_sum", "value")); assert!(declared_non_negative(&data, "foo_count", "value")); } + +/// The `Scan` signs of a declared histogram family `foo` whose `value` +/// is not declared (sign test plan T6, replacing the cost's +/// `declared_non_negative` asserts above): `foo_count` counts +/// observations, a counter, so its values are ≥ +0 (`NonNegative`); +/// `foo_sum` adds the observations, which may be negative (a histogram +/// of temperature deltas), so it may fall and have either sign +/// (`Unknown`). Today `foo_sum` reads as a counter, `NonNegative`. +#[test] +#[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] +fn histogram_metadata() { + use asap_types::ir::schema::DataType; + use asap_types::stages::{Memo, ValueSign}; + use asap_types::test_support::memo_fixtures::scan; + // provisional answer Q8: only a declaration on the family's `value` + // gives `_sum` a sign; the Scan rule, not `series_facts`, changes. + let data = over_family("", "foo", Histogram).data_workload.unwrap(); + let mut memo = Memo::new(&data); + for (series, expected) in [ + ("foo_sum", ValueSign::Unknown), + ("foo_count", ValueSign::NonNegative), + ] { + let group = memo + .find_or_add_group(scan(series, &[("job", DataType::Utf8)], vec![])) + .unwrap(); + let properties = &memo.groups()[group.0 as usize].group_properties; + let value = properties.schema.column_id("value").unwrap(); + assert_eq!(properties.column_signs[value], expected, "{series}"); + } +} diff --git a/crates/integration-tests/Cargo.toml b/crates/integration-tests/Cargo.toml index 49d278e7b..46c2c4eb1 100644 --- a/crates/integration-tests/Cargo.toml +++ b/crates/integration-tests/Cargo.toml @@ -31,6 +31,10 @@ serde_json = "1" tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] } asap-executor = { path = "../executor", features = ["test-support"] } +# The sign tests enumerate PromQL's function names from the parser, a list +# independent of the planner's own name tables. +metricsql_parser = { path = "../metricsql-parser-vendored" } +strum = "0.26" futures = "0.3" [[test]] diff --git a/crates/integration-tests/tests/main.rs b/crates/integration-tests/tests/main.rs index bebcbab67..2973e1376 100644 --- a/crates/integration-tests/tests/main.rs +++ b/crates/integration-tests/tests/main.rs @@ -35,3 +35,7 @@ mod sql_frequency; mod sql_to_post_asap; mod stage3_objective; mod time_range; +mod value_sign_consumers; +mod value_sign_executor_zeros; +mod value_sign_function_keys; +mod value_sign_oracle; diff --git a/crates/integration-tests/tests/value_sign_consumers.rs b/crates/integration-tests/tests/value_sign_consumers.rs new file mode 100644 index 000000000..6a7b5b979 --- /dev/null +++ b/crates/integration-tests/tests/value_sign_consumers.rs @@ -0,0 +1,279 @@ +//! The sign conditions of the sketches a plan selects, checked on the plans +//! the facade selects (T5.5). Each condition reads the sign the memo +//! derived for the sketch's own state group, never a sign recomputed from +//! declarations: +//! +//! - a Count-Min-family state (`Cms`, `CmsWithHeap`, `HydraCms`, ...) +//! counts weights, and its estimate never falls below the true count only +//! when every weight is non-negative: its state column must be +//! `NonNegative`. `topk(3, sum by (job) (neg))` must not count `neg`'s +//! values in one; +//! - a DDSketch keeps a relative error only when the values it holds have +//! one sign: `abs(quantile_over_time(0.3, mixed[5m]))` with a relative +//! 5% target must not read a DDSketch over `mixed`, whose 0.3-quantile +//! may be `-0.001` while the sketch answers `+0.001`; +//! - a relative error on each value of a `sum` or `avg` bounds the error of +//! the result only when the values have one sign: `sum(x̂ - x) ≤ ε·|sum +//! x|` needs `|sum x| = sum |x|`. + +use std::collections::BTreeSet; + +use asap_devtools::planning_stages::lower_promql_roots; +use asap_planner::{ + run_planning, DeploymentCapabilities, PlanSearchSettings, PlanningModels, PlanningWorkload, + SketchAlgorithm, +}; +use asap_types::ir::operator::AggIntent; +use asap_types::ir::scalar::{resolve_column_ref, ColumnRef}; +use asap_types::ir::{NonASAPOp, Operator}; +use asap_types::stages::{GroupProperties, ValueSign}; +use serde_json::json; + +const SOURCES: [&str; 4] = ["pos", "neg", "mixed", "c_total"]; + +/// `X` is replaced by the source. +const TEMPLATES: [&str; 6] = [ + "sum by (job) (quantile_over_time(0.9, X[5m]))", + "avg(quantile_over_time(0.5, X[5m]))", + "topk(3, sum by (job) (X))", + "sum(-quantile_over_time(0.9, X[5m]))", + "abs(quantile_over_time(0.3, X[5m]))", + "count_over_time(X[5m])", +]; + +#[derive(Debug, Clone, Copy, PartialEq)] +enum Target { + Relative, + Absolute, + Exact, +} + +fn accuracy(target: Target) -> serde_json::Value { + match target { + Target::Relative => json!({"accuracy": { + "max_error": 0.05, "failure_probability": 0.3, + "accuracy_scope": "whole_result", "accuracy_metric": "relative", + }}), + Target::Absolute => json!({"accuracy": { + "max_error": 0.05, "failure_probability": 0.3, + "accuracy_scope": "whole_result", "accuracy_metric": "absolute", + }}), + Target::Exact => json!({}), + } +} + +fn workload(query: &str, target: Target) -> PlanningWorkload { + let gauge = |sign: Option<&str>| { + let mut facts = + json!({"series_count": 1000, "metric_type": "gauge", "sample_interval": 1000}); + if let Some(sign) = sign { + facts["column_signs"] = json!({"value": sign}); + } + facts + }; + serde_json::from_value(json!({ + "query_workload": { + "language": "promql", + "repeating_queries": [{ + "query": query, + "demand": {"fixed_interval_at": {"interval": 60000, "evaluation_phase": 0}}, + "requirements": accuracy(target), + "predictability": {"predictable": {"known_at": null}}, + }], + }, + "data_workload": { + "arrival": {"ingesting": {"rows_per_second": 4000.0}}, + "sources": { + "pos": gauge(Some("non_negative")), + "neg": gauge(Some("non_positive")), + "mixed": gauge(None), + "c_total": {"series_count": 1000, "metric_type": "counter", "sample_interval": 1000}, + }, + }, + })) + .expect("the workload deserializes") +} + +fn is_count_min_family(algorithm: SketchAlgorithm) -> bool { + matches!( + algorithm, + SketchAlgorithm::Cms + | SketchAlgorithm::CmsWithHeap + | SketchAlgorithm::CmsWithSpaceSaving + | SketchAlgorithm::CmsFold + | SketchAlgorithm::HydraCms + ) +} + +/// The sign of the column a `sum` or `avg` measure reads in its input +/// group: its column, or the sample value. +fn measured_sign(measure: &AggIntent, input: &GroupProperties) -> Option> { + let column = match measure { + AggIntent::Sum { col } | AggIntent::Avg { col } => *col, + _ => return None, + }; + let column = column.or_else(|| resolve_column_ref(&ColumnRef::SampleValue, &input.schema).ok()); + Some(column.and_then(|c| input.column_signs.get(c).copied())) +} + +/// T5.5. For each of 72 workloads (4 sources × 6 templates × 3 targets), +/// the plan the facade selects meets every sketch's sign condition. Today +/// `abs(quantile_over_time(0.3, mixed[5m]))` under a relative target is +/// expected to read a DDSketch over `mixed` (B12). // provisional answer Q7 +#[test] +fn selected_sketches_satisfy_their_sign_conditions() { + let capabilities = DeploymentCapabilities { + supported_summaries: Some(asap_executor::supported_summary_families()), + ..DeploymentCapabilities::default() + }; + let mut violations = Vec::new(); + let mut not_planned = Vec::new(); + let mut sketches_over_pos = 0usize; + let mut selected_sketches = Vec::new(); + for source in SOURCES { + for template in TEMPLATES { + for target in [Target::Relative, Target::Absolute, Target::Exact] { + let query = template.replace('X', source); + let label = format!("{query} [{target:?}]"); + let workload = workload(&query, target); + let roots = lower_promql_roots(&workload).expect("the query lowers"); + let entries: Vec<_> = workload.query_workload.entries().collect(); + let data = workload.data_workload.clone().unwrap_or_default(); + let models = PlanningModels::builtin().with_deployment_capabilities(&capabilities); + let run = match run_planning( + roots, + &entries, + &data, + models, + PlanSearchSettings { inspect: true }, + ) { + Ok(run) => run, + Err(e) => { + not_planned.push(format!("{label}: {e}")); + continue; + } + }; + let (dag, physical) = match (&run.selection, &run.physical_asap_candidates) { + (Ok((dag, _)), Some(physical)) => (dag, physical), + (selection, _) => { + not_planned.push(format!("{label}: {:?}", selection.as_ref().err())); + continue; + } + }; + let memo = &physical.logical_candidates.memo; + let group_of = |node: &asap_planner::PhysicalASAPDAG, id: usize| { + node.nodes + .iter() + .find(|n| n.id == id) + .and_then(|n| n.selected_physical_candidate) + .map(|c| physical.physical_candidates[c.0].logical_candidate_id) + }; + // The selected nodes that build or merge a sketch state. + let sketch_nodes: BTreeSet = dag + .nodes + .iter() + .filter(|node| { + group_of(dag, node.id).is_some_and(|candidate| { + memo.groups()[candidate.group.0 as usize] + .group_properties + .sketch_algorithm_of_state() + .is_some() + }) + }) + .map(|node| node.id) + .collect(); + for &id in &sketch_nodes { + let candidate = group_of(dag, id).unwrap(); + let properties = &memo.groups()[candidate.group.0 as usize].group_properties; + let algorithm = properties.sketch_algorithm_of_state().unwrap(); + selected_sketches.push(format!("{label}: {algorithm:?}")); + if source == "pos" { + sketches_over_pos += 1; + } + if is_count_min_family(algorithm) + && properties.state_column_sign() != Some(ValueSign::NonNegative) + { + violations.push(format!( + "{label}: {algorithm:?} state column sign {:?}, not NonNegative", + properties.state_column_sign() + )); + } + if algorithm == SketchAlgorithm::DDSketch + && target == Target::Relative + && !properties + .state_column_sign() + .is_some_and(ValueSign::is_one_sign) + { + violations.push(format!( + "{label}: DDSketch under a relative target, state column sign {:?}", + properties.state_column_sign() + )); + } + } + if target != Target::Relative { + continue; + } + // A relative `sum` or `avg` above a sketch reads values of + // one sign. + let reaches_sketch = |id: usize| { + let mut pending = vec![id]; + let mut seen = BTreeSet::new(); + while let Some(node) = pending.pop() { + if !seen.insert(node) { + continue; + } + if node != id && sketch_nodes.contains(&node) { + return true; + } + pending.extend( + dag.edges + .iter() + .filter(|e| e.consumer == node) + .map(|e| e.producer), + ); + } + false + }; + for node in &dag.nodes { + let Some(candidate) = group_of(dag, node.id) else { + continue; + }; + let expression = &memo.candidate(candidate).expression.operator; + let Operator::NonASAP(NonASAPOp::Aggregate { + measures, child, .. + }) = expression + else { + continue; + }; + if !reaches_sketch(node.id) { + continue; + } + let input = &memo.groups()[child.0 as usize].group_properties; + for measure in measures { + let Some(sign) = measured_sign(measure, input) else { + continue; + }; + if !sign.is_some_and(ValueSign::is_one_sign) { + violations.push(format!( + "{label}: relative {measure:?} above a sketch reads sign {sign:?}" + )); + } + } + } + } + } + } + println!("selected sketches ({}):", selected_sketches.len()); + for s in &selected_sketches { + println!(" SELECTED {s}"); + } + println!("not planned ({}): {not_planned:#?}", not_planned.len()); + for v in &violations { + println!("VIOLATION {v}"); + } + assert!( + sketches_over_pos > 0, + "no sketch was selected over `pos`: the check is vacuous" + ); + assert!(violations.is_empty(), "{violations:#?}"); +} diff --git a/crates/integration-tests/tests/value_sign_executor_zeros.rs b/crates/integration-tests/tests/value_sign_executor_zeros.rs new file mode 100644 index 000000000..6a3fe144a --- /dev/null +++ b/crates/integration-tests/tests/value_sign_executor_zeros.rs @@ -0,0 +1,200 @@ +//! What the executor computes at signed zeros (sign test plan, T4 rows f1, +//! f2 and f3). A value's sign is its IEEE sign bit, so `-0.0` is +//! `NonPositive`; these rows check that the executor keeps the sign bit +//! where the planner's sign rules need it: +//! +//! - f1: a sum of only `-0.0` values is `-0.0` (the IEEE additive identity +//! is `-0.0`; starting a sum at `+0.0` gives `+0.0 + -0.0 = +0.0`); +//! - f2: `sgn(-0.0)` is `-0.0`, as in Prometheus (`sgn(v)` returns `v` for +//! a zero); +//! - f3: `clamp_min` / `clamp_max` resolve signed zeros as Go's +//! `math.Max(+0, -0) = +0` and `math.Min(+0, -0) = -0`. +//! +//! Each expected value is the IEEE / Prometheus result, written out by hand. +use std::collections::BTreeMap; +use std::sync::Arc; + +use asap_executor::physical_planner::{compile, promql_rows}; +use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::sources::InMemoryDataStore; +use asap_executor::values::{Batch, Value}; +use asap_integration_tests::fixtures::lower_promql; +use asap_types::accuracy::AccuracyTarget; +use asap_types::ir::operator::Source; +use asap_types::ir::NonASAPOp; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; +use futures::{executor::block_on, StreamExt}; + +/// Evaluation time; a `[1m]` window is `(240 s, 300 s]`. +const EVALUATION_MS: i64 = 300_000; + +/// One raw sample of `x{instance}`. +#[derive(Clone)] +struct RawDataSample { + instance: &'static str, + timestamp_ms: i64, + value: f64, +} + +/// `values` of `x{instance}`, 10 s apart from 250 s. +fn series(instance: &'static str, values: &[f64]) -> Vec { + values + .iter() + .enumerate() + .map(|(i, value)| RawDataSample { + instance, + timestamp_ms: 250_000 + 10_000 * i as i64, + value: *value, + }) + .collect() +} + +/// One sample `value` of `x{instance}` at the evaluation time, which an +/// instant selector reads (its lookback is the sample interval). +fn latest(instance: &'static str, value: f64) -> Vec { + vec![RawDataSample { + instance, + timestamp_ms: EVALUATION_MS, + value, + }] +} + +/// `query`, lowered exactly with series identity and run at query time over +/// `samples` of `x`: every Float64 value of its answer. +fn evaluate(query: &str, samples: &[RawDataSample]) -> Vec { + let root = lower_promql(query, AccuracyTarget::exact()).unwrap(); + let root = promql_rows::with_series_identity(&root).unwrap(); + let dag = asap_types::test_support::forwarders::physical_asap_dag_at_query_time(&root).unwrap(); + // Every query reads one series, `x`, from one `Scan`. + let scans: Vec<_> = dag + .nodes + .iter() + .filter_map(|node| match &node.payload { + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { source, .. }) => { + Some((source.clone(), node)) + } + _ => None, + }) + .collect(); + let [(source, scan)] = &scans[..] else { + panic!("{query}: one scan of x expected, found {}", scans.len()); + }; + assert_eq!(*source, Source::TimeSeries { metric: "x".into() }); + let schema = Arc::new(scan.output_schema.clone()); + let rows = samples + .iter() + .map(|sample| { + let labels = BTreeMap::from([ + ("__name__".to_string(), "x".to_string()), + ("instance".to_string(), sample.instance.to_string()), + ]); + promql_rows::series_row(&schema, &labels, sample.timestamp_ms, sample.value).unwrap() + }) + .collect(); + let batch = Batch::try_new(schema.clone(), rows).unwrap(); + let store = InMemoryDataStore::default() + .with_raw_data(source.clone(), schema, vec![batch]) + .unwrap(); + let roots = [dag.roots[0] as u64]; + let bound = compile(&dag, &[], &roots) + .unwrap() + .instantiate(Arc::new(store)) + .unwrap(); + let context = RunContext::new( + Scope::Query { + evaluation_time_ms: EVALUATION_MS, + }, + Limits::default(), + ) + .unwrap(); + let batches: Vec = block_on(async { + let mut output = bound.execute(&roots, context).unwrap().remove(0); + let mut batches = Vec::new(); + while let Some(batch) = output.next().await { + batches.push((*batch.unwrap()).clone()); + } + batches + }); + batches + .iter() + .flat_map(|batch| batch.to_rows()) + .flat_map(|row| { + row.into_iter().filter_map(|cell| match cell { + Value::Float64(value) => Some(value), + _ => None, + }) + }) + .collect() +} + +/// `answers` hold exactly `expected`'s bits (so `-0.0` and `+0.0` differ). +fn assert_bits(query: &str, answers: &[f64], expected: &[f64]) { + let bits = |values: &[f64]| values.iter().map(|v| v.to_bits()).collect::>(); + assert_eq!( + bits(answers), + bits(expected), + "{query}: answered {answers:?}, expected {expected:?}" + ); +} + +/// f1: a sum, or a mean, of only `-0.0` values is `-0.0`: `sum_over_time` +/// and `avg_over_time` over a window of `-0.0`s, and `sum` over two series +/// whose latest sample is `-0.0` (provisional answer Q1: sums start at +/// `-0.0` and add the Kahan compensation only when it is nonzero). +/// +/// Fails today: each is `+0.0`. +#[test] +fn f1_a_sum_of_negative_zeros_keeps_its_sign_bit() { + let window = series("a", &[-0.0, -0.0, -0.0]); + let two = [latest("a", -0.0), latest("b", -0.0)].concat(); + let mut wrong = Vec::new(); + for (query, samples) in [ + ("sum_over_time(x[1m])", &window), + ("avg_over_time(x[1m])", &window), + ("sum(x)", &two), + ("avg(x)", &two), + ] { + let answers = evaluate(query, samples); + // provisional answer Q1: every answer is -0.0. + if answers + .iter() + .map(|v| v.to_bits()) + .ne([(-0.0f64).to_bits()]) + { + wrong.push(format!("{query}: answered {answers:?}, expected [-0.0]")); + } + } + assert!(wrong.is_empty(), "{}", wrong.join("\n")); +} + +/// f2: `sgn(-0.0)` is `-0.0` and `sgn(+0.0)` is `+0.0`, as in Prometheus +/// (provisional answer Q2). +/// +/// Fails today: the executor returns the literal `0.0` for `-0.0`. +#[test] +fn f2_sgn_of_negative_zero_is_negative_zero() { + for (value, expected) in [(-0.0, -0.0), (0.0, 0.0), (-3.0, -1.0)] { + let samples = latest("a", value); + // provisional answer Q2 + assert_bits("sgn(x)", &evaluate("sgn(x)", &samples), &[expected]); + } +} + +/// f3: `clamp_min(-0.0, 0)` is `max(+0, -0) = +0` and +/// `clamp_max(+0.0, -0)` is `min(-0, +0) = -0`, as Go resolves signed +/// zeros (provisional answer Q3). +/// +/// Rust leaves `f64::max(-0.0, +0.0)` unspecified, so this may fail today. +#[test] +fn f3_clamp_resolves_signed_zeros_as_prometheus() { + for (query, value, expected) in [ + ("clamp_min(x, 0)", -0.0, 0.0), + ("clamp_max(x, -0)", 0.0, -0.0), + ("clamp(x, 0, 5)", -0.0, 0.0), + ("clamp(x, -5, -0)", 0.0, -0.0), + ] { + let samples = latest("a", value); + // provisional answer Q3 + assert_bits(query, &evaluate(query, &samples), &[expected]); + } +} diff --git a/crates/integration-tests/tests/value_sign_function_keys.rs b/crates/integration-tests/tests/value_sign_function_keys.rs new file mode 100644 index 000000000..236032e0e --- /dev/null +++ b/crates/integration-tests/tests/value_sign_function_keys.rs @@ -0,0 +1,273 @@ +//! Every PromQL math or date function name parses to one typed key, the +//! `MathFunc` or `TimeFunc` that both the value-sign rules and the error +//! contracts match on (sign test plan, T2.1 and T2.2). +//! +//! The names come from the vendored MetricsQL parser's +//! `TransformFunction` list, which is independent of the planner's own name +//! tables: a function the planner lowers (`promql_function_arity` accepts +//! `promql_`) but the parse functions do not know fails T2.1, and a +//! typed key no name produces fails T2.2. +use std::collections::BTreeMap; +use std::mem::discriminant; + +use asap_types::ir::operator::{MathFunc, TimeFunc}; +use asap_types::ir::scalar::expr_ir::ScalarValue; +use asap_types::ir::scalar::scalar_type_rules::{ + math_function_of_call, promql_function_arity, time_function_of_call, +}; +use asap_types::ir::scalar::ScalarExpr; +use metricsql_parser::functions::TransformFunction; +use strum::IntoEnumIterator; + +/// The literal bounds each call passes after its value argument, chosen so +/// that every parameter has a distinct value: `clamp(v, 1, 2)`, +/// `round(v, 0.5)`. +fn literals(name: &str, arity: usize) -> Vec { + match (name, arity) { + ("clamp", 3) => vec![1.0, 2.0], + ("clamp_min", 2) => vec![3.0], + ("clamp_max", 2) => vec![4.0], + ("round", 2) => vec![0.5], + (_, arity) => vec![7.0; arity - 1], + } +} + +/// `promql_(value, literals…)`. +fn call_args(literals: &[f64]) -> Vec> { + std::iter::once(ScalarExpr::Column(1)) + .chain( + literals + .iter() + .map(|value| ScalarExpr::Literal(ScalarValue::Float64(*value))), + ) + .collect() +} + +/// The typed key of every PromQL math or date function, by hand from the +/// PromQL function reference, with the literals of [`literals`]. +fn expected_keys() -> BTreeMap<&'static str, Key> { + use MathFunc::*; + let math = |f| Key::Math(f); + let time = |f| Key::Time(f); + BTreeMap::from([ + ("abs", math(Abs)), + ("ceil", math(Ceil)), + ("floor", math(Floor)), + ("exp", math(Exp)), + ("ln", math(Ln)), + ("log2", math(Log2)), + ("log10", math(Log10)), + ("sqrt", math(Sqrt)), + ("sgn", math(Sgn)), + ("sin", math(Sin)), + ("cos", math(Cos)), + ("tan", math(Tan)), + ("asin", math(Asin)), + ("acos", math(Acos)), + ("atan", math(Atan)), + ("sinh", math(Sinh)), + ("cosh", math(Cosh)), + ("tanh", math(Tanh)), + ("asinh", math(Asinh)), + ("acosh", math(Acosh)), + ("atanh", math(Atanh)), + ("deg", math(Deg)), + ("rad", math(Rad)), + ("round", math(Round { to_nearest: 0.5 })), + ("clamp", math(Clamp { min: 1.0, max: 2.0 })), + ("clamp_min", math(ClampMin { min: 3.0 })), + ("clamp_max", math(ClampMax { max: 4.0 })), + ("minute", time(TimeFunc::Minute)), + ("hour", time(TimeFunc::Hour)), + ("day_of_week", time(TimeFunc::DayOfWeek)), + ("day_of_month", time(TimeFunc::DayOfMonth)), + ("day_of_year", time(TimeFunc::DayOfYear)), + ("month", time(TimeFunc::Month)), + ("year", time(TimeFunc::Year)), + ("days_in_month", time(TimeFunc::DaysInMonth)), + ]) +} + +#[derive(Debug, Clone, PartialEq)] +enum Key { + Math(MathFunc), + Time(TimeFunc), +} + +/// The key `promql_(value, literals…)` parses to, if any. +fn parsed_key(name: &str, arity: usize) -> Option { + let full = format!("promql_{name}"); + let args = call_args(&literals(name, arity)); + math_function_of_call(&full, &args) + .map(Key::Math) + .or_else(|| time_function_of_call(&full).map(Key::Time)) +} + +/// The parser's function names the planner lowers, with their arity. +fn lowered_names() -> Vec<(&'static str, usize)> { + TransformFunction::iter() + .filter_map(|function| { + let name = function.name(); + promql_function_arity(&format!("promql_{name}")).map(|arity| (name, arity)) + }) + .collect() +} + +/// T2.1: every PromQL function the planner lowers parses to a typed key, +/// the exact one. The kept names are exactly the 35 math and date +/// functions (the plan's "31" counts only the one-argument ones). +/// +/// Fails today: the parse functions are not implemented. +#[test] +fn every_promql_function_name_parses_to_a_typed_key() { + let lowered = lowered_names(); + let expected = expected_keys(); + let mut kept: Vec<&str> = lowered.iter().map(|(name, _)| *name).collect(); + kept.sort_unstable(); + let mut listed: Vec<&str> = expected.keys().copied().collect(); + listed.sort_unstable(); + assert_eq!( + kept, listed, + "the PromQL functions promql_function_arity accepts" + ); + let mut wrong = Vec::new(); + for (name, arity) in lowered { + let got = parsed_key(name, arity); + let want = expected.get(name).cloned(); + if got != want { + wrong.push(format!("promql_{name}: parsed {got:?}, expected {want:?}")); + } + } + assert!(wrong.is_empty(), "{}", wrong.join("\n")); +} + +/// Every `MathFunc` variant, through an exhaustive match (no `_` arm). +fn all_math_funcs() -> Vec { + use MathFunc::*; + let all = vec![ + Abs, + Ceil, + Floor, + Exp, + Ln, + Log2, + Log10, + Sqrt, + Sgn, + Sin, + Cos, + Tan, + Asin, + Acos, + Atan, + Sinh, + Cosh, + Tanh, + Asinh, + Acosh, + Atanh, + Deg, + Rad, + Round { to_nearest: 1.0 }, + Clamp { min: 0.0, max: 1.0 }, + ClampMin { min: 0.0 }, + ClampMax { max: 1.0 }, + ]; + for function in &all { + match function { + Abs + | Ceil + | Floor + | Exp + | Ln + | Log2 + | Log10 + | Sqrt + | Sgn + | Sin + | Cos + | Tan + | Asin + | Acos + | Atan + | Sinh + | Cosh + | Tanh + | Asinh + | Acosh + | Atanh + | Deg + | Rad + | Round { .. } + | Clamp { .. } + | ClampMin { .. } + | ClampMax { .. } => {} + } + } + all +} + +/// Every `TimeFunc` variant, through an exhaustive match. +fn all_time_funcs() -> Vec { + use TimeFunc::*; + let all = vec![ + Timestamp, + Minute, + Hour, + DayOfWeek, + DayOfMonth, + DayOfYear, + Month, + Year, + DaysInMonth, + ]; + for function in &all { + match function { + Timestamp | Minute | Hour | DayOfWeek | DayOfMonth | DayOfYear | Month | Year + | DaysInMonth => {} + } + } + all +} + +/// T2.2: every typed key is produced by some PromQL name, except +/// `TimeFunc::Timestamp`, which reads a sample's time, not its value, and +/// lowers to `AggIntent::TimeFn`. +/// +/// Fails today: the parse functions are not implemented. +#[test] +fn every_typed_key_is_reachable_from_a_name() { + let produced: Vec = lowered_names() + .into_iter() + .filter_map(|(name, arity)| parsed_key(name, arity)) + .collect(); + let mut unreachable = Vec::new(); + for function in all_math_funcs() { + let reached = produced + .iter() + .any(|key| matches!(key, Key::Math(f) if discriminant(f) == discriminant(&function))); + if !reached { + unreachable.push(format!("{function:?}")); + } + } + for function in all_time_funcs() { + if function == TimeFunc::Timestamp { + continue; + } + if !produced.contains(&Key::Time(function)) { + unreachable.push(format!("{function:?}")); + } + } + assert!( + unreachable.is_empty(), + "keys no name produces: {unreachable:?}" + ); +} + +/// A bound that is not a literal names no typed key: +/// `promql_clamp_min(value, value)` → `None`. +#[test] +fn a_non_literal_bound_has_no_typed_key() { + let args: Vec> = vec![ScalarExpr::Column(1), ScalarExpr::Column(1)]; + assert_eq!(math_function_of_call("promql_clamp_min", &args), None); +} diff --git a/crates/integration-tests/tests/value_sign_oracle.rs b/crates/integration-tests/tests/value_sign_oracle.rs new file mode 100644 index 000000000..9457b63b9 --- /dev/null +++ b/crates/integration-tests/tests/value_sign_oracle.rs @@ -0,0 +1,1751 @@ +//! The execution oracle for value signs: a memo group's `column_signs` is a +//! claim about every non-NaN, non-NULL value the group's expression +//! computes exactly, at every evaluation time. These tests check the claim +//! against values the executor computes, never against a sign the code +//! derives itself. +//! +//! - `every_known_sign_holds_on_evaluated_values` (and its nightly 200-seed +//! twin): every subtree node of many queries, planned alone in a memo and +//! run exactly at query time over generated data, at several evaluation +//! times. A column claimed `NonPositive` holding `+0.0` is a +//! counterexample: `sum_over_time(neg[5m])` over a window of only `-0.0` +//! evaluates to `+0.0` (`-0.0 + +0.0 = +0.0`). +//! - `exact_accumulator_realizations_keep_the_group_sign`: the same group +//! placed at ingestion time, as a tumbling-window chain of exact states +//! merged and finalized at query time. +//! - `a_groups_sign_does_not_depend_on_its_queries`: one expression has one +//! sign in every memo, whatever queries share it, and no exact candidate +//! of a group derives the opposite sign. +//! +//! The generated data satisfies every declaration: a source declared +//! `non_positive` holds only values whose sign bit is set (`-0.0`, +//! `-5e-324`, `-inf`, ...). A declaration is an assumption the planner may +//! rely on, so data that breaks it would test nothing. + +use std::collections::{BTreeMap, HashMap}; +use std::rc::Rc; +use std::sync::Arc; +use std::time::Duration; + +use asap_devtools::planner_fuzz::generate::{random_workload, Rng}; +use asap_devtools::planner_fuzz::Language; +use asap_devtools::planning_stages::{ + lower_promql_roots, lower_sql_roots, run_stages_until, sql_catalog, RunUntil, +}; +use asap_executor::physical_planner::{compile, promql_rows, test_support::views_at}; +use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::sources::InMemoryDataStore; +use asap_executor::values::{Batch, Value}; +use asap_planner::{PlanningModels, PlanningWorkload, SqlCatalog}; +use asap_types::ir::operator::{Reduction, Source}; +use asap_types::ir::scalar::ColumnRef; +use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema, PROMQL_SERIES_IDENTITY}; +use asap_types::ir::{ASAPOp, NonASAPOp, Operator, OperatorNode, QueryRoot, TimeRangeKind}; +use asap_types::plan::physical_asap_dag::{PhysicalASAPDAG, PhysicalASAPOperatorPayload}; +use asap_types::stages::{Memo, MemoGroupId, ValueSign}; +use asap_types::summary_type::{ExactKind, ExactParams, SummaryUpdate}; +use asap_types::test_support::forwarders::{ + column_signs_of_candidate_expression, physical_asap_dag_at_query_time_with_node_ids, +}; +use asap_types::workload::DataWorkload; +use futures::{executor::block_on, StreamExt}; +use serde_json::{json, Value as Json}; + +// --------------------------------------------------------------------------- +// The timeline every generated source follows, one sample per second per +// series: +// +// (0, 21 min] ordinary values of the declared sign; `+inf` and NaN +// appear only in (0, 10.5 min], so a 5-minute window +// ending at 21 min holds finite values only; +// (21 min, 42 min] only zeros of the declared sign: `-0.0` for a +// non-positive source, `+0.0` for a non-negative one; +// (42 min, 63 min] no samples at all. +// +// Evaluation times: 10.5 min (non-finite values in the window), 21 min +// (ordinary), 42 min (a window of up to 21 min holds only zeros, `-0.0` +// for `neg`), 63 min (every window of up to 21 min is empty). +// --------------------------------------------------------------------------- + +const STEP_MS: i64 = 1_000; +const NON_FINITE_END_MS: i64 = 630_000; +const ORDINARY_END_MS: i64 = 1_260_000; +const ZEROS_END_MS: i64 = 2_520_000; +const EMPTY_END_MS: i64 = 3_780_000; +const SERIES: usize = 12; + +const EVALUATION_TIMES: [(&str, i64); 4] = [ + ("non_finite_window", NON_FINITE_END_MS), + ("ordinary_window", ORDINARY_END_MS), + ("only_zeros_window", ZEROS_END_MS), + ("empty_window", EMPTY_END_MS), +]; + +/// Recorded on the first run (Q12): how many (query, subtree node) pairs of +/// the CI oracle run executed at every evaluation time. Coverage below it +/// means the executor or the lowering lost operators the oracle used to +/// check. // provisional answer Q12 +const MIN_COVERED_NODES_CI: usize = 1683; + +/// Deterministic pseudo-random draw in `[0, 1)` from `key`. +fn unit_draw(mut key: u64) -> f64 { + key ^= key >> 33; + key = key.wrapping_mul(0xff51_afd7_ed55_8ccd); + key ^= key >> 33; + key = key.wrapping_mul(0xc4ce_b9fe_1a85_ec53); + key ^= key >> 33; + (key >> 11) as f64 / (1u64 << 53) as f64 +} + +fn name_hash(name: &str) -> u64 { + name.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| { + (h ^ b as u64).wrapping_mul(0x100_0000_01b3) + }) +} + +/// What one generated series holds, chosen from the source's facts so that +/// the data satisfies its declaration. +#[derive(Debug, Clone, Copy, PartialEq)] +enum SeriesKind { + /// Declared `non_negative`: values with a clear sign bit, `+0.0`, + /// `5e-324`, `1e300`, `+inf`, ... + NonNegative, + /// Declared `non_positive`: the mirror image, `-0.0`, `-5e-324`, + /// `-inf`, ... + NonPositive, + /// Undeclared: both, plus series crossing zero and series alternating + /// `+0.0` and `-0.0`. + Mixed, + /// An undeclared or non-negative counter: monotone, resets to `+0.0`. + Counter, + /// An undeclared histogram `_sum`: decreasing, the running sum of + /// negative observations (a latency offset, a balance change). + DecreasingSum, + /// The SQL `p` column: values in `[-1, 1]` of the declared sign. + Fraction(Declared), + /// The SQL `bytes` Int64 column: `0`, `1` or `5000`. + Bytes, +} + +#[derive(Debug, Clone, Copy, PartialEq)] +enum Declared { + NonNegative, + NonPositive, + Undeclared, +} + +/// The non-negative pattern of series `s` at sample `k` (draw `r`). The +/// special values come first: `+0.0`, the smallest subnormal, tiny and huge +/// magnitudes, and `1e11`, a time before year 1 when negated. +fn non_negative_value(s: usize, k: u64, t_ms: i64, r: f64) -> f64 { + const FINITE: [f64; 7] = [0.0, 5e-324, 1e-300, 0.3, 1.0, 1e300, 1e11]; + const NON_FINITE: [f64; 2] = [f64::INFINITY, f64::NAN]; + let pareto = (1.0 - r).powf(-1.0 / 0.8) * (1 + s % 7) as f64; + match s { + // Every special value in turn; +inf and NaN only early on. + 0 if t_ms <= NON_FINITE_END_MS && k % 9 >= 7 => NON_FINITE[(k % 9 - 7) as usize], + 0 => FINITE[(k % 7) as usize], + 1 => 0.0, + 2 => r * 1000.0, + 3 => pareto, + 4 => [0.0, 5e-324, 1e-300][(k % 3) as usize], + 5 => [1e300, 1.0, 0.3][(k % 3) as usize], + 6 if t_ms <= NON_FINITE_END_MS && k % 41 == 0 => f64::INFINITY, + 6 => FINITE[((k + 3) % 7) as usize], + 7 if k % 37 == 0 => FINITE[((k / 37) % 7) as usize], + 7 => r * 100.0, + 8 => (r * 10.0).floor() / 10.0, + 9 if r < 0.05 => 0.0, + 9 => pareto, + 10 => r, + _ => (r * 5.0).floor(), + } +} + +/// The values of one series of `kind` over the timeline: `(t_ms, value)`. +fn series_values(kind: SeriesKind, s: usize, key: u64) -> Vec<(i64, f64)> { + let mut out = Vec::new(); + let mut counter = 0.0f64; + let mut t_ms = STEP_MS; + let mut k = 0u64; + while t_ms <= ZEROS_END_MS { + let r = unit_draw(key ^ (s as u64) << 40 ^ k.wrapping_mul(0x9e37_79b9_7f4a_7c15)); + let zeros = t_ms > ORDINARY_END_MS; + let value = match kind { + SeriesKind::NonNegative if zeros => 0.0, + SeriesKind::NonNegative => non_negative_value(s, k, t_ms, r), + SeriesKind::NonPositive if zeros => -0.0, + SeriesKind::NonPositive => -non_negative_value(s, k, t_ms, r), + SeriesKind::Mixed if zeros => [0.0, -0.0][s % 2], + SeriesKind::Mixed => match s % 4 { + 0 => non_negative_value(s, k, t_ms, r), + 1 => -non_negative_value(s, k, t_ms, r), + 2 => (r - 0.5) * 100.0, + _ => [0.0, -0.0][(k % 2) as usize], + }, + SeriesKind::Counter => { + if zeros || s == 1 || k % (150 + 17 * s as u64) == 0 { + // A reset (a restart) to +0.0; flat at +0.0 later on. + counter = 0.0; + } else { + counter += (r * 10.0 * (1 + s % 5) as f64).floor(); + } + counter + } + SeriesKind::DecreasingSum => { + counter -= r * 10.0; + counter + } + SeriesKind::Fraction(declared) => { + let magnitude = if zeros || k % 23 == 0 { 0.0 } else { r }; + match declared { + Declared::NonNegative => magnitude, + Declared::NonPositive => -magnitude, + Declared::Undeclared if zeros => [0.0, -0.0][s % 2], + Declared::Undeclared => 2.0 * r - 1.0, + } + } + SeriesKind::Bytes if zeros => 0.0, + SeriesKind::Bytes => [0.0, 1.0, 5000.0][((k + s as u64) % 3) as usize], + }; + out.push((t_ms, value)); + t_ms += STEP_MS; + k += 1; + } + out +} + +fn declared_sign(facts: &Json, column: &str) -> Declared { + match facts["column_signs"][column].as_str() { + Some("non_negative") => Declared::NonNegative, + Some("non_positive") => Declared::NonPositive, + _ => Declared::Undeclared, + } +} + +fn gauge_kind(declared: Declared) -> SeriesKind { + match declared { + Declared::NonNegative => SeriesKind::NonNegative, + Declared::NonPositive => SeriesKind::NonPositive, + Declared::Undeclared => SeriesKind::Mixed, + } +} + +/// The kind of the series of PromQL metric `metric`, from its facts (or +/// its histogram family's): a counter declared `non_positive` holds +/// non-positive values, as its declaration says. // provisional answer Q8 +fn metric_kind(sources: &Json, metric: &str) -> SeriesKind { + if let Some(facts) = sources.get(metric) { + let declared = declared_sign(facts, "value"); + return match (facts["metric_type"].as_str(), declared) { + (Some("counter"), Declared::NonPositive) => SeriesKind::NonPositive, + (Some("counter"), _) => SeriesKind::Counter, + _ => gauge_kind(declared), + }; + } + for suffix in ["_sum", "_count", "_bucket"] { + let Some(family) = metric.strip_suffix(suffix) else { + continue; + }; + let Some(facts) = sources.get(family) else { + continue; + }; + // "Declared" is the family's `value` entry. // provisional answer Q8 + return match (suffix, declared_sign(facts, "value")) { + (_, Declared::NonPositive) => SeriesKind::NonPositive, + ("_sum", Declared::Undeclared) => SeriesKind::DecreasingSum, + _ => SeriesKind::Counter, + }; + } + SeriesKind::Mixed +} + +/// How the generated series are labelled: the review workloads match +/// `job="j0"`, the fuzz generator `job="job1"`. +#[derive(Debug, Clone, Copy)] +enum Labels { + Review, + Fuzz, +} + +fn series_labels(scheme: Labels, metric: &str, s: usize) -> BTreeMap { + let mut labels = BTreeMap::from([("__name__".to_string(), metric.to_string())]); + let pairs: Vec<(&str, String)> = match scheme { + Labels::Review => vec![ + ("instance", format!("i{s}")), + ("job", format!("j{}", s % 3)), + ("host", format!("h{}", s % 4)), + ("region", format!("r{}", s % 2)), + ], + Labels::Fuzz => vec![ + ("job", format!("job{}", s % 5 + 1)), + ("instance", format!("instance{}", s + 1)), + ("region", format!("region{}", s % 3 + 1)), + ], + }; + labels.extend(pairs.into_iter().map(|(k, v)| (k.to_string(), v))); + labels +} + +/// The value of a SQL text column `column` in "series" `s`. +fn sql_text(column: &str, s: usize) -> String { + match column { + "src" => format!("s{}", s % 6), + "dst" => format!("d{s}"), + _ => format!("{column}{}", s + 1), + } +} + +/// The union of the schemas `dag`'s `Scan`s read from each source, by +/// column name. +fn scanned_schemas(dag: &PhysicalASAPDAG) -> BTreeMap { + let mut schemas: BTreeMap = BTreeMap::new(); + for node in &dag.nodes { + let PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { source, schema, .. }) = + &node.payload + else { + continue; + }; + let known = schemas + .entry(source.clone()) + .or_insert_with(|| schema.clone()); + for field in &schema.fields { + if !known.fields.iter().any(|f| f.name == field.name) { + known.fields.push(field.clone()); + } + } + } + schemas +} + +/// The data of one oracle case: its facts, label scheme and seed. +struct CaseData { + facts: DataWorkload, + sources: Json, + labels: Labels, + seed: u64, + /// The generated raw data per source and scanned schema: queries of one + /// case scan the same sources with the same columns. + batches: std::cell::RefCell>, +} + +impl CaseData { + fn new(facts: Json, labels: Labels, seed: u64) -> Self { + let sources = facts["sources"].clone(); + Self { + facts: serde_json::from_value(facts).expect("facts deserialize"), + sources, + labels, + seed, + batches: Default::default(), + } + } + + /// The raw data every `Scan` of `dag` reads, generated per declared + /// sign. + fn store_for(&self, dag: &PhysicalASAPDAG) -> Result { + let mut store = InMemoryDataStore::default(); + for (source, schema) in scanned_schemas(dag) { + let key = (source.clone(), format!("{schema:?}")); + let schema = Arc::new(schema); + let cached = self.batches.borrow().get(&key).cloned(); + let batch = match cached { + Some(batch) => batch, + None => { + let rows = match &source { + Source::TimeSeries { metric } => self.time_series_rows(&schema, metric)?, + Source::Table { table_ref } => self.table_rows(&schema, table_ref)?, + }; + let batch = Batch::try_new(schema.clone(), rows).map_err(|e| e.to_string())?; + self.batches.borrow_mut().insert(key, batch.clone()); + batch + } + }; + store = store + .with_raw_data(source, schema, vec![batch]) + .map_err(|e| e.to_string())?; + } + Ok(store) + } + + fn time_series_rows( + &self, + schema: &Arc, + metric: &str, + ) -> Result>, String> { + let kind = metric_kind(&self.sources, metric); + let mut rows = Vec::new(); + for s in 0..SERIES { + let labels = series_labels(self.labels, metric, s); + for (t_ms, value) in series_values(kind, s, self.seed ^ name_hash(metric)) { + rows.push( + promql_rows::series_row(schema, &labels, t_ms, value) + .map_err(|e| e.to_string())?, + ); + } + } + Ok(rows) + } + + fn table_rows(&self, schema: &Arc, table: &str) -> Result>, String> { + let facts = &self.sources[table]; + let mut columns = Vec::new(); + for (index, field) in schema.fields.iter().enumerate() { + let generated: Option> = match &field.dtype { + _ if Some(index) == schema.time_index => None, + FieldDataType::Plain(DataType::Int64) => Some(Vec::new()), + FieldDataType::Plain(DataType::Float64) => Some(Vec::new()), + FieldDataType::Plain(DataType::Utf8) => None, + other => { + return Err(format!( + "no generated data for {table}.{}: {other:?}", + field.name + )) + } + }; + columns.push(generated); + } + let mut rows = Vec::new(); + for s in 0..SERIES { + let per_column: Vec>> = schema + .fields + .iter() + .zip(&columns) + .map(|(field, numeric)| { + numeric.as_ref().map(|_| { + let declared = declared_sign(facts, &field.name); + let kind = match (&field.dtype, field.name.as_str()) { + (FieldDataType::Plain(DataType::Int64), _) => SeriesKind::Bytes, + (_, "p") => SeriesKind::Fraction(declared), + _ => gauge_kind(declared), + }; + series_values(kind, s, self.seed ^ name_hash(&field.name)) + }) + }) + .collect(); + let steps = (ZEROS_END_MS / STEP_MS) as usize; + for step in 0..steps { + let t_ms = (step as i64 + 1) * STEP_MS; + let row = schema + .fields + .iter() + .enumerate() + .map(|(index, field)| { + if Some(index) == schema.time_index { + return Value::Timestamp(t_ms); + } + match (&field.dtype, &per_column[index]) { + (FieldDataType::Plain(DataType::Int64), Some(values)) => { + Value::Int64(values[step].1 as i64) + } + (_, Some(values)) => Value::Float64(values[step].1), + _ => Value::Utf8(sql_text(&field.name, s).into()), + } + }) + .collect(); + rows.push(row); + } + } + Ok(rows) + } +} + +// --------------------------------------------------------------------------- +// The oracle. +// --------------------------------------------------------------------------- + +/// One value that breaks its column's claimed sign. +#[derive(Debug)] +struct Counterexample { + case: String, + query: String, + node: String, + column: String, + evaluation: String, + row: String, + value: String, + derived: ValueSign, +} + +#[derive(Default)] +struct OracleReport { + counterexamples: Vec, + /// `(case, query, node kind, reason)` of every node or query the + /// executor or the lowering cannot run. + skipped: Vec<(String, String, String, String)>, + /// `(node kind, column, observed sign) -> (count, an example query)`: + /// Unknown columns whose evaluated values all had one sign bit. + precision: BTreeMap<(String, String, &'static str), (usize, String)>, + covered_nodes: usize, + checked_values: usize, +} + +impl OracleReport { + fn print(&self, title: &str) { + println!("==== value sign oracle: {title} ===="); + println!( + "covered nodes: {}, checked values: {}, counterexamples: {}, skipped: {}", + self.covered_nodes, + self.checked_values, + self.counterexamples.len(), + self.skipped.len() + ); + // Every counterexample, one line per (case, query, node, column, + // evaluation time, value class), with how many values it stands for + // and the first one: a window of 12 series breaking a sign at once + // is one line, not twelve. + let mut grouped: BTreeMap< + (&str, &str, &str, &str, &str, String, &str), + (usize, &Counterexample), + > = BTreeMap::new(); + for c in &self.counterexamples { + let class = match c.value.as_str() { + "0.0" | "-0.0" => c.value.as_str(), + v if v.starts_with('-') => "negative", + _ => "positive", + }; + let entry = grouped + .entry(( + &c.case, + &c.query, + &c.node, + &c.column, + &c.evaluation, + format!("{:?}", c.derived), + class, + )) + .or_insert((0, c)); + entry.0 += 1; + } + println!( + "---- counterexamples ({} values, {} lines) ----", + self.counterexamples.len(), + grouped.len() + ); + for ((case, query, node, column, evaluation, derived, class), (count, first)) in &grouped { + println!( + "COUNTEREXAMPLE x{count} case={case} query={query:?} node={node} column={column} \ + eval={evaluation} derived={derived} values={class} first={} row=[{}]", + first.value, first.row + ); + } + let mut by_reason: BTreeMap<(String, String), (usize, String)> = BTreeMap::new(); + for (case, query, node, reason) in &self.skipped { + let entry = by_reason + .entry((node.clone(), reason.chars().take(160).collect())) + .or_insert((0, format!("{case}: {query}"))); + entry.0 += 1; + } + println!( + "---- skipped ({} total, {} kinds) ----", + self.skipped.len(), + by_reason.len() + ); + for ((node, reason), (count, example)) in &by_reason { + println!("SKIPPED x{count} node={node} reason={reason} e.g. {example}"); + } + println!( + "---- precision: Unknown columns with values of one sign ({}) ----", + self.precision.len() + ); + for ((node, column, sign), (count, example)) in &self.precision { + println!( + "PRECISION x{count} node={node} column={column} observed={sign} e.g. {example}" + ); + } + } +} + +/// The operator kind of `payload`: `Aggregate`, `TimeShift`, ... +fn operator_kind(payload: &PhysicalASAPOperatorPayload) -> String { + let text = format!("{payload:?}"); + let inner = text.split_once('(').map(|(_, rest)| rest).unwrap_or(&text); + inner + .split(|c: char| !c.is_alphanumeric() && c != '_') + .next() + .unwrap_or(inner) + .to_string() +} + +/// A short readable rendering of `payload` for a report line. +fn operator_text(payload: &PhysicalASAPOperatorPayload) -> String { + let text = format!("{payload:?}"); + let text: String = text.split_whitespace().collect::>().join(" "); + text.chars().take(240).collect() +} + +/// A row's text cells, for locating a counterexample's series. +fn row_text(schema: &Schema, row: &[Value]) -> String { + schema + .fields + .iter() + .zip(row) + .filter_map(|(field, cell)| match cell { + Value::Utf8(text) if field.name == PROMQL_SERIES_IDENTITY => Some( + promql_rows::decode_series_identity(text) + .map(|labels| format!("{labels:?}")) + .unwrap_or_else(|_| text.to_string()), + ), + Value::Utf8(text) if !text.is_empty() => Some(format!("{}={text}", field.name)), + Value::Timestamp(t) => Some(format!("{}@{t}", field.name)), + _ => None, + }) + .collect::>() + .join(",") +} + +/// A numeric cell as the value whose sign bit it carries: an Int64 `v` +/// counts as `v as f64`, so int `0` is `+0.0`. NULL, NaN and non-numeric +/// cells carry no claim (`None`). // provisional answer Q5, Q6 +fn numeric(cell: &Value) -> Option { + match cell { + Value::Float64(v) if v.is_nan() => None, + Value::Float64(v) => Some(*v), + Value::Int64(v) => Some(*v as f64), + _ => None, + } +} + +fn run_node( + dag: &PhysicalASAPDAG, + store: &Arc, + id: usize, + evaluation_time_ms: i64, +) -> Result, String> { + let roots = [id as u64]; + let compiled = compile(dag, &[], &roots).map_err(|e| format!("compile: {e}"))?; + let bound = compiled + .instantiate(store.clone()) + .map_err(|e| format!("instantiate: {e}"))?; + let context = RunContext::new(Scope::Query { evaluation_time_ms }, Limits::default()) + .map_err(|e| format!("context: {e}"))?; + block_on(async { + let mut output = bound + .execute(&roots, context) + .map_err(|e| format!("execute: {e}"))? + .remove(0); + let mut batches = Vec::new(); + while let Some(batch) = output.next().await { + batches.push((*batch.map_err(|e| format!("stream: {e}"))?).clone()); + } + Ok(batches) + }) +} + +/// The signs the memo derives for `node` planned alone: the root group's +/// schema field names and `column_signs`. +fn derived_signs( + node: &Rc, + facts: &DataWorkload, +) -> Result<(Vec, Vec), String> { + let memo = + asap_stage0_memo::memo_of_queries(&[QueryRoot::Operator(node.clone())], &[None], facts) + .map_err(|e| format!("memo: {e}"))?; + let QueryRoot::Operator(group) = &memo.queries()[0].root else { + return Err("memo: scalar root".into()); + }; + let properties = &memo.groups()[group.0 as usize].group_properties; + Ok(( + properties + .schema + .fields + .iter() + .map(|f| f.name.clone()) + .collect(), + properties.column_signs.clone(), + )) +} + +/// Check every subtree node of `root` (query `query` of case `case`) +/// against the values the executor computes at every evaluation time. +fn check_root( + case: &str, + query: &str, + root: &QueryRoot, + data: &CaseData, + report: &mut OracleReport, +) { + let skip = |report: &mut OracleReport, node: &str, reason: String| { + report.skipped.push(( + case.to_string(), + query.to_string(), + node.to_string(), + reason, + )) + }; + let (dag, nodes) = + match physical_asap_dag_at_query_time_with_node_ids(std::slice::from_ref(root)) { + Ok(exported) => exported, + Err(e) => return skip(report, "", format!("export: {e}")), + }; + let store = match data.store_for(&dag) { + Ok(store) => Arc::new(store), + Err(e) => return skip(report, "", format!("data: {e}")), + }; + for node in &dag.nodes { + let kind = operator_kind(&node.payload); + let (names, signs) = match derived_signs(&nodes[node.id], &data.facts) { + Ok(derived) => derived, + Err(e) => { + skip(report, &kind, e); + continue; + } + }; + let output_names: Vec = node + .output_schema + .fields + .iter() + .map(|f| f.name.clone()) + .collect(); + if names != output_names || signs.len() != names.len() { + skip( + report, + &kind, + format!( + "schema: group {names:?} with {} signs, node {output_names:?}", + signs.len() + ), + ); + continue; + } + // Per column: the sign bits seen, for the precision list. + let mut seen_negative = vec![false; signs.len()]; + let mut seen_positive = vec![false; signs.len()]; + let mut ran_everywhere = true; + // A raw `Scan` returns the same rows at every time: run it once. + let scan = matches!( + node.payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) + ); + let times = if scan { + &EVALUATION_TIMES[..1] + } else { + &EVALUATION_TIMES[..] + }; + for &(evaluation, time_ms) in times { + let batches = match run_node(&dag, &store, node.id, time_ms) { + Ok(batches) => batches, + Err(e) => { + skip(report, &kind, e); + ran_everywhere = false; + break; + } + }; + for batch in &batches { + for row in batch.to_rows() { + for (column, cell) in row.iter().enumerate() { + let Some(value) = numeric(cell) else { continue }; + report.checked_values += 1; + let negative = value.is_sign_negative(); + seen_negative[column] |= negative; + seen_positive[column] |= !negative; + let holds = match signs[column] { + ValueSign::NonNegative => !negative, + ValueSign::NonPositive => negative, + ValueSign::Unknown => true, + }; + if !holds { + report.counterexamples.push(Counterexample { + case: case.to_string(), + query: query.to_string(), + node: format!("#{} {}", node.id, operator_text(&node.payload)), + column: names[column].clone(), + evaluation: format!("{evaluation}@{time_ms}ms"), + row: row_text(batch.schema(), &row), + value: format!("{value:?}"), + derived: signs[column], + }); + } + } + } + } + } + if !ran_everywhere { + continue; + } + report.covered_nodes += 1; + for (column, sign) in signs.iter().enumerate() { + if *sign != ValueSign::Unknown || seen_negative[column] == seen_positive[column] { + continue; + } + let observed = if seen_negative[column] { + "all sign bit set" + } else { + "all sign bit clear" + }; + let entry = report + .precision + .entry((kind.clone(), names[column].clone(), observed)) + .or_insert((0, format!("{case}: {query}"))); + entry.0 += 1; + } + } +} + +/// One query of an oracle case, as a single-query workload with no +/// accuracy requirement: the exact plan. +fn single_query_workload(language: &Json, query: &str, facts: &Json) -> PlanningWorkload { + serde_json::from_value(json!({ + "query_workload": { + "language": language, + "repeating_queries": [{ + "query": query, + "demand": {"fixed_interval_at": {"interval": 60000, "evaluation_phase": 0}}, + "requirements": {}, + "predictability": {"predictable": {"known_at": null}}, + }], + }, + "data_workload": facts, + })) + .expect("a single-query workload deserializes") +} + +/// An oracle case: queries in one language over one set of facts. +struct OracleCase { + name: String, + language: Language, + /// SQL only: the catalog the queries are lowered over. + catalog: Option, + /// `(workload name, query text)`. + queries: Vec<(String, String)>, + facts: Json, + labels: Labels, + seed: u64, +} + +fn run_case(case: &OracleCase, report: &mut OracleReport) { + let data = CaseData::new(case.facts.clone(), case.labels, case.seed); + let language = match case.language { + Language::Promql => json!("promql"), + Language::Sql => json!({"sql": "datafusion_sql"}), + }; + for (workload_name, query) in &case.queries { + let case_name = format!("{}/{workload_name}", case.name); + let workload = single_query_workload(&language, query, &case.facts); + // The production lowering: PromQL with each series' identity as a + // column, as planning lowers it. + let lowered = match (&case.language, &case.catalog) { + (Language::Promql, _) => lower_promql_roots(&workload), + (Language::Sql, Some(catalog)) => lower_sql_roots(&workload, catalog), + (Language::Sql, None) => Err("no SQL catalog".into()), + }; + match lowered { + Ok(roots) => { + for root in &roots { + check_root(&case_name, query, root, &data, report); + } + } + Err(e) => report.skipped.push(( + case_name, + query.clone(), + "".into(), + format!("lowering: {e}"), + )), + } + } +} + +// --------------------------------------------------------------------------- +// The fixed adversarial list: the sign review workloads, inlined with their +// facts (series counts and sample intervals normalized to the generated +// data: 12 series, one sample per second). +// --------------------------------------------------------------------------- + +#[derive(Debug, Clone, Copy, PartialEq)] +enum Facts { + /// `data` N, `neg` P, `mixed` U, `data_total` counter, `negc` counter + /// declared P. + Review, + /// `data` declared P. + DataNonPositive, + /// `data` undeclared. + DataUndeclared, + /// `flows`: `bytes` Int64 declared N, `p` undeclared. + FlowsBytesNonNegative, + /// `flows`: `bytes` N and `p` N. + FlowsBytesAndPNonNegative, + /// `flows`: `p` N, `bytes` undeclared. + FlowsPNonNegative, + /// `flows`: `bytes` N, `p` declared P (values in `(-1, -0.0]`). + FlowsPNonPositive, + /// This plan's sources: `pos` N, `neg` P, `mixed` U, `c_total` counter, + /// `foo` histogram family, all undeclared but `pos`/`neg`. + Findings, +} + +fn gauge(sign: Option<&str>) -> Json { + let mut facts = json!({"series_count": 12, "metric_type": "gauge", "sample_interval": 1000}); + if let Some(sign) = sign { + facts["column_signs"] = json!({"value": sign}); + } + facts +} + +fn counter(sign: Option<&str>) -> Json { + let mut facts = gauge(sign); + facts["metric_type"] = json!("counter"); + facts +} + +fn flows(bytes: Option<&str>, p: Option<&str>) -> Json { + let mut signs = serde_json::Map::new(); + if let Some(sign) = bytes { + signs.insert("bytes".into(), json!(sign)); + } + if let Some(sign) = p { + signs.insert("p".into(), json!(sign)); + } + json!({"flows": { + "rows_held": 36000, + "distinct_values_per_label": {"src": 6, "dst": 300}, + "column_signs": signs, + }}) +} + +impl Facts { + fn json(self) -> Json { + let n = Some("non_negative"); + let p = Some("non_positive"); + let sources = match self { + Facts::Review => json!({ + "data": gauge(n), "neg": gauge(p), "mixed": gauge(None), + "data_total": counter(None), "negc": counter(p), + }), + Facts::DataNonPositive => json!({"data": gauge(p)}), + Facts::DataUndeclared => json!({"data": gauge(None)}), + Facts::FlowsBytesNonNegative => flows(n, None), + Facts::FlowsBytesAndPNonNegative => flows(n, n), + Facts::FlowsPNonNegative => flows(None, n), + Facts::FlowsPNonPositive => flows(n, p), + Facts::Findings => json!({ + "pos": gauge(n), "neg": gauge(p), "mixed": gauge(None), + "c_total": counter(None), + "foo": {"series_count": 12, "metric_type": "histogram", "sample_interval": 1000}, + }), + }; + let rows_per_second = if self.is_sql() { 12.0 } else { 60.0 }; + json!({"arrival": {"ingesting": {"rows_per_second": rows_per_second}}, "sources": sources}) + } + + fn is_sql(self) -> bool { + matches!( + self, + Facts::FlowsBytesNonNegative + | Facts::FlowsBytesAndPNonNegative + | Facts::FlowsPNonNegative + | Facts::FlowsPNonPositive + ) + } +} + +/// The review workloads' SQL table: `bytes` is an Int64 column, so integer +/// zeros (`-0` is `0`) are exercised. +fn flows_schema() -> Schema { + Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("src", DataType::Utf8, false), + Field::plain("dst", DataType::Utf8, false), + Field::plain("bytes", DataType::Int64, false), + Field::plain("p", DataType::Float64, false), + ], + 0, + vec![], + ) +} + +/// The sign review workloads: r1302 `s01`–`s40`, `x01`–`x07`; r1309 +/// `e01`–`e11` and `a`, `b`, `c`, `d`, `i`, `k`. +const REVIEW_QUERIES: &[(&str, Facts, &str)] = &[ + ("r1302/s01_q_gt1", Facts::Review, "avg by (job) (quantile_over_time(1.5, data[5m]))"), + ("r1302/s02_q_neg", Facts::Review, "avg by (job) (quantile_over_time(-0.5, data[5m]))"), + ("r1302/s03_q_nan", Facts::Review, "avg by (job) (quantile_over_time(NaN, data[5m]))"), + ("r1302/s04_rate_counter", Facts::Review, "sum by (job) (rate(data_total[5m]))"), + ("r1302/s05_increase_counter", Facts::Review, "sum by (job) (increase(data_total[5m]))"), + ("r1302/s06_rate_neg_gauge", Facts::Review, "sum by (job) (rate(neg[5m]))"), + ("r1302/s07_irate_gauge", Facts::Review, "avg by (job) (irate(data[5m]))"), + ("r1302/s08_deriv", Facts::Review, "avg by (job) (deriv(data[5m]))"), + ("r1302/s09_delta", Facts::Review, "avg by (job) (delta(data[5m]))"), + ("r1302/s10_sub", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) - quantile_over_time(0.5, data[5m]))"), + ("r1302/s11_neg", Facts::Review, "avg by (job) (-quantile_over_time(0.9, data[5m]))"), + ("r1302/s12_negneg_mixed", Facts::Review, "avg by (job) (-(-quantile_over_time(0.9, mixed[5m])))"), + ("r1302/s13_abs_mixed", Facts::Review, "avg by (job) (abs(quantile_over_time(0.9, mixed[5m])))"), + ("r1302/s14_clamp", Facts::Review, "avg by (job) (clamp(quantile_over_time(0.9, data[5m]), -5, 5))"), + ("r1302/s15_clamp_min0_mixed", Facts::Review, "avg by (job) (clamp_min(quantile_over_time(0.9, mixed[5m]), 0))"), + ("r1302/s16_times_neg_const", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) * -2)"), + ("r1302/s17_neg_const_times", Facts::Review, "avg by (job) (-2 * quantile_over_time(0.9, data[5m]))"), + ("r1302/s18_const_minus", Facts::Review, "avg by (job) (100 - quantile_over_time(0.9, data[5m]))"), + ("r1302/s19_scalar_cmp_left_mixed", Facts::Review, "avg by (job) (100 > quantile_over_time(0.9, mixed[5m]))"), + ("r1302/s20_cmp_bool", Facts::Review, "avg by (job) (quantile_over_time(0.9, mixed[5m]) > bool 1)"), + ("r1302/s21_nonpos_source", Facts::Review, "avg by (job) (quantile_over_time(0.9, neg[5m]))"), + ("r1302/s22_or_union", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) or quantile_over_time(0.9, neg[5m]))"), + ("r1302/s23_add_mixed_signs", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) + on(instance, job, host, region) quantile_over_time(0.9, neg[5m]))"), + ("r1302/s24_div_signs", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) / on(instance, job, host, region) quantile_over_time(0.9, neg[5m]))"), + ("r1302/s24m_div_noacc", Facts::Review, "sum by (job) (sum_over_time(data[5m]) / on(instance, job, host, region) sum_over_time(neg[5m]))"), + ("r1302/s24n_div_noacc", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) / on(instance, job, host, region) quantile_over_time(0.9, neg[5m]))"), + ("r1302/s25_unless", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) unless quantile_over_time(0.9, neg[5m]))"), + ("r1302/s26_relabel", Facts::Review, "avg by (job) (label_replace(quantile_over_time(0.9, data[5m]), \"x\", \"$1\", \"instance\", \"(.*)\"))"), + ("r1302/s27_subquery_deriv", Facts::Review, "avg by (job) (quantile_over_time(0.9, deriv(data[1m])[5m:30s]))"), + ("r1302/s28_pow", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) ^ 3)"), + ("r1302/s29_scalar_mul", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) * scalar(min(neg)))"), + ("r1302/s30_mod", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) % 7)"), + ("r1302/s31_rate_times_q", Facts::Review, "sum by (job) (rate(data_total[5m]) * on(instance, job, host, region) quantile_over_time(0.9, data[5m]))"), + ("r1302/s32_stddev", Facts::Review, "sum by (job) (stddev_over_time(data[5m]))"), + ("r1302/s33_sgn_mixed", Facts::Review, "avg by (job) (sgn(quantile_over_time(0.9, mixed[5m])))"), + ("r1302/s34_offset", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m] offset 1m))"), + ("r1302/s35_group_right", Facts::Review, "quantile_over_time(0.9, neg[5m]) * on(job) group_right sum by (job) (data)"), + ("r1302/s36_sum_of_group_right", Facts::Review, "sum by (job) (quantile_over_time(0.9, neg[5m]) * on(instance) group_right quantile_over_time(0.9, data[5m]))"), + ("r1302/s37_sum_without", Facts::Review, "sum without (instance, host) (quantile_over_time(0.9, data[5m]))"), + ("r1302/s38_vector_neg", Facts::Review, "sum(vector(-1))"), + ("r1302/s39_minover_mixed_abs", Facts::Review, "avg by (job) (abs(min_over_time(mixed[5m])))"), + ("r1302/s40_histq", Facts::Review, "sum(histogram_quantile(0.9, rate(data_total[5m])))"), + ("r1302/x01_sql_case", Facts::FlowsBytesAndPNonNegative, "SELECT src, SUM(CASE WHEN p > 0.5 THEN bytes ELSE -bytes END) AS s FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1302/x02_sql_avg", Facts::FlowsBytesNonNegative, "SELECT src, AVG(bytes) AS a FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1302/x03_sql_neg", Facts::FlowsBytesNonNegative, "SELECT src, SUM(bytes * -1) AS s FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1302/x04_sql_abs", Facts::FlowsBytesNonNegative, "SELECT src, SUM(ABS(bytes - 5000)) AS s FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1302/x05_sql_sub", Facts::FlowsBytesAndPNonNegative, "SELECT src, AVG(bytes - p) AS a FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1302/x06_sql_cast", Facts::FlowsBytesNonNegative, "SELECT src, SUM(CAST(bytes - 1 AS BIGINT)) AS s FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1302/x07_sql_negp", Facts::FlowsPNonNegative, "SELECT src, SUM(-p) AS s FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + ("r1309/e01_quantile_grouped", Facts::Review, "avg by (region) (quantile_over_time(0.9, data[5m]))"), + ("r1309/e02_rm_long_w01_q3", Facts::Review, "avg by (region) (quantile_over_time(0.95, data[300s]))"), + ("r1309/e03_rm_w01_q3_q8#0", Facts::Review, "avg by (region) (quantile_over_time(0.95, data[100s]))"), + ("r1309/e03_rm_w01_q3_q8#1", Facts::Review, "topk by (region) (4, rate(data_total[20m]))"), + ("r1309/e03a_avgq_alone", Facts::Review, "avg by (region) (quantile_over_time(0.95, data[100s]))"), + ("r1309/e03b_topk_rate_alone", Facts::Review, "topk by (region) (4, rate(data_total[20m]))"), + ("r1309/e04_rm_w02_q2_q7#0", Facts::Review, "avg by (region) (quantile_over_time(0.75, data[100s]))"), + ("r1309/e04_rm_w02_q2_q7#1", Facts::Review, "topk by (region) (4, increase(data_total[5m]))"), + ("r1309/e04a_alone", Facts::Review, "avg by (region) (quantile_over_time(0.75, data[100s]))"), + ("r1309/e04b_alone", Facts::Review, "topk by (region) (4, increase(data_total[5m]))"), + ("r1309/e05_example_avg", Facts::Review, "avg by (job) (quantile_over_time(0.99, data[300s]))"), + ("r1309/e06_example_sum", Facts::Review, "sum by (job) (quantile_over_time(0.99, data[300s]))"), + ("r1309/e07_example_unknown", Facts::DataUndeclared, "avg by (job) (quantile_over_time(0.99, data[300s]))"), + ("r1309/e08_example_nonpos", Facts::DataNonPositive, "avg by (job) (quantile_over_time(0.99, data[300s]))"), + ("r1309/e09_example_sum_nonpos", Facts::DataNonPositive, "sum by (job) (quantile_over_time(0.5, data[300s]))"), + ("r1309/e10_example_avg_all", Facts::Review, "avg(quantile_over_time(0.5, data[300s]))"), + ("r1309/e11_example_sum_neg", Facts::Review, "sum by (job) (-quantile_over_time(0.9, data[300s]))"), + ("r1309/a01_and_subset", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) and quantile_over_time(0.9, neg{job!=\"j0\"}[5m]))"), + ("r1309/a02_unless_subset", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) unless quantile_over_time(0.9, neg{job=\"j0\"}[5m]))"), + ("r1309/a03_or_subset_left", Facts::Review, "avg by (job) (quantile_over_time(0.9, data{job=\"j0\"}[5m]) or quantile_over_time(0.5, data[5m]))"), + ("r1309/a03b_or_same_series", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) or quantile_over_time(0.5, data[5m]))"), + ("r1309/a03c_or_disjoint_metrics", Facts::Review, "max by (job) (quantile_over_time(0.9, data[5m]) or quantile_over_time(0.9, neg[5m]))"), + ("r1309/a04_and_on", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) and on(job) quantile_over_time(0.9, neg{instance=\"i0\"}[5m]))"), + ("r1309/a05_unless_ignoring", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) unless ignoring(instance, host, region) quantile_over_time(0.9, neg{job=\"j1\"}[5m]))"), + ("r1309/a06_and_on_agg_right", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) and on(job) (avg by (job) (quantile_over_time(0.9, neg[5m]))))"), + ("r1309/a07_unless_on_value", Facts::Review, "max by (job) (quantile_over_time(0.9, data[5m]) unless on(value) quantile_over_time(0.9, neg[5m]))"), + ("r1309/a07b_and_on_value", Facts::Review, "max by (job) (quantile_over_time(0.9, data[5m]) and on(value) quantile_over_time(0.9, data[5m]))"), + ("r1309/a07c_unless_on_avg", Facts::Review, "max by (job) (quantile_over_time(0.9, data[5m]) unless on(avg) avg by (job) (quantile_over_time(0.9, neg[5m])))"), + ("r1309/a08_relabel_src_value", Facts::Review, "avg by (job) (label_replace(quantile_over_time(0.9, data[5m]), \"x\", \"$1\", \"value\", \"(.*)\"))"), + ("r1309/a09_relabel_dst_value", Facts::Review, "avg by (job) (label_replace(quantile_over_time(0.9, data[5m]), \"value\", \"z\", \"job\", \"(.*)\"))"), + ("r1309/a10_relabel_group_by_dst", Facts::Review, "avg by (x) (label_replace(quantile_over_time(0.9, data[5m]), \"x\", \"$1\", \"job\", \"(.*)\"))"), + ("r1309/a11_label_join", Facts::Review, "avg by (job) (label_join(quantile_over_time(0.9, data[5m]), \"x\", \"-\", \"job\", \"instance\"))"), + ("r1309/a12_relabel_src_avg", Facts::Review, "max(label_replace(avg by (job) (quantile_over_time(0.9, data[5m])), \"x\", \"$1\", \"avg\", \"(.*)\"))"), + ("r1309/a12b_relabel_bare", Facts::Review, "label_replace(quantile_over_time(0.9, data[5m]), \"x\", \"$1\", \"job\", \"(.*)\")"), + ("r1309/a13_abs_cross_zero_avg", Facts::Review, "avg by (job) (abs(quantile_over_time(0.3, mixed[5m])))"), + ("r1309/a14_abs_cross_zero", Facts::Review, "abs(quantile_over_time(0.3, mixed[5m]))"), + ("r1309/a15_neg_cross_zero", Facts::Review, "-quantile_over_time(0.3, mixed[5m])"), + ("r1309/a16_sum_neg_unknown", Facts::Review, "sum by (job) (-quantile_over_time(0.3, mixed[5m]))"), + ("r1309/a17_unless_then_sum", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) unless on(job) quantile_over_time(0.9, neg{job=\"j2\"}[5m]))"), + ("r1309/a18_or_then_sum_mixed_sign", Facts::Review, "sum by (job) (quantile_over_time(0.9, data{job=\"j0\"}[5m]) or quantile_over_time(0.9, neg[5m]))"), + ("r1309/a19_and_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) and quantile_over_time(0.9, neg{job!=\"j1\"}[5m])"), + ("r1309/a20_or_bare_same", Facts::Review, "quantile_over_time(0.9, data[5m]) or quantile_over_time(0.1, data[5m])"), + ("r1309/b01_and_subset_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) and quantile_over_time(0.9, neg{job!=\"j0\"}[5m])"), + ("r1309/b02_unless_subset_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) unless quantile_over_time(0.9, neg{job=\"j0\"}[5m])"), + ("r1309/b02g_unless_full_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) unless quantile_over_time(0.9, neg[5m])"), + ("r1309/b02h_and_full_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) and quantile_over_time(0.9, neg[5m])"), + ("r1309/b05_unless_ignoring_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) unless ignoring(instance, host, region) quantile_over_time(0.9, neg{job=\"j1\"}[5m])"), + ("r1309/b06_and_on_agg_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) and on(job) avg by (job) (quantile_over_time(0.9, neg[5m]))"), + ("r1309/b07c_unless_on_avg_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) unless on(avg) avg by (job) (quantile_over_time(0.9, neg[5m]))"), + ("r1309/b17_unless_on_job_bare", Facts::Review, "quantile_over_time(0.9, data[5m]) unless on(job) quantile_over_time(0.9, neg{job=\"j2\"}[5m])"), + ("r1309/b20_or_gappy_both", Facts::Review, "quantile_over_time(0.9, data[5m]) or quantile_over_time(0.9, mixed[5m])"), + ("r1309/b21_or_same_metric_q", Facts::Review, "quantile_over_time(0.5, neg[5m]) or quantile_over_time(0.9, neg[5m])"), + ("r1309/b22_avg_and_on_instance", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) and on(instance) quantile_over_time(0.9, neg[5m]))"), + ("r1309/b23_avg_unless_on_instance", Facts::Review, "avg by (job) (quantile_over_time(0.9, data[5m]) unless on(instance) quantile_over_time(0.9, neg[5m]))"), + ("r1309/b24_sum_or_on_instance", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) or on(instance) quantile_over_time(0.5, data[5m]))"), + ("r1309/c01_q03_mixed_bare", Facts::Review, "quantile_over_time(0.3, mixed[5m])"), + ("r1309/c02_q03_data_bare", Facts::Review, "quantile_over_time(0.3, data[5m])"), + ("r1309/d01_and_exact_right", Facts::Review, "quantile_over_time(0.3, mixed[5m]) and max_over_time(neg[5m])"), + ("r1309/d02_and_sketch_right", Facts::Review, "quantile_over_time(0.3, mixed[5m]) and quantile_over_time(0.9, neg[5m])"), + ("r1309/i01_sum_over_time_match", Facts::Review, "sum by (job) (sum_over_time(data{job!=\"j2\"}[5m]))"), + ("r1309/i02_sum_over_time", Facts::Review, "sum_over_time(data[5m])"), + ("r1309/i03_count_over_time_match", Facts::Review, "count_over_time(data{job=~\"j0|j1\"}[5m])"), + ("r1309/i04_max_over_time_match", Facts::Review, "max_over_time(data{job=\"j1\"}[5m])"), + ("r1309/i05_sum_instant", Facts::Review, "sum by (job) (data)"), + ("r1309/i06_max_by", Facts::Review, "max by (job) (data)"), + ("r1309/i07_sum_of_max_over_time", Facts::Review, "sum by (job) (max_over_time(data[5m]))"), + ("r1309/i08_count_by", Facts::Review, "count by (job) (data{job!=\"j0\"})"), + ("r1309/i09_div_sum", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) / on(instance, job, host, region) quantile_over_time(0.9, neg[5m]))"), + ("r1309/i09b_div_sum_acc", Facts::Review, "sum by (job) (quantile_over_time(0.9, data[5m]) / on(instance, job, host, region) quantile_over_time(0.9, neg[5m]))"), + ("r1309/i10_avg_over_time", Facts::Review, "avg_over_time(data{job=\"j0\"}[5m])"), + ("r1309/i11_min_over_time", Facts::Review, "min_over_time(data[5m])"), + ("r1309/k01_rate_negc", Facts::Review, "rate(negc[5m])"), + ("r1309/k02_sum_rate_negc", Facts::Review, "sum by (job) (rate(negc[5m]))"), + ("r1309/k03_increase_negc", Facts::Review, "increase(negc[5m])"), + ("r1309/k04_avg_rate_negc_acc", Facts::Review, "avg by (job) (rate(negc[5m]))"), +]; + +/// The new queries for this plan's findings, over [`Facts::Findings`] (or +/// the `flows` table for SQL). +const FINDING_QUERIES: &[(&str, Facts, &str)] = &[ + // F1: a sum or average of -0.0 values. // provisional answer Q1 + ("F1", Facts::Findings, "sum_over_time(neg[5m])"), + ("F1", Facts::Findings, "avg_over_time(neg[5m])"), + ("F1", Facts::Findings, "sum by (job) (neg)"), + // F2: sgn(-0.0). // provisional answer Q2 + ("F2", Facts::Findings, "sgn(neg)"), + // F3: clamps at signed zeros. // provisional answer Q3 + ("F3", Facts::Findings, "clamp_min(neg, 0)"), + ("F3", Facts::Findings, "clamp_max(pos, -0)"), + ("F3", Facts::Findings, "clamp(mixed, 0, -0)"), + // F4: year of a time before year 1 (`-1e11` s). // provisional answer Q4 + ("F4", Facts::Findings, "year(neg)"), + // F5: integer zeros carry no sign bit. // provisional answer Q5 + ("F5", Facts::FlowsBytesNonNegative, "SELECT -bytes AS v FROM flows WHERE ts >= now() - INTERVAL '5 minutes'"), + ("F5", Facts::FlowsBytesNonNegative, "SELECT bytes * -1 AS v FROM flows WHERE ts >= now() - INTERVAL '5 minutes'"), + ("F5", Facts::FlowsBytesNonNegative, "SELECT bytes / -2 AS v FROM flows WHERE ts >= now() - INTERVAL '5 minutes'"), + ("F5", Facts::FlowsBytesNonNegative, "SELECT src, -COUNT(p) AS c FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + // B14: an undeclared histogram `_sum` of negative observations + // decreases, so its rate is negative. // provisional answer Q8 + ("B14", Facts::Findings, "rate(foo_sum[5m])"), + // B15: a cast to an integer of values in (-1, 0) gives int 0. + // // provisional answer Q5 + ("B15", Facts::FlowsPNonPositive, "SELECT CAST(p AS BIGINT) AS c FROM flows WHERE ts >= now() - INTERVAL '5 minutes'"), + ("B15", Facts::FlowsPNonPositive, "SELECT src, SUM(CAST(p AS BIGINT)) AS s FROM flows WHERE ts >= now() - INTERVAL '5 minutes' GROUP BY src"), + // A vector/vector `bool` comparison returns 0 or 1 (`+0.0` for false), + // whatever its left side's sign. + ("bool", Facts::Findings, "neg > bool neg"), + ("bool", Facts::Findings, "neg > bool (neg * 2)"), + // I6: evaluation contexts copy their input's sign. + ("I6", Facts::Findings, "neg offset 1m"), + ("I6", Facts::Findings, "neg @ 100"), + ("I6", Facts::Findings, "neg[5m:30s]"), + ("I6", Facts::Findings, "sum_over_time(neg[5m:30s])"), +]; + +fn fixed_cases() -> Vec { + let mut by_facts: BTreeMap = BTreeMap::new(); + for (name, facts, query) in REVIEW_QUERIES.iter().chain(FINDING_QUERIES) { + let key = format!("{facts:?}"); + let case = by_facts.entry(key.clone()).or_insert_with(|| OracleCase { + name: format!("fixed/{key}"), + language: if facts.is_sql() { + Language::Sql + } else { + Language::Promql + }, + catalog: facts + .is_sql() + .then(|| SqlCatalog::new().with_table("flows", flows_schema())), + queries: Vec::new(), + facts: facts.json(), + labels: Labels::Review, + seed: 1, + }); + case.queries.push((name.to_string(), query.to_string())); + } + by_facts.into_values().collect() +} + +/// The three source-sign variants of a fuzz workload: as generated (every +/// source declared `non_negative`), every source declared +/// `non_positive`, and every source undeclared. +fn fuzz_cases(seeds: std::ops::Range) -> Vec { + let mut cases = Vec::new(); + for seed in seeds { + let workload = random_workload(&mut Rng::new(seed)); + let facts = serde_json::to_value(&workload.data).expect("facts serialize"); + let tables: Vec = workload.tables.iter().map(Json::to_string).collect(); + for variant in ["as_generated", "non_positive", "undeclared"] { + let mut facts = facts.clone(); + for source in facts["sources"] + .as_object_mut() + .into_iter() + .flat_map(|s| s.values_mut()) + { + let source = source.as_object_mut().expect("source facts are an object"); + match variant { + "non_positive" => { + source.insert("column_signs".into(), json!({"value": "non_positive"})); + } + "undeclared" => { + source.remove("column_signs"); + } + _ => {} + } + } + let catalog = match workload.language { + Language::Promql => None, + Language::Sql => Some(sql_catalog(&tables).expect("generated tables parse").0), + }; + cases.push(OracleCase { + name: format!("fuzz/seed{seed}/{variant}"), + language: workload.language, + catalog, + queries: workload + .queries + .iter() + .enumerate() + .map(|(i, q)| (format!("q{i}"), q.text().to_string())) + .collect(), + facts, + labels: Labels::Fuzz, + seed, + }); + } + } + cases +} + +fn run_oracle(cases: &[OracleCase], title: &str) -> OracleReport { + let mut report = OracleReport::default(); + let started = std::time::Instant::now(); + for case in cases { + run_case(case, &mut report); + eprintln!( + "oracle: {} done at {:.1} s, {} covered nodes", + case.name, + started.elapsed().as_secs_f64(), + report.covered_nodes + ); + } + report.print(title); + report +} + +/// T3.1. Every known sign holds on every value the executor computes for +/// the node's group, over the fixed adversarial list, this plan's new +/// queries, and 50 fuzz seeds in three source-sign variants. The expected +/// signs come only from the evaluated values. // provisional answer Q12 +#[test] +fn every_known_sign_holds_on_evaluated_values() { + let mut cases = fixed_cases(); + cases.extend(fuzz_cases(0..50)); + let report = run_oracle(&cases, "fixed list + 50 fuzz seeds x 3 sign variants"); + assert!( + report.covered_nodes >= MIN_COVERED_NODES_CI, + "covered nodes dropped: {} < {MIN_COVERED_NODES_CI}", + report.covered_nodes + ); + assert!( + report.counterexamples.is_empty(), + "{} values break their column's derived sign (listed above)", + report.counterexamples.len() + ); +} + +/// T3.1 over 200 fuzz seeds, run nightly. // provisional answer Q12 +#[test] +#[ignore = "nightly"] +fn every_known_sign_holds_on_evaluated_values_over_200_fuzz_seeds() { + let report = run_oracle(&fuzz_cases(0..200), "200 fuzz seeds x 3 sign variants"); + assert!( + report.counterexamples.is_empty(), + "{} values break their column's derived sign (listed above)", + report.counterexamples.len() + ); +} + +// --------------------------------------------------------------------------- +// T3.2: the ingestion-time placement of one exact group. +// --------------------------------------------------------------------------- + +const MINUTE_MS: i64 = 60_000; +/// The chain's pane width and the query's window. +const PANE_MS: i64 = MINUTE_MS; +const WINDOW_MS: i64 = 5 * MINUTE_MS; + +fn chain_samples_schema() -> Schema { + Schema::with_time_index( + vec![ + Field::plain("job", DataType::Utf8, false), + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + ], + 1, + vec![], + ) +} + +fn shared(operator: Operator) -> Rc { + OperatorNode::new_shared(operator).expect("a valid operator") +} + +/// `Finalize(SummaryMerge(TimeRange(5m, SummaryTumblingWindowChain(kind +/// per job of metric's value, 1-minute panes))))`: the exact `kind` of each +/// job's values in the last 5 minutes, as an ingestion-time chain computes +/// it. Returns the finalize node and the chain node. +fn finalized_chain(metric: &str, kind: ExactKind) -> (Rc, Rc) { + let params = match kind { + ExactKind::Sum => ExactParams::Sum, + ExactKind::Count => ExactParams::Count, + ExactKind::Min => ExactParams::Min, + ExactKind::Max => ExactParams::Max, + ExactKind::Increase => ExactParams::Increase, + ExactKind::Rate => ExactParams::Rate, + ExactKind::IRate => ExactParams::IRate, + }; + let scan = shared(Operator::NonASAP(NonASAPOp::Scan { + source: Source::TimeSeries { + metric: metric.into(), + }, + predicates: vec![], + schema: chain_samples_schema(), + })); + let chain = shared(Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: scan, + summary_type: FieldDataType::ExactAggregate(kind, params), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::by(vec![0]), + filter: None, + tumbling_window_width_ms: PANE_MS as u64, + })); + let range = shared(Operator::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_millis(WINDOW_MS as u64), + kind: TimeRangeKind::Range, + child: chain.clone(), + })); + let merge = shared(Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![range], + group_by: None, + })); + let finalize = shared(Operator::ASAP(ASAPOp::FinalizeExactAccumulator { + child: merge, + })); + (finalize, chain) +} + +/// The samples of `metric` (`pos`: non-negative, `neg`: non-positive), one +/// every 10 s per job over `(0, 30 min]`: ordinary values up to 20 min, +/// then only zeros of the declared sign (`-0.0` for `neg`), with no sample +/// at all in `(27 min, 28 min]`, an empty pane inside the window ending at +/// 30 min. +fn chain_samples(metric: &str) -> Vec<(String, i64, f64)> { + let mut samples = Vec::new(); + for job in 0..3usize { + let mut t = 10_000; + let mut k = 0u64; + while t <= 30 * MINUTE_MS { + let empty_pane = t > 27 * MINUTE_MS && t <= 28 * MINUTE_MS; + if !empty_pane { + let r = unit_draw(name_hash(metric) ^ (job as u64) << 32 ^ k); + // The finite special values only: a NaN or inf state would + // hide the zeros' sign. + let magnitude = if t > 20 * MINUTE_MS { + 0.0 + } else { + match job { + 0 => [0.0, 5e-324, 1e-300, 0.3, 1.0, 1e300][(k % 6) as usize], + 1 => 0.0, + _ => r * 100.0, + } + }; + let value = if metric == "neg" { + -magnitude + } else { + magnitude + }; + samples.push((format!("j{job}"), t, value)); + } + t += 10_000; + k += 1; + } + } + samples +} + +fn chain_store(metric: &str) -> InMemoryDataStore { + let schema = Arc::new(chain_samples_schema()); + let rows = chain_samples(metric) + .into_iter() + .map(|(job, t, value)| { + vec![ + Value::Utf8(job.into()), + Value::Timestamp(t), + Value::Float64(value), + ] + }) + .collect(); + let batch = Batch::try_new(schema.clone(), rows).unwrap(); + InMemoryDataStore::default() + .with_raw_data( + Source::TimeSeries { + metric: metric.into(), + }, + schema, + vec![batch], + ) + .unwrap() +} + +/// Run `ids` of `dag` once, every output's rows. +fn run_ids( + dag: &asap_executor::plan::PhysicalDAG<'static, Batch, asap_executor::values::SchemaRef>, + ids: &[u64], + context: RunContext, +) -> Result>>, String> { + block_on(async { + let mut all = Vec::new(); + for mut output in dag.execute(ids, context).map_err(|e| e.to_string())? { + let mut rows = Vec::new(); + while let Some(batch) = output.next().await { + rows.extend(batch.map_err(|e| e.to_string())?.to_rows()); + } + all.push(rows); + } + Ok(all) + }) +} + +/// The finalized values of `kind` over `metric` at each evaluation time, +/// from a chain built at ingestion pane by pane, appended to the store and +/// merged at query time: `(evaluation time, job, value)`. +fn chain_answers( + metric: &str, + kind: ExactKind, + times_ms: &[i64], +) -> Result, String> { + let (finalize, chain) = finalized_chain(metric, kind); + let (mut dag, nodes) = + physical_asap_dag_at_query_time_with_node_ids(&[QueryRoot::Operator(finalize.clone())]) + .map_err(|e| e.to_string())?; + let id_of = |node: &Rc| nodes.iter().position(|n| Rc::ptr_eq(n, node)).unwrap(); + let (chain_id, finalize_id) = (id_of(&chain), id_of(&finalize)); + dag.nodes + .iter_mut() + .find(|node| node.id == chain_id) + .unwrap() + .retention_ms = Some(60 * MINUTE_MS as u64); + let views = views_at(&mut dag, &[chain_id as u64]).map_err(|e| format!("views: {e}"))?; + let refresh = + compile(&dag, &[], &[chain_id as u64]).map_err(|e| format!("compile chain: {e}"))?; + let query = + compile(&dag, &views, &[finalize_id as u64]).map_err(|e| format!("compile query: {e}"))?; + let chain_schema = Arc::new( + dag.nodes + .iter() + .find(|node| node.id == chain_id) + .unwrap() + .output_schema + .clone(), + ); + let mut store = Arc::new(chain_store(metric)); + let mut window_end = PANE_MS; + while window_end <= 30 * MINUTE_MS { + let context = RunContext::new( + Scope::Ingestion { + window_start_ms: window_end - PANE_MS, + window_end_ms: window_end, + }, + Limits::default(), + ) + .unwrap(); + let rows = run_ids( + &refresh + .instantiate(store.clone()) + .map_err(|e| e.to_string())?, + &[chain_id as u64], + context, + )? + .remove(0); + let batch = Batch::try_new(chain_schema.clone(), rows).map_err(|e| e.to_string())?; + Arc::get_mut(&mut store) + .unwrap() + .append_materialized_view(&views[0], (window_end - PANE_MS, window_end), vec![batch]) + .map_err(|e| format!("append: {e}"))?; + window_end += PANE_MS; + } + let mut answers = Vec::new(); + for &evaluation_time_ms in times_ms { + let context = + RunContext::new(Scope::Query { evaluation_time_ms }, Limits::default()).unwrap(); + let rows = run_ids( + &query + .instantiate(store.clone()) + .map_err(|e| e.to_string())?, + &[finalize_id as u64], + context, + )? + .remove(0); + for row in rows { + let job = row + .iter() + .find_map(|cell| match cell { + Value::Utf8(job) => Some(job.to_string()), + _ => None, + }) + .unwrap_or_default(); + for cell in row { + if matches!(cell, Value::Float64(_) | Value::Int64(_)) { + answers.push((evaluation_time_ms, job.clone(), cell)); + } + } + } + } + Ok(answers) +} + +/// T3.2. One exact group placed at ingestion time: a chain of exact states +/// in 1-minute panes, merged over 5 minutes and finalized at query time. +/// Every finalized value satisfies the finalize group's sign. At 30 min +/// the window of `neg` holds only `-0.0` and one empty pane: an exact Sum +/// state starts at `+0.0`, so a merged `-0.0` total turns into `+0.0`, +/// against `NonPositive` (F1). // provisional answer Q1 +#[test] +fn exact_accumulator_realizations_keep_the_group_sign() { + let facts: DataWorkload = serde_json::from_value(json!({ + "arrival": {"ingesting": {"rows_per_second": 0.6}}, + "sources": {"pos": gauge(Some("non_negative")), "neg": gauge(Some("non_positive"))}, + })) + .unwrap(); + let kinds = [ + ExactKind::Sum, + ExactKind::Min, + ExactKind::Max, + ExactKind::Count, + ExactKind::Rate, + ExactKind::Increase, + ]; + let mut counterexamples = Vec::new(); + let mut not_run = Vec::new(); + let mut checked = 0usize; + for metric in ["pos", "neg"] { + for kind in kinds { + let (finalize, _) = finalized_chain(metric, kind); + let finalize_node_signs = match derived_signs(&finalize, &facts) { + Ok((names, signs)) => { + println!("{metric} {kind:?}: group columns {names:?} signs {signs:?}"); + signs + } + Err(e) => { + not_run.push(format!("{metric} {kind:?}: {e}")); + continue; + } + }; + // The answers' numeric columns are the group's numeric columns. + let numeric_signs: Vec = finalize + .schema + .fields + .iter() + .zip(&finalize_node_signs) + .filter(|(field, _)| { + matches!( + field.dtype, + FieldDataType::Plain(DataType::Float64 | DataType::Int64) + ) + }) + .map(|(_, sign)| *sign) + .collect(); + let answers = match chain_answers(metric, kind, &[20 * MINUTE_MS, 30 * MINUTE_MS]) { + Ok(answers) => answers, + Err(e) => { + not_run.push(format!("{metric} {kind:?}: {e}")); + continue; + } + }; + let [sign] = numeric_signs[..] else { + not_run.push(format!( + "{metric} {kind:?}: numeric columns {numeric_signs:?}" + )); + continue; + }; + for (time_ms, job, cell) in answers { + let Some(value) = numeric(&cell) else { + continue; + }; + checked += 1; + let holds = match sign { + ValueSign::NonNegative => !value.is_sign_negative(), + ValueSign::NonPositive => value.is_sign_negative(), + ValueSign::Unknown => true, + }; + if !holds { + counterexamples.push(format!( + "{metric} {kind:?} at {time_ms} ms, job {job}: {value:?} against {sign:?}" + )); + } + } + } + } + println!("checked {checked} finalized values; not run: {not_run:#?}"); + for c in &counterexamples { + println!("COUNTEREXAMPLE {c}"); + } + assert!( + not_run.is_empty(), + "realizations that did not run: {not_run:#?}" + ); + assert!(checked > 0, "no finalized value was checked"); + assert!(counterexamples.is_empty(), "{counterexamples:#?}"); +} + +// --------------------------------------------------------------------------- +// T3.3: one group, one sign, whatever queries share it. +// --------------------------------------------------------------------------- + +/// `group`'s expression with every input group replaced by its own +/// rendering: equal across memos exactly when the expressions are. +fn rendered(memo: &Memo, group: MemoGroupId, cache: &mut HashMap) -> String { + if let Some(text) = cache.get(&group) { + return text.clone(); + } + let expression = &memo.groups()[group.0 as usize].expression; + let children: Vec = expression.children().into_iter().copied().collect(); + let child_texts: HashMap = children + .iter() + .map(|child| (*child, rendered(memo, *child, cache))) + .collect(); + let text = format!( + "{:?}", + expression.map_children(|child| child_texts[child].clone()) + ); + cache.insert(group, text.clone()); + text +} + +/// Whether two signs contradict: one says `NonNegative`, the other +/// `NonPositive`. `Unknown` contradicts nothing. +fn contradicts(a: ValueSign, b: ValueSign) -> bool { + matches!( + (a, b), + (ValueSign::NonNegative, ValueSign::NonPositive) + | (ValueSign::NonPositive, ValueSign::NonNegative) + ) +} + +/// T3.3. A group's sign is derived once, from its expression: the same +/// expression has the same `column_signs` in every memo, whichever queries +/// share it and in whatever order they arrive, after Stage 1 has added its +/// rewrite candidates (finer-aggregate reuse for `q2` over `q1`). And no +/// exact candidate of a group derives a sign that contradicts the group's. +/// // provisional answer Q11 +#[test] +fn a_groups_sign_does_not_depend_on_its_queries() { + let q1 = "sum by (job, host) (rate(c_total[5m]))"; + let q2 = "sum by (job) (rate(c_total[5m]))"; + let q3 = "sum(-quantile_over_time(0.9, pos[5m]))"; + let q4 = "sum(-quantile_over_time(0.9, pos[5m])) unless neg"; + let facts = Facts::Findings.json(); + let data: DataWorkload = serde_json::from_value(facts.clone()).unwrap(); + let capabilities = asap_planner::DeploymentCapabilities { + supported_summaries: Some(asap_executor::supported_summary_families()), + ..asap_planner::DeploymentCapabilities::default() + }; + let mut memos: Vec<(String, Memo)> = Vec::new(); + for queries in [vec![q1, q2], vec![q2, q1], vec![q2], vec![q3, q4], vec![q4]] { + let workload: PlanningWorkload = serde_json::from_value(json!({ + "query_workload": { + "language": "promql", + "repeating_queries": queries.iter().map(|query| json!({ + "query": query, + "demand": {"fixed_interval_at": {"interval": 60000, "evaluation_phase": 0}}, + "requirements": {"accuracy": { + "max_error": 0.05, "failure_probability": 0.3, + "accuracy_scope": "whole_result", "accuracy_metric": "relative", + }}, + "predictability": {"predictable": {"known_at": null}}, + })).collect::>(), + }, + "data_workload": facts, + })) + .unwrap(); + let roots = lower_promql_roots(&workload).unwrap(); + let entries: Vec<_> = workload.query_workload.entries().collect(); + let models = PlanningModels::builtin().with_deployment_capabilities(&capabilities); + let run = run_stages_until(roots, &entries, &data, models, RunUntil::Pass2).unwrap(); + let label = queries.join(" ; "); + memos.push(( + format!("[{label}] Stage 0"), + run.stage0_memo_groups.clone().expect("Stage 0 ran"), + )); + let pass2 = run.run.logical_asap_candidates.unwrap_or_else(|| { + panic!( + "[{label}]: Pass 2 did not run: {:?}", + run.run.selection.err() + ) + }); + memos.push((format!("[{label}] Pass 2"), pass2.memo)); + } + // The same expression, the same signs, in every memo holding it. + let mut signs_of: BTreeMap)>> = BTreeMap::new(); + for (label, memo) in &memos { + let mut cache = HashMap::new(); + for index in 0..memo.groups().len() { + let group = MemoGroupId(index as u32); + signs_of + .entry(rendered(memo, group, &mut cache)) + .or_default() + .push(( + label.clone(), + memo.groups()[index].group_properties.column_signs.clone(), + )); + } + } + let shared_expressions = signs_of.values().filter(|memos| memos.len() > 1).count(); + let disagreements: Vec = signs_of + .iter() + .filter(|(_, signs)| signs.iter().any(|(_, s)| *s != signs[0].1)) + .map(|(expression, signs)| { + format!( + "{}: {signs:?}", + expression.chars().take(300).collect::() + ) + }) + .collect(); + println!( + "{} distinct expressions, {shared_expressions} in more than one memo, {} disagree", + signs_of.len(), + disagreements.len() + ); + for d in &disagreements { + println!("DISAGREEMENT {d}"); + } + assert!( + shared_expressions > 0, + "no expression is shared across memos" + ); + // Every exact candidate's own signs never contradict its group's. A + // sketch build or readout computes estimates, which `column_signs` does + // not cover, so only exact candidates are compared. + let mut contradictions = Vec::new(); + let mut checked_candidates = 0usize; + let mut candidate_errors = Vec::new(); + for (label, memo) in memos.iter().filter(|(label, _)| label.ends_with("Pass 2")) { + for group in memo.groups() { + for candidate in &group.candidates { + let operator = &candidate.expression.operator; + let estimate = matches!(operator, Operator::ASAP(ASAPOp::SummaryEstimate { .. })); + if estimate || candidate.logical_properties.parameters().is_some() { + continue; + } + let group_signs = &group.group_properties.column_signs; + match column_signs_of_candidate_expression(memo, operator) { + Ok(signs) => { + checked_candidates += 1; + let contradicting = signs.len() != group_signs.len() + || signs + .iter() + .zip(group_signs) + .any(|(c, g)| contradicts(*c, *g)); + if contradicting { + contradictions.push(format!( + "{label}: candidate {signs:?} vs group {group_signs:?}: {}", + format!("{operator:?}") + .chars() + .take(240) + .collect::() + )); + } + } + Err(e) => candidate_errors.push(format!("{label}: {e}")), + } + } + } + } + println!("checked {checked_candidates} exact candidates"); + for c in &contradictions { + println!("CONTRADICTION {c}"); + } + assert!(disagreements.is_empty(), "{disagreements:#?}"); + assert!(candidate_errors.is_empty(), "{candidate_errors:#?}"); + assert!(checked_candidates > 0, "no exact candidate was checked"); + assert!(contradictions.is_empty(), "{contradictions:#?}"); +} diff --git a/crates/planner/src/installable/tests.rs b/crates/planner/src/installable/tests.rs index 5e5025c40..76b926f67 100644 --- a/crates/planner/src/installable/tests.rs +++ b/crates/planner/src/installable/tests.rs @@ -363,3 +363,70 @@ fn a_root_time_shift_is_read_whole_by_the_installer_not_by_the_cost() { assert_eq!(retention.len(), 1); assert_eq!(retention[0].lookback_ms(), None); } + +/// `max_over_time(neg[300s])` over `neg`, whose `value` is declared +/// non-positive, planned exactly: a plan whose root's value column is +/// `NonPositive` (the max of values ≤ 0 is one of them). +async fn plan_over_non_positive_values() -> InstallablePlan { + let workload: crate::PlanningWorkload = serde_json::from_str( + r#"{"query_workload": {"language": "promql", "repeating_queries": [{ + "query": "max_over_time(neg[300s])", + "demand": {"fixed_interval_at": {"interval": 1000, "evaluation_phase": 0}}, + "requirements": {}, + "predictability": {"predictable": {"known_at": null}}}]}, + "data_workload": {"arrival": {"ingesting": {"rows_per_second": 10000.0}}, + "sources": {"neg": {"series_count": 1000, "metric_type": "gauge", + "sample_interval": 100, "column_signs": {"value": "non_positive"}}}}}"#, + ) + .unwrap(); + let (plan, _) = plan_installable(crate::UserInput::new( + &workload, + crate::FrontendInput::Promql, + crate::PlanningModels::builtin(), + )) + .await + .expect("a plan"); + assert!(!plan.dag.nodes.is_empty()); + plan +} + +/// Every physical node carries its output columns' signs, one per output +/// field, equal to those of the memo group its selected candidate is in: +/// the executor and the cost read them from the plan, not from the memo. +/// DAG validation rejects a node whose sign list has another length. +#[tokio::test] +#[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] +async fn physical_nodes_carry_their_groups_column_signs() { + let plan = plan_over_non_positive_values().await; + // Intended assertions, once the field exists: + // for node in &plan.dag.nodes { + // assert_eq!(node.column_signs.len(), node.output_schema.fields.len()); + // } + // let root = &plan.dag.nodes[plan.dag.roots[0] as usize]; + // let value = root.output_schema.column_id("value").unwrap(); + // assert_eq!(root.column_signs[value], ValueSign::NonPositive); + // and a DAG whose node has one sign too few fails validation. + let _ = plan; + todo!("PR S2: PhysicalASAPDAGNode.column_signs") +} + +/// A plan file keeps every node's column signs: serialized and read back, +/// the plan is equal but for each node's `selected_physical_candidate`, +/// which the plan file does not carry, and the root's value column is +/// still `NonPositive`. +#[tokio::test] +#[ignore = "PR S2: PhysicalASAPDAGNode.column_signs"] +async fn column_signs_survive_plan_serialization() { + let mut plan = plan_over_non_positive_values().await; + let decoded: InstallablePlan = + serde_json::from_str(&serde_json::to_string(&plan).unwrap()).unwrap(); + for node in &mut plan.dag.nodes { + node.selected_physical_candidate = None; + } + assert_eq!(decoded, plan); + // Intended assertion, once the field exists: + // let root = &decoded.dag.nodes[decoded.dag.roots[0] as usize]; + // let value = root.output_schema.column_id("value").unwrap(); + // assert_eq!(root.column_signs[value], ValueSign::NonPositive); + todo!("PR S2: PhysicalASAPDAGNode.column_signs") +} diff --git a/crates/stage2-physical-optimization/src/tests/candidates.rs b/crates/stage2-physical-optimization/src/tests/candidates.rs index 02ec2887d..59ea5e039 100644 --- a/crates/stage2-physical-optimization/src/tests/candidates.rs +++ b/crates/stage2-physical-optimization/src/tests/candidates.rs @@ -490,3 +490,213 @@ fn a_time_independent_group_without_a_grid_is_materialized_across_evaluations() assert!(offered(&physical, pinned).contains(&properties(IT_NONE))); assert_eq!(pruned(&physical, pinned), vec![]); } + +/// A Count-Min state (`Cms`, `CmsWithHeap`, `HydraCms`) estimates a count +/// as the minimum over its rows of counters that only grow, which +/// over-estimates only while every weight is non-negative: with weights +/// +5 and -5 hashed to one counter, a key that saw only +5 reads 0. So +/// Stage 2 offers a Count-Min build exactly when its state column's sign +/// (the weights it is updated with) is `NonNegative`, and prunes it with +/// `CountMinWeightsNotProvenNonNegative` otherwise. Each build is keyed +/// by the series and weighted by: +/// +/// | Weight | Sign (hand-derived) | Expected | +/// |---|---|---| +/// | `pos` (declared non-negative) | N | offered | +/// | `neg` (declared non-positive) | P | pruned | +/// | `mixed` (undeclared) | U | pruned | +/// | `rate(c_total[5m])` (a counter) | N | offered | +/// | `clamp_min(mixed, 0)` | N: `max(x, +0)` is ≥ +0, `max(-0.0, +0.0)` = +0 | offered | +/// | `Constant(1.0)` over `mixed` | N | offered | +/// | `Constant(-1.0)` over `mixed` | P | pruned | +/// +/// Each runs for `Cms` and `CmsWithHeap`. Every mismatch is listed, then +/// the test fails once with all of them; it then checks +/// `state_column_sign` against the same hand-derived signs. Today the +/// `clamp_min` row is pruned (the function has no sign rule). +#[test] +fn count_min_is_offered_only_over_non_negative_weights() { + use asap_types::ir::operator::AggIntent; + use asap_types::ir::scalar::{resolve_column_ref, ScalarValue}; + use asap_types::ir::{NonASAPOp, ProjectItem, ScalarExpr}; + use asap_types::stages::MemoGroupId; + use asap_types::summary_type::parameter_list; + use asap_types::test_support::memo_fixtures::aggregate; + use asap_types::workload::PrometheusMetricType; + use ValueSign::{NonNegative, NonPositive, Unknown}; + + let declared = |sign| SourceFacts { + column_signs: BTreeMap::from([("value".to_string(), sign)]), + ..SourceFacts::default() + }; + let data = DataWorkload { + sources: BTreeMap::from([ + ("pos".to_string(), declared(NonNegative)), + ("neg".to_string(), declared(NonPositive)), + ("mixed".to_string(), SourceFacts::default()), + ( + "c_total".to_string(), + SourceFacts { + metric_type: Some(PrometheusMetricType::Counter), + ..SourceFacts::default() + }, + ), + ]), + ..DataWorkload::default() + }; + /// The group of each row's weight column: a scan, a rate or a clamp, + /// built over a scan by the groups listed bottom-up (each one's own + /// candidate feeding the next). + enum WeightInput { + Scan(&'static str), + RateOf(&'static str), + ClampMinZeroOf(&'static str), + } + let weight_input = |memo: &mut Memo, input: &WeightInput| -> Vec { + match input { + WeightInput::Scan(metric) => vec![scan_group(memo, metric, &["job"], vec![])], + WeightInput::RateOf(metric) => { + let scan = scan_group(memo, metric, &["job"], vec![]); + let window = group(memo, range(300_000, scan)); + let rate = group( + memo, + aggregate(Reduction::PerEntity, AggIntent::Rate, window), + ); + vec![scan, window, rate] + } + WeightInput::ClampMinZeroOf(metric) => { + let scan = scan_group(memo, metric, &["job"], vec![]); + let schema = &memo.groups()[scan.0 as usize].group_properties.schema; + let value = resolve_column_ref(&ColumnRef::SampleValue, schema).unwrap(); + let cols = schema + .fields + .iter() + .enumerate() + .map(|(column, field)| ProjectItem { + alias: Some(field.name.clone()), + expr: if column == value { + ScalarExpr::FunctionCall { + name: "promql_clamp_min".into(), + args: vec![ + ScalarExpr::Column(column), + ScalarExpr::Literal(ScalarValue::Float64(0.0)), + ], + } + } else { + ScalarExpr::Column(column) + }, + }) + .collect(); + let clamped = group( + memo, + Operator::NonASAP(NonASAPOp::Project { cols, child: scan }), + ); + vec![scan, clamped] + } + } + }; + let sample_value = || SummaryInputExpr::Column(ColumnRef::SampleValue); + let rows = [ + ("pos", WeightInput::Scan("pos"), sample_value(), NonNegative), + ("neg", WeightInput::Scan("neg"), sample_value(), NonPositive), + ("mixed", WeightInput::Scan("mixed"), sample_value(), Unknown), + ( + "rate(c_total[5m])", + WeightInput::RateOf("c_total"), + sample_value(), + NonNegative, + ), + // provisional answer Q3: the executor resolves max(-0.0, +0.0) + // as +0.0 (as Go does), so clamp_min(x, 0) is never -0.0. + ( + "clamp_min(mixed, 0)", + WeightInput::ClampMinZeroOf("mixed"), + sample_value(), + NonNegative, + ), + ( + "Constant(1.0)", + WeightInput::Scan("mixed"), + SummaryInputExpr::Constant(1.0), + NonNegative, + ), + ( + "Constant(-1.0)", + WeightInput::Scan("mixed"), + SummaryInputExpr::Constant(-1.0), + NonPositive, + ), + ]; + let mut mismatches = Vec::new(); + let mut state_signs = Vec::new(); + // `HydraCms` is checked by the same rule, but it lists no build + // parameters (`parameter_list` is empty), so the memo accepts no + // HydraCms build candidate to offer or prune. + assert!(parameter_list(SketchAlgorithm::HydraCms).is_empty()); + for algorithm in [SketchAlgorithm::Cms, SketchAlgorithm::CmsWithHeap] { + for (name, input, weight, sign) in &rows { + let mut memo = Memo::new(&data); + let chain = weight_input(&mut memo, input); + let producer = *chain.last().unwrap(); + let state = sketch_group( + &mut memo, + Operator::ASAP(ASAPOp::SummaryAgg { + child: producer, + summary_type: FieldDataType::Sketch(algorithm), + input: SummaryUpdate { + item: Some(SummaryInputExpr::EntityIdentity( + EntityIdentity::PromqlLabelSet { excluding: vec![] }, + )), + weight: weight.clone(), + }, + reduction: Reduction::Reduce(Default::default()), + filter: None, + }), + algorithm, + 1, + ); + query(&mut memo, state, None); + let (producer_candidate, state_candidate) = + (candidate_id(producer, 0), candidate_id(state, 0)); + let evaluation_grid_of_group = vec![None; memo.groups().len()]; + let edges = chain + .windows(2) + .map(|pair| edge(candidate_id(pair[0], 0), candidate_id(pair[1], 0), 0, true)) + .chain([edge(producer_candidate, state_candidate, 0, true)]) + .collect(); + let logical = logical(memo, edges, evaluation_grid_of_group); + let physical = generate(&logical, &ingesting(None), &retaining_everything()); + let kept = offered(&physical, state_candidate); + let count_min_pruned = pruned(&physical, state_candidate) + .iter() + .any(|(_, reason)| *reason == PruneReason::CountMinWeightsNotProvenNonNegative); + let expected = match *sign == NonNegative { + true => "offered", + false => "pruned", + }; + let outcome = match (kept.is_empty(), count_min_pruned) { + (false, false) => "offered", + (true, true) => "pruned", + (false, true) => "offered and pruned", + (true, false) => "neither offered nor pruned for its weights", + }; + if outcome != expected { + mismatches.push(format!( + "{algorithm:?} over {name}: {outcome}, expected {expected}" + )); + } + state_signs.push((algorithm, *name, logical, state, *sign)); + } + } + assert!(mismatches.is_empty(), "{mismatches:#?}"); + // The consumer reads the state column's sign through one method. + for (algorithm, name, logical, state, sign) in state_signs { + assert_eq!( + logical.memo.groups()[state.0 as usize] + .group_properties + .state_column_sign(), + Some(sign), + "{algorithm:?} over {name}" + ); + } +} diff --git a/crates/types/src/ir/scalar/scalar_type_rules.rs b/crates/types/src/ir/scalar/scalar_type_rules.rs index 81be9e466..e92ec77e2 100644 --- a/crates/types/src/ir/scalar/scalar_type_rules.rs +++ b/crates/types/src/ir/scalar/scalar_type_rules.rs @@ -138,6 +138,25 @@ pub fn promql_function_arity(name: &str) -> Option { }) } +/// The math function a call computes: PromQL's `promql_(value, +/// numeric literal…)` and SQL's `abs(x)`; `None` for any other call or a +/// non-literal bound. `promql_clamp_min(value, 0)` → +/// `Some(MathFunc::ClampMin { min: 0.0 })`. +pub fn math_function_of_call( + name: &str, + args: &[crate::ir::scalar::ScalarExpr], +) -> Option { + let _ = (name, args); + todo!("sign derivation by construction: math_function_of_call") +} + +/// The date function a call computes: `promql_hour(value)` → +/// `Some(TimeFunc::Hour)`; `None` for any other call. +pub fn time_function_of_call(name: &str) -> Option { + let _ = name; + todo!("sign derivation by construction: time_function_of_call") +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/types/src/stages/logical/group/column_properties.rs b/crates/types/src/stages/logical/group/column_properties.rs index b3dd1dd6d..3795c312c 100644 --- a/crates/types/src/stages/logical/group/column_properties.rs +++ b/crates/types/src/stages/logical/group/column_properties.rs @@ -74,6 +74,15 @@ pub enum ColumnLineage { }, } +impl ValueSign { + /// Whether every non-NaN value has one sign bit: `NonNegative` or + /// `NonPositive`. Then a relative error ε on each value is at most ε on + /// their sum. `NonPositive.is_one_sign()` → true; `Unknown` → false. + pub fn is_one_sign(self) -> bool { + todo!("sign derivation by construction: ValueSign::is_one_sign") + } +} + use ValueSign::{NonNegative, NonPositive, Unknown}; type Input<'m> = MemoGroupAsInput<'m>; diff --git a/crates/types/src/stages/logical/group/mod.rs b/crates/types/src/stages/logical/group/mod.rs index cf087d8ea..31b58c2ff 100644 --- a/crates/types/src/stages/logical/group/mod.rs +++ b/crates/types/src/stages/logical/group/mod.rs @@ -105,6 +105,14 @@ pub struct GroupProperties { } impl GroupProperties { + /// The sign of a state group's state column, its first non-plain field: + /// the sign of the values its states are updated with. A DDSketch build + /// over a latency declared non-negative → `Some(NonNegative)`; a `Scan` + /// group, which has no state column → `None`. + pub fn state_column_sign(&self) -> Option { + todo!("sign derivation by construction: GroupProperties::state_column_sign") + } + /// The sketch algorithm of the state the group outputs; `None` for /// rows, values and exact states. pub fn sketch_algorithm_of_state(&self) -> Option { diff --git a/crates/types/src/test_support/forwarders.rs b/crates/types/src/test_support/forwarders.rs index e3092249c..3df68db27 100644 --- a/crates/types/src/test_support/forwarders.rs +++ b/crates/types/src/test_support/forwarders.rs @@ -130,3 +130,16 @@ pub fn population_over_selector( }); Rc::new(OperatorNode::with_schema(evaluation, root.schema.clone())) } + +/// The column signs `expression`, a candidate expression over `memo`'s +/// groups, derives on its own, without adding it to the memo: the signs a +/// new group computing it would get. A test compares them with the signs +/// of the group the candidate joined, which an exact candidate must never +/// contradict (`NonNegative` against `NonPositive`). +pub fn column_signs_of_candidate_expression( + memo: &crate::stages::Memo, + expression: &Operator, +) -> Result, crate::stages::MemoGroupError> { + let _ = (memo, expression); + todo!("sign derivation by construction: column_signs_of_candidate_expression") +} diff --git a/crates/types/src/test_support/memo_fixtures.rs b/crates/types/src/test_support/memo_fixtures.rs index db6fcb344..8966c6f35 100644 --- a/crates/types/src/test_support/memo_fixtures.rs +++ b/crates/types/src/test_support/memo_fixtures.rs @@ -7,6 +7,7 @@ use std::time::Duration; use crate::ir::operator::arguments::{ AtModifier, BinaryOpKind, PromQLVectorSetOpKind, Reduction, Source, SourceColumn, TimeShift, + VectorMatch, }; use crate::ir::operator::AggIntent; use crate::ir::scalar::expr_ir::{ArithmeticOpKind, CompareOpKind}; @@ -173,7 +174,7 @@ pub fn arithmetic( lhs: MemoGroupId, rhs: MemoGroupId, ) -> Operator { - binary_op(BinaryOpKind::Arithmetic(kind), lhs, rhs) + binary_op(BinaryOpKind::Arithmetic(kind), None, false, lhs, rhs) } /// `lhs and rhs`, `lhs or rhs` or `lhs unless rhs`, series by series, @@ -183,18 +184,28 @@ pub fn set_operator( lhs: MemoGroupId, rhs: MemoGroupId, ) -> Operator { - binary_op(BinaryOpKind::Set(kind), lhs, rhs) + binary_op(BinaryOpKind::Set(kind), None, false, lhs, rhs) } -fn binary_op(kind: BinaryOpKind, lhs: MemoGroupId, rhs: MemoGroupId) -> Operator { +/// `lhs rhs` with an optional vector matching and PromQL's `bool` +/// modifier. For example, `neg > bool neg` is +/// `binary_op(BinaryOpKind::Compare(CompareOpKind::Gt), None, true, neg, neg)`, +/// whose values are 0 or 1 whatever the sign of `neg`. +pub fn binary_op( + kind: BinaryOpKind, + vector_match: Option, + return_bool: bool, + lhs: MemoGroupId, + rhs: MemoGroupId, +) -> Operator { Operator::NonASAP(NonASAPOp::BinaryOp { operator: BinaryOperator { checked_relative_division: false, checked_finite_division: false, kind, - vector_match: None, + vector_match, }, - return_bool: false, + return_bool, lhs, rhs, }) diff --git a/crates/types/tests/main.rs b/crates/types/tests/main.rs index a5afa977b..1c4f993ab 100644 --- a/crates/types/tests/main.rs +++ b/crates/types/tests/main.rs @@ -11,4 +11,6 @@ mod schema_rebuilding; mod structure_contract; mod summary_merge_structure; mod tumbling_window_chain; +mod value_sign_derivation_sites; +mod value_sign_rules; mod workload_compatibility; diff --git a/crates/types/tests/value_sign_derivation_sites.rs b/crates/types/tests/value_sign_derivation_sites.rs new file mode 100644 index 000000000..e490ca828 --- /dev/null +++ b/crates/types/tests/value_sign_derivation_sites.rs @@ -0,0 +1,253 @@ +//! Signs are derived in one place (sign test plan T4 b13): every +//! `ValueSign` a group's columns get is produced by the derivation in +//! `column_properties.rs`, once per memo group, or read from the +//! workload's declarations. Every consumer reads the stored property +//! (`column_signs`, `ValueSign::is_one_sign`, +//! `GroupProperties::state_column_sign`) and never recomputes it: a +//! second copy of a rule drifts from the first, as Stage 2's own lookup +//! of "the state column is the first non-plain field" could. +//! +//! The test scans the non-test Rust source of every crate under +//! `crates/*/src`: files under a `tests` or `test_support` directory, +//! files named `tests.rs` or `test_fixtures.rs`, and inline +//! `#[cfg(test)] mod … { … }` blocks are skipped. + +use std::path::{Path, PathBuf}; + +/// The derivation's file, relative to the workspace root. +const DERIVATION: &str = "crates/types/src/stages/logical/group/column_properties.rs"; + +/// Functions outside [`DERIVATION`] allowed to return a `ValueSign`: each +/// reads a stored sign, none derives one. `(file, function name)`. +const SIGN_READERS: &[(&str, &str)] = &[( + "crates/types/src/stages/logical/group/mod.rs", + "state_column_sign", +)]; + +fn workspace_root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../..") + .canonicalize() + .expect("the workspace root") +} + +/// Every `.rs` file below `directory`, but test files and directories. +fn non_test_rust_files(directory: &Path, files: &mut Vec) { + let mut entries: Vec<_> = std::fs::read_dir(directory) + .unwrap_or_else(|error| panic!("{}: {error}", directory.display())) + .map(|entry| entry.unwrap().path()) + .collect(); + entries.sort(); + for path in entries { + let name = path.file_name().unwrap().to_string_lossy().into_owned(); + if path.is_dir() { + if name != "tests" && name != "test_support" { + non_test_rust_files(&path, files); + } + } else if name.ends_with(".rs") && name != "tests.rs" && name != "test_fixtures.rs" { + files.push(path); + } + } +} + +/// The text from `open`, the index of a `{`, through its matching `}`. +fn braced_block_end(text: &str, open: usize) -> usize { + let mut depth = 0usize; + for (offset, byte) in text.as_bytes()[open..].iter().enumerate() { + match byte { + b'{' => depth += 1, + b'}' => { + depth -= 1; + if depth == 0 { + return open + offset + 1; + } + } + _ => {} + } + } + text.len() +} + +/// `text` without its inline `#[cfg(test)] mod name { … }` blocks: a +/// `#[cfg(test)] mod tests;` declaration (its file is skipped by name) +/// stays. +fn without_inline_test_modules(text: &str) -> String { + let mut kept = String::new(); + let mut rest = text; + while let Some(attribute) = rest.find("#[cfg(test)]") { + let after = &rest[attribute + "#[cfg(test)]".len()..]; + let trimmed = after.trim_start(); + let inline_module = trimmed.starts_with("mod ") + && trimmed + .find(['{', ';']) + .is_some_and(|end| trimmed.as_bytes()[end] == b'{'); + if !inline_module { + kept.push_str(&rest[..attribute + "#[cfg(test)]".len()]); + rest = after; + continue; + } + kept.push_str(&rest[..attribute]); + let module_start = rest.len() - trimmed.len(); + let open = module_start + trimmed.find('{').unwrap(); + rest = &rest[braced_block_end(rest, open)..]; + } + kept.push_str(rest); + kept +} + +/// The non-test source of each crate file, keyed by its path relative to +/// the workspace root (`crates//src/…`). +fn non_test_sources() -> Vec<(String, String)> { + let root = workspace_root(); + let mut files = Vec::new(); + let mut crates: Vec<_> = std::fs::read_dir(root.join("crates")) + .unwrap() + .map(|entry| entry.unwrap().path().join("src")) + .filter(|src| src.is_dir()) + .collect(); + crates.sort(); + for src in crates { + non_test_rust_files(&src, &mut files); + } + files + .into_iter() + .map(|path| { + let text = std::fs::read_to_string(&path).unwrap(); + let relative = path + .strip_prefix(&root) + .unwrap() + .to_string_lossy() + .replace('\\', "/"); + (relative, without_inline_test_modules(&text)) + }) + .collect() +} + +/// The names of `source`'s functions whose return type mentions +/// `ValueSign` (`fn agree(a: ValueSign, b: ValueSign) -> ValueSign`, +/// `-> Option`, `-> Vec`). +fn functions_returning_a_sign(source: &str) -> Vec { + let mut found = Vec::new(); + let mut rest = source; + while let Some(start) = rest.find("fn ") { + let preceded_by_identifier = start > 0 && { + let before = rest.as_bytes()[start - 1]; + before.is_ascii_alphanumeric() || before == b'_' + }; + let signature_and_rest = &rest[start + 3..]; + rest = signature_and_rest; + if preceded_by_identifier { + continue; + } + let end = signature_and_rest + .find(['{', ';']) + .unwrap_or(signature_and_rest.len()); + let signature = &signature_and_rest[..end]; + let name: String = signature + .chars() + .take_while(|c| c.is_alphanumeric() || *c == '_') + .collect(); + if signature + .split_once("->") + .is_some_and(|(_, returns)| returns.contains("ValueSign")) + { + found.push(name); + } + } + found +} + +/// The body of `fn name` in `source`, or `None` when it has none. +fn function_body<'a>(source: &'a str, name: &str) -> Option<&'a str> { + let start = source.find(&format!("fn {name}("))?; + let open = start + source[start..].find('{')?; + Some(&source[open..braced_block_end(source, open)]) +} + +/// No function outside the derivation produces a `ValueSign` but the +/// listed readers of a stored one; the error contract's +/// `values_of_one_sign` (if it is kept) asks `is_one_sign`, not its own +/// `NonNegative | NonPositive` match; Stage 2's Count-Min check and the +/// DDSketch relative gate read `state_column_sign`, and Stage 2 indexes +/// no `column_signs` itself. Every violation is listed, then the test +/// fails once with all of them. Today Stage 2 looks the state column up +/// inline and `values_of_one_sign` matches the two signs itself. +#[test] +fn b13_no_sign_is_derived_outside_the_derivation() { + let sources = non_test_sources(); + assert!( + sources.iter().any(|(path, _)| path == DERIVATION), + "the derivation file is scanned" + ); + assert!( + sources + .iter() + .any(|(path, source)| path == DERIVATION + && !functions_returning_a_sign(source).is_empty()), + "the scanner finds the derivation's own sign functions" + ); + let mut violations = Vec::new(); + for (path, source) in &sources { + if path == DERIVATION { + continue; + } + for name in functions_returning_a_sign(source) { + if !SIGN_READERS.contains(&(path.as_str(), name.as_str())) { + violations.push(format!("{path}: fn {name} returns a ValueSign")); + } + } + } + let crate_source = |prefix: &str| -> String { + sources + .iter() + .filter(|(path, _)| path.starts_with(prefix)) + .map(|(_, source)| source.as_str()) + .collect() + }; + let stage2 = crate_source("crates/stage2-physical-optimization/src/"); + if !stage2.contains("state_column_sign(") { + violations.push("Stage 2 does not call state_column_sign".into()); + } + if stage2.contains("column_signs") { + violations.push("Stage 2 reads column_signs itself".into()); + } + // provisional answer Q7: the DDSketch relative gate is at the logical + // edge, in the accuracy crate, and reads state_column_sign. + if !crate_source("crates/accuracy/src/").contains("state_column_sign(") { + violations + .push("the accuracy crate's DDSketch gate does not call state_column_sign".into()); + } + let error_contract = crate_source("crates/accuracy/src/error_contract/"); + if let Some(body) = function_body(&error_contract, "values_of_one_sign") { + if !body.contains("is_one_sign") { + violations.push("values_of_one_sign does not call is_one_sign".into()); + } + } + assert!(violations.is_empty(), "{violations:#?}"); +} + +/// The scanner's own helpers on hand-written source, so a parsing slip +/// does not pass the scan above vacuously. +#[test] +fn the_derivation_site_scanner_reads_signatures_and_skips_test_modules() { + let source = "fn agree(a: ValueSign) -> ValueSign { a }\n\ + pub fn state_column_sign(&self) -> Option { None }\n\ + fn is_one_sign(self) -> bool { true }\n\ + fn reads(sign: ValueSign) {}\n\ + #[cfg(test)]\nmod tests { fn inside() -> ValueSign { todo!() } }\n\ + #[cfg(test)]\nmod external;\n"; + let stripped = without_inline_test_modules(source); + assert!(!stripped.contains("inside")); + assert!(stripped.contains("mod external;")); + assert_eq!( + functions_returning_a_sign(&stripped), + vec!["agree".to_string(), "state_column_sign".to_string()] + ); + assert_eq!( + function_body( + "fn values_of_one_sign(x: u8) -> bool { x.is_one_sign() }", + "values_of_one_sign" + ), + Some("{ x.is_one_sign() }") + ); +} diff --git a/crates/types/tests/value_sign_rules.rs b/crates/types/tests/value_sign_rules.rs new file mode 100644 index 000000000..2da9ac726 --- /dev/null +++ b/crates/types/tests/value_sign_rules.rs @@ -0,0 +1,2364 @@ +//! The value-sign transfer rules, one table row per rule (sign test plan, +//! sections T1 and T4). Each row builds one operator over a `Scan` of a +//! source whose `value` sign is known, adds it to a memo and reads the sign +//! the memo derives for the column under test. +//! +//! Every expected sign is derived by hand from IEEE 754 and from what the +//! executor computes, never from the code under test. A sign is the IEEE +//! sign bit: `NonNegative` (N) holds for `+0.0`, positives and `+inf`; +//! `NonPositive` (P) for `-0.0`, negatives and `-inf`; NaN and NULL are +//! excluded; `Unknown` (U) claims nothing. An `Int64` value counts as +//! `v as f64`, so an int `0` is N, and an int column is P only when every +//! value is `< 0`. +//! +//! The sources every row reads: +//! +//! ```text +//! pos gauge [ts, value, job] value declared N +//! neg gauge [ts, value, job] value declared P +//! mixed gauge [ts, value, job] nothing declared +//! c_total counter value N by the counter default +//! negc counter value declared P (the declaration wins) +//! h histogram family nothing declared: h_sum, h_count, h_bucket +//! hd histogram family value declared N +//! flows SQL table [ts, bytes Int64 N, p Float64 U, debt Int64 P, +//! delta Int64 U, gain Float64 N, loss Float64 P] +//! t_pos SQL table [v Float64 N] +//! t_neg SQL table [v Float64 P] +//! ``` +//! +//! A row whose expectation depends on a provisional answer to a question of +//! the plan says so with `provisional answer QN`. A test collects every +//! wrong row and fails once, listing them all. +use std::collections::BTreeMap; +use std::time::Duration; + +use asap_types::ir::operator::arguments::{ + AtModifier, BinaryOpKind, GroupKeys, GroupSide, JoinKind, PromQLVectorSetOpKind, Reduction, + RelationalSetOpKind, SampleKind, Source, VectorGrouping, VectorMatch, VectorMatchKind, + WindowFuncKind, +}; +use asap_types::ir::operator::maintained_population::{ + CurrentSeriesInput, MaintainedPopulation, PopulationInput, +}; +use asap_types::ir::operator::{AggIntent, MathFunc, TimeFunc}; +use asap_types::ir::scalar::expr_ir::{ArithmeticOpKind, CompareOpKind, ScalarValue}; +use asap_types::ir::scalar::scalar_type_rules::{math_function_of_call, time_function_of_call}; +use asap_types::ir::scalar::{ColumnRef, ProjectItem, SortKey}; +use asap_types::ir::schema::{DataType, Field, FieldDataType, Schema}; +use asap_types::ir::{ + ASAPOp, ExprSemantics, NonASAPOp, Operator, Predicate, ScalarExpr, TimeRangeKind, +}; +use asap_types::stages::{ColumnLineage, Memo, MemoGroupId, ValueSign}; +use asap_types::summary_type::{ + ExactKind, SketchAlgorithm, SketchStatistic, SummaryInputExpr, SummaryUpdate, +}; +use asap_types::test_support::memo_fixtures::{aggregate, binary_op, range, readout, shift}; +use asap_types::workload::{DataWorkload, PrometheusMetricType, SourceFacts}; + +use ValueSign::{NonNegative as N, NonPositive as P, Unknown as U}; + +const TS: usize = 0; +const VALUE: usize = 1; +const JOB: usize = 2; +const MINUTE_MS: i64 = 60_000; + +// flows' columns. +const BYTES: usize = 1; +const P_COL: usize = 2; +const DEBT: usize = 3; +const DELTA: usize = 4; +const GAIN: usize = 5; +const LOSS: usize = 6; + +/// Every wrong row of one table, reported together. +#[derive(Default)] +struct Rows { + wrong: Vec, + checked: usize, +} + +impl Rows { + fn check(&mut self, row: impl std::fmt::Display, got: ValueSign, expected: ValueSign) { + self.checked += 1; + if got != expected { + self.wrong + .push(format!("{row}: derived {got:?}, expected {expected:?}")); + } + } + + fn fail(&mut self, row: impl std::fmt::Display, why: impl std::fmt::Display) { + self.checked += 1; + self.wrong.push(format!("{row}: {why}")); + } + + fn finish(self, table: &str) { + assert!( + self.wrong.is_empty(), + "{table}: {} of {} rows wrong:\n {}", + self.wrong.len(), + self.checked, + self.wrong.join("\n ") + ); + } +} + +fn facts(sign: Option, metric_type: Option) -> SourceFacts { + SourceFacts { + metric_type, + column_signs: sign + .map(|sign| BTreeMap::from([("value".to_string(), sign)])) + .unwrap_or_default(), + ..SourceFacts::default() + } +} + +fn table_facts(signs: &[(&str, ValueSign)]) -> SourceFacts { + SourceFacts { + column_signs: signs + .iter() + .map(|(column, sign)| (column.to_string(), *sign)) + .collect(), + ..SourceFacts::default() + } +} + +/// The memo every row is added to (the module doc's sources). +fn memo() -> Memo { + use PrometheusMetricType::{Counter, Gauge, Histogram}; + Memo::new(&DataWorkload { + sources: BTreeMap::from([ + ("pos".to_string(), facts(Some(N), Some(Gauge))), + ("neg".to_string(), facts(Some(P), Some(Gauge))), + ("mixed".to_string(), facts(None, Some(Gauge))), + ("c_total".to_string(), facts(None, Some(Counter))), + ("negc".to_string(), facts(Some(P), Some(Counter))), + ("h".to_string(), facts(None, Some(Histogram))), + ("hd".to_string(), facts(Some(N), Some(Histogram))), + ( + "flows".to_string(), + table_facts(&[("bytes", N), ("debt", P), ("gain", N), ("loss", P)]), + ), + ("t_pos".to_string(), table_facts(&[("v", N)])), + ("t_neg".to_string(), table_facts(&[("v", P)])), + ]), + ..DataWorkload::default() + }) +} + +fn add(memo: &mut Memo, expression: Operator) -> MemoGroupId { + let shown = format!("{expression:?}"); + memo.find_or_add_group(expression) + .unwrap_or_else(|error| panic!("cannot add {shown}: {error:?}")) +} + +fn try_add(memo: &mut Memo, expression: Operator) -> Result { + memo.find_or_add_group(expression) + .map_err(|error| format!("{error:?}")) +} + +fn properties(memo: &Memo, group: MemoGroupId) -> &asap_types::stages::GroupProperties { + &memo.groups()[group.0 as usize].group_properties +} + +fn signs(memo: &Memo, group: MemoGroupId) -> &[ValueSign] { + &properties(memo, group).column_signs +} + +fn sign_of(memo: &Memo, group: MemoGroupId, column: &str) -> ValueSign { + let schema = &properties(memo, group).schema; + let index = schema + .column_id(column) + .unwrap_or_else(|| panic!("no column {column} in {schema:?}")); + signs(memo, group)[index] +} + +/// The one numeric column `group` computes itself (its measure, readout or +/// expression): lineage `Computed` by `group`, a plain numeric type. +fn computed_value_sign(memo: &Memo, group: MemoGroupId) -> ValueSign { + let properties = properties(memo, group); + let computed: Vec = properties + .column_lineage + .iter() + .zip(&properties.schema.fields) + .enumerate() + .filter(|(_, (lineage, field))| { + matches!(lineage, ColumnLineage::Computed { group: g, .. } if *g == group) + && matches!( + field.dtype, + FieldDataType::Plain(DataType::Float64 | DataType::Int64) + ) + }) + .map(|(index, _)| index) + .collect(); + let [column] = computed[..] else { + panic!( + "expected one computed numeric column, found {computed:?} in {:?}", + properties.schema + ); + }; + properties.column_signs[column] +} + +/// `Scan metric`: `[ts, value, job]`. +fn series(metric: &str) -> Operator { + asap_types::test_support::memo_fixtures::scan(metric, &[("job", DataType::Utf8)], vec![]) +} + +fn table(name: &str, columns: &[(&str, DataType)]) -> Operator { + Operator::NonASAP(NonASAPOp::Scan { + source: Source::Table { + table_ref: name.into(), + }, + predicates: vec![], + schema: Schema::new( + columns + .iter() + .map(|(name, dtype)| Field::plain(*name, dtype.clone(), false)) + .collect(), + ), + }) +} + +fn flows() -> Operator { + table( + "flows", + &[ + ("ts", DataType::Timestamp), + ("bytes", DataType::Int64), + ("p", DataType::Float64), + ("debt", DataType::Int64), + ("delta", DataType::Int64), + ("gain", DataType::Float64), + ("loss", DataType::Float64), + ], + ) +} + +/// The gauge whose `value` has `sign`. +fn gauge_of(sign: ValueSign) -> &'static str { + match sign { + N => "pos", + P => "neg", + U => "mixed", + } +} + +const SIGNS: [ValueSign; 3] = [N, P, U]; + +fn f64_literal(value: f64) -> ScalarExpr { + ScalarExpr::Literal(ScalarValue::Float64(value)) +} + +fn column(index: usize) -> Box> { + Box::new(ScalarExpr::Column(index)) +} + +/// `Project(ts, expr AS value, job)` over a series. +fn map_value(expr: ScalarExpr, child: MemoGroupId) -> Operator { + let item = |expr, alias: &str| ProjectItem { + alias: Some(alias.into()), + expr, + }; + Operator::NonASAP(NonASAPOp::Project { + cols: vec![ + item(ScalarExpr::Column(TS), "ts"), + item(expr, "value"), + item(ScalarExpr::Column(JOB), "job"), + ], + child, + }) +} + +/// `SELECT expr AS v` over a table: the sign of `v`. +fn projected_sign(memo: &mut Memo, expr: ScalarExpr, child: MemoGroupId) -> ValueSign { + let group = add( + memo, + Operator::NonASAP(NonASAPOp::Project { + cols: vec![ProjectItem { + alias: Some("v".into()), + expr, + }], + child, + }), + ); + signs(memo, group)[0] +} + +/// The sign of `value` after `expr` maps it, over the gauge of sign `input`. +fn mapped_value_sign( + memo: &mut Memo, + input: ValueSign, + expr: ScalarExpr, +) -> ValueSign { + let scan = add(memo, series(gauge_of(input))); + let group = add(memo, map_value(expr, scan)); + signs(memo, group)[VALUE] +} + +fn vector_of(expr: ScalarExpr) -> Operator { + Operator::NonASAP(NonASAPOp::PromqlVectorFromScalar(expr)) +} + +// ── T1.1 ──────────────────────────────────────────────────────────────── + +/// T1.1: a `Scan` reads the declared signs; a counter's `value` is N without +/// a declaration, and a declaration wins over that default. An undeclared +/// histogram's `_sum` is U: a histogram may observe negative values, so +/// its `_sum` decreases (B14). A label is U. +/// +/// Fails today: `h_sum` is N, because `series_facts` types a family's +/// `_sum` as a counter. +#[test] +fn scan_signs_follow_declarations_and_metric_types() { + let mut memo = memo(); + let mut rows = Rows::default(); + for (metric, expected, note) in [ + ("pos", N, "declared N"), + ("neg", P, "declared P"), + ("mixed", U, "undeclared gauge"), + ("c_total", N, "a counter"), + // provisional answer Q8: the declaration wins over the counter default. + ("negc", P, "a counter declared P"), + // B14 (user decision Q3): an undeclared `_sum` has no sign. + ("h_sum", U, "undeclared histogram _sum"), + ("h_count", N, "histogram _count, a count"), + ("h_bucket", U, "histogram _bucket"), + // provisional answer Q8: "declared" means the family's `value` entry. + ("hd_sum", N, "histogram _sum, family declares N"), + ] { + let group = add(&mut memo, series(metric)); + rows.check( + format!("Scan {metric} value ({note})"), + signs(&memo, group)[VALUE], + expected, + ); + } + let pos = add(&mut memo, series("pos")); + rows.check("Scan pos label job", signs(&memo, pos)[JOB], U); + let flows = add(&mut memo, flows()); + rows.check("Scan flows.bytes", signs(&memo, flows)[BYTES], N); + rows.check("Scan flows.p", signs(&memo, flows)[P_COL], U); + rows.check("Scan flows.debt", signs(&memo, flows)[DEBT], P); + rows.finish("T1.1"); +} + +/// T4 b14: the undeclared histogram `_sum` alone. +#[test] +fn b14_an_undeclared_histogram_sum_has_no_sign() { + let mut memo = memo(); + let group = add(&mut memo, series("h_sum")); + assert_eq!(signs(&memo, group)[VALUE], U); +} + +/// T4 b04: a gauge can be declared non-negative. +#[test] +fn b04_a_gauge_can_be_declared_non_negative() { + let mut memo = memo(); + let group = add(&mut memo, series("pos")); + assert_eq!(signs(&memo, group)[VALUE], N); +} + +// ── T1.2 ──────────────────────────────────────────────────────────────── + +/// T1.2: operators that keep their input's rows copy every column's sign; +/// a written or added column is U; set-like operators keep what their +/// inputs agree on. +#[test] +fn row_operators_copy_their_input_sign() { + let mut memo = memo(); + let mut rows = Rows::default(); + for input in [N, P] { + let scan = add(&mut memo, series(gauge_of(input))); + let ranged = add(&mut memo, range(MINUTE_MS, scan)); + let instant = |child| { + Operator::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Instant, + child, + }) + }; + let selector = add(&mut memo, instant(scan)); + let operators: Vec<(&str, Operator)> = vec![ + ( + "Filter(value > 1)", + Operator::NonASAP(NonASAPOp::Filter { + pred: Predicate(ScalarExpr::Compare { + left: column(VALUE), + op: CompareOpKind::Gt, + right: Box::new(f64_literal(1.0)), + semantics: ExprSemantics::Promql, + }), + child: scan, + }), + ), + ( + "Sort(value)", + Operator::NonASAP(NonASAPOp::Sort { + keys: vec![SortKey { + expr: ScalarExpr::Column(VALUE), + ascending: true, + nulls_first: false, + }], + partition_by: GroupKeys::default(), + child: scan, + }), + ), + ( + "Limit 5", + Operator::NonASAP(NonASAPOp::Limit { + n: Some(5), + offset: 0, + partition_by: GroupKeys::default(), + child: scan, + }), + ), + ( + "Dedup(job)", + Operator::NonASAP(NonASAPOp::Dedup { + cols: vec![TS, VALUE, JOB], + child: scan, + }), + ), + ("TimeRange[1m]", range(MINUTE_MS, scan)), + ("TimeShift(offset 1m)", shift(MINUTE_MS, None, selector)), + ( + "TimeShift(@ 100)", + shift(0, Some(AtModifier::Timestamp(100_000)), selector), + ), + ( + "TimeShift(@ end())", + shift(0, Some(AtModifier::End), selector), + ), + ( + "PromqlSubquery[5m:30s]", + Operator::NonASAP(NonASAPOp::PromqlSubquery { + range: Duration::from_secs(300), + resolution: Some(Duration::from_secs(30)), + child: selector, + }), + ), + ( + "PromqlSeriesSample(limitk 2)", + Operator::NonASAP(NonASAPOp::PromqlSeriesSample { + by: GroupKeys::default(), + kind: SampleKind::LimitK(2), + child: selector, + }), + ), + ( + "PromqlInfoEnrich", + Operator::NonASAP(NonASAPOp::PromqlInfoEnrich { + selector: vec![], + child: selector, + }), + ), + ( + "MaintainPopulation", + Operator::ASAP(ASAPOp::MaintainPopulation { + child: scan, + population: MaintainedPopulation { + input: PopulationInput::CurrentSeries(CurrentSeriesInput { + metric: gauge_of(input).into(), + matchers: vec![], + grouping: vec![], + without: false, + lookback_ms: 300_000, + }), + max_k: 1, + quantiles: false, + }, + }), + ), + ]; + let _ = ranged; + for (name, operator) in operators { + let row = format!("{name} over {}", gauge_of(input)); + match try_add(&mut memo, operator) { + Ok(group) => rows.check(row, sign_of(&memo, group, "value"), input), + Err(error) => rows.fail(row, format!("cannot build: {error}")), + } + } + // A relabel writes its destination label; the value is copied. + for dst in ["x", "job"] { + let row = format!("PromqlRelabel(dst {dst}) over {}", gauge_of(input)); + let relabel = Operator::NonASAP(NonASAPOp::PromqlRelabel { + dst: dst.into(), + value: ScalarExpr::Literal(ScalarValue::Utf8("a".into())), + child: selector, + }); + match try_add(&mut memo, relabel) { + Ok(group) => { + rows.check( + format!("{row}: value"), + sign_of(&memo, group, "value"), + input, + ); + rows.check(format!("{row}: {dst}"), sign_of(&memo, group, dst), U); + } + Err(error) => rows.fail(row, format!("cannot build: {error}")), + } + } + } + // `Values` rows 1 and -1: no one sign. + let values = Operator::NonASAP(NonASAPOp::Values { + rows: vec![vec![f64_literal(1.0)], vec![f64_literal(-1.0)]], + schema: Schema::new(vec![Field::plain("v", DataType::Float64, false)]), + }); + match try_add(&mut memo, values) { + Ok(group) => rows.check("Values(1, -1)", signs(&memo, group)[0], U), + Err(error) => rows.fail("Values(1, -1)", format!("cannot build: {error}")), + } + // The window column `lag(p)`: p crosses zero, so no sign. + let flows = add(&mut memo, flows()); + let window = Operator::NonASAP(NonASAPOp::SQLWindowFunc { + func: WindowFuncKind::Lag, + args: vec![ScalarExpr::Column(P_COL)], + partition_by: GroupKeys::default(), + order_by: vec![SortKey { + expr: ScalarExpr::Column(TS), + ascending: true, + nulls_first: false, + }], + frame: None, + output_name: "prev".into(), + child: flows, + }); + match try_add(&mut memo, window) { + Ok(group) => { + rows.check( + "SQLWindowFunc lag(p): prev", + sign_of(&memo, group, "prev"), + U, + ); + rows.check( + "SQLWindowFunc lag(p): bytes", + sign_of(&memo, group, "bytes"), + N, + ); + } + Err(error) => rows.fail("SQLWindowFunc lag(p)", format!("cannot build: {error}")), + } + // Joins of t_pos (v N) and t_neg (v P). + let t_pos = add(&mut memo, table("t_pos", &[("v", DataType::Float64)])); + let t_neg = add(&mut memo, table("t_neg", &[("v", DataType::Float64)])); + for (kind, expected) in [ + (JoinKind::Inner, vec![N, P]), + (JoinKind::Semi, vec![N]), + (JoinKind::Anti, vec![N]), + ] { + let join = Operator::NonASAP(NonASAPOp::Join { + kind: kind.clone(), + pred: Predicate(ScalarExpr::Literal(ScalarValue::Boolean(true))), + left: t_pos, + right: t_neg, + }); + match try_add(&mut memo, join) { + Ok(group) => { + let got = signs(&memo, group).to_vec(); + if got != expected { + rows.fail( + format!("Join {kind:?}(t_pos, t_neg)"), + format!("derived {got:?}, expected {expected:?}"), + ); + } else { + rows.checked += 1; + } + } + Err(error) => rows.fail(format!("Join {kind:?}"), format!("cannot build: {error}")), + } + } + for (right, expected) in [(t_pos, N), (t_neg, U)] { + let name = if right == t_pos { "t_pos" } else { "t_neg" }; + let set_op = Operator::NonASAP(NonASAPOp::SetOp { + kind: RelationalSetOpKind::Union, + all: true, + left: t_pos, + right, + }); + match try_add(&mut memo, set_op) { + Ok(group) => rows.check( + format!("SetOp union all(t_pos, {name})"), + signs(&memo, group)[0], + expected, + ), + Err(error) => rows.fail( + format!("SetOp(t_pos, {name})"), + format!("cannot build: {error}"), + ), + } + let concat = Operator::NonASAP(NonASAPOp::Concat { + children: vec![t_pos, right], + }); + match try_add(&mut memo, concat) { + Ok(group) => rows.check( + format!("Concat(t_pos, {name})"), + signs(&memo, group)[0], + expected, + ), + Err(error) => rows.fail( + format!("Concat(t_pos, {name})"), + format!("cannot build: {error}"), + ), + } + } + rows.finish("T1.2"); +} + +// ── T1.3 ──────────────────────────────────────────────────────────────── + +/// T1.3: `vector(s)` has the sign bit of its literal: `-0` is P, NaN U, +/// `vector(time())` U (a time is a value the planner knows no sign of). +#[test] +fn vector_from_scalar_has_its_literals_sign() { + let mut memo = memo(); + let mut rows = Rows::default(); + for (shown, expr, expected) in [ + ("vector(1)", f64_literal(1.0), N), + ("vector(-1)", f64_literal(-1.0), P), + ("vector(0)", f64_literal(0.0), N), + ("vector(-0)", f64_literal(-0.0), P), + ("vector(NaN)", f64_literal(f64::NAN), U), + ("vector(+Inf)", f64_literal(f64::INFINITY), N), + ("vector(-Inf)", f64_literal(f64::NEG_INFINITY), P), + ("vector(time())", ScalarExpr::EvalTimestamp, U), + ] { + match try_add(&mut memo, vector_of(expr)) { + Ok(group) => rows.check(shown, sign_of(&memo, group, "value"), expected), + Err(error) => rows.fail(shown, format!("cannot build: {error}")), + } + } + rows.finish("T1.3"); +} + +// ── T1.4 ──────────────────────────────────────────────────────────────── + +/// Every `AggIntent` variant, through an exhaustive match (no `_` arm): a +/// new variant fails to compile here until it is given a row. Each comes +/// with its expected sign over an N, P and U input (columns in that order). +fn every_measure() -> Vec<(AggIntent, [ValueSign; 3], &'static str)> { + let col = Some(VALUE); + let mut rows = Vec::new(); + // The exhaustive witness: each variant names the rows below that cover it. + let witness = |measure: &AggIntent| -> &'static str { + use AggIntent::*; + match measure { + Count + | Cardinality { .. } + | CountValues { .. } + | Changes + | Resets + | Group + | Absent { .. } + | AbsentOverTime + | PresentOverTime + | StdDev { .. } + | Variance { .. } + | FrequencyL2 { .. } => "count-like: N", + FrequencyEntropy { .. } => "entropy: U", + Min { .. } | Max { .. } | FirstOverTime | LastOverTime => "keeps the sign", + Sum { .. } | Avg { .. } => "keeps the sign (Q1)", + Quantile { .. } => "keeps the sign inside [0, 1]", + Rate | IRate | Increase => "N over N", + Math(_) => "per math function", + TimeFn(_) => "per time function", + TsOfMinOverTime | TsOfMaxOverTime | TsOfFirstOverTime | TsOfLastOverTime => { + "timestamps: U" + } + PearsonCorr { .. } + | Delta + | IDelta + | Deriv + | PredictLinear { .. } + | DoubleExpSmoothing { .. } + | HistogramCount + | HistogramSum + | HistogramAvg + | HistogramStdDev + | HistogramStdVar + | HistogramFraction { .. } + | HistogramQuantile { .. } + | MadOverTime + | TopK { .. } + | Extension { .. } => "U", + } + }; + let all_n = [N, N, N]; + let keeps = [N, P, U]; + let none = [U, U, U]; + for measure in [ + AggIntent::Count, + AggIntent::Cardinality { cols: vec![VALUE] }, + AggIntent::CountValues { + label: "version".into(), + }, + AggIntent::Changes, + AggIntent::Resets, + AggIntent::Group, + AggIntent::Absent { + labels: BTreeMap::new(), + }, + AggIntent::AbsentOverTime, + AggIntent::PresentOverTime, + AggIntent::StdDev { + col, + population: true, + }, + AggIntent::Variance { + col, + population: true, + }, + AggIntent::FrequencyL2 { col }, + ] { + rows.push((measure, all_n, "N whatever the input")); + } + // User decision Q2: entropy is U. + rows.push((AggIntent::FrequencyEntropy { col }, none, "entropy")); + for measure in [ + AggIntent::Min { col }, + AggIntent::Max { col }, + AggIntent::FirstOverTime, + AggIntent::LastOverTime, + ] { + rows.push((measure, keeps, "a selected input value")); + } + // provisional answer Q1: sums start at -0.0, so a sum of P values is P + // (the executor fix); without it a sum of only -0.0 is +0.0. + rows.push((AggIntent::Sum { col }, keeps, "Q1")); + rows.push((AggIntent::Avg { col }, keeps, "Q1")); + for q in [0.0, 0.5, 1.0, -0.0] { + rows.push((AggIntent::Quantile { col, q }, keeps, "q in [0, 1]")); + } + for q in [1.5, -0.5, f64::NAN] { + rows.push(( + AggIntent::Quantile { col, q }, + none, + "q outside [0, 1]: ±Inf", + )); + } + for measure in [AggIntent::Rate, AggIntent::IRate, AggIntent::Increase] { + rows.push((measure, [N, U, U], "a decrease reads as a reset")); + } + for (function, expected) in math_function_rows() { + rows.push((AggIntent::Math(function), expected, "per T1.6")); + } + for (function, expected) in time_function_rows() { + rows.push((AggIntent::TimeFn(function), expected, "per T1.7")); + } + for measure in [ + AggIntent::TsOfMinOverTime, + AggIntent::TsOfMaxOverTime, + AggIntent::TsOfFirstOverTime, + AggIntent::TsOfLastOverTime, + ] { + rows.push((measure, none, "a timestamp")); + } + for measure in [ + AggIntent::PearsonCorr { + left: VALUE, + right: VALUE, + }, + AggIntent::Delta, + AggIntent::IDelta, + AggIntent::Deriv, + AggIntent::PredictLinear { seconds: 60.0 }, + AggIntent::DoubleExpSmoothing { + smoothing: 0.5, + trend: 0.5, + }, + AggIntent::HistogramCount, + AggIntent::HistogramSum, + AggIntent::HistogramAvg, + AggIntent::HistogramStdDev, + AggIntent::HistogramStdVar, + AggIntent::HistogramFraction { + lower: 0.0, + upper: 1.0, + }, + AggIntent::HistogramQuantile { q: 0.5, le: JOB }, + AggIntent::MadOverTime, + AggIntent::Extension { + ext_kind: "custom".into(), + payload: serde_json::Value::Null, + }, + ] { + rows.push((measure, none, "no rule")); + } + for (measure, _, _) in &rows { + let _ = witness(measure); + } + rows +} + +/// T1.4: one row per `AggIntent` variant over N, P and U inputs, so a rule +/// that returns a constant fails at least one row. A per-series range +/// function reads `X[1m]`; any other measure reads the instant `X` (the +/// first of the two the memo accepts). `TopK` alone ranks rows and is in +/// T1.2's sense a copy; as one of several measures it is U (below). +/// +/// Fails today: entropy is N, every `Math(f)` but `Abs` is U and every +/// `TimeFn` is U. +#[test] +fn every_measure_has_a_sign_rule() { + let mut memo = memo(); + let mut rows = Rows::default(); + for (measure, expected, why) in every_measure() { + for (input, expected) in SIGNS.into_iter().zip(expected) { + let scan = add(&mut memo, series(gauge_of(input))); + let window = add(&mut memo, range(MINUTE_MS, scan)); + let row = format!("{measure:?} over {} ({why})", gauge_of(input)); + let reduction = match measure { + AggIntent::Count + | AggIntent::Cardinality { .. } + | AggIntent::CountValues { .. } + | AggIntent::Group + | AggIntent::StdDev { .. } + | AggIntent::Variance { .. } + | AggIntent::FrequencyL2 { .. } + | AggIntent::FrequencyEntropy { .. } + | AggIntent::PearsonCorr { .. } => Reduction::by(vec![JOB]), + _ => Reduction::PerEntity, + }; + let group = try_add( + &mut memo, + aggregate(reduction.clone(), measure.clone(), window), + ) + .or_else(|_| try_add(&mut memo, aggregate(reduction, measure.clone(), scan))); + match group { + Ok(group) => rows.check(row, computed_value_sign(&memo, group), expected), + Err(error) => rows.fail(row, format!("cannot build: {error}")), + } + } + } + // TopK as one of several measures: U whatever the input. + for input in SIGNS { + let scan = add(&mut memo, series(gauge_of(input))); + let row = format!("[Sum, TopK] over {}", gauge_of(input)); + let several = Operator::NonASAP(NonASAPOp::Aggregate { + reduction: Reduction::by(vec![JOB]), + measures: vec![ + AggIntent::Sum { col: Some(VALUE) }, + AggIntent::TopK { k: 2 }, + ], + output_names: vec![String::new(), String::new()], + filters: vec![], + having: None, + child: scan, + }); + match try_add(&mut memo, several) { + Ok(group) => { + let last = *signs(&memo, group).last().unwrap(); + rows.check(format!("{row}: topk"), last, U); + } + // The memo may reject a top-k among other measures: then the row + // has nothing to derive. + Err(error) => println!("{row}: not a valid aggregate ({error})"), + } + } + rows.finish("T1.4"); +} + +/// T4 b07: entropy, as a measure and as a readout, has no sign (user +/// decision Q2): a one-item entropy evaluates `0.0 - (+0.0 * log2(1))`, +/// and an estimate (UnivMon) may give `-0.0`. +#[test] +fn b07_entropy_has_no_sign() { + let mut memo = memo(); + let scan = add(&mut memo, series("pos")); + let window = add(&mut memo, range(MINUTE_MS, scan)); + let measure = add( + &mut memo, + aggregate( + Reduction::by(vec![JOB]), + AggIntent::FrequencyEntropy { col: Some(VALUE) }, + scan, + ), + ); + let state = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::UnivMon), + window, + ), + ); + let read = add(&mut memo, readout(SketchStatistic::FrequencyEntropy, state)); + assert_eq!(computed_value_sign(&memo, measure), U, "measure"); + assert_eq!(computed_value_sign(&memo, read), U, "readout"); +} + +// ── T1.5 ──────────────────────────────────────────────────────────────── + +const OPERATORS: [ArithmeticOpKind; 7] = [ + ArithmeticOpKind::Add, + ArithmeticOpKind::Sub, + ArithmeticOpKind::Mul, + ArithmeticOpKind::Div, + ArithmeticOpKind::Mod, + ArithmeticOpKind::Pow, + ArithmeticOpKind::Atan2, +]; + +/// The sign of `l op r` over Float64 operands of signs `l` and `r`, derived +/// by hand from IEEE 754 (cells NN, NP, NU, PN, PP, PU, UN, UP, UU): +/// - `+`: `+0 + +0 = +0`, `-0 + -0 = -0`; mixed signs cancel. +/// - `-` is `l + (-r)`: `+0 - -0 = +0`, `-0 - +0 = -0`. +/// - `*`, `/`: the XOR of the sign bits (`+0 * -1 = -0`, `1 / -0 = -inf`). +/// - `%` (fmod): the dividend's sign, `fmod(-0, y) = -0`. +/// - `^` (powf): `powf(x ≥ +0, y) ≥ +0`, including `powf(+0, -1) = +inf`; +/// `powf(-2, 2) = 4` but `powf(-2, 3) = -8`. +/// - `atan2(l, r)`: the sign of `l`, including `atan2(±0, -1) = ±π`. +fn float_arithmetic(op: &ArithmeticOpKind) -> [ValueSign; 9] { + match op { + ArithmeticOpKind::Add => [N, U, U, U, P, U, U, U, U], + ArithmeticOpKind::Sub => [U, N, U, P, U, U, U, U, U], + ArithmeticOpKind::Mul | ArithmeticOpKind::Div => [N, P, U, P, N, U, U, U, U], + ArithmeticOpKind::Mod => [N, N, N, P, P, P, U, U, U], + ArithmeticOpKind::Pow => [N, N, N, U, U, U, U, U, U], + // provisional answer Q10: atan2 has its left operand's sign. + ArithmeticOpKind::Atan2 => [N, N, N, P, P, P, U, U, U], + } +} + +/// The same over Int64 operands, where an int `0` is N and an int P value +/// is `< 0` (provisional answer Q5): +/// - `-`: `0 - 0 = 0` and `1 - 2 = -1`, so NN is U; `0 - (-1) = 1` N; +/// `-1 - 0 = -1` P. +/// - `*`: `0 * -3 = 0` (N, not P), so NP and PN are U; `-2 * -3 = 6` N. +/// - `/`: `1 / -2 = 0` and `-1 / 2 = 0`, so NP and PN are U; `-1 / -2 = 0` +/// and `-4 / -2 = 2`, so PP is N. +/// - `%`: the dividend's sign, but `-4 % 2 = 0`, so a P dividend is U; +/// `5 % -3 = 2`, so an N dividend is N. +/// - `+`, `^` and `atan2` as for floats (`^` and `atan2` of ints are floats). +fn int_arithmetic(op: &ArithmeticOpKind) -> [ValueSign; 9] { + match op { + ArithmeticOpKind::Add => [N, U, U, U, P, U, U, U, U], + ArithmeticOpKind::Sub => [U, N, U, P, U, U, U, U, U], + ArithmeticOpKind::Mul => [N, U, U, U, N, U, U, U, U], + ArithmeticOpKind::Div => [N, U, U, U, N, U, U, U, U], + ArithmeticOpKind::Mod => [N, N, N, U, U, U, U, U, U], + ArithmeticOpKind::Pow => [N, N, N, U, U, U, U, U, U], + ArithmeticOpKind::Atan2 => [N, N, N, P, P, P, U, U, U], + } +} + +fn arithmetic_expr( + op: ArithmeticOpKind, + left: ScalarExpr, + right: ScalarExpr, + semantics: ExprSemantics, +) -> ScalarExpr { + ScalarExpr::Arithmetic { + op, + left: Box::new(left), + right: Box::new(right), + semantics, + } +} + +/// T1.5: 63 cells per operand type, each run through a vector `BinaryOp` +/// (`pos * neg`) and a scalar `Project` (`gain * loss` over `flows`); and +/// the Int64 cells through `Project` over `flows`' int columns. +/// +/// Fails today: `-`, `%`, `^` and `atan2` are U, and the Int rows follow +/// the float rules (F5). +#[test] +fn arithmetic_signs_for_every_operator_and_operand_sign() { + let mut memo = memo(); + let mut rows = Rows::default(); + let flows = add(&mut memo, flows()); + let float_column = |sign| match sign { + N => GAIN, + P => LOSS, + U => P_COL, + }; + let int_column = |sign| match sign { + N => BYTES, + P => DEBT, + U => DELTA, + }; + for op in OPERATORS { + let float = float_arithmetic(&op); + let int = int_arithmetic(&op); + let mut cell = 0; + for left in SIGNS { + for right in SIGNS { + let lhs = add(&mut memo, series(gauge_of(left))); + let rhs = add(&mut memo, series(gauge_of(right))); + let shown = format!("{} {op} {}", gauge_of(left), gauge_of(right)); + match try_add( + &mut memo, + binary_op(BinaryOpKind::Arithmetic(op.clone()), None, false, lhs, rhs), + ) { + Ok(group) => rows.check( + format!("vector {shown}"), + sign_of(&memo, group, "value"), + float[cell], + ), + Err(error) => { + rows.fail(format!("vector {shown}"), format!("cannot build: {error}")) + } + } + let expr = arithmetic_expr( + op.clone(), + ScalarExpr::Column(float_column(left)), + ScalarExpr::Column(float_column(right)), + ExprSemantics::Sql, + ); + rows.check( + format!("Float64 {left:?} {op} {right:?}"), + projected_sign(&mut memo, expr, flows), + float[cell], + ); + let expr = arithmetic_expr( + op.clone(), + ScalarExpr::Column(int_column(left)), + ScalarExpr::Column(int_column(right)), + ExprSemantics::Sql, + ); + // provisional answer Q5: Int64 results are type-aware. + rows.check( + format!("Int64 {left:?} {op} {right:?}"), + projected_sign(&mut memo, expr, flows), + int[cell], + ); + cell += 1; + } + } + } + rows.finish("T1.5"); +} + +/// T4 b06: `sum(rate(c_total[5m])) - min(neg)` is N - P = N. +/// +/// Fails today: the difference is U (the arithmetic wildcard). +#[test] +fn b06_a_difference_of_opposite_signs_has_a_sign() { + let mut memo = memo(); + let counter = add(&mut memo, series("c_total")); + let window = add(&mut memo, range(5 * MINUTE_MS, counter)); + let rate = add( + &mut memo, + aggregate(Reduction::PerEntity, AggIntent::Rate, window), + ); + let everything = Reduction::Reduce(GroupKeys::by(vec![])); + let sum = add( + &mut memo, + aggregate( + everything.clone(), + AggIntent::Sum { col: Some(VALUE) }, + rate, + ), + ); + let neg = add(&mut memo, series("neg")); + let min = add( + &mut memo, + aggregate(everything, AggIntent::Min { col: Some(VALUE) }, neg), + ); + let difference = add( + &mut memo, + binary_op( + BinaryOpKind::Arithmetic(ArithmeticOpKind::Sub), + None, + false, + sum, + min, + ), + ); + assert_eq!(computed_value_sign(&memo, sum), N, "sum(rate(c_total))"); + assert_eq!(computed_value_sign(&memo, min), P, "min(neg)"); + assert_eq!(*signs(&memo, difference).last().unwrap(), N, "difference"); +} + +/// T4 b05: a quantile outside [0, 1] has no sign; `pos / 1000` keeps N. +#[test] +fn b05_quantile_outside_the_unit_interval_and_a_scaled_value() { + let mut memo = memo(); + let scan = add(&mut memo, series("pos")); + let window = add(&mut memo, range(5 * MINUTE_MS, scan)); + let quantile = add( + &mut memo, + aggregate( + Reduction::PerEntity, + AggIntent::Quantile { + col: Some(VALUE), + q: 1.5, + }, + window, + ), + ); + assert_eq!(computed_value_sign(&memo, quantile), U); + let scaled = mapped_value_sign( + &mut memo, + N, + arithmetic_expr( + ArithmeticOpKind::Div, + ScalarExpr::Column(VALUE), + f64_literal(1000.0), + ExprSemantics::Promql, + ), + ); + assert_eq!(scaled, N); +} + +/// T4 f5: SQL `-bytes` over `bytes = 0` is the int `0`, which is N, not P: +/// the negation of an int N column has no sign (provisional answer Q5). +/// The same for `bytes * -1` and `bytes / -2`, and for `-COUNT(p)`. +/// +/// Fails today: each is labelled P. +#[test] +fn f5_integer_negation_of_zero_has_no_sign() { + let mut memo = memo(); + let flows = add(&mut memo, flows()); + let int = |value| ScalarExpr::Literal(ScalarValue::Int64(value)); + let mut rows = Rows::default(); + for (shown, expr) in [ + ( + "-bytes", + ScalarExpr::Negative { + expr: column(BYTES), + semantics: ExprSemantics::Sql, + }, + ), + ( + "bytes * -1", + arithmetic_expr( + ArithmeticOpKind::Mul, + ScalarExpr::Column(BYTES), + int(-1), + ExprSemantics::Sql, + ), + ), + ( + "bytes / -2", + arithmetic_expr( + ArithmeticOpKind::Div, + ScalarExpr::Column(BYTES), + int(-2), + ExprSemantics::Sql, + ), + ), + ] { + // provisional answer Q5 + rows.check(shown, projected_sign(&mut memo, expr, flows), U); + } + let count = add( + &mut memo, + aggregate( + Reduction::Reduce(GroupKeys::by(vec![])), + AggIntent::Count, + flows, + ), + ); + let count_column = properties(&memo, count).schema.fields.len() - 1; + let negated = ScalarExpr::Negative { + expr: column(count_column), + semantics: ExprSemantics::Sql, + }; + let count_is_int = properties(&memo, count).schema.fields[count_column].dtype + == FieldDataType::Plain(DataType::Int64); + // A float count's negation is P (`-(+0.0) = -0.0`); an int count's is U. + rows.check( + format!("-COUNT(p) (count is Int64: {count_is_int})"), + projected_sign(&mut memo, negated, count), + if count_is_int { U } else { P }, + ); + rows.finish("f5"); +} + +/// T4 b15: `CAST(p AS BIGINT)` of a value in (-1, 0) is the int `0`, which +/// is N: an integer cast of P values has no sign. +/// +/// Fails today: the cast keeps P. +#[test] +fn b15_an_integer_cast_of_non_positive_values_has_no_sign() { + let mut memo = memo(); + let flows = add(&mut memo, flows()); + let cast = ScalarExpr::Cast { + expr: column(LOSS), + to: DataType::Int64, + try_cast: false, + }; + assert_eq!(projected_sign(&mut memo, cast, flows), U); +} + +// ── T1.6 ──────────────────────────────────────────────────────────────── + +/// The sign of `f(v)` over v of sign N, P, U, by hand from IEEE 754 and the +/// executor's formulas: +/// - `acos(1) = acosh(1) = +0`; `exp`, `cosh` ≥ 1 or ≥ +0. +/// - `ceil(-0.5) = -0`, `floor(+0.5) = +0`, `sqrt(-0) = -0`, +/// `asinh` and `atanh(-0) = 0.5·ln_1p(-0) = -0` keep the sign bit. +/// - `round(v, to) = floor(v/to + 0.5)·to`: `round(-0.3) = +0`, so a P input +/// has no sign; `to < 0`: `round(+0, -1) = floor(+0.5)·-1 = -0`. +/// - clamp is `max(min, min(max, v))`, Go's signed zeros: +/// `max(+0, -0) = +0`, `min(+0, -0) = -0`. +fn math_function_rows() -> Vec<(MathFunc, [ValueSign; 3])> { + use MathFunc::*; + let all_n = [N, N, N]; + let keeps = [N, P, U]; + let none = [U, U, U]; + let mut rows = Vec::new(); + for f in [Abs, Exp, Acos, Cosh, Acosh] { + rows.push((f, all_n)); + } + for f in [ + Ceil, Floor, Sqrt, Asin, Atan, Sinh, Tanh, Asinh, Atanh, Deg, Rad, + ] { + rows.push((f, keeps)); + } + // provisional answer Q2: the executor returns `x` for a zero, so + // `sgn(-0) = -0` and Sgn keeps the sign. + rows.push((Sgn, keeps)); + for f in [Ln, Log2, Log10, Sin, Cos, Tan] { + rows.push((f, none)); + } + for to_nearest in [1.0, 0.5] { + rows.push((Round { to_nearest }, [N, U, U])); + } + for to_nearest in [-1.0, 0.0, f64::NAN] { + rows.push((Round { to_nearest }, none)); + } + // provisional answer Q3: every `*` cell of the plan's T1.6 table needs + // the executor to resolve signed zeros as Go does. + rows.push(( + Clamp { + min: 0.0, + max: 100.0, + }, + all_n, + )); + rows.push(( + Clamp { + min: -100.0, + max: -1.0, + }, + [P, P, P], + )); + rows.push(( + Clamp { + min: -5.0, + max: 5.0, + }, + keeps, + )); + rows.push(( + Clamp { + min: -0.0, + max: 5.0, + }, + keeps, + )); + rows.push(( + Clamp { + min: 0.0, + max: -0.0, + }, + none, + )); + rows.push((ClampMin { min: 0.0 }, all_n)); + rows.push((ClampMin { min: 5.0 }, all_n)); + rows.push((ClampMin { min: -5.0 }, keeps)); + rows.push((ClampMin { min: -0.0 }, keeps)); + rows.push((ClampMax { max: -1.0 }, [P, P, P])); + rows.push((ClampMax { max: -0.0 }, [P, P, P])); + rows.push((ClampMax { max: 5.0 }, keeps)); + rows.push((ClampMax { max: 0.0 }, keeps)); + // The exhaustive witness: a new variant fails to compile here until it + // is given a row above. + for (function, _) in &rows { + match function { + Abs + | Ceil + | Floor + | Exp + | Ln + | Log2 + | Log10 + | Sqrt + | Sgn + | Sin + | Cos + | Tan + | Asin + | Acos + | Atan + | Sinh + | Cosh + | Tanh + | Asinh + | Acosh + | Atanh + | Deg + | Rad + | Round { .. } + | Clamp { .. } + | ClampMin { .. } + | ClampMax { .. } => {} + } + } + rows +} + +/// PromQL's name and literal arguments for `function`: +/// `ClampMin { min: 0 }` → `promql_clamp_min(value, 0)`. +fn promql_call(function: &MathFunc) -> (String, Vec) { + use MathFunc::*; + let (name, literals) = match function { + Abs => ("abs", vec![]), + Ceil => ("ceil", vec![]), + Floor => ("floor", vec![]), + Exp => ("exp", vec![]), + Ln => ("ln", vec![]), + Log2 => ("log2", vec![]), + Log10 => ("log10", vec![]), + Sqrt => ("sqrt", vec![]), + Sgn => ("sgn", vec![]), + Sin => ("sin", vec![]), + Cos => ("cos", vec![]), + Tan => ("tan", vec![]), + Asin => ("asin", vec![]), + Acos => ("acos", vec![]), + Atan => ("atan", vec![]), + Sinh => ("sinh", vec![]), + Cosh => ("cosh", vec![]), + Tanh => ("tanh", vec![]), + Asinh => ("asinh", vec![]), + Acosh => ("acosh", vec![]), + Atanh => ("atanh", vec![]), + Deg => ("deg", vec![]), + Rad => ("rad", vec![]), + Round { to_nearest } => ("round", vec![*to_nearest]), + Clamp { min, max } => ("clamp", vec![*min, *max]), + ClampMin { min } => ("clamp_min", vec![*min]), + ClampMax { max } => ("clamp_max", vec![*max]), + }; + (format!("promql_{name}"), literals) +} + +fn call(name: &str, args: Vec>) -> ScalarExpr { + ScalarExpr::FunctionCall { + name: name.into(), + args, + } +} + +fn promql_call_expr(function: &MathFunc) -> ScalarExpr { + let (name, literals) = promql_call(function); + let args = std::iter::once(ScalarExpr::Column(VALUE)) + .chain(literals.into_iter().map(f64_literal)) + .collect(); + call(&name, args) +} + +/// T1.6: every math function, built as PromQL lowers it, +/// `FunctionCall "promql_"(value, literals…)`, so the row also exercises +/// `math_function_of_call`; plus SQL `abs`/`ABS`, a non-literal bound and +/// an unknown name. +/// +/// Fails today: every function but `abs` is U. +#[test] +fn every_math_function_has_a_sign_rule() { + let mut memo = memo(); + let mut rows = Rows::default(); + for (function, expected) in math_function_rows() { + for (input, expected) in SIGNS.into_iter().zip(expected) { + let expr = promql_call_expr(&function); + let row = format!("{function:?} over {}", gauge_of(input)); + rows.check(row, mapped_value_sign(&mut memo, input, expr), expected); + } + } + for name in ["abs", "ABS"] { + let expr = call(name, vec![ScalarExpr::Column(VALUE)]); + rows.check( + format!("SQL {name}(mixed)"), + mapped_value_sign(&mut memo, U, expr), + N, + ); + } + let non_literal = call( + "promql_clamp_min", + vec![ScalarExpr::Column(VALUE), ScalarExpr::EvalTimestamp], + ); + rows.check( + "promql_clamp_min(mixed, time())", + mapped_value_sign(&mut memo, U, non_literal), + U, + ); + // A registered function that is no math function (the memo rejects an + // unregistered name such as `foo`): `nullif(value, 1)` over `mixed`. + let other = call("nullif", vec![ScalarExpr::Column(VALUE), f64_literal(1.0)]); + rows.check( + "nullif(mixed, 1)", + mapped_value_sign(&mut memo, U, other), + U, + ); + rows.finish("T1.6"); +} + +/// The date functions over v of sign N, P, U: minute … days_in_month are in +/// [0, 366] for any finite v; `year(v)` is negative for +/// `v < -62135596800` (before year 1), so it is N over N only +/// (provisional answer Q4). A non-finite v gives NaN. +fn time_function_rows() -> Vec<(TimeFunc, [ValueSign; 3])> { + use TimeFunc::*; + let witness = |function: &TimeFunc| match function { + Timestamp | Minute | Hour | DayOfWeek | DayOfMonth | DayOfYear | Month | Year + | DaysInMonth => {} + }; + let rows = vec![ + (Minute, [N, N, N]), + (Hour, [N, N, N]), + (DayOfWeek, [N, N, N]), + (DayOfMonth, [N, N, N]), + (DayOfYear, [N, N, N]), + (Month, [N, N, N]), + (DaysInMonth, [N, N, N]), + // provisional answer Q4 + (Year, [N, U, U]), + ]; + rows.iter().for_each(|(f, _)| witness(f)); + rows +} + +fn time_function_name(function: TimeFunc) -> &'static str { + use TimeFunc::*; + match function { + Timestamp => "timestamp", + Minute => "minute", + Hour => "hour", + DayOfWeek => "day_of_week", + DayOfMonth => "day_of_month", + DayOfYear => "day_of_year", + Month => "month", + Year => "year", + DaysInMonth => "days_in_month", + } +} + +/// T1.7: every date function, built as `FunctionCall "promql_"(value)`. +/// +/// Fails today: each is U (only the year over P/U rows pass). +#[test] +fn every_time_function_has_a_sign_rule() { + let mut memo = memo(); + let mut rows = Rows::default(); + for (function, expected) in time_function_rows() { + for (input, expected) in SIGNS.into_iter().zip(expected) { + let name = format!("promql_{}", time_function_name(function)); + let expr = call(&name, vec![ScalarExpr::Column(VALUE)]); + rows.check( + format!("{name} over {}", gauge_of(input)), + mapped_value_sign(&mut memo, input, expr), + expected, + ); + } + } + rows.finish("T1.7"); +} + +/// T4 f4: `year(v)` at `v = -1e11` is year -1199, so `year(neg)` has no +/// sign (provisional answer Q4). +#[test] +fn f4_year_before_year_one_is_negative() { + let mut memo = memo(); + let expr = call("promql_year", vec![ScalarExpr::Column(VALUE)]); + assert_eq!(mapped_value_sign(&mut memo, P, expr), U); +} + +// ── T1.8 ──────────────────────────────────────────────────────────────── + +/// T1.8: every `ScalarExpr` variant (an exhaustive match below), over +/// `flows`. +/// +/// Fails today: a cast of P to an integer keeps P (B15) and the negation of +/// an int N column is P (F5). +#[test] +fn every_scalar_expression_has_a_sign_rule() { + let mut memo = memo(); + let mut rows = Rows::default(); + let flows = add(&mut memo, flows()); + // The sub-query variants read a group: `t_pos`'s one column, and + // `scalar(v)` the instant vector `pos`. + let t_pos = add(&mut memo, table("t_pos", &[("v", DataType::Float64)])); + let pos_series = add(&mut memo, series("pos")); + let float_column = |sign| match sign { + N => GAIN, + P => LOSS, + U => P_COL, + }; + let int_column = |sign| match sign { + N => BYTES, + P => DEBT, + U => DELTA, + }; + let mut row = |rows: &mut Rows, shown: String, expr: ScalarExpr, expected| { + let witness = |expr: &ScalarExpr| match expr { + ScalarExpr::Column(_) + | ScalarExpr::Literal(_) + | ScalarExpr::Negative { .. } + | ScalarExpr::Compare { .. } + | ScalarExpr::BoolAnd(_) + | ScalarExpr::BoolOr(_) + | ScalarExpr::Not(_) + | ScalarExpr::IsNull(_) + | ScalarExpr::IsNotNull(_) + | ScalarExpr::Cast { .. } + | ScalarExpr::InList { .. } + | ScalarExpr::FunctionCall { .. } + | ScalarExpr::Arithmetic { .. } + | ScalarExpr::Case { .. } + | ScalarExpr::CurrentTimestamp + | ScalarExpr::EvalTimestamp + | ScalarExpr::PromqlScalarFromVector(_) + | ScalarExpr::ScalarSubquery(_) + | ScalarExpr::Exists { .. } + | ScalarExpr::InSubquery { .. } => {} + }; + witness(&expr); + let project = Operator::NonASAP(NonASAPOp::Project { + cols: vec![ProjectItem { + alias: Some("v".into()), + expr, + }], + child: flows, + }); + match try_add(&mut memo, project) { + Ok(group) => rows.check(shown, signs(&memo, group)[0], expected), + Err(error) => rows.fail(shown, format!("cannot build: {error}")), + } + }; + for sign in SIGNS { + row( + &mut rows, + format!("Column float {sign:?}"), + ScalarExpr::Column(float_column(sign)), + sign, + ); + row( + &mut rows, + format!("Column int {sign:?}"), + ScalarExpr::Column(int_column(sign)), + sign, + ); + } + for (value, expected) in [ + (1.0, N), + (-1.0, P), + (0.0, N), + (-0.0, P), + (f64::NAN, U), + (f64::INFINITY, N), + (f64::NEG_INFINITY, P), + ] { + row( + &mut rows, + format!("Float64 literal {value:?}"), + f64_literal(value), + expected, + ); + } + for (value, expected) in [(0, N), (-3, P), (7, N)] { + row( + &mut rows, + format!("Int64 literal {value}"), + ScalarExpr::Literal(ScalarValue::Int64(value)), + expected, + ); + } + for (shown, literal) in [ + ("Utf8", ScalarValue::Utf8("a".into())), + ("Boolean", ScalarValue::Boolean(true)), + ("Null", ScalarValue::Null), + ("Timestamp", ScalarValue::Timestamp(0)), + ] { + row( + &mut rows, + format!("{shown} literal"), + ScalarExpr::Literal(literal), + U, + ); + } + let negative = |expr| ScalarExpr::Negative { + expr: Box::new(expr), + semantics: ExprSemantics::Sql, + }; + for (sign, expected) in [(N, P), (P, N), (U, U)] { + row( + &mut rows, + format!("Negative float {sign:?}"), + negative(ScalarExpr::Column(float_column(sign))), + expected, + ); + } + // provisional answer Q5: `-0` of an int is `0`, which is N. + for (sign, expected) in [(N, U), (P, N), (U, U)] { + row( + &mut rows, + format!("Negative int {sign:?}"), + negative(ScalarExpr::Column(int_column(sign))), + expected, + ); + } + let cast = |expr, to| ScalarExpr::Cast { + expr: Box::new(expr), + to, + try_cast: false, + }; + for sign in SIGNS { + row( + &mut rows, + format!("Cast float {sign:?} to Float64"), + cast(ScalarExpr::Column(float_column(sign)), DataType::Float64), + sign, + ); + row( + &mut rows, + format!("Cast int {sign:?} to Float64"), + cast(ScalarExpr::Column(int_column(sign)), DataType::Float64), + sign, + ); + row( + &mut rows, + format!("Cast float {sign:?} to Utf8"), + cast(ScalarExpr::Column(float_column(sign)), DataType::Utf8), + U, + ); + } + // B15: `CAST(-0.5 AS BIGINT)` is the int 0, which is N. + for (sign, expected) in [(N, N), (P, U), (U, U)] { + row( + &mut rows, + format!("Cast float {sign:?} to Int64"), + cast(ScalarExpr::Column(float_column(sign)), DataType::Int64), + expected, + ); + } + let is_positive = |column| ScalarExpr::Compare { + left: Box::new(ScalarExpr::Column(column)), + op: CompareOpKind::Gt, + right: Box::new(f64_literal(0.0)), + semantics: ExprSemantics::Sql, + }; + let case = |branches: Vec<(ScalarExpr, ScalarExpr)>, + else_expr: Option>| ScalarExpr::Case { + operand: None, + branches, + else_expr: else_expr.map(Box::new), + }; + // provisional answer Q10: a Case has the sign its branches agree on; + // provisional answer Q6: a missing ELSE is NULL, which is excluded. + row( + &mut rows, + "CASE WHEN p > 0 THEN gain ELSE bytes END".into(), + case( + vec![(is_positive(P_COL), ScalarExpr::Column(GAIN))], + Some(ScalarExpr::Column(BYTES)), + ), + N, + ); + row( + &mut rows, + "CASE WHEN p > 0 THEN loss ELSE debt END".into(), + case( + vec![(is_positive(P_COL), ScalarExpr::Column(LOSS))], + Some(ScalarExpr::Column(DEBT)), + ), + P, + ); + row( + &mut rows, + "CASE WHEN p > 0 THEN gain ELSE loss END".into(), + case( + vec![(is_positive(P_COL), ScalarExpr::Column(GAIN))], + Some(ScalarExpr::Column(LOSS)), + ), + U, + ); + row( + &mut rows, + "CASE WHEN p > 0 THEN loss END".into(), + case(vec![(is_positive(P_COL), ScalarExpr::Column(LOSS))], None), + P, + ); + let boolean = || is_positive(P_COL); + for (shown, expr) in [ + ("Compare p > 0", boolean()), + ("BoolAnd", ScalarExpr::BoolAnd(vec![boolean(), boolean()])), + ("BoolOr", ScalarExpr::BoolOr(vec![boolean(), boolean()])), + ("Not", ScalarExpr::Not(Box::new(boolean()))), + ("IsNull", ScalarExpr::IsNull(column(GAIN))), + ("IsNotNull", ScalarExpr::IsNotNull(column(GAIN))), + ( + "InList", + ScalarExpr::InList { + expr: column(GAIN), + list: vec![f64_literal(1.0)], + negated: false, + }, + ), + ("CurrentTimestamp", ScalarExpr::CurrentTimestamp), + ("EvalTimestamp", ScalarExpr::EvalTimestamp), + ] { + row(&mut rows, shown.into(), expr, U); + } + for (shown, expr) in [ + ("ScalarSubquery(t_pos)", ScalarExpr::ScalarSubquery(t_pos)), + ( + "Exists(t_pos)", + ScalarExpr::Exists { + subquery: t_pos, + negated: false, + }, + ), + ( + "InSubquery(gain, t_pos)", + ScalarExpr::InSubquery { + expr: column(GAIN), + subquery: t_pos, + negated: false, + }, + ), + ( + "PromqlScalarFromVector(pos)", + ScalarExpr::PromqlScalarFromVector(pos_series), + ), + ] { + row(&mut rows, shown.into(), expr, U); + } + rows.finish("T1.8"); +} + +// ── T1.9 ──────────────────────────────────────────────────────────────── + +/// T1.9: vector binary operators: a filter keeps the left side's values, a +/// union keeps what both agree on, a `bool` comparison is 0 or 1, and +/// `group_right` takes the right ("many") side. +#[test] +fn binary_op_values_and_labels() { + let mut memo = memo(); + let mut rows = Rows::default(); + let pos = add(&mut memo, series("pos")); + let neg = add(&mut memo, series("neg")); + let mixed = add(&mut memo, series("mixed")); + let one = add(&mut memo, vector_of(f64_literal(1.0))); + let minus_one = add(&mut memo, vector_of(f64_literal(-1.0))); + let zero = add(&mut memo, vector_of(f64_literal(0.0))); + let set = |kind| BinaryOpKind::Set(kind); + for (shown, operator, expected) in [ + ( + "pos > 1", + binary_op( + BinaryOpKind::Compare(CompareOpKind::Gt), + None, + false, + pos, + one, + ), + N, + ), + ( + "neg > -1", + binary_op( + BinaryOpKind::Compare(CompareOpKind::Gt), + None, + false, + neg, + minus_one, + ), + P, + ), + ( + "pos and neg", + binary_op(set(PromQLVectorSetOpKind::And), None, false, pos, neg), + N, + ), + ( + "neg unless pos", + binary_op(set(PromQLVectorSetOpKind::Unless), None, false, neg, pos), + P, + ), + ( + "pos or neg", + binary_op(set(PromQLVectorSetOpKind::Or), None, false, pos, neg), + U, + ), + ( + "pos or pos", + binary_op(set(PromQLVectorSetOpKind::Or), None, false, pos, pos), + N, + ), + // `a > bool b` between vectors lowers to `Compare` with + // `return_bool`: its value is 0 or 1, so N whatever the left side + // (`neg > bool -1` is `+0.0` or `1.0`, never P). + ( + "mixed > bool 0", + binary_op( + BinaryOpKind::Compare(CompareOpKind::Gt), + None, + true, + mixed, + zero, + ), + N, + ), + ( + "neg > bool -1", + binary_op( + BinaryOpKind::Compare(CompareOpKind::Gt), + None, + true, + neg, + minus_one, + ), + N, + ), + ( + "CompareBool: mixed > 0", + binary_op( + BinaryOpKind::CompareBool(CompareOpKind::Gt), + None, + false, + mixed, + zero, + ), + N, + ), + ( + "neg * on(job) group_right pos", + binary_op( + BinaryOpKind::Arithmetic(ArithmeticOpKind::Mul), + Some(VectorMatch { + kind: VectorMatchKind::On, + labels: vec!["job".into()], + grouping: Some(VectorGrouping { + side: GroupSide::Right, + labels: vec![], + }), + }), + false, + neg, + pos, + ), + P, + ), + ] { + match try_add(&mut memo, operator) { + Ok(group) => { + rows.check( + format!("{shown}: value"), + sign_of(&memo, group, "value"), + expected, + ); + if let Some(job) = properties(&memo, group).schema.column_id("job") { + rows.check(format!("{shown}: job"), signs(&memo, group)[job], U); + } + } + Err(error) => rows.fail(shown, format!("cannot build: {error}")), + } + } + rows.finish("T1.9"); +} + +// ── T1.10 ─────────────────────────────────────────────────────────────── + +fn weighted_state( + algorithm: SketchAlgorithm, + weight: SummaryInputExpr, + child: MemoGroupId, +) -> Operator { + Operator::ASAP(ASAPOp::SummaryAgg { + child, + summary_type: FieldDataType::Sketch(algorithm), + input: SummaryUpdate { + item: Some(SummaryInputExpr::Column(ColumnRef::Named("job".into()))), + weight, + }, + reduction: Reduction::PerEntity, + filter: None, + }) +} + +/// The state column of `group`: its first non-plain field. +fn state_sign(memo: &Memo, group: MemoGroupId) -> ValueSign { + let properties = properties(memo, group); + let state = properties + .schema + .fields + .iter() + .position(|field| !field.is_plain()) + .expect("a state column"); + properties.column_signs[state] +} + +/// The exhaustive witness over `FieldDataType` (provisional answer Q9): a +/// new state kind fails to compile here until a finalize/readout rule is +/// written for it. +fn field_data_type_witness(dtype: &FieldDataType) { + match dtype { + FieldDataType::Plain(_) + | FieldDataType::ExactAggregate(..) + | FieldDataType::Sketch(_) + | FieldDataType::SummarySample(..) + | FieldDataType::Wavelet(..) + | FieldDataType::StatModel(..) => {} + } +} + +/// T1.10: a state records its weight's sign; readouts and finalized +/// accumulators derive theirs; merges keep what their inputs agree on. +/// +/// Fails today: the entropy readout is N. +#[test] +fn summary_states_and_readouts() { + let mut memo = memo(); + let mut rows = Rows::default(); + let windows: Vec<(ValueSign, MemoGroupId)> = SIGNS + .into_iter() + .map(|sign| { + let scan = add(&mut memo, series(gauge_of(sign))); + (sign, add(&mut memo, range(MINUTE_MS, scan))) + }) + .collect(); + for &(sign, window) in &windows { + let state = add( + &mut memo, + weighted_state( + SketchAlgorithm::Cms, + SummaryInputExpr::Column(ColumnRef::SampleValue), + window, + ), + ); + rows.check( + format!("Cms state weighted by value of {sign:?}"), + state_sign(&memo, state), + sign, + ); + } + let (_, pos_window) = windows[0]; + for (weight, expected) in [ + (SummaryInputExpr::Constant(1.0), N), + (SummaryInputExpr::Constant(-1.0), P), + (SummaryInputExpr::Constant(-0.0), P), + ( + SummaryInputExpr::Tuple(vec![SummaryInputExpr::Constant(1.0)]), + U, + ), + ] { + let shown = format!("Cms state weighted by {weight:?}"); + match try_add( + &mut memo, + weighted_state(SketchAlgorithm::Cms, weight, pos_window), + ) { + Ok(state) => rows.check(shown, state_sign(&memo, state), expected), + Err(error) => rows.fail(shown, format!("cannot build: {error}")), + } + } + // Readouts over KLL / HLL / UnivMon states of each input sign. + for &(sign, window) in &windows { + let kll = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::Kll), + window, + ), + ); + for (q, expected) in [(0.9, sign), (1.5, U)] { + let read = add(&mut memo, readout(SketchStatistic::Quantile { q }, kll)); + rows.check( + format!("KLL quantile {q} over {sign:?}"), + computed_value_sign(&memo, read), + expected, + ); + } + let hll = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::Hll), + window, + ), + ); + let read = add(&mut memo, readout(SketchStatistic::Cardinality, hll)); + rows.check( + format!("HLL cardinality over {sign:?}"), + computed_value_sign(&memo, read), + N, + ); + let univmon = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::UnivMon), + window, + ), + ); + let read = add(&mut memo, readout(SketchStatistic::FrequencyL2, univmon)); + rows.check( + format!("UnivMon L2 over {sign:?}"), + computed_value_sign(&memo, read), + N, + ); + // User decision Q2. + let read = add( + &mut memo, + readout(SketchStatistic::FrequencyEntropy, univmon), + ); + rows.check( + format!("UnivMon entropy over {sign:?}"), + computed_value_sign(&memo, read), + U, + ); + for (algorithm, expected) in [ + (SketchAlgorithm::Cms, sign), + (SketchAlgorithm::CountSketch, U), + ] { + let state = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(algorithm), + window, + ), + ); + let read = add( + &mut memo, + readout( + SketchStatistic::PointCount { + key: ColumnRef::Named("job".into()), + value: Some("a".into()), + }, + state, + ), + ); + rows.check( + format!("{algorithm:?} point count over {sign:?}"), + computed_value_sign(&memo, read), + expected, + ); + } + } + // Finalized exact accumulators: the aggregate's sign over the state's. + for &(sign, window) in &windows { + for (kind, expected) in [ + (ExactKind::Sum, sign), + (ExactKind::Min, sign), + (ExactKind::Max, sign), + (ExactKind::Count, N), + (ExactKind::Rate, if sign == N { N } else { U }), + (ExactKind::Increase, if sign == N { N } else { U }), + (ExactKind::IRate, if sign == N { N } else { U }), + ] { + // provisional answer Q1 for the Sum over P row. + let state = add( + &mut memo, + asap_types::test_support::memo_fixtures::state(FieldDataType::exact(kind), window), + ); + field_data_type_witness(&properties(&memo, state).schema.fields[VALUE].dtype); + let finalized = add( + &mut memo, + Operator::ASAP(ASAPOp::FinalizeExactAccumulator { child: state }), + ); + rows.check( + format!("Finalize {kind:?} over {sign:?}"), + signs(&memo, finalized)[VALUE], + expected, + ); + } + } + // Merges of one definition (the memo rejects a merge of two sources, + // `Coverage(DefinitionMismatch)`, so a merge of N with P states cannot be + // built): N with N is N. + let kll_of = |memo: &mut Memo, window| { + add( + memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::Kll), + window, + ), + ) + }; + let pos_kll = kll_of(&mut memo, windows[0].1); + let pos_shifted = { + let scan = add(&mut memo, series("pos")); + let shifted = add(&mut memo, shift(MINUTE_MS, None, scan)); + let window = add(&mut memo, range(MINUTE_MS, shifted)); + kll_of(&mut memo, window) + }; + for (shown, children, expected) in [( + "SummaryMerge(pos, pos offset 1m)", + vec![pos_kll, pos_shifted], + N, + )] { + match try_add( + &mut memo, + Operator::ASAP(ASAPOp::SummaryMerge { + children, + group_by: None, + }), + ) { + Ok(group) => rows.check(shown, state_sign(&memo, group), expected), + Err(error) => rows.fail(shown, format!("cannot build: {error}")), + } + } + rows.finish("T1.10"); +} + +/// T4 b09: a finalized rate over P values has no sign: samples -5 then -3 +/// are an increase of 2, samples -3 then -5 a reset and an increase of -5. +#[test] +fn b09_a_finalized_rate_over_non_positive_values_has_no_sign() { + let mut memo = memo(); + let scan = add(&mut memo, series("neg")); + let window = add(&mut memo, range(MINUTE_MS, scan)); + let state = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::exact(ExactKind::Rate), + window, + ), + ); + let finalized = add( + &mut memo, + Operator::ASAP(ASAPOp::FinalizeExactAccumulator { child: state }), + ); + assert_eq!(signs(&memo, finalized)[VALUE], U); +} + +/// T4 b03: a quantile read from KLL panes merged over `pos` keeps N. +#[test] +fn b03_a_quantile_of_merged_panes_keeps_its_sign() { + let mut memo = memo(); + let scan = add(&mut memo, series("pos")); + let window = add(&mut memo, range(MINUTE_MS, scan)); + let shifted = add(&mut memo, shift(MINUTE_MS, None, scan)); + let earlier = add(&mut memo, range(MINUTE_MS, shifted)); + let kll = |memo: &mut Memo, window| { + add( + memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::Kll), + window, + ), + ) + }; + let a = kll(&mut memo, window); + let b = kll(&mut memo, earlier); + let merged = add( + &mut memo, + Operator::ASAP(ASAPOp::SummaryMerge { + children: vec![a, b], + group_by: None, + }), + ); + let read = add( + &mut memo, + readout(SketchStatistic::Quantile { q: 0.9 }, merged), + ); + assert_eq!(computed_value_sign(&memo, read), N); +} + +/// T4 b01: `rate(mixed[5m])` has no sign: a gauge's decrease reads as a +/// reset. +#[test] +fn b01_rate_of_an_undeclared_gauge_has_no_sign() { + let mut memo = memo(); + let scan = add(&mut memo, series("mixed")); + let window = add(&mut memo, range(5 * MINUTE_MS, scan)); + let rate = add( + &mut memo, + aggregate(Reduction::PerEntity, AggIntent::Rate, window), + ); + assert_eq!(computed_value_sign(&memo, rate), U); +} + +/// T4 b10 (sign part): `-quantile_over_time(0.9, pos[5m])` is P and its +/// `sum by (job)` keeps P (provisional answer Q1). +#[test] +fn b10_negation_keeps_its_sign_through_a_sum() { + let mut memo = memo(); + let scan = add(&mut memo, series("pos")); + let window = add(&mut memo, range(5 * MINUTE_MS, scan)); + let quantile = add( + &mut memo, + aggregate( + Reduction::PerEntity, + AggIntent::Quantile { + col: Some(VALUE), + q: 0.9, + }, + window, + ), + ); + let negated = add( + &mut memo, + map_value( + ScalarExpr::Negative { + expr: column(VALUE), + semantics: ExprSemantics::Promql, + }, + quantile, + ), + ); + let sum = add( + &mut memo, + aggregate( + Reduction::by(vec![JOB]), + AggIntent::Sum { col: Some(VALUE) }, + negated, + ), + ); + assert_eq!(signs(&memo, negated)[VALUE], P); + assert_eq!(computed_value_sign(&memo, sum), P); +} + +/// T4 b10 (key part): PromQL `abs`, `promql_abs` and SQL `ABS` all parse to +/// `MathFunc::Abs`, the key the error contract also matches on. +/// +/// Fails today: `math_function_of_call` is not implemented. +#[test] +fn b10_abs_spellings_parse_to_one_key() { + for name in ["abs", "ABS", "promql_abs"] { + let args: Vec> = vec![ScalarExpr::Column(VALUE)]; + assert_eq!( + math_function_of_call(name, &args), + Some(MathFunc::Abs), + "{name}" + ); + } +} + +/// T4 b11 (sign part): `q(pos) / q(neg)` is P, the sign of the exact values. +#[test] +fn b11_quotient_sign_is_of_exact_values() { + let mut memo = memo(); + let quantile = |memo: &mut Memo, metric| { + let scan = add(memo, series(metric)); + let window = add(memo, range(5 * MINUTE_MS, scan)); + add( + memo, + aggregate( + Reduction::PerEntity, + AggIntent::Quantile { + col: Some(VALUE), + q: 0.9, + }, + window, + ), + ) + }; + let numerator = quantile(&mut memo, "pos"); + let denominator = quantile(&mut memo, "neg"); + let quotient = add( + &mut memo, + binary_op( + BinaryOpKind::Arithmetic(ArithmeticOpKind::Div), + None, + false, + numerator, + denominator, + ), + ); + assert_eq!(sign_of(&memo, quotient, "value"), P); +} + +/// T4 b11 (precondition part): an estimated `+0.0` divisor where the exact +/// one is `-0.0` flips an infinite quotient's sign; the `/` contract must +/// require a checked division when the divisor reads an estimate. +#[test] +#[ignore = "S3: checked division when the divisor reads an estimate"] +fn b11_estimated_zero_divisor_needs_a_checked_division() { + todo!("S3: BinaryOperator::checked_relative_division set when the divisor reads an estimate") +} + +// ── T1.11 ─────────────────────────────────────────────────────────────── + +/// T1.11: `is_one_sign` and `state_column_sign`. +/// +/// Fails today: both are new (`todo!()`). +#[test] +fn value_sign_one_sign_and_state_column_sign() { + assert!(N.is_one_sign()); + assert!(P.is_one_sign()); + assert!(!U.is_one_sign()); + let mut memo = memo(); + let scan = add(&mut memo, series("pos")); + let window = add(&mut memo, range(MINUTE_MS, scan)); + let ddsketch = add( + &mut memo, + asap_types::test_support::memo_fixtures::state( + FieldDataType::Sketch(SketchAlgorithm::DDSketch), + window, + ), + ); + assert_eq!(properties(&memo, ddsketch).state_column_sign(), Some(N)); + assert_eq!(properties(&memo, scan).state_column_sign(), None); + // A chain's state column comes after its computed `window_end` + // (column 0, U): the state's sign, not the first column's. + let chain = add( + &mut memo, + Operator::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: scan, + summary_type: FieldDataType::Sketch(SketchAlgorithm::DDSketch), + input: SummaryUpdate::column(ColumnRef::SampleValue), + reduction: Reduction::PerEntity, + filter: None, + tumbling_window_width_ms: 60_000, + }), + ); + assert_eq!(properties(&memo, chain).state_column_sign(), Some(N)); +} + +// ── T2.3 ──────────────────────────────────────────────────────────────── + +const COLUMN_PROPERTIES: &str = include_str!("../src/stages/logical/group/column_properties.rs"); + +/// T2.3: the sign module denies wildcard enum arms (user decision Q5), and +/// no tuple match in it ends in `_ =>`, which the lint cannot see +/// (provisional answer Q9). +/// +/// Fails today: the attribute is absent, and `arithmetic_sign` matches +/// `(op, left, right)` with a `_ => Unknown` arm. +#[test] +fn the_sign_module_denies_wildcard_enum_arms() { + let first_code_line = COLUMN_PROPERTIES + .lines() + .map(str::trim) + .find(|line| !line.is_empty() && !line.starts_with("//")) + .unwrap_or_default(); + assert_eq!( + first_code_line, "#![deny(clippy::wildcard_enum_match_arm)]", + "column_properties.rs must start with the lint" + ); + let mut tuple_wildcards = Vec::new(); + let lines: Vec<&str> = COLUMN_PROPERTIES.lines().collect(); + for (start, line) in lines.iter().enumerate() { + let Some(at) = line.find("match (") else { + continue; + }; + // The arms of this match: the lines up to the closing brace at the + // match's own indentation. + let indent = line.len() - line.trim_start().len(); + let _ = at; + for (offset, arm) in lines[start + 1..].iter().enumerate() { + let arm_indent = arm.len() - arm.trim_start().len(); + if arm.trim() == "}" && arm_indent == indent { + break; + } + if arm.trim_start().starts_with("_ =>") { + tuple_wildcards.push(format!("line {}: {}", start + offset + 2, arm.trim())); + } + } + } + assert!( + tuple_wildcards.is_empty(), + "tuple matches ending in a wildcard: {tuple_wildcards:?}" + ); +} + +/// The parse functions return `None` for a call that is not a PromQL math +/// or date function. +#[test] +fn parse_functions_reject_other_calls() { + let args: Vec> = vec![ScalarExpr::Column(VALUE)]; + assert_eq!(math_function_of_call("foo", &args), None); + assert_eq!(math_function_of_call("promql_hour", &args), None); + assert_eq!(time_function_of_call("promql_abs"), None); + assert_eq!(time_function_of_call("hour"), None); +}