From 8aba172e56f385ba1a7314f1b97e73f04ca489a9 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:23:57 +0000 Subject: [PATCH 1/8] feat(executor): run grouped stddev/variance, corr, group, count_values, exact topk and without grouping Reduce aggregates the frontends emit that the executor refused: Reduction::StdDev/Variance (Welford, population and sample, SQL NULL rules), Reduction::PearsonCorr (DataFusion corr), Reduction::Group, count_values as a value-text column plus a count, an exact Aggregate{Reduce, [TopK]} as per-partition Sort + Limit + ranked-row Project, and GroupKeys::without resolved to the kept columns (series identity less the excluded labels and __name__) for Aggregate, SummaryAgg, Sort and Limit. SQL sample STDDEV/VAR_SAMP are typed nullable. The exact top-k lowering is the only one now that Stage 2 no longer lowers TopK, so the executor reads the ranked-row helpers in types::ir::schema::topk, which become pub. The planner tests run the installable plan's DAG directly and declare the SQL table at rest; an ingesting whole-table aggregate hits a planner install bug (#1320). --- crates/executor/src/expressions/columnar.rs | 2 +- crates/executor/src/expressions/mod.rs | 66 ++- .../src/operators/aggregate/columnar.rs | 148 ++++++ .../executor/src/operators/aggregate/mod.rs | 52 ++ crates/executor/src/physical_planner/mod.rs | 237 +++++++++ crates/executor/tests/physical_semantics.rs | 269 ++++++++++ .../tests/exact_reduce_execution.rs | 280 +++++++++++ crates/integration-tests/tests/main.rs | 1 + crates/planner/tests/main.rs | 1 + crates/planner/tests/reduce_aggregates.rs | 463 ++++++++++++++++++ crates/types/src/ir/operator/non_asap.rs | 18 +- .../types/src/ir/schema/aggregate_schema.rs | 35 +- crates/types/src/ir/schema/topk/mod.rs | 4 +- 13 files changed, 1544 insertions(+), 32 deletions(-) create mode 100644 crates/integration-tests/tests/exact_reduce_execution.rs create mode 100644 crates/planner/tests/reduce_aggregates.rs diff --git a/crates/executor/src/expressions/columnar.rs b/crates/executor/src/expressions/columnar.rs index d718588d2..d844f27f5 100644 --- a/crates/executor/src/expressions/columnar.rs +++ b/crates/executor/src/expressions/columnar.rs @@ -740,7 +740,7 @@ fn evaluate_per_key_combination( fn expression_columns(expression: &Expression, columns: &mut Vec) { use Expression as E; match expression { - E::Column(c) | E::ExactFloat64(c) => columns.push(*c), + E::Column(c) | E::ExactFloat64(c) | E::FloatText(c) => columns.push(*c), E::LabelSet { column, .. } | E::Label { column, .. } | E::LabelIdentity { column, .. } => { columns.push(*column) } diff --git a/crates/executor/src/expressions/mod.rs b/crates/executor/src/expressions/mod.rs index 5473015d0..1cf4103b4 100644 --- a/crates/executor/src/expressions/mod.rs +++ b/crates/executor/src/expressions/mod.rs @@ -74,8 +74,8 @@ pub enum Expression { column: usize, name: String, }, - /// Canonical encoding of a label map less `excluding`, identical to - /// `promql_rows::encode_series_identity`. + /// Canonical encoding of a label map, or of a series identity, less + /// `excluding`, identical to `promql_rows::encode_series_identity`. LabelIdentity { column: usize, #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -85,6 +85,11 @@ pub enum Expression { value: Value, dtype: DataType, }, + /// A Float64 column's value as text, as Go's + /// `strconv.FormatFloat(v, 'f', -1, 64)` writes it (PromQL + /// `count_values`): the shortest decimal that reads back as `v`, + /// without exponent; `NaN`, `+Inf` and `-Inf`. + FloatText(usize), Negate(Box), Arithmetic { op: ArithmeticOpKind, @@ -132,6 +137,7 @@ impl Expression { } Column(_) | ExactFloat64(_) + | FloatText(_) | LabelSet { .. } | Label { .. } | LabelIdentity { .. } @@ -158,6 +164,7 @@ impl Expression { } Column(_) | ExactFloat64(_) + | FloatText(_) | LabelSet { .. } | Label { .. } | LabelIdentity { .. } @@ -248,6 +255,15 @@ impl Expression { } Ok((expected, false)) } + FloatText(column) => { + if plain(input, *column)? != (&DataType::Float64, false) { + return Err(invalid("value text requires a non-null Float64")); + } + Ok((DataType::Utf8, false)) + } + LabelIdentity { column, .. } if plain(input, *column)? == (&DataType::Utf8, false) => { + Ok((DataType::Utf8, false)) + } Label { column, .. } | LabelIdentity { column, .. } => { if plain(input, *column)? != ( @@ -410,21 +426,45 @@ impl Expression { } Value::Utf8(found.unwrap_or_else(|| "".into())) } - LabelIdentity { column, excluding } => { - let Value::Map(entries) = &row[*column] else { - return Err(invalid("label identity requires a map")); + FloatText(column) => { + let Value::Float64(value) = row[*column] else { + return Err(invalid("value text requires a Float64")); }; + Value::Utf8( + if value.is_nan() { + "NaN".into() + } else if value == f64::INFINITY { + "+Inf".into() + } else if value == f64::NEG_INFINITY { + "-Inf".into() + } else { + value.to_string() + } + .into(), + ) + } + LabelIdentity { column, excluding } => { let mut labels = std::collections::BTreeMap::new(); - for (key, value) in entries.iter() { - let (Value::Utf8(key), Value::Utf8(value)) = (key, value) else { - return Err(invalid("label identity requires Utf8 entries")); - }; - if excluding.iter().any(|label| label.as_str() == key.as_ref()) { - continue; + match &row[*column] { + Value::Utf8(identity) => { + labels = + crate::physical_planner::promql_rows::decode_series_identity(identity)?; + labels.retain(|name, _| !excluding.contains(name)); } - if labels.insert(key.to_string(), value.to_string()).is_some() { - return Err(invalid("duplicate label name")); + Value::Map(entries) => { + for (key, value) in entries.iter() { + let (Value::Utf8(key), Value::Utf8(value)) = (key, value) else { + return Err(invalid("label identity requires Utf8 entries")); + }; + if excluding.iter().any(|label| label.as_str() == key.as_ref()) { + continue; + } + if labels.insert(key.to_string(), value.to_string()).is_some() { + return Err(invalid("duplicate label name")); + } + } } + _ => return Err(invalid("label identity requires a map or series identity")), } Value::Utf8( crate::physical_planner::promql_rows::encode_series_identity(&labels)?.into(), diff --git a/crates/executor/src/operators/aggregate/columnar.rs b/crates/executor/src/operators/aggregate/columnar.rs index 9dcc37262..e96a058b8 100644 --- a/crates/executor/src/operators/aggregate/columnar.rs +++ b/crates/executor/src/operators/aggregate/columnar.rs @@ -68,6 +68,76 @@ impl RunningAvg { } } +/// Welford's running count, mean and sum of squared deviations from the +/// mean, as Prometheus `stddev`/`stdvar` and DataFusion's variance update +/// them: the same operations, so the same rounding. +#[derive(Clone, Copy, Default)] +struct Moments { + count: u64, + mean: f64, + squared_deviations: f64, +} + +impl Moments { + fn add(&mut self, value: f64) { + self.count += 1; + let delta = value - self.mean; + self.mean += delta / self.count as f64; + self.squared_deviations += delta * (value - self.mean); + } + + /// The variance over n (`population`) or n - 1; `None` with no value, + /// or one value of a sample. + fn variance(&self, population: bool) -> Option { + let divisor = if population { + self.count + } else { + self.count.saturating_sub(1) + }; + (divisor > 0).then(|| self.squared_deviations / divisor as f64) + } +} + +/// DataFusion's `corr` state: Welford's running means of both inputs, the +/// co-moment and each input's squared deviations. +#[derive(Clone, Copy, Default)] +struct CoMoments { + count: u64, + mean_left: f64, + mean_right: f64, + co_moment: f64, + left: Moments, + right: Moments, +} + +impl CoMoments { + fn add(&mut self, left: f64, right: f64) { + self.count += 1; + let count = self.count as f64; + let delta_left = left - self.mean_left; + self.mean_left += delta_left / count; + self.mean_right += (right - self.mean_right) / count; + self.co_moment += delta_left * (right - self.mean_right); + self.left.add(left); + self.right.add(right); + } + + /// The population covariance over both population standard + /// deviations, as DataFusion divides them. + fn correlation(&self) -> Option { + if self.mean_left.is_nan() && self.mean_right.is_nan() { + return Some(f64::NAN); + } + if self.mean_left.is_nan() || self.mean_right.is_nan() || self.count < 2 { + return None; + } + let covariance = self.co_moment / self.count as f64; + let left = self.left.variance(true)?.sqrt(); + let right = self.right.variance(true)?.sqrt(); + (left != 0. && right != 0.).then(|| covariance / left / right) + } +} + /// One measure's running state per group. enum MeasureStates { /// Rows selected per group. @@ -99,6 +169,12 @@ enum MeasureStates { }, /// The non-NULL values per group (a quantile or percentile). CollectedValues(Vec>), + /// A standard deviation's or variance's moments. + Moments(Vec), + /// A correlation's moments. + CoMoments(Vec), + /// No state: every group's value is the same (`group`). + Constant, } impl MeasureStates { @@ -138,6 +214,9 @@ impl MeasureStates { Reduction::Quantile { .. } | Reduction::Percentile { .. } => { Self::CollectedValues(Vec::new()) } + Reduction::StdDev { .. } | Reduction::Variance { .. } => Self::Moments(Vec::new()), + Reduction::PearsonCorr { .. } => Self::CoMoments(Vec::new()), + Reduction::Group => Self::Constant, }) } @@ -159,6 +238,9 @@ impl MeasureStates { Self::Cardinality { distinct, .. } => grow(distinct, groups, 0), Self::Frequency { totals, .. } => grow(totals, groups, 0), Self::CollectedValues(v) => grow(v, groups, Vec::new()), + Self::Moments(v) => grow(v, groups, Moments::default()), + Self::CoMoments(v) => grow(v, groups, CoMoments::default()), + Self::Constant => 0, } } } @@ -359,11 +441,45 @@ fn update_measure( } } } + ( + MeasureStates::Moments(moments), + Reduction::StdDev { column, .. } | Reduction::Variance { column, .. }, + ) => { + let values = numbers(batch, *column)?; + for (row, &group) in groups.iter().enumerate() { + if let Some(value) = values[row].filter(|_| selected(row)) { + moments[group as usize].add(value); + } + } + } + (MeasureStates::CoMoments(moments), Reduction::PearsonCorr { left, right }) => { + let (lefts, rights) = (numbers(batch, *left)?, numbers(batch, *right)?); + for (row, &group) in groups.iter().enumerate() { + if let (Some(left), Some(right), true) = (lefts[row], rights[row], selected(row)) { + moments[group as usize].add(left, right); + } + } + } + (MeasureStates::Constant, _) => {} _ => unreachable!("a measure's states match its reduction"), } Ok(()) } +/// The values of the numeric column `column` of `batch` as Float64, NULL +/// as `None`. +fn numbers(batch: &Batch, column: usize) -> Result>, Error> { + let array = batch.column(column); + if let Some(floats) = array.as_any().downcast_ref::() { + return Ok(floats.iter().collect()); + } + let integers = array + .as_any() + .downcast_ref::() + .ok_or_else(|| invalid("numeric aggregate value required"))?; + Ok(integers.iter().map(|v| v.map(|v| v as f64)).collect()) +} + /// Each group's value of one measure, in `order`. fn finish_measure( states: MeasureStates, @@ -477,6 +593,38 @@ fn finish_measure( .collect(), _ => unreachable!("quantile states answer a quantile or percentile"), }, + MeasureStates::Moments(moments) => { + let (Reduction::StdDev { population, .. } | Reduction::Variance { population, .. }) = + measure + else { + unreachable!("moments answer a standard deviation or variance") + }; + order + .iter() + .map(|&g| { + let variance = moments[g as usize].variance(*population); + let value = if matches!(measure, Reduction::StdDev { .. }) { + variance.map(f64::sqrt) + } else { + variance + }; + match value { + Some(value) => Value::Float64(value), + None if promql => Value::Float64(f64::NAN), + None => Value::Null, + } + }) + .collect() + } + MeasureStates::CoMoments(moments) => order + .iter() + .map(|&g| { + moments[g as usize] + .correlation() + .map_or(Value::Null, Value::Float64) + }) + .collect(), + MeasureStates::Constant => order.iter().map(|_| Value::Float64(1.)).collect(), }; arrow_columns::encode( dtype, diff --git a/crates/executor/src/operators/aggregate/mod.rs b/crates/executor/src/operators/aggregate/mod.rs index e914f171e..50d966ac2 100644 --- a/crates/executor/src/operators/aggregate/mod.rs +++ b/crates/executor/src/operators/aggregate/mod.rs @@ -77,6 +77,32 @@ impl Operator { } (t.clone(), nullable || groups.is_empty()) } + Reduction::StdDev { column, population } + | Reduction::Variance { column, population } => { + let (t, nullable) = plain(&input, *column)?; + if !matches!(t, DataType::Int64 | DataType::Float64) { + return Err(invalid("numeric aggregate input required")); + } + // SQL: NULL for fewer than two values (sample) or none + // (population); a PromQL group always has a value. + ( + DataType::Float64, + !input.has_promql_series_identity() + && (!population || groups.is_empty() || nullable), + ) + } + Reduction::PearsonCorr { left, right } => { + for column in [left, right] { + if !matches!( + plain(&input, *column)?.0, + DataType::Int64 | DataType::Float64 + ) { + return Err(invalid("correlation requires numeric inputs")); + } + } + (DataType::Float64, true) + } + Reduction::Group => (DataType::Float64, false), }; fields.push(result_field(name, t, n)); } @@ -130,6 +156,8 @@ impl Operator { | Reduction::Min(_) | Reduction::Max(_) | Reduction::Percentile { .. } + | Reduction::StdDev { .. } + | Reduction::Variance { .. } ) { output.fields[groups.len() + index].nullable = true; } @@ -237,6 +265,30 @@ pub enum Reduction { /// The fraction, in [0, 1]. q: f64, }, + /// Standard deviation of a numeric column's non-NULL values: the + /// square root of [`Reduction::Variance`]. + StdDev { + column: usize, + population: bool, + }, + /// Variance of a numeric column's non-NULL values, by Welford's + /// update as Prometheus `stdvar` and DataFusion `var_pop`/`var_samp` + /// compute it: the squared deviations' sum over n (population) or + /// n - 1 (sample); NaN once a value is NaN or infinite. SQL gives NULL + /// for no value, or one value of a sample; PromQL NaN. + Variance { + column: usize, + population: bool, + }, + /// SQL `corr(left, right)`, DataFusion's: Pearson's correlation over + /// the rows where both are non-NULL; NULL for fewer than two rows, a + /// constant input or one input's mean NaN, NaN when both are. + PearsonCorr { + left: usize, + right: usize, + }, + /// PromQL `group`: 1 for every group. + Group, } pub(super) fn execute<'a>( operator: &'a Operator, diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index bcc74765e..06ed66042 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -255,6 +255,125 @@ fn compile_internal( inputs = vec![auxiliary]; schemas.truncate(1); } + // A `without` grouping groups by the columns it keeps, after + // removing the excluded labels from a series identity. + let resolved; + let node = match resolve_without(node, &schemas) + .map_err(|error| invalid(format!("node {id}: {error}")))? + { + Some((rewritten, projection)) => { + if let Some(projection) = projection { + // Helper 3: the lowerings below use helpers 0 to 2. + let helper = helper_id(id, 3)?; + schemas = vec![projection.schema()]; + physical_dag.add(helper, inputs, projection)?; + inputs = vec![helper]; + } + resolved = rewritten; + &resolved + } + None => node, + }; + if let Payload::NonASAP(NonASAPOp::Aggregate { + reduction: PlannerReduction::Reduce(keys), + measures, + filters, + having: None, + .. + }) = &node.payload + { + let [input] = schemas.as_slice() else { + return Err(invalid(format!("node {id}: an aggregate reads one input"))); + }; + // An exact top-k (PromQL `topk`, SQL's heavy hitter): the k + // largest rows of its child per partition, then its ranked + // rows' columns. + if let [AggIntent::TopK { k, .. }] = measures.as_slice() { + if filters.iter().any(Option::is_some) { + return Err(invalid(format!("node {id}: a filtered top-k"))); + } + let Some(NonASAPOp::Aggregate { child, .. }) = restored[&id].non_asap() else { + unreachable!("an aggregate restores as an aggregate") + }; + let ranked = |error: String| invalid(format!("node {id}: top-k: {error}")); + let value = planner_types::ir::schema::topk::ranked_column(child) + .map_err(|error| ranked(error.to_string()))?; + let columns = planner_types::ir::schema::topk::ranked_row_projection( + child, + &PlannerReduction::Reduce(keys.clone()), + &output.fields[keys.len()].name, + ) + .map_err(|error| ranked(error.to_string()))? + .into_iter() + .map(|item| match item.expr { + ScalarExpr::Column(column) => { + Ok((item.alias.unwrap_or_default(), Expression::Column(column))) + } + _ => Err(ranked("a ranked row projects columns".into())), + }) + .collect::>()?; + let partitions = groups(input, keys)?; + physical_dag.add( + auxiliary, + inputs, + Operator::sort( + input.clone(), + vec![SortKey { + column: value, + descending: true, + nulls_first: false, + }], + partitions.clone(), + )?, + )?; + physical_dag.add( + helper_id(id, 1)?, + vec![auxiliary], + Operator::limit(input.clone(), *k as u64, 0, partitions)?, + )?; + physical_dag.add( + id, + vec![helper_id(id, 1)?], + Operator::project(input.clone(), columns)?.with_output_schema(output)?, + )?; + continue; + } + // PromQL `count_values("l", v)`: a count per group and value, + // the value as text in the label `l`, which replaces a + // grouping label of that name. + if let [AggIntent::CountValues { label }] = measures.as_slice() { + if filters.iter().any(Option::is_some) { + return Err(invalid(format!("node {id}: a filtered count_values"))); + } + let value = named_column(input, &ColumnRef::SampleValue)?; + let mut columns: Vec<_> = input + .fields + .iter() + .enumerate() + .map(|(i, field)| (field.name.clone(), Expression::Column(i))) + .collect(); + columns.push((label.clone(), Expression::FloatText(value))); + let text = input.fields.len(); + let mut partitions = groups(input, keys)?; + match partitions + .iter() + .position(|&key| input.fields[key].name == *label) + { + Some(key) => partitions[key] = text, + None => partitions.push(text), + } + let project = Operator::project(input.clone(), columns)?; + let aggregate = Operator::aggregate( + project.schema(), + partitions, + vec![(String::new(), Reduction::Count)], + )? + .with_output_schema(output)?; + physical_dag.add(auxiliary, inputs, project)?; + physical_dag.add(id, vec![auxiliary], aggregate)?; + continue; + } + } if let Payload::ASAP(ASAPOp::MaintainPopulation { population, .. }) = &node.payload { use planner_types::ir::operator::maintained_population::PopulationInput; let PopulationInput::CurrentSeries(spec) = &population.input else { @@ -1136,6 +1255,19 @@ fn bind_operation(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result Reduction::Avg(column(*col)?), AggIntent::Min { col } => Reduction::Min(column(*col)?), AggIntent::Max { col } => Reduction::Max(column(*col)?), + AggIntent::StdDev { col, population } => Reduction::StdDev { + column: column(*col)?, + population: *population, + }, + AggIntent::Variance { col, population } => Reduction::Variance { + column: column(*col)?, + population: *population, + }, + AggIntent::PearsonCorr { left, right } => Reduction::PearsonCorr { + left: *left, + right: *right, + }, + AggIntent::Group => Reduction::Group, // PromQL `quantile by (G) (φ, v)` exactly: the // group's values, interpolated as Prometheus does // (NaN first). A filtered one has no kernel: the IR @@ -1434,6 +1566,111 @@ fn named_column(input: &SchemaRef, column: &ColumnRef) -> Result { _ => Err(invalid("summary update column missing or ambiguous")), } } +/// `node` with its `without` grouping (PromQL `sum without (instance) (m)`) +/// over `inputs` resolved to the columns it keeps +/// ([`without_kept_columns`](planner_types::ir::schema::aggregate_schema::without_kept_columns)), +/// as a `by` grouping; `None` for a node without one. A kept series +/// identity, or label map, keeps every label but the excluded ones and +/// `__name__`: the returned projection rewrites it in place first, for an +/// aggregation (an `Aggregate` or a `SummaryAgg`), whose output keeps that +/// label set. A ranking (a top-k, `Sort` or `Limit`) keeps each row's +/// labels, so it is refused one. +fn resolve_without( + node: &PhysicalASAPDAGNode, + inputs: &[SchemaRef], +) -> Result)>, Error> { + let (keys, aggregation) = match &node.payload { + // `histogram_quantile` reads its `without([le])` grouping itself. + Payload::NonASAP(NonASAPOp::Aggregate { measures, .. }) + if matches!(measures.as_slice(), [AggIntent::HistogramQuantile { .. }]) => + { + return Ok(None) + } + Payload::NonASAP(NonASAPOp::Aggregate { + reduction: PlannerReduction::Reduce(keys), + measures, + .. + }) => { + let ranking = matches!(measures.as_slice(), [AggIntent::TopK { .. }]); + (keys, !ranking) + } + Payload::ASAP(ASAPOp::SummaryAgg { + reduction: PlannerReduction::Reduce(keys), + .. + }) => (keys, true), + Payload::NonASAP( + NonASAPOp::Sort { partition_by, .. } | NonASAPOp::Limit { partition_by, .. }, + ) => (partition_by, false), + _ => return Ok(None), + }; + if !keys.is_without() { + return Ok(None); + } + let [input] = inputs else { + return Err(invalid("a without grouping reads one input")); + }; + if keys.keys().iter().any(|&i| i >= input.fields.len()) { + return Err(invalid("grouping column out of range")); + } + let excluded: Vec = keys + .keys() + .iter() + .map(|&i| input.fields[i].name.clone()) + .collect(); + let kept = + planner_types::ir::schema::aggregate_schema::without_kept_columns(input, keys.keys()); + let mut rewrites = false; + let columns = (0..input.fields.len()) + .map(|i| { + let field = &input.fields[i]; + let label_map = matches!(field.dtype, FieldDataType::Plain(DataType::Map { .. })); + let expression = if !kept.contains(&i) { + Expression::Column(i) + } else if field.name == promql_rows::SERIES_IDENTITY_COLUMN { + rewrites = true; + Expression::LabelIdentity { + column: i, + excluding: excluded + .iter() + .cloned() + .chain(["__name__".to_string()]) + .collect(), + } + } else if label_map { + rewrites = true; + Expression::LabelSet { + column: i, + labels: excluded.clone(), + without: true, + } + } else { + Expression::Column(i) + }; + (field.name.clone(), expression) + }) + .collect(); + if rewrites && !aggregation { + return Err(invalid( + "a without partition over series identity needs each row's labels kept", + )); + } + let mut node = node.clone(); + let kept = GroupKeys::by(kept); + match &mut node.payload { + Payload::NonASAP(NonASAPOp::Aggregate { reduction, .. }) + | Payload::ASAP(ASAPOp::SummaryAgg { reduction, .. }) => { + *reduction = PlannerReduction::Reduce(kept) + } + Payload::NonASAP( + NonASAPOp::Sort { partition_by, .. } | NonASAPOp::Limit { partition_by, .. }, + ) => *partition_by = kept, + _ => unreachable!("matched above"), + } + let projection = rewrites + .then(|| Operator::project(input.clone(), columns)) + .transpose()?; + Ok(Some((node, projection))) +} fn groups(input: &SchemaRef, groups: &GroupKeys) -> Result, Error> { if groups.is_without() { return Err(invalid("grouping without requires resolved label columns")); diff --git a/crates/executor/tests/physical_semantics.rs b/crates/executor/tests/physical_semantics.rs index 043d86c3a..80abfadee 100644 --- a/crates/executor/tests/physical_semantics.rs +++ b/crates/executor/tests/physical_semantics.rs @@ -1389,3 +1389,272 @@ fn exact_cardinality_grouping_normalizes_float_identities() { [Value::Int64(2), Value::Int64(0)] )); } + +/// The value of row `row`'s column `column`: `Some` float, `None` for NULL. +fn float_at(rows: &[Vec], row: usize, column: usize) -> Option { + match rows[row][column] { + Value::Float64(value) => Some(value), + Value::Null => None, + ref other => panic!("a DOUBLE: {other:?}"), + } +} + +fn floats(values: &[Option]) -> Vec> { + values + .iter() + .map(|v| vec![v.map_or(Value::Null, Value::Float64)]) + .collect() +} + +/// SQL `STDDEV`/`VAR_SAMP` and `STDDEV_POP`/`VAR_POP` as DataFusion and +/// Postgres define them: NULLs ignored; the sample forms NULL for fewer +/// than two values, the population forms for none; NaN once a value is +/// NaN or infinite. +#[test] +fn sql_variance_and_stddev_follow_their_definitions() { + let input = schema(&[("v", DataType::Float64, true)]); + let measures = || { + vec![ + ( + "var_pop".into(), + Reduction::Variance { + column: 0, + population: true, + }, + ), + ( + "var_samp".into(), + Reduction::Variance { + column: 0, + population: false, + }, + ), + ( + "stddev_pop".into(), + Reduction::StdDev { + column: 0, + population: true, + }, + ), + ( + "stddev_samp".into(), + Reduction::StdDev { + column: 0, + population: false, + }, + ), + ] + }; + let run = |values: &[Option]| { + let op = Operator::aggregate(input.clone(), vec![], measures()).unwrap(); + let rows = unary(input.clone(), vec![floats(values)], op); + (0..4).map(|c| float_at(&rows, 0, c)).collect::>() + }; + let close = |got: Vec>, expected: [Option; 4]| { + for (got, expected) in got.iter().zip(expected) { + match (got, expected) { + (Some(a), Some(b)) => assert!((a - b).abs() < 1e-12, "{got:?} vs {expected:?}"), + _ => assert_eq!(*got, expected), + } + } + }; + // 1, 2, 4 and a NULL: mean 7/3, squared deviations 14/3. + close( + run(&[Some(1.), None, Some(2.), Some(4.)]), + [ + Some(14. / 9.), + Some(7. / 3.), + Some((14f64 / 9.).sqrt()), + Some((7f64 / 3.).sqrt()), + ], + ); + close(run(&[Some(5.)]), [Some(0.), None, Some(0.), None]); + close(run(&[None]), [None, None, None, None]); + close(run(&[]), [None, None, None, None]); + for poison in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + for values in [[Some(poison), Some(1.)], [Some(1.), Some(poison)]] { + assert!(run(&values).iter().all(|v| v.unwrap().is_nan())); + } + } + // An Int64 column reads as Float64. + let integers = schema(&[("v", DataType::Int64, false)]); + let op = Operator::aggregate( + integers.clone(), + vec![], + vec![( + "var_samp".into(), + Reduction::Variance { + column: 0, + population: false, + }, + )], + ) + .unwrap(); + let rows = unary( + integers, + vec![vec![vec![Value::Int64(1)], vec![Value::Int64(3)]]], + op, + ); + assert_eq!(float_at(&rows, 0, 0), Some(2.)); +} + +/// Over PromQL series, `stddev` and `stdvar` are population statistics +/// (Welford's update, as Prometheus computes them): a single value has +/// deviation 0, a single infinite one NaN. +#[test] +fn promql_stddev_of_one_series() { + let mut input = (*schema(&[ + ( + planner_types::ir::schema::PROMQL_SERIES_IDENTITY, + DataType::Utf8, + false, + ), + ("value", DataType::Float64, false), + ])) + .clone(); + input.closed = true; + let input = Arc::new(input); + for (value, expected) in [(3., 0.), (f64::INFINITY, f64::NAN)] { + let op = Operator::aggregate( + input.clone(), + vec![], + vec![( + "stddev".into(), + Reduction::StdDev { + column: 1, + population: true, + }, + )], + ) + .unwrap(); + let rows = unary( + input.clone(), + vec![vec![vec![Value::Utf8("{}".into()), Value::Float64(value)]]], + op, + ); + let got = float_at(&rows, 0, 0).unwrap(); + assert!( + got == expected || (got.is_nan() && expected.is_nan()), + "{got}" + ); + } +} + +/// SQL `corr` as DataFusion computes it: over the rows where both inputs +/// are non-NULL, NULL for fewer than two, a constant input, or one input's +/// NaN; NaN when both have one. +#[test] +fn correlation_follows_datafusion() { + let input = schema(&[ + ("a", DataType::Float64, true), + ("b", DataType::Float64, true), + ]); + let run = |pairs: &[(Option, Option)]| { + let op = Operator::aggregate( + input.clone(), + vec![], + vec![("corr".into(), Reduction::PearsonCorr { left: 0, right: 1 })], + ) + .unwrap(); + let rows = pairs + .iter() + .map(|(a, b)| { + vec![ + a.map_or(Value::Null, Value::Float64), + b.map_or(Value::Null, Value::Float64), + ] + }) + .collect(); + float_at(&unary(input.clone(), vec![rows], op), 0, 0) + }; + let line = [ + (Some(1.), Some(3.)), + (Some(2.), Some(5.)), + (Some(3.), Some(7.)), + ]; + assert!((run(&line).unwrap() - 1.).abs() < 1e-12); + let falling = [(Some(1.), Some(3.)), (Some(2.), Some(1.)), (None, Some(9.))]; + assert!((run(&falling).unwrap() + 1.).abs() < 1e-12); + // (1, 1), (2, 3), (3, 2): covariance 1/3, both variances 2/3. + let scattered = [ + (Some(1.), Some(1.)), + (Some(2.), Some(3.)), + (Some(3.), Some(2.)), + ]; + assert!((run(&scattered).unwrap() - 0.5).abs() < 1e-12); + assert_eq!(run(&[(Some(1.), Some(2.))]), None); + assert_eq!(run(&[]), None); + assert_eq!(run(&[(Some(1.), Some(2.)), (Some(1.), Some(5.))]), None); + assert_eq!( + run(&[(Some(1.), Some(2.)), (Some(f64::NAN), Some(5.))]), + None + ); + assert!( + run(&[(Some(1.), Some(2.)), (Some(f64::NAN), Some(f64::NAN))]) + .unwrap() + .is_nan() + ); +} + +/// PromQL `group` is 1 for every group, whatever its values. +#[test] +fn group_is_one_per_group() { + let input = schema(&[ + ("job", DataType::Utf8, false), + ("v", DataType::Float64, false), + ]); + let op = Operator::aggregate( + input.clone(), + vec![0], + vec![("group".into(), Reduction::Group)], + ) + .unwrap(); + let rows = unary( + input, + vec![vec![ + vec![Value::Utf8("a".into()), Value::Float64(f64::NAN)], + vec![Value::Utf8("a".into()), Value::Float64(-3.)], + vec![Value::Utf8("b".into()), Value::Float64(0.)], + ]], + op, + ); + assert_eq!(rows.len(), 2); + assert!((0..2).all(|row| float_at(&rows, row, 1) == Some(1.))); +} + +/// `count_values` labels a value as Go's `FormatFloat(v, 'f', -1, 64)` +/// writes it: the shortest round-tripping decimal, never an exponent. +#[test] +fn float_text_matches_go_format_float() { + let input = schema(&[("v", DataType::Float64, false)]); + let cases = [ + (1., "1"), + (2.5, "2.5"), + (0.1, "0.1"), + (-0., "-0"), + (1e21, "1000000000000000000000"), + (1e-7, "0.0000001"), + (f64::NAN, "NaN"), + (f64::INFINITY, "+Inf"), + (f64::NEG_INFINITY, "-Inf"), + ]; + let op = Operator::project( + input.clone(), + vec![("text".into(), Expression::FloatText(0))], + ) + .unwrap(); + let rows = unary( + input, + vec![cases + .iter() + .map(|(v, _)| vec![Value::Float64(*v)]) + .collect()], + op, + ); + for (row, (_, text)) in rows.iter().zip(cases) { + assert!( + matches!(&row[0], Value::Utf8(got) if got.as_ref() == text), + "{row:?} vs {text}" + ); + } +} diff --git a/crates/integration-tests/tests/exact_reduce_execution.rs b/crates/integration-tests/tests/exact_reduce_execution.rs new file mode 100644 index 000000000..5d41655b5 --- /dev/null +++ b/crates/integration-tests/tests/exact_reduce_execution.rs @@ -0,0 +1,280 @@ +//! Cross-series aggregates as the PromQL frontend lowers them, compiled +//! and run by the executor with no planning pass in between: an exact +//! `topk` stays `Aggregate{Reduce, [TopK]}` through planning and the +//! executor lowers it to a `Sort` and a `Limit`, a `without` grouping +//! groups by the labels it keeps, and `count_values` labels each value. +//! Answers are checked against Prometheus's. + +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::{AggIntent, Source}; +use asap_types::ir::schema::PROMQL_SERIES_IDENTITY; +use asap_types::ir::NonASAPOp; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; +use futures::{executor::block_on, StreamExt}; + +/// Evaluation time; every sample is 0.5 s before it, inside the instant +/// lookback of a one-second sample interval. +const EVALUATION_MS: i64 = 300_000; + +/// One answer: its labels and value. +type Answer = (BTreeMap, f64); + +fn labels(pairs: &[(&str, &str)]) -> BTreeMap { + pairs + .iter() + .map(|(name, value)| (name.to_string(), value.to_string())) + .collect() +} + +/// The gauge `x`: per `(job, instance)` its value. +const SERIES: [(&str, &str, f64); 5] = [ + ("a", "1", 1.0), + ("a", "2", 2.0), + ("a", "3", 4.0), + ("b", "1", 2.0), + ("b", "2", 8.0), +]; + +/// `query` lowered exactly by the frontend, with series identity, run over +/// [`SERIES`] bound at its `Scan` of `x`: each answer's labels (its series +/// identity's and each non-empty label column's) and value. +fn execute(query: &str) -> 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(); + let scan = dag + .nodes + .iter() + .find(|node| { + matches!( + node.payload, + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) + ) + }) + .unwrap(); + let schema = Arc::new(scan.output_schema.clone()); + let rows = SERIES + .iter() + .map(|(job, instance, value)| { + let labels = labels(&[("__name__", "x"), ("job", job), ("instance", instance)]); + promql_rows::series_row(&schema, &labels, EVALUATION_MS - 500, *value).unwrap() + }) + .collect(); + let batch = Batch::try_new(schema.clone(), rows).unwrap(); + let store = InMemoryDataStore::default() + .with_raw_data( + Source::TimeSeries { metric: "x".into() }, + schema, + vec![batch], + ) + .unwrap(); + let roots = [dag.roots[0] as u64]; + let bound = compile(&dag, &[], &roots) + .unwrap_or_else(|error| panic!("{query}: {error}")) + .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 + }); + let mut answers: Vec = batches + .iter() + .flat_map(|batch| { + batch.to_rows().into_iter().map(|row| { + let mut labels = BTreeMap::new(); + let mut value = None; + for (field, cell) in batch.schema().fields.iter().zip(&row) { + match cell { + Value::Utf8(text) if field.name == PROMQL_SERIES_IDENTITY => { + labels.extend(promql_rows::decode_series_identity(text).unwrap()) + } + Value::Utf8(text) if !text.is_empty() => { + labels.insert(field.name.clone(), text.to_string()); + } + Value::Float64(v) => value = Some(*v), + Value::Int64(v) => value = Some(*v as f64), + _ => {} + } + } + (labels, value.expect("a sample value")) + }) + }) + .collect(); + answers.sort_by(|a, b| a.0.cmp(&b.0)); + answers +} + +/// The frontend's exact `topk` is an `Aggregate` of `TopK`, which the +/// executor runs: the 2 largest job sums, with their jobs. +#[test] +fn exact_topk_of_a_grouped_sum_runs() { + let root = lower_promql("topk(2, sum by (job) (x))", AccuracyTarget::exact()).unwrap(); + assert!( + matches!( + root.non_asap(), + Some(NonASAPOp::Aggregate { measures, .. }) + if matches!(measures.as_slice(), [AggIntent::TopK { .. }]) + ), + "{:?}", + root.operator + ); + assert_eq!( + execute("topk(2, sum by (job) (x))"), + vec![ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b")]), 10.0) + ] + ); +} + +/// `topk by (job)` ranks series within each job and returns them as they +/// are, metric name included. +#[test] +fn exact_topk_per_partition_keeps_series_labels() { + assert_eq!( + execute("topk by (job) (1, x)"), + vec![ + ( + labels(&[("__name__", "x"), ("instance", "2"), ("job", "b")]), + 8.0 + ), + ( + labels(&[("__name__", "x"), ("instance", "3"), ("job", "a")]), + 4.0 + ), + ] + ); +} + +/// `without` groups by the labels it keeps, `__name__` dropped, through +/// the exact `Aggregate` (the planned workload test reaches it through an +/// exact accumulator). +#[test] +fn without_grouping_keeps_every_other_label() { + assert_eq!( + execute("max without (instance) (x)"), + vec![ + (labels(&[("job", "a")]), 4.0), + (labels(&[("job", "b")]), 8.0) + ] + ); + assert_eq!( + execute("count without (job, instance) (x)"), + vec![(labels(&[]), 5.0)] + ); +} + +/// A `count_values` label named like a grouping label replaces it, as in +/// Prometheus: the groups are the values alone. +#[test] +fn count_values_label_replaces_a_grouping_label() { + assert_eq!( + execute(r#"count_values by (job) ("job", x)"#), + vec![ + (labels(&[("job", "1")]), 1.0), + (labels(&[("job", "2")]), 2.0), + (labels(&[("job", "4")]), 1.0), + (labels(&[("job", "8")]), 1.0), + ] + ); + assert_eq!( + execute(r#"count_values by (job) ("v", x)"#), + vec![ + (labels(&[("job", "a"), ("v", "1")]), 1.0), + (labels(&[("job", "a"), ("v", "2")]), 1.0), + (labels(&[("job", "a"), ("v", "4")]), 1.0), + (labels(&[("job", "b"), ("v", "2")]), 1.0), + (labels(&[("job", "b"), ("v", "8")]), 1.0), + ] + ); +} + +/// A `Sort` and a `Limit` partitioned `without (b)` over a table (no +/// frontend emits one; the IR allows it) partition by every other column +/// but the time and the value: the largest value per `a`. +#[test] +fn without_partitions_over_a_table_rank_per_kept_column() { + use asap_types::ir::operator::GroupKeys; + use asap_types::ir::scalar::SortKey; + use asap_types::ir::schema::{DataType, Field, Schema}; + use asap_types::ir::{Operator, OperatorNode, ScalarExpr}; + let scan = OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Scan { + source: Source::Table { + table_ref: "t".into(), + }, + predicates: vec![], + schema: Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + Field::plain("a", DataType::Utf8, false), + Field::plain("b", DataType::Utf8, false), + ], + 0, + vec![], + ), + })) + .unwrap(); + let sorted = OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Sort { + keys: vec![SortKey { + expr: ScalarExpr::Column(1), + ascending: false, + nulls_first: false, + }], + partition_by: GroupKeys::without(vec![3]), + child: scan, + })) + .unwrap(); + let limited = OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::Limit { + n: Some(1), + offset: 0, + partition_by: GroupKeys::without(vec![3]), + child: sorted, + })) + .unwrap(); + let row = |value: f64, a: &str, b: &str| { + vec![ + Value::Timestamp(0), + Value::Float64(value), + Value::Utf8(a.into()), + Value::Utf8(b.into()), + ] + }; + let rows = crate::physical_common::execute_raw_rows( + &limited, + vec![row(1., "x", "1"), row(5., "x", "2"), row(3., "y", "1")], + ); + let mut ranked: Vec<_> = rows + .iter() + .map(|row| match (&row[1], &row[2], &row[3]) { + (Value::Float64(value), Value::Utf8(a), Value::Utf8(b)) => { + (a.to_string(), b.to_string(), *value) + } + other => panic!("{other:?}"), + }) + .collect(); + ranked.sort_by(|x, y| x.0.cmp(&y.0)); + assert_eq!( + ranked, + [("x".into(), "2".into(), 5.), ("y".into(), "1".into(), 3.)] + ); +} diff --git a/crates/integration-tests/tests/main.rs b/crates/integration-tests/tests/main.rs index 2a06f2b5c..bebcbab67 100644 --- a/crates/integration-tests/tests/main.rs +++ b/crates/integration-tests/tests/main.rs @@ -9,6 +9,7 @@ mod planning_common; mod aggregate; mod binary_op; mod crate_graph; +mod exact_reduce_execution; mod filtered_aggregates; mod finer_aggregate_reuse; mod frontend_timestamps; diff --git a/crates/planner/tests/main.rs b/crates/planner/tests/main.rs index d17ff5aa4..d16b381fa 100644 --- a/crates/planner/tests/main.rs +++ b/crates/planner/tests/main.rs @@ -7,6 +7,7 @@ mod time_series; mod columnar_batches; mod e2e_plan; mod exact_only_reads; +mod reduce_aggregates; mod runtime_install; mod sql_table_scans; mod summary_sharing; diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs new file mode 100644 index 000000000..9ee5f90ef --- /dev/null +++ b/crates/planner/tests/reduce_aggregates.rs @@ -0,0 +1,463 @@ +//! Cross-series (`Reduce`) aggregates planned exactly with +//! `plan_installable` and run with the Planner's executor over raw data: +//! PromQL `stddev`/`stdvar`, `group`, `count_values`, `without` grouping +//! and an exact `topk` over a grouped sum of rates; SQL `STDDEV`, +//! `VARIANCE` and `CORR`. Each answer is checked against the value the +//! PromQL or SQL definition gives for the same data. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use asap_executor::physical_planner::compile; +use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::sources::InMemoryDataStore; +use asap_executor::values::{Batch, Value}; +use asap_planner::{ + plan_installable, FrontendInput, InstallablePlan, PlanningModels, PlanningWorkload, SqlCatalog, + UserInput, +}; +use asap_types::ir::operator::Source; +use asap_types::ir::schema::{DataType, Field, Schema}; +use asap_types::ir::NonASAPOp; +use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; +use futures::{executor::block_on, StreamExt}; + +use crate::time_series::{series_labels, time_series_tables, RawDataSample}; + +/// The evaluation time of every run. +const EVALUATION_MS: i64 = 1_800_000_000_000; + +/// No approximation. +const EXACT: &str = r#"{"max_error": 0.0, "failure_probability": 0.0}"#; + +/// One repeating `query` in `language` (its workload JSON), every 10 s, +/// exact, over `source`: 10 series sampled every 10 s, 1,000 rows held, +/// arriving as `arrival` (a `DataArrival` as JSON). +fn workload(language: &str, query: &str, source: &str, arrival: &str) -> PlanningWorkload { + serde_json::from_str(&format!( + r#"{{"query_workload": {{"language": {language}, "repeating_queries": [ + {{"query": {query:?}, + "demand": {{"fixed_interval_at": {{"interval": 10000, "evaluation_phase": 0}}}}, + "requirements": {{"accuracy": {EXACT}}}, + "predictability": {{"predictable": {{"known_at": null}}}}}}]}}, + "data_workload": {{"arrival": {arrival}, + "sources": {{"{source}": {{"series_count": 10, "sample_interval": 10000, + "rows_held": 1000}}}}}}}}"# + )) + .unwrap() +} + +fn query_scope() -> RunContext { + RunContext::new( + Scope::Query { + evaluation_time_ms: EVALUATION_MS, + }, + Limits::default(), + ) + .unwrap() +} + +/// The batches of `plan`'s one query, run at query time over `store`: the +/// whole DAG computed from raw data, no materialized view read as stored. +fn run( + plan: &InstallablePlan, + store: impl FnOnce(&asap_planner::PhysicalASAPDAG) -> InMemoryDataStore, +) -> Vec { + let dag = &plan.dag; + let roots = [dag.roots[0] as u64]; + let compiled = compile(dag, &[], &roots).expect("the executor compiles the plan"); + let instance = compiled.instantiate(Arc::new(store(dag))).unwrap(); + block_on(async { + let mut output = instance + .execute(compiled.roots(), query_scope()) + .unwrap() + .remove(0); + let mut batches = Vec::new(); + while let Some(batch) = output.next().await { + batches.push((*batch.expect("the query runs")).clone()); + } + batches + }) +} + +// ── PromQL ─────────────────────────────────────────────────────────────── + +/// One sample of `metric` with `labels` at `timestamp_ms`. +fn sample(metric: &str, labels: &[(&str, &str)], timestamp_ms: i64, value: f64) -> RawDataSample { + let mut all: BTreeMap = labels + .iter() + .map(|(name, value)| (name.to_string(), value.to_string())) + .collect(); + all.insert("__name__".into(), metric.into()); + RawDataSample { + labels: all, + timestamp_ms, + value, + } +} + +/// The gauge `m`, one sample per series 5 s before the evaluation: job +/// `a` holds 1, 2 and 4, job `b` holds 2 and (with a `zone` label) 2.5. +fn gauge_samples() -> Vec { + let at = EVALUATION_MS - 5_000; + vec![ + sample("m", &[("job", "a"), ("instance", "1")], at, 1.0), + sample("m", &[("job", "a"), ("instance", "2")], at, 2.0), + sample("m", &[("job", "a"), ("instance", "3")], at, 4.0), + sample("m", &[("job", "b"), ("instance", "1")], at, 2.0), + sample( + "m", + &[("job", "b"), ("instance", "2"), ("zone", "z")], + at, + 2.5, + ), + ] +} + +/// The counter `c`: per job `j1`..`j4`, two instances counting up `n` and +/// `n + 1` per 10 s, sampled every 10 s over the last 10 minutes. +fn counter_samples() -> Vec { + let mut samples = Vec::new(); + for (job, n) in [("j1", 1.0), ("j2", 2.0), ("j3", 3.0), ("j4", 4.0)] { + for (instance, slope) in [("1", n), ("2", n + 1.0)] { + for step in 0..60 { + samples.push(sample( + "c", + &[("job", job), ("instance", instance)], + EVALUATION_MS - 600_000 + step * 10_000, + slope * step as f64, + )); + } + } + } + samples +} + +async fn plan_promql(query: &str, metric: &str) -> InstallablePlan { + plan_installable(UserInput::new( + &workload( + r#""promql""#, + query, + metric, + r#"{"ingesting": {"rows_per_second": 100.0}}"#, + ), + FrontendInput::Promql, + PlanningModels::builtin(), + )) + .await + .unwrap_or_else(|error| panic!("{query}: {error}")) + .0 +} + +/// Each answer of the PromQL `query` over `samples`: its labels (the +/// series identity's and each non-empty label column's) and its value. +async fn promql( + query: &str, + metric: &str, + samples: &[RawDataSample], +) -> Vec<(BTreeMap, f64)> { + let plan = plan_promql(query, metric).await; + let mut answers: Vec<_> = run(&plan, |dag| time_series_tables(dag, samples)) + .iter() + .flat_map(|batch| { + batch.to_rows().into_iter().map(|row| { + let value = row + .iter() + .rev() + .find_map(|v| match v { + Value::Float64(v) => Some(*v), + Value::Int64(v) => Some(*v as f64), + _ => None, + }) + .expect("a sample value"); + (series_labels(batch.schema(), &row), value) + }) + }) + .collect(); + answers.sort_by(|a, b| a.0.cmp(&b.0)); + answers +} + +fn labels(pairs: &[(&str, &str)]) -> BTreeMap { + pairs + .iter() + .map(|(name, value)| (name.to_string(), value.to_string())) + .collect() +} + +fn assert_close( + got: &[(BTreeMap, f64)], + expected: &[(BTreeMap, f64)], +) { + assert_eq!(got.len(), expected.len(), "{got:?} vs {expected:?}"); + for ((got_labels, got), (labels, value)) in got.iter().zip(expected) { + assert_eq!(got_labels, labels, "{got:?} vs {expected:?}"); + assert!((got - value).abs() < 1e-12, "{labels:?}: {got} vs {value}"); + } +} + +/// `stddev` and `stdvar` are population statistics: job `a`'s 1, 2, 4 have +/// mean 7/3 and variance 14/9; job `b`'s 2, 2.5 variance 1/16. +#[tokio::test] +async fn promql_stddev_and_stdvar_by_job() { + let stddev = promql("stddev by (job) (m)", "m", &gauge_samples()).await; + assert_close( + &stddev, + &[ + (labels(&[("job", "a")]), (14.0f64 / 9.0).sqrt()), + (labels(&[("job", "b")]), 0.25), + ], + ); + let stdvar = promql("stdvar by (job) (m)", "m", &gauge_samples()).await; + assert_close( + &stdvar, + &[ + (labels(&[("job", "a")]), 14.0 / 9.0), + (labels(&[("job", "b")]), 0.0625), + ], + ); +} + +/// `group` answers 1 per group, whatever the values. +#[tokio::test] +async fn promql_group_by_job() { + let answers = promql("group by (job) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 1.0), + (labels(&[("job", "b")]), 1.0), + ], + ); +} + +/// `count_values` counts each distinct value under a new label holding the +/// value as Go's `FormatFloat(v, 'f', -1, 64)` writes it. +#[tokio::test] +async fn promql_count_values() { + let answers = promql(r#"count_values("v", m)"#, "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("v", "1")]), 1.0), + (labels(&[("v", "2")]), 2.0), + (labels(&[("v", "2.5")]), 1.0), + (labels(&[("v", "4")]), 1.0), + ], + ); +} + +/// `sum without (instance)` groups by every other label, `__name__` +/// dropped: `{job="b"}` and `{job="b", zone="z"}` are different groups. +#[tokio::test] +async fn promql_sum_without_instance() { + let answers = promql("sum without (instance) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b")]), 2.0), + (labels(&[("job", "b"), ("zone", "z")]), 2.5), + ], + ); +} + +/// An exact `topk` over a grouped sum of rates answers the 3 largest of +/// the grouped sum's answers, with their jobs. +#[tokio::test] +async fn promql_exact_topk_of_grouped_rates() { + let sums = promql("sum by (job) (rate(c[5m]))", "c", &counter_samples()).await; + assert_eq!(sums.len(), 4); + let mut expected = sums.clone(); + expected.sort_by(|a, b| b.1.total_cmp(&a.1)); + expected.truncate(3); + expected.sort_by(|a, b| a.0.cmp(&b.0)); + let top = promql( + "topk(3, sum by (job) (rate(c[5m])))", + "c", + &counter_samples(), + ) + .await; + assert_close(&top, &expected); + // Jobs j2..j4: their rates sum to (2n + 1) / 10 per second. + assert_close( + &top, + &[ + (labels(&[("job", "j2")]), 0.5), + (labels(&[("job", "j3")]), 0.7), + (labels(&[("job", "j4")]), 0.9), + ], + ); +} + +/// A per-group `topk` over per-series increases (the shape of PromQL +/// `topk by (region) (4, increase(data_total[5m]))`) answers, per job, the +/// series with the largest increase, with all its labels: instance 2, +/// which counts one more per sample. +#[tokio::test] +async fn promql_exact_topk_by_job_of_increases() { + let increases = promql("increase(c[5m])", "c", &counter_samples()).await; + assert_eq!(increases.len(), 8); + let mut expected: Vec<_> = increases + .iter() + .filter(|(labels, _)| labels.get("instance").map(String::as_str) == Some("2")) + .cloned() + .collect(); + expected.sort_by(|a, b| a.0.cmp(&b.0)); + let top = promql( + "topk by (job) (1, increase(c[5m]))", + "c", + &counter_samples(), + ) + .await; + assert_close(&top, &expected); +} + +// ── SQL ────────────────────────────────────────────────────────────────── + +/// `t(ts, k, x, a, b)`; `x`, `a` and `b` nullable. +fn table_schema() -> Schema { + Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("k", DataType::Utf8, false), + Field::plain("x", DataType::Float64, true), + Field::plain("a", DataType::Float64, true), + Field::plain("b", DataType::Float64, true), + ], + 0, + vec![], + ) +} + +/// One row of `t`: `(k, x, a, b)`. +type TableRow = (&'static str, Option, Option, Option); + +/// Rows `(k, x, a, b)`: `k1` has x 1, 2, 4 and a NULL; `k2` one x; `k3` +/// only a NULL x. `(a, b)` pairs lie on `b = 2a + 1` but one with a NULL. +fn table_rows() -> Vec { + vec![ + ("k1", Some(1.0), Some(1.0), Some(3.0)), + ("k1", Some(2.0), Some(2.0), Some(5.0)), + ("k1", Some(4.0), Some(3.0), Some(7.0)), + ("k1", None, Some(4.0), None), + ("k2", Some(5.0), Some(5.0), Some(11.0)), + ("k3", None, None, Some(1.0)), + ] +} + +/// Each answer row of the SQL `query` over [`table_rows`], a table at +/// rest (an ingesting table hits #1320). +async fn sql(query: &str) -> Vec> { + let catalog = SqlCatalog::new().with_table("t", table_schema()); + let plan = plan_installable(UserInput::new( + &workload(r#"{"sql": "datafusion_sql"}"#, query, "t", r#""at_rest""#), + FrontendInput::Sql { catalog: &catalog }, + PlanningModels::builtin(), + )) + .await + .unwrap_or_else(|error| panic!("{query}: {error}")) + .0; + let mut rows: Vec<_> = run(&plan, |dag| { + let schema = dag + .nodes + .iter() + .find_map(|node| match &node.payload { + PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { schema, .. }) => { + Some(Arc::new(schema.clone())) + } + _ => None, + }) + .expect("the plan scans the table"); + let float = |v: Option| v.map_or(Value::Null, Value::Float64); + let rows = table_rows() + .into_iter() + .enumerate() + .map(|(i, (k, x, a, b))| { + schema + .fields + .iter() + .map(|field| match field.name.as_str() { + "ts" => Value::Timestamp(EVALUATION_MS - 1000 * (i as i64 + 1)), + "k" => Value::Utf8(k.into()), + "x" => float(x), + "a" => float(a), + "b" => float(b), + other => panic!("column {other}"), + }) + .collect() + }) + .collect(); + let batch = Batch::try_new(schema.clone(), rows).unwrap(); + InMemoryDataStore::default() + .with_raw_data( + Source::Table { + table_ref: "t".into(), + }, + schema, + vec![batch], + ) + .unwrap() + }) + .iter() + .flat_map(Batch::to_rows) + .collect(); + rows.sort_by(|a, b| format!("{a:?}").cmp(&format!("{b:?}"))); + rows +} + +fn float(value: &Value) -> Option { + match value { + Value::Float64(v) => Some(*v), + Value::Null => None, + other => panic!("a DOUBLE: {other:?}"), + } +} + +fn assert_floats(row: &[Value], expected: &[Option]) { + let got: Vec<_> = row.iter().map(float).collect(); + assert_eq!(got.len(), expected.len(), "{row:?}"); + for (got, expected) in got.iter().zip(expected) { + match (got, expected) { + (Some(got), Some(expected)) => { + assert!((got - expected).abs() < 1e-12, "{row:?} vs {expected}") + } + _ => assert_eq!(got, expected, "{row:?}"), + } + } +} + +/// SQL `STDDEV` and `VAR_SAMP` are the sample statistics (`STDDEV_SAMP`, +/// `VAR_SAMP`), NULLs ignored, NULL for fewer than two values; +/// `STDDEV_POP` and `VAR_POP` are NULL only for no value. `k1`'s 1, 2, 4: +/// sample variance 7/3, population 14/9. +#[tokio::test] +async fn sql_stddev_and_variance_by_key() { + let rows = + sql("SELECT k, STDDEV(x), VAR_SAMP(x), STDDEV_POP(x), VAR_POP(x) FROM t GROUP BY k").await; + let keys: Vec<_> = rows.iter().map(|row| format!("{:?}", row[0])).collect(); + assert_eq!( + keys, + ["k1", "k2", "k3"].map(|k| format!("{:?}", Value::Utf8(k.into()))) + ); + assert_floats( + &rows[0][1..], + &[ + Some((7.0f64 / 3.0).sqrt()), + Some(7.0 / 3.0), + Some((14.0f64 / 9.0).sqrt()), + Some(14.0 / 9.0), + ], + ); + assert_floats(&rows[1][1..], &[None, None, Some(0.0), Some(0.0)]); + assert_floats(&rows[2][1..], &[None, None, None, None]); +} + +/// `CORR(a, b)` over the rows where both are non-NULL: they lie on a line +/// of positive slope, so the correlation is 1. +#[tokio::test] +async fn sql_corr() { + let rows = sql("SELECT corr(a, b) FROM t").await; + assert_eq!(rows.len(), 1); + assert_floats(&rows[0], &[Some(1.0)]); +} diff --git a/crates/types/src/ir/operator/non_asap.rs b/crates/types/src/ir/operator/non_asap.rs index 49a361f31..ff5103d77 100644 --- a/crates/types/src/ir/operator/non_asap.rs +++ b/crates/types/src/ir/operator/non_asap.rs @@ -637,14 +637,30 @@ impl NonASAPOp { } if child.result_kind() == OperatorResultKind::Relation { for (index, measure) in measures.iter().enumerate() { + // SQL's sample `STDDEV`/`VAR_SAMP` is NULL for a group + // of fewer than two values; the others, and the + // population forms, for a group of no value. + let sample = matches!( + measure, + AggIntent::StdDev { + population: false, + .. + } | AggIntent::Variance { + population: false, + .. + } + ); if matches!( measure, AggIntent::Sum { .. } | AggIntent::Avg { .. } | AggIntent::Min { .. } | AggIntent::Max { .. } + | AggIntent::StdDev { .. } + | AggIntent::Variance { .. } ) { - let nullable = offset == 0 + let nullable = sample + || offset == 0 || filters.get(index).is_some_and(Option::is_some) || measure .input_cols() diff --git a/crates/types/src/ir/schema/aggregate_schema.rs b/crates/types/src/ir/schema/aggregate_schema.rs index f32702697..b571e872b 100644 --- a/crates/types/src/ir/schema/aggregate_schema.rs +++ b/crates/types/src/ir/schema/aggregate_schema.rs @@ -250,6 +250,20 @@ fn measure_input_column(in_schema: &Schema, intent: &AggIntent) -> Option Vec { + let value = crate::ir::scalar::column_resolution::resolve_column_ref( + &ColumnRef::SampleValue, + in_schema, + ) + .ok(); + (0..in_schema.fields.len()) + .filter(|&i| in_schema.time_index != Some(i) && value != Some(i) && !excluded.contains(&i)) + .collect() +} + /// Output schema of a `without(excluded)` aggregate: the kept labels (every /// input label column except the `excluded` positions, the time axis, and the /// sample-value column) followed by the aggregate output column(s). Unlike the @@ -270,21 +284,12 @@ fn without_output_schema( )); } } - let mut out_cols: Vec = Vec::new(); - let mut sources: Vec = Vec::new(); - for (i, col) in in_schema.fields.iter().enumerate() { - let is_time = in_schema.time_index == Some(i); - let is_value = crate::ir::scalar::column_resolution::resolve_column_ref( - &ColumnRef::SampleValue, - in_schema, - ) - .ok() - == Some(i); - if !is_time && !is_value && !excluded.contains(&i) { - out_cols.push(col.clone()); - sources.push(OutputColumnSource::InputColumn(i)); - } - } + let kept = without_kept_columns(in_schema, excluded); + let mut out_cols: Vec = kept.iter().map(|&i| in_schema.fields[i].clone()).collect(); + let mut sources: Vec = kept + .into_iter() + .map(OutputColumnSource::InputColumn) + .collect(); let probe = in_schema .column_id("value") .and_then(|i| in_schema.fields.get(i)) diff --git a/crates/types/src/ir/schema/topk/mod.rs b/crates/types/src/ir/schema/topk/mod.rs index ad4cb7a8c..784517920 100644 --- a/crates/types/src/ir/schema/topk/mod.rs +++ b/crates/types/src/ir/schema/topk/mod.rs @@ -14,7 +14,7 @@ use crate::ir::{ASAPOp, NonASAPOp, Operator, ScalarExpr}; /// `FinalizeExactAccumulator` or `SummaryEstimate`, under the `Project` /// restoring its names (a renaming) and a value `HAVING`'s `Filter`. Over /// per-series rows (PromQL `topk by (..) (k, rows)`) it is the sample value. -pub(crate) fn ranked_column(child: &I) -> Result { +pub fn ranked_column(child: &I) -> Result { match ranked_source(child) { RankedSource::TopK(reduction) => Ok(reduction.group_keys().map_or(0, |keys| keys.len())), RankedSource::OneRowPerItem(Reduction::Reduce(_)) => Ok(child.schema().fields.len() - 1), @@ -140,7 +140,7 @@ pub(crate) fn topk_output_name(k: usize, output_names: &[String]) -> String { /// Ordered projection of ranked rows: partition keys, ranked value, then /// item identity. TopK retains rows; partition keys alone are not unique. -pub(crate) fn ranked_row_projection( +pub fn ranked_row_projection( child: &I, reduction: &Reduction, output_name: &str, From 233228c3484abfefb587a8b588f962e047ba0cba Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 07:24:33 +0000 Subject: [PATCH 2/8] fix(executor): round-1 review fixes for reduce aggregates - count_values ... without (...) installs and matches Prometheus: the IR adds the value-label column under without, and the without projection drops a same-named label from the series identity before grouping. - The IR marks a series identity kept by without as computed (OutputColumnSource::AddedLabel), with the rule documented. - Sort gains fetch: Option, a bounded per-group heap keeping the first n rows of each group; exact top-k and the current-series top-k lower to it instead of Sort then Limit. - The without identity rewrite parses borrowed labels and encodes once. - CoMoments is six flat fields (bit-identical, tested); the variance and correlation kernels read the Float64 array directly. - lower_exact_topk / lower_count_values / top_k_sort extracted from compile_internal; WITHOUT_PROJECTION_HELPER names the fixed helper index; one grouping bounds check. - Test helpers shared through physical_common and time_series; duplicate integration top-k and without cases dropped. - physical-compile-coverage.md: reduce aggregates section, without row fixed. --- crates/executor/src/expressions/mod.rs | 28 +- .../src/operators/aggregate/columnar.rs | 158 ++++++--- .../executor/src/operators/aggregate/mod.rs | 7 +- crates/executor/src/operators/mod.rs | 4 + crates/executor/src/operators/sort.rs | 296 +++++++++++++++-- crates/executor/src/operators/unchecked.rs | 6 +- crates/executor/src/physical_planner/mod.rs | 304 +++++++++++------- crates/executor/tests/blocking_resources.rs | 72 +++++ crates/executor/tests/current_series_heap.rs | 4 +- crates/executor/tests/physical_dag.rs | 2 + crates/executor/tests/physical_semantics.rs | 103 ++++++ crates/executor/tests/promql_values.rs | 1 + .../stored_tumbling_window_memory_limit.rs | 2 +- .../tests/exact_reduce_execution.rs | 172 ++-------- .../tests/per_series_exact_execution.rs | 89 +---- .../tests/physical_common/mod.rs | 64 ++++ crates/planner/tests/reduce_aggregates.rs | 133 ++++---- crates/planner/tests/runtime_install.rs | 99 ++---- crates/planner/tests/time_series/mod.rs | 60 +++- .../types/src/ir/schema/aggregate_schema.rs | 80 ++++- crates/types/src/ir/schema/series_identity.rs | 14 +- .../develop_docs/physical-compile-coverage.md | 18 +- 22 files changed, 1162 insertions(+), 554 deletions(-) diff --git a/crates/executor/src/expressions/mod.rs b/crates/executor/src/expressions/mod.rs index 1cf4103b4..fc97cff31 100644 --- a/crates/executor/src/expressions/mod.rs +++ b/crates/executor/src/expressions/mod.rs @@ -88,7 +88,8 @@ pub enum Expression { /// A Float64 column's value as text, as Go's /// `strconv.FormatFloat(v, 'f', -1, 64)` writes it (PromQL /// `count_values`): the shortest decimal that reads back as `v`, - /// without exponent; `NaN`, `+Inf` and `-Inf`. + /// without exponent; `NaN`, `+Inf` and `-Inf`. `2.5` is `"2.5"`, `1e21` + /// is `"1000000000000000000000"` and `-0.0` is `"-0"`. FloatText(usize), Negate(Box), Arithmetic { @@ -103,6 +104,23 @@ pub enum Expression { Not(Box), IsNull(Box), } +/// The series identity `identity` (`promql_rows::encode_series_identity`'s +/// JSON of every label) less the labels `excluding`, encoded the same way: +/// `{"__name__":"m","instance":"1","job":"a"}` less `instance` and +/// `__name__` is `{"job":"a"}`. The labels are borrowed from `identity` +/// (copied only when JSON escapes them), and `identity` is not checked to be +/// canonical: the executor wrote it. +fn identity_less(identity: &str, excluding: &[String]) -> Result { + use std::borrow::Cow; + /// One label name or value, borrowed unless JSON escapes it. + #[derive(serde::Deserialize, serde::Serialize, PartialEq, Eq, PartialOrd, Ord)] + struct Text<'a>(#[serde(borrow)] Cow<'a, str>); + let mut labels: std::collections::BTreeMap = + serde_json::from_str(identity).map_err(|error| invalid(&error.to_string()))?; + labels.retain(|name, _| !excluding.iter().any(|label| label.as_str() == name.0)); + serde_json::to_string(&labels).map_err(|error| invalid(&error.to_string())) +} + impl Expression { pub fn planner(expression: CompiledExpression) -> Self { Self::Planner(Box::new(expression)) @@ -257,7 +275,7 @@ impl Expression { } FloatText(column) => { if plain(input, *column)? != (&DataType::Float64, false) { - return Err(invalid("value text requires a non-null Float64")); + return Err(invalid("float text requires a non-null Float64")); } Ok((DataType::Utf8, false)) } @@ -428,7 +446,7 @@ impl Expression { } FloatText(column) => { let Value::Float64(value) = row[*column] else { - return Err(invalid("value text requires a Float64")); + return Err(invalid("float text requires a Float64")); }; Value::Utf8( if value.is_nan() { @@ -447,9 +465,7 @@ impl Expression { let mut labels = std::collections::BTreeMap::new(); match &row[*column] { Value::Utf8(identity) => { - labels = - crate::physical_planner::promql_rows::decode_series_identity(identity)?; - labels.retain(|name, _| !excluding.contains(name)); + return Ok(Value::Utf8(identity_less(identity, excluding)?.into())); } Value::Map(entries) => { for (key, value) in entries.iter() { diff --git a/crates/executor/src/operators/aggregate/columnar.rs b/crates/executor/src/operators/aggregate/columnar.rs index e96a058b8..351cbdd32 100644 --- a/crates/executor/src/operators/aggregate/columnar.rs +++ b/crates/executor/src/operators/aggregate/columnar.rs @@ -69,13 +69,14 @@ impl RunningAvg { } /// Welford's running count, mean and sum of squared deviations from the -/// mean, as Prometheus `stddev`/`stdvar` and DataFusion's variance update -/// them: the same operations, so the same rounding. +/// mean (its M2), as Prometheus `stddev`/`stdvar` and DataFusion's variance +/// update them: the same operations, so the same rounding. The values 1, 2 +/// and 4 give count 3, mean 7/3 and a sum of squared deviations of 14/3. #[derive(Clone, Copy, Default)] struct Moments { count: u64, mean: f64, - squared_deviations: f64, + sum_squared_deviations: f64, } impl Moments { @@ -83,31 +84,34 @@ impl Moments { self.count += 1; let delta = value - self.mean; self.mean += delta / self.count as f64; - self.squared_deviations += delta * (value - self.mean); + self.sum_squared_deviations += delta * (value - self.mean); } /// The variance over n (`population`) or n - 1; `None` with no value, - /// or one value of a sample. + /// or one value of a sample. 1, 2 and 4: 14/9, or 7/3 for a sample. fn variance(&self, population: bool) -> Option { let divisor = if population { self.count } else { self.count.saturating_sub(1) }; - (divisor > 0).then(|| self.squared_deviations / divisor as f64) + (divisor > 0).then(|| self.sum_squared_deviations / divisor as f64) } } -/// DataFusion's `corr` state: Welford's running means of both inputs, the -/// co-moment and each input's squared deviations. +/// DataFusion's `corr` state, Welford's update over pairs: the count, both +/// inputs' running means and sums of squared deviations, and the +/// co-moment (the sum of products of both deviations). The pairs (1, 3), +/// (2, 5) and (3, 7) give count 3, means 2 and 5, sums of squared +/// deviations 2 and 8, and co-moment 4: a correlation of 4 / √(2 × 8) = 1. #[derive(Clone, Copy, Default)] struct CoMoments { count: u64, mean_left: f64, mean_right: f64, + sum_squared_deviations_left: f64, + sum_squared_deviations_right: f64, co_moment: f64, - left: Moments, - right: Moments, } impl CoMoments { @@ -115,11 +119,12 @@ impl CoMoments { self.count += 1; let count = self.count as f64; let delta_left = left - self.mean_left; + let delta_right = right - self.mean_right; self.mean_left += delta_left / count; - self.mean_right += (right - self.mean_right) / count; + self.mean_right += delta_right / count; self.co_moment += delta_left * (right - self.mean_right); - self.left.add(left); - self.right.add(right); + self.sum_squared_deviations_left += delta_left * (left - self.mean_left); + self.sum_squared_deviations_right += delta_right * (right - self.mean_right); } /// The population covariance over both population standard @@ -131,9 +136,10 @@ impl CoMoments { if self.mean_left.is_nan() || self.mean_right.is_nan() || self.count < 2 { return None; } - let covariance = self.co_moment / self.count as f64; - let left = self.left.variance(true)?.sqrt(); - let right = self.right.variance(true)?.sqrt(); + let count = self.count as f64; + let covariance = self.co_moment / count; + let left = (self.sum_squared_deviations_left / count).sqrt(); + let right = (self.sum_squared_deviations_right / count).sqrt(); (left != 0. && right != 0.).then(|| covariance / left / right) } } @@ -275,6 +281,21 @@ fn update_measure( .cloned() .ok_or_else(|| invalid("floating aggregate value required")) }; + // A numeric column's values as Float64, Int64 converted: the + // variance and correlation kernels read both. + let float64_values = |column: usize| -> Result { + let array = batch.column(column); + if let Some(floats) = array.as_any().downcast_ref::() { + return Ok(floats.clone()); + } + let integers = array + .as_any() + .downcast_ref::() + .ok_or_else(|| invalid("numeric aggregate value required"))?; + Ok(arrow::compute::kernels::arity::unary(integers, |v| { + v as f64 + })) + }; match (states, measure) { (MeasureStates::Count(counts), _) => { for (row, &group) in groups.iter().enumerate() { @@ -445,18 +466,18 @@ fn update_measure( MeasureStates::Moments(moments), Reduction::StdDev { column, .. } | Reduction::Variance { column, .. }, ) => { - let values = numbers(batch, *column)?; + let values = float64_values(*column)?; for (row, &group) in groups.iter().enumerate() { - if let Some(value) = values[row].filter(|_| selected(row)) { - moments[group as usize].add(value); + if selected(row) && values.is_valid(row) { + moments[group as usize].add(values.value(row)); } } } (MeasureStates::CoMoments(moments), Reduction::PearsonCorr { left, right }) => { - let (lefts, rights) = (numbers(batch, *left)?, numbers(batch, *right)?); + let (lefts, rights) = (float64_values(*left)?, float64_values(*right)?); for (row, &group) in groups.iter().enumerate() { - if let (Some(left), Some(right), true) = (lefts[row], rights[row], selected(row)) { - moments[group as usize].add(left, right); + if selected(row) && lefts.is_valid(row) && rights.is_valid(row) { + moments[group as usize].add(lefts.value(row), rights.value(row)); } } } @@ -466,20 +487,6 @@ fn update_measure( Ok(()) } -/// The values of the numeric column `column` of `batch` as Float64, NULL -/// as `None`. -fn numbers(batch: &Batch, column: usize) -> Result>, Error> { - let array = batch.column(column); - if let Some(floats) = array.as_any().downcast_ref::() { - return Ok(floats.iter().collect()); - } - let integers = array - .as_any() - .downcast_ref::() - .ok_or_else(|| invalid("numeric aggregate value required"))?; - Ok(integers.iter().map(|v| v.map(|v| v as f64)).collect()) -} - /// Each group's value of one measure, in `order`. fn finish_measure( states: MeasureStates, @@ -735,3 +742,82 @@ pub(super) async fn aggregate( order.len(), ) } + +#[cfg(test)] +mod tests { + use super::{CoMoments, Moments}; + + /// The correlation state as two [`Moments`] beside its own count and + /// means, the layout [`CoMoments`] replaced: the reference its result + /// must equal bit for bit. + #[derive(Default)] + struct TwoMoments { + count: u64, + mean_left: f64, + mean_right: f64, + co_moment: f64, + left: Moments, + right: Moments, + } + + impl TwoMoments { + fn add(&mut self, left: f64, right: f64) { + self.count += 1; + let count = self.count as f64; + let delta_left = left - self.mean_left; + self.mean_left += delta_left / count; + self.mean_right += (right - self.mean_right) / count; + self.co_moment += delta_left * (right - self.mean_right); + self.left.add(left); + self.right.add(right); + } + + fn correlation(&self) -> Option { + if self.mean_left.is_nan() && self.mean_right.is_nan() { + return Some(f64::NAN); + } + if self.mean_left.is_nan() || self.mean_right.is_nan() || self.count < 2 { + return None; + } + let covariance = self.co_moment / self.count as f64; + let left = self.left.variance(true)?.sqrt(); + let right = self.right.variance(true)?.sqrt(); + (left != 0. && right != 0.).then(|| covariance / left / right) + } + } + + /// Over pairs of every magnitude, after every pair, the flat state's + /// correlation has the same bits as the two-`Moments` layout's. + #[test] + fn co_moments_equal_two_moments_bit_for_bit() { + let mut flat = CoMoments::default(); + let mut reference = TwoMoments::default(); + let mut state = 0x2545_f491_4f6c_dd1du64; + for i in 0..10_000u64 { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + let unit = (state >> 11) as f64 / (1u64 << 53) as f64; + let scale = [1e-300, 1e-3, 1.0, 1e15, 1e300][i as usize % 5]; + let (left, right) = (1e15 + unit * scale, unit * 3.0 - 0.3 * i as f64); + flat.add(left, right); + reference.add(left, right); + assert_eq!( + flat.correlation().map(f64::to_bits), + reference.correlation().map(f64::to_bits), + "after pair {i}" + ); + } + for (left, right) in [(f64::NAN, 1.0), (1.0, f64::INFINITY)] { + let (mut flat, mut reference) = (CoMoments::default(), TwoMoments::default()); + for _ in 0..3 { + flat.add(left, right); + reference.add(left, right); + } + assert_eq!( + flat.correlation().map(f64::to_bits), + reference.correlation().map(f64::to_bits) + ); + } + } +} diff --git a/crates/executor/src/operators/aggregate/mod.rs b/crates/executor/src/operators/aggregate/mod.rs index 50d966ac2..e4b578d36 100644 --- a/crates/executor/src/operators/aggregate/mod.rs +++ b/crates/executor/src/operators/aggregate/mod.rs @@ -84,7 +84,9 @@ impl Operator { return Err(invalid("numeric aggregate input required")); } // SQL: NULL for fewer than two values (sample) or none - // (population); a PromQL group always has a value. + // (population); a PromQL group always has a value. The + // IR's aggregate schema defines this nullability; this + // derives it again to check the declared output. ( DataType::Float64, !input.has_promql_series_identity() @@ -275,7 +277,8 @@ pub enum Reduction { /// update as Prometheus `stdvar` and DataFusion `var_pop`/`var_samp` /// compute it: the squared deviations' sum over n (population) or /// n - 1 (sample); NaN once a value is NaN or infinite. SQL gives NULL - /// for no value, or one value of a sample; PromQL NaN. + /// for no value, or one value of a sample; PromQL NaN. The values 1, 2 + /// and 4 have mean 7/3: population variance 14/9, sample variance 7/3. Variance { column: usize, population: bool, diff --git a/crates/executor/src/operators/mod.rs b/crates/executor/src/operators/mod.rs index 989950635..fbe6ef9e7 100644 --- a/crates/executor/src/operators/mod.rs +++ b/crates/executor/src/operators/mod.rs @@ -138,6 +138,10 @@ enum Kind { Sort { keys: Vec, groups: Vec, + /// Keep only the first `fetch` rows of each group, in sort order + /// (`topk by (job) (3, m)` gives `Some(3)`); `None` sorts every row. + #[serde(default, skip_serializing_if = "Option::is_none")] + fetch: Option, }, Window { intent: Box>, diff --git a/crates/executor/src/operators/sort.rs b/crates/executor/src/operators/sort.rs index 7c672e26b..a8513be34 100644 --- a/crates/executor/src/operators/sort.rs +++ b/crates/executor/src/operators/sort.rs @@ -1,6 +1,17 @@ use super::*; impl Operator { - pub fn sort(input: SchemaRef, keys: Vec, groups: Vec) -> Result { + /// Sort `input`'s rows by `keys` within each group of the `groups` + /// columns, groups in key order. `fetch` keeps only the first `fetch` + /// rows of each group, in sort order: `topk by (job) (3, m)` is a sort + /// by value descending, grouped by `job`, with `fetch` `Some(3)`, which + /// holds 3 rows per job instead of every input row. `None` sorts every + /// row. + pub fn sort( + input: SchemaRef, + keys: Vec, + groups: Vec, + fetch: Option, + ) -> Result { validate_groups(&input, &groups)?; for key in &keys { if !ordered(plain(&input, key.column)?.0) { @@ -8,7 +19,11 @@ impl Operator { } } Ok(Self { - kind: Kind::Sort { keys, groups }, + kind: Kind::Sort { + keys, + groups, + fetch, + }, inputs: vec![input.clone()], output: input, }) @@ -26,26 +41,31 @@ pub(super) fn execute<'a>( context: RunContext, ) -> Result, Error> { let input = inputs.pop().ok_or_else(|| invalid("input missing"))?; - let Kind::Sort { keys, groups } = &operator.kind else { + let Kind::Sort { + keys, + groups, + fetch, + } = &operator.kind + else { unreachable!() }; + if let Some(fetch) = *fetch { + return Ok(futures::stream::once(sort_with_fetch( + input, + &operator.inputs[0], + keys, + groups, + fetch, + context, + )) + .boxed_local()); + } Ok(futures::stream::once(async move { let input = collect_batch(input, &operator.inputs[0], &context).await?; let batch = &input.batch; let rows = batch.num_rows(); // The sort keys' values, column by column. - let key_values = keys - .iter() - .map(|key| decoded(batch, key.column)) - .collect::, _>>()?; - for values in &key_values { - if values - .iter() - .any(|value| matches!(value, Value::Map(_)) && nested_nan(value)) - { - return Err(invalid("NaN in collection sort key")); - } - } + let key_values = sort_key_values(batch, keys)?; // Groups in key order, then rows by the sort keys; ties keep input // order (a stable sort). let mut table = super::group_table::GroupTable::new(groups.clone()); @@ -66,13 +86,11 @@ pub(super) fn execute<'a>( rank[ids[a as usize] as usize] .cmp(&rank[ids[b as usize] as usize]) .then_with(|| { - keys.iter() - .zip(&key_values) - .map(|(key, values)| { - compare_key(&values[a as usize], &values[b as usize], key) - }) - .find(|order| order.is_ne()) - .unwrap_or(std::cmp::Ordering::Equal) + compare_rows( + keys, + key_values.iter().map(|values| &values[a as usize]), + key_values.iter().map(|values| &values[b as usize]), + ) }) }, &context, @@ -83,6 +101,240 @@ pub(super) fn execute<'a>( .boxed_local()) } +/// The sort keys' values of `batch`, column by column; an error for a NaN +/// inside a collection key, which has no order. +fn sort_key_values(batch: &Batch, keys: &[SortKey]) -> Result>, Error> { + let key_values = keys + .iter() + .map(|key| decoded(batch, key.column)) + .collect::, _>>()?; + for values in &key_values { + if values + .iter() + .any(|value| matches!(value, Value::Map(_)) && nested_nan(value)) + { + return Err(invalid("NaN in collection sort key")); + } + } + Ok(key_values) +} + +/// The order of two rows by `keys`, given each row's key values in order: +/// the first key that differs decides. +fn compare_rows<'v>( + keys: &[SortKey], + a: impl Iterator, + b: impl Iterator, +) -> std::cmp::Ordering { + keys.iter() + .zip(a.zip(b)) + .map(|(key, (a, b))| compare_key(a, b, key)) + .find(|order| order.is_ne()) + .unwrap_or(std::cmp::Ordering::Equal) +} + +/// One row a sort with `fetch` holds: its key values, its position in the +/// input (which breaks ties, as the stable sort keeps input order) and +/// where its row is held: a chunk of held rows, and the row of the input +/// batch that chunk was taken from. +struct HeldRow<'k> { + keys: Vec, + input_position: u64, + chunk: u32, + row: u32, + sort: &'k [SortKey], +} + +impl PartialEq for HeldRow<'_> { + fn eq(&self, other: &Self) -> bool { + self.cmp(other).is_eq() + } +} +impl Eq for HeldRow<'_> {} +impl PartialOrd for HeldRow<'_> { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} +/// Sort order: a row that sorts later is greater, so a group's max-heap +/// has its last kept row on top. +impl Ord for HeldRow<'_> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + compare_rows(self.sort, self.keys.iter(), other.keys.iter()) + .then(self.input_position.cmp(&other.input_position)) + } +} + +/// Rows a sort with `fetch` holds, taken from one input batch: `rows` are +/// that batch's rows `input_rows` (ascending). A row a later batch +/// displaces stays until [`compact`] rewrites the kept rows into one chunk. +struct HeldChunk { + rows: Batch, + input_rows: Vec, +} + +/// The first `fetch` rows of each group of `input` by `keys`, groups in key +/// order, ties in input order, as a full sort and a per-group limit give +/// them. Each group keeps a heap of at most `fetch` rows, read a batch at a +/// time, so it holds `groups × fetch` rows (plus displaced rows, until they +/// are compacted away) instead of its whole input. +async fn sort_with_fetch( + mut input: Input<'_, Batch>, + schema: &SchemaRef, + keys: &[SortKey], + groups: &[usize], + fetch: u64, + context: RunContext, +) -> Result { + use std::collections::BinaryHeap; + let mut table = super::group_table::GroupTable::new(groups.to_vec()); + let mut heaps: Vec> = Vec::new(); + let mut chunks: Vec = Vec::new(); + let mut workspace = Workspace::new(&context)?; + let mut work = Cooperative::new(&context); + let mut input_position = 0u64; + let mut kept_rows = 0usize; + let mut key_bytes = 0usize; + let held_bytes = |chunks: &[HeldChunk], key_bytes: usize, kept_rows: usize| { + chunks + .iter() + .map(|chunk| chunk.rows.memory_bytes() + 4 * chunk.input_rows.len()) + .sum::() + + key_bytes + + kept_rows * std::mem::size_of::() + }; + let fetch = usize::try_from(fetch).unwrap_or(usize::MAX); + while let Some(batch) = input.next().await { + let batch = batch?; + work.checkpoint_rows(batch.num_rows()).await?; + if fetch == 0 { + continue; + } + let held = held_bytes(&chunks, key_bytes, kept_rows); + let ids = chunked_group_ids(&mut table, &batch, &mut workspace, held, &mut work).await?; + heaps.resize_with(table.len(), BinaryHeap::new); + let key_values = sort_key_values(&batch, keys)?; + let chunk = chunks.len() as u32; + // Whether each row of `batch` is still kept. + let mut kept = vec![false; batch.num_rows()]; + for (row, &group) in ids.iter().enumerate() { + let heap = &mut heaps[group as usize]; + let position = input_position + row as u64; + let sorts_before = |last: &HeldRow| { + compare_rows( + keys, + key_values.iter().map(|values| &values[row]), + last.keys.iter(), + ) + .then(position.cmp(&last.input_position)) + .is_lt() + }; + if heap.len() >= fetch && !heap.peek().is_some_and(sorts_before) { + continue; + } + let held = HeldRow { + keys: key_values + .iter() + .map(|values| values[row].clone()) + .collect(), + input_position: position, + chunk, + row: row as u32, + sort: keys, + }; + key_bytes += held.keys.iter().map(Value::bytes).sum::(); + heap.push(held); + kept[row] = true; + kept_rows += 1; + if heap.len() > fetch { + let displaced = heap.pop().expect("a full heap"); + key_bytes -= displaced.keys.iter().map(Value::bytes).sum::(); + kept_rows -= 1; + if displaced.chunk == chunk { + kept[displaced.row as usize] = false; + } + } + } + input_position += batch.num_rows() as u64; + let input_rows: Vec = (0..batch.num_rows() as u32) + .filter(|&row| kept[row as usize]) + .collect(); + if !input_rows.is_empty() { + chunks.push(HeldChunk { + rows: batch.take(&arrow::array::UInt32Array::from(input_rows.clone()))?, + input_rows, + }); + } + let chunk_rows = chunks.iter().map(|c| c.input_rows.len()).sum::(); + if chunk_rows > 2 * kept_rows.max(1) { + compact(schema, &mut chunks, &mut heaps)?; + } + workspace.set_bytes(table.memory_bytes() + held_bytes(&chunks, key_bytes, kept_rows))?; + } + compact(schema, &mut chunks, &mut heaps)?; + // Groups in key order, each group's rows in sort order. + let mut order = Vec::with_capacity(kept_rows); + for group in table.groups_in_key_order() { + if let Some(heap) = heaps.get_mut(group as usize) { + order.extend( + std::mem::take(heap) + .into_sorted_vec() + .iter() + .map(|held| held.row), + ); + } + } + match chunks.pop() { + Some(kept) => kept.rows.take(&arrow::array::UInt32Array::from(order)), + None => Batch::try_new(schema.clone(), Vec::new()), + } +} + +/// Rewrite the rows `heaps` keep into one chunk, in heap order, dropping +/// displaced rows, and point each kept row at its row there. +fn compact( + schema: &SchemaRef, + chunks: &mut Vec, + heaps: &mut [std::collections::BinaryHeap], +) -> Result<(), Error> { + if chunks.is_empty() { + return Ok(()); + } + let mut offsets = Vec::with_capacity(chunks.len()); + let mut total = 0u32; + for chunk in chunks.iter() { + offsets.push(total); + total += chunk.input_rows.len() as u32; + } + let mut rows = Vec::new(); + for heap in heaps.iter_mut() { + let mut held = std::mem::take(heap).into_vec(); + for row in &mut held { + let chunk = &chunks[row.chunk as usize]; + let index = chunk + .input_rows + .binary_search(&row.row) + .expect("a kept row is in its chunk"); + rows.push(offsets[row.chunk as usize] + index as u32); + row.chunk = 0; + row.row = rows.len() as u32 - 1; + } + *heap = std::collections::BinaryHeap::from(held); + } + let all = Batch::concat( + schema, + &chunks + .iter() + .map(|chunk| chunk.rows.clone()) + .collect::>(), + )?; + *chunks = vec![HeldChunk { + rows: all.take(&arrow::array::UInt32Array::from(rows.clone()))?, + input_rows: (0..rows.len() as u32).collect(), + }]; + Ok(()) +} + /// The order of two values of one sort key: NULLs first or last, NaN after /// every number, then the key's direction. fn compare_key(a: &Value, b: &Value, key: &SortKey) -> std::cmp::Ordering { diff --git a/crates/executor/src/operators/unchecked.rs b/crates/executor/src/operators/unchecked.rs index 84c6a44c6..69c47e522 100644 --- a/crates/executor/src/operators/unchecked.rs +++ b/crates/executor/src/operators/unchecked.rs @@ -136,7 +136,11 @@ impl TryFrom for Operator { } } Kind::Limit { n, offset, groups } => Operator::limit(input(0)?, n, offset, groups)?, - Kind::Sort { keys, groups } => Operator::sort(input(0)?, keys, groups)?, + Kind::Sort { + keys, + groups, + fetch, + } => Operator::sort(input(0)?, keys, groups, fetch)?, Kind::Window { intent, coordinate, diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index 06ed66042..4fcb35bc0 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -82,6 +82,11 @@ thread_local! { /// Planner ID range, so every boundary choice yields a sub-DAG of the same /// lowering and candidate cuts need not renumber operators. A node lowering to /// several helpers takes consecutive indices below its base. +/// +/// Lowerings number their helpers from 0 up (`helper_id(id, 0)`, then 1, +/// 2, ...), except the projection a `without` grouping adds before the +/// node's own lowering ([`resolve_without`]): it takes the last index, +/// [`WITHOUT_PROJECTION_HELPER`], so no lowering's helpers reach it. fn helper_id(node: NodeId, index: u64) -> Result { if index >= 1 << 16 { return Err(invalid(format!("node {node} needs more than 2^16 helpers"))); @@ -89,6 +94,11 @@ fn helper_id(node: NodeId, index: u64) -> Result { Ok(u64::MAX - (node << 16) - index) } +/// The helper index of a `without` grouping's projection: the last one, so +/// it never collides with the helpers the node's own lowering numbers from +/// 0. +const WITHOUT_PROJECTION_HELPER: u64 = (1 << 16) - 1; + fn compile_internal( dag: &PhysicalASAPDAG, views: &[MaterializedView], @@ -263,8 +273,7 @@ fn compile_internal( { Some((rewritten, projection)) => { if let Some(projection) = projection { - // Helper 3: the lowerings below use helpers 0 to 2. - let helper = helper_id(id, 3)?; + let helper = helper_id(id, WITHOUT_PROJECTION_HELPER)?; schemas = vec![projection.schema()]; physical_dag.add(helper, inputs, projection)?; inputs = vec![helper]; @@ -275,102 +284,21 @@ fn compile_internal( None => node, }; if let Payload::NonASAP(NonASAPOp::Aggregate { - reduction: PlannerReduction::Reduce(keys), + reduction: PlannerReduction::Reduce(_), measures, - filters, having: None, .. }) = &node.payload { - let [input] = schemas.as_slice() else { - return Err(invalid(format!("node {id}: an aggregate reads one input"))); - }; - // An exact top-k (PromQL `topk`, SQL's heavy hitter): the k - // largest rows of its child per partition, then its ranked - // rows' columns. - if let [AggIntent::TopK { k, .. }] = measures.as_slice() { - if filters.iter().any(Option::is_some) { - return Err(invalid(format!("node {id}: a filtered top-k"))); - } + if let [AggIntent::TopK { .. }] = measures.as_slice() { let Some(NonASAPOp::Aggregate { child, .. }) = restored[&id].non_asap() else { unreachable!("an aggregate restores as an aggregate") }; - let ranked = |error: String| invalid(format!("node {id}: top-k: {error}")); - let value = planner_types::ir::schema::topk::ranked_column(child) - .map_err(|error| ranked(error.to_string()))?; - let columns = planner_types::ir::schema::topk::ranked_row_projection( - child, - &PlannerReduction::Reduce(keys.clone()), - &output.fields[keys.len()].name, - ) - .map_err(|error| ranked(error.to_string()))? - .into_iter() - .map(|item| match item.expr { - ScalarExpr::Column(column) => { - Ok((item.alias.unwrap_or_default(), Expression::Column(column))) - } - _ => Err(ranked("a ranked row projects columns".into())), - }) - .collect::>()?; - let partitions = groups(input, keys)?; - physical_dag.add( - auxiliary, - inputs, - Operator::sort( - input.clone(), - vec![SortKey { - column: value, - descending: true, - nulls_first: false, - }], - partitions.clone(), - )?, - )?; - physical_dag.add( - helper_id(id, 1)?, - vec![auxiliary], - Operator::limit(input.clone(), *k as u64, 0, partitions)?, - )?; - physical_dag.add( - id, - vec![helper_id(id, 1)?], - Operator::project(input.clone(), columns)?.with_output_schema(output)?, - )?; + lower_exact_topk(&mut physical_dag, node, child, inputs, &schemas, &output)?; continue; } - // PromQL `count_values("l", v)`: a count per group and value, - // the value as text in the label `l`, which replaces a - // grouping label of that name. - if let [AggIntent::CountValues { label }] = measures.as_slice() { - if filters.iter().any(Option::is_some) { - return Err(invalid(format!("node {id}: a filtered count_values"))); - } - let value = named_column(input, &ColumnRef::SampleValue)?; - let mut columns: Vec<_> = input - .fields - .iter() - .enumerate() - .map(|(i, field)| (field.name.clone(), Expression::Column(i))) - .collect(); - columns.push((label.clone(), Expression::FloatText(value))); - let text = input.fields.len(); - let mut partitions = groups(input, keys)?; - match partitions - .iter() - .position(|&key| input.fields[key].name == *label) - { - Some(key) => partitions[key] = text, - None => partitions.push(text), - } - let project = Operator::project(input.clone(), columns)?; - let aggregate = Operator::aggregate( - project.schema(), - partitions, - vec![(String::new(), Reduction::Count)], - )? - .with_output_schema(output)?; - physical_dag.add(auxiliary, inputs, project)?; - physical_dag.add(id, vec![auxiliary], aggregate)?; + if let [AggIntent::CountValues { .. }] = measures.as_slice() { + lower_count_values(&mut physical_dag, node, inputs, &schemas, &output)?; continue; } } @@ -449,23 +377,10 @@ fn compile_internal( .map(|name| named_column(&input, &ColumnRef::Named(name.clone()))) .collect::, _>>()?; let value = named_column(&input, &ColumnRef::SampleValue)?; - physical_dag.add( - auxiliary, - inputs, - Operator::sort( - input.clone(), - vec![SortKey { - column: value, - descending: true, - nulls_first: false, - }], - groups.clone(), - )?, - )?; physical_dag.add( id, - vec![auxiliary], - Operator::limit(input, *k as u64, 0, groups)?.with_output_schema(output)?, + inputs, + top_k_sort(&input, value, groups, *k as u64)?.with_output_schema(output)?, )?; continue; } @@ -988,7 +903,10 @@ pub(crate) fn require_sound_merge(input: &SchemaRef) -> Result<(), Error> { } /// Bind a Planner node against the schemas supplied by its deployment edges. -/// This is the same checked path used by complete DAG binding. +/// This is the same checked path used by complete DAG binding for a node +/// that lowers to one operator. A node that lowers to several (an exact +/// top-k, `count_values`, or a `without` grouping over a series identity) +/// is lowered only by [`compile`]; here it is refused. pub fn compile_node(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result { for schema in inputs { crate::values::validate_schema(schema)?; @@ -1171,6 +1089,7 @@ fn bind_operation(node: &PhysicalASAPDAGNode, inputs: &[SchemaRef]) -> Result>()?, groups(input, partition_by)?, + None, ), NonASAPOp::Limit { n, @@ -1566,6 +1485,150 @@ fn named_column(input: &SchemaRef, column: &ColumnRef) -> Result { _ => Err(invalid("summary update column missing or ambiguous")), } } +/// The `Sort` an exact top-k lowers to: the first `k` rows of each +/// `partitions` group by the column `value`, largest first, NULL and NaN +/// last. `topk by (job) (2, m)` keeps each job's two largest samples. +fn top_k_sort( + input: &SchemaRef, + value: usize, + partitions: Vec, + k: u64, +) -> Result { + Operator::sort( + input.clone(), + vec![SortKey { + column: value, + descending: true, + nulls_first: false, + }], + partitions, + Some(k), + ) +} + +/// Lower `node`, an exact top-k `Aggregate{Reduce(keys), [TopK{k}]}` over +/// `child` (PromQL `topk`, SQL's heavy hitter), into `physical_dag`: the +/// first `k` rows of its input per `keys` partition by the ranked value +/// ([`top_k_sort`]), then its ranked rows' columns. `topk(2, sum by (job) +/// (x))` over job sums `a=7, b=10, c=3` answers `b=10` and `a=7`. +fn lower_exact_topk( + physical_dag: &mut CompiledPhysicalDAG, + node: &PhysicalASAPDAGNode, + child: &std::rc::Rc, + inputs: Vec, + schemas: &[SchemaRef], + output: &SchemaRef, +) -> Result<(), Error> { + let id = node.id as NodeId; + let Payload::NonASAP(NonASAPOp::Aggregate { + reduction: PlannerReduction::Reduce(keys), + measures, + filters, + .. + }) = &node.payload + else { + return Err(invalid(format!("node {id}: a top-k is an aggregate"))); + }; + let [AggIntent::TopK { k, .. }] = measures.as_slice() else { + return Err(invalid(format!("node {id}: a top-k has one measure"))); + }; + let [input] = schemas else { + return Err(invalid(format!("node {id}: an aggregate reads one input"))); + }; + if filters.iter().any(Option::is_some) { + return Err(invalid(format!("node {id}: a filtered top-k"))); + } + let ranked = |error: String| invalid(format!("node {id}: top-k: {error}")); + let value = planner_types::ir::schema::topk::ranked_column(child) + .map_err(|error| ranked(error.to_string()))?; + let columns = planner_types::ir::schema::topk::ranked_row_projection( + child, + &PlannerReduction::Reduce(keys.clone()), + &output.fields[keys.len()].name, + ) + .map_err(|error| ranked(error.to_string()))? + .into_iter() + .map(|item| match item.expr { + ScalarExpr::Column(column) => { + Ok((item.alias.unwrap_or_default(), Expression::Column(column))) + } + _ => Err(ranked("a ranked row projects columns".into())), + }) + .collect::>()?; + let sort = helper_id(id, 0)?; + physical_dag.add( + sort, + inputs, + top_k_sort(input, value, groups(input, keys)?, *k as u64)?, + )?; + physical_dag.add( + id, + vec![sort], + Operator::project(input.clone(), columns)?.with_output_schema(output.clone())?, + ) +} + +/// Lower `node`, PromQL `count_values("l", v)` as +/// `Aggregate{Reduce(keys), [CountValues{l}]}`, into `physical_dag`: a +/// projection adding the value as text in the label `l` +/// ([`Expression::FloatText`]), then a count per group and value text. `l` +/// replaces a grouping label of that name: `count_values by (job) ("job", +/// v)` over `{job=a} 2` and `{job=b} 2` answers `{job="2"} 2`. +fn lower_count_values( + physical_dag: &mut CompiledPhysicalDAG, + node: &PhysicalASAPDAGNode, + inputs: Vec, + schemas: &[SchemaRef], + output: &SchemaRef, +) -> Result<(), Error> { + let id = node.id as NodeId; + let Payload::NonASAP(NonASAPOp::Aggregate { + reduction: PlannerReduction::Reduce(keys), + measures, + filters, + .. + }) = &node.payload + else { + return Err(invalid(format!("node {id}: count_values is an aggregate"))); + }; + let [AggIntent::CountValues { label }] = measures.as_slice() else { + return Err(invalid(format!("node {id}: count_values has one measure"))); + }; + let [input] = schemas else { + return Err(invalid(format!("node {id}: an aggregate reads one input"))); + }; + if filters.iter().any(Option::is_some) { + return Err(invalid(format!("node {id}: a filtered count_values"))); + } + let value = named_column(input, &ColumnRef::SampleValue)?; + let mut columns: Vec<_> = input + .fields + .iter() + .enumerate() + .map(|(i, field)| (field.name.clone(), Expression::Column(i))) + .collect(); + columns.push((label.clone(), Expression::FloatText(value))); + let text = input.fields.len(); + let mut partitions = groups(input, keys)?; + match partitions + .iter() + .position(|&key| input.fields[key].name == *label) + { + Some(key) => partitions[key] = text, + None => partitions.push(text), + } + let project = Operator::project(input.clone(), columns)?; + let aggregate = Operator::aggregate( + project.schema(), + partitions, + vec![(String::new(), Reduction::Count)], + )? + .with_output_schema(output.clone())?; + let projection = helper_id(id, 0)?; + physical_dag.add(projection, inputs, project)?; + physical_dag.add(id, vec![projection], aggregate) +} + /// `node` with its `without` grouping (PromQL `sum without (instance) (m)`) /// over `inputs` resolved to the columns it keeps /// ([`without_kept_columns`](planner_types::ir::schema::aggregate_schema::without_kept_columns)), @@ -1579,7 +1642,7 @@ fn resolve_without( node: &PhysicalASAPDAGNode, inputs: &[SchemaRef], ) -> Result)>, Error> { - let (keys, aggregation) = match &node.payload { + let (keys, output_has_one_label_set_per_group) = match &node.payload { // `histogram_quantile` reads its `without([le])` grouping itself. Payload::NonASAP(NonASAPOp::Aggregate { measures, .. }) if matches!(measures.as_slice(), [AggIntent::HistogramQuantile { .. }]) => @@ -1609,14 +1672,24 @@ fn resolve_without( let [input] = inputs else { return Err(invalid("a without grouping reads one input")); }; - if keys.keys().iter().any(|&i| i >= input.fields.len()) { - return Err(invalid("grouping column out of range")); - } + check_grouping_columns(input, keys.keys())?; let excluded: Vec = keys .keys() .iter() .map(|&i| input.fields[i].name.clone()) .collect(); + // `count_values("zone", v)`'s label replaces a kept label `zone`, as + // Prometheus overwrites it before grouping: the kept label set drops it + // too, so `{job=b, zone=z}` and `{job=b}` with the value 2 are one group + // `{job=b, zone="2"}`. + let replaced = match &node.payload { + Payload::NonASAP(NonASAPOp::Aggregate { measures, .. }) => match measures.as_slice() { + [AggIntent::CountValues { label }] => Some(label.clone()), + _ => None, + }, + _ => None, + }; + let removed: Vec = excluded.iter().cloned().chain(replaced).collect(); let kept = planner_types::ir::schema::aggregate_schema::without_kept_columns(input, keys.keys()); let mut rewrites = false; @@ -1630,7 +1703,7 @@ fn resolve_without( rewrites = true; Expression::LabelIdentity { column: i, - excluding: excluded + excluding: removed .iter() .cloned() .chain(["__name__".to_string()]) @@ -1640,7 +1713,7 @@ fn resolve_without( rewrites = true; Expression::LabelSet { column: i, - labels: excluded.clone(), + labels: removed.clone(), without: true, } } else { @@ -1649,7 +1722,7 @@ fn resolve_without( (field.name.clone(), expression) }) .collect(); - if rewrites && !aggregation { + if rewrites && !output_has_one_label_set_per_group { return Err(invalid( "a without partition over series identity needs each row's labels kept", )); @@ -1675,10 +1748,15 @@ fn groups(input: &SchemaRef, groups: &GroupKeys) -> Result, Error> { if groups.is_without() { return Err(invalid("grouping without requires resolved label columns")); } - if groups.keys().iter().any(|&i| i >= input.fields.len()) { + check_grouping_columns(input, groups.keys())?; + Ok(groups.keys().to_vec()) +} +/// An error unless every column of `columns` is a column of `input`. +fn check_grouping_columns(input: &SchemaRef, columns: &[usize]) -> Result<(), Error> { + if columns.iter().any(|&i| i >= input.fields.len()) { return Err(invalid("grouping column out of range")); } - Ok(groups.keys().to_vec()) + Ok(()) } fn expression( expr: &ScalarExpr, diff --git a/crates/executor/tests/blocking_resources.rs b/crates/executor/tests/blocking_resources.rs index ac068af60..b7f7409c8 100644 --- a/crates/executor/tests/blocking_resources.rs +++ b/crates/executor/tests/blocking_resources.rs @@ -118,6 +118,7 @@ fn blocking_reductions_yield_and_release_memory_on_cancellation() { nulls_first: false, }], vec![], + None, ) .unwrap(), Operator::aggregate(schema(1), vec![], vec![("sum".into(), Reduction::Sum(0))]).unwrap(), @@ -138,6 +139,76 @@ fn blocking_reductions_yield_and_release_memory_on_cancellation() { } } +// A sort that keeps the first `fetch` rows of each group holds those rows, +// not its input: 4 groups of 2 rows out of 262,144 rows run in a memory +// limit the full sort's gathered input exceeds. +#[test] +fn sort_with_fetch_holds_groups_times_fetch_rows() { + use asap_executor::operators::SortKey; + let input = schema(2); + let run = |fetch: Option| { + let batches = (0..64) + .map(|batch| { + let rows = (0..4096) + .map(|row| { + let i = batch * 4096 + row; + vec![Value::Int64(i % 4), Value::Int64(i)] + }) + .collect(); + Batch::try_new(input.clone(), rows).unwrap() + }) + .collect(); + let mut dag = PhysicalDAG::default(); + dag.add(0, vec![], Operator::source(input.clone(), batches).unwrap()) + .unwrap(); + let key = SortKey { + column: 1, + descending: true, + nulls_first: false, + }; + dag.add( + 1, + vec![0], + Operator::sort(input.clone(), vec![key], vec![0], fetch).unwrap(), + ) + .unwrap(); + let context = context(1024 * 1024); + let rows = block_on(async { + let mut output = dag.execute(&[1], context.clone()).unwrap().remove(0); + let mut rows = Vec::new(); + while let Some(batch) = output.next().await { + rows.extend(batch?.to_rows()); + } + Ok::<_, Error>(rows) + }); + assert_eq!(context.retained_bytes(), 0); + rows + }; + assert!(matches!(run(None), Err(Error::MemoryLimit))); + let top: Vec = run(Some(2)) + .unwrap() + .iter() + .map(|row| match row[1] { + Value::Int64(i) => i, + _ => unreachable!(), + }) + .collect(); + let last = 64 * 4096 - 1; + assert_eq!( + top, + [ + last - 3, + last - 7, + last - 2, + last - 6, + last - 1, + last - 5, + last, + last - 4 + ] + ); +} + // Merge-sort rounds preserve input order for tied keys across chunk boundaries. #[test] fn cooperative_sort_preserves_ties_across_chunks() { @@ -164,6 +235,7 @@ fn cooperative_sort_preserves_ties_across_chunks() { nulls_first: false, }], vec![], + None, ) .unwrap(), ) diff --git a/crates/executor/tests/current_series_heap.rs b/crates/executor/tests/current_series_heap.rs index 58988fe00..139941149 100644 --- a/crates/executor/tests/current_series_heap.rs +++ b/crates/executor/tests/current_series_heap.rs @@ -355,8 +355,10 @@ fn population_fixture_compiles_with_dynamic_identity() { let physical = compile(&logical, &[], &[logical.roots[0] as u64]).unwrap(); let bytes = String::from_utf8(serde_json::to_vec(&physical).unwrap()).unwrap(); assert!(bytes.contains("CurrentSeries")); + // The top-1 per job is one sort keeping each job's first row. assert!(bytes.contains("Sort")); - assert!(bytes.contains("Limit")); + assert!(bytes.contains(r#""fetch":1"#), "{bytes}"); + assert!(!bytes.contains("Limit")); let rows = [("a", 10_000, 100.), ("a", 50_000, 1.), ("b", 50_000, 20.)] .into_iter() .map(|(member, at, value)| { diff --git a/crates/executor/tests/physical_dag.rs b/crates/executor/tests/physical_dag.rs index e1b2ca278..98c67cc59 100644 --- a/crates/executor/tests/physical_dag.rs +++ b/crates/executor/tests/physical_dag.rs @@ -147,6 +147,7 @@ fn grouped_sort_limit_across_batches() { nulls_first: false, }], vec![0], + None, ) .unwrap(), ) @@ -1073,6 +1074,7 @@ fn assert_weighted_rate_topk(count_sketch: bool) { nulls_first: false, }], vec![0], + None, ) .unwrap(), ) diff --git a/crates/executor/tests/physical_semantics.rs b/crates/executor/tests/physical_semantics.rs index 80abfadee..39c475f49 100644 --- a/crates/executor/tests/physical_semantics.rs +++ b/crates/executor/tests/physical_semantics.rs @@ -190,6 +190,7 @@ fn sort_direction_null_placement_and_ties() { nulls_first, }], vec![], + None, ) .unwrap(); let result = unary( @@ -208,6 +209,108 @@ fn sort_direction_null_placement_and_ties() { } } +// A sort that keeps the first `fetch` rows of each group answers the same +// rows, in the same order, as a full sort and a per-group limit: ties in +// input order, NaN after every number, NULLs where the key puts them, also +// across batches and with fewer rows than `fetch` in a group. +#[test] +fn sort_with_fetch_equals_sort_then_limit() { + let input = schema(&[ + ("g", DataType::Int64, false), + ("v", DataType::Float64, true), + ("id", DataType::Int64, false), + ]); + let values = [ + Some(3.), + Some(f64::NAN), + None, + Some(3.), + Some(1.), + Some(f64::INFINITY), + Some(3.), + Some(-0.), + Some(0.), + Some(f64::NAN), + Some(2.), + None, + Some(3.), + Some(-1.), + ]; + // Groups 0 and 1 hold six or seven rows; group 2 holds one. + let rows: Vec> = values + .iter() + .enumerate() + .map(|(i, v)| { + let group = if i == 13 { 2 } else { i as i64 % 2 }; + vec![ + Value::Int64(group), + v.map_or(Value::Null, Value::Float64), + Value::Int64(i as i64), + ] + }) + .collect(); + let batches = || { + vec![ + rows[..5].to_vec(), + vec![], + rows[5..9].to_vec(), + rows[9..].to_vec(), + ] + }; + let ids = |rows: Vec>| -> Vec { + rows.iter() + .map(|row| match row[2] { + Value::Int64(id) => id, + _ => unreachable!(), + }) + .collect() + }; + for groups in [vec![], vec![0]] { + for (descending, nulls_first) in + [(true, false), (true, true), (false, false), (false, true)] + { + let key = vec![SortKey { + column: 1, + descending, + nulls_first, + }]; + for fetch in [0, 1, 2, 3, 8] { + let fetched = unary( + input.clone(), + batches(), + Operator::sort(input.clone(), key.clone(), groups.clone(), Some(fetch)) + .unwrap(), + ); + let mut dag = PhysicalDAG::default(); + let source = batches() + .into_iter() + .map(|rows| Batch::try_new(input.clone(), rows).unwrap()) + .collect(); + dag.add(0, vec![], Operator::source(input.clone(), source).unwrap()) + .unwrap(); + dag.add( + 1, + vec![0], + Operator::sort(input.clone(), key.clone(), groups.clone(), None).unwrap(), + ) + .unwrap(); + dag.add( + 2, + vec![1], + Operator::limit(input.clone(), fetch, 0, groups.clone()).unwrap(), + ) + .unwrap(); + let limited = collect(&dag, 2); + assert_eq!( + ids(fetched), + ids(limited), + "groups {groups:?}, descending {descending}, nulls first {nulls_first}, fetch {fetch}" + ); + } + } + } +} + // Outer joins preserve unmatched NULLs, while semi/anti joins preserve left multiplicity. #[test] fn joins_nulls_duplicates_and_empty_sides() { diff --git a/crates/executor/tests/promql_values.rs b/crates/executor/tests/promql_values.rs index 9e04d1975..f221f7246 100644 --- a/crates/executor/tests/promql_values.rs +++ b/crates/executor/tests/promql_values.rs @@ -166,6 +166,7 @@ fn compile_sort(descending: bool, grouping: &GroupKeys) -> Result, f64); -fn labels(pairs: &[(&str, &str)]) -> BTreeMap { - pairs - .iter() - .map(|(name, value)| (name.to_string(), value.to_string())) - .collect() -} - /// The gauge `x`: per `(job, instance)` its value. const SERIES: [(&str, &str, f64); 5] = [ ("a", "1", 1.0), @@ -44,78 +33,31 @@ const SERIES: [(&str, &str, f64); 5] = [ ]; /// `query` lowered exactly by the frontend, with series identity, run over -/// [`SERIES`] bound at its `Scan` of `x`: each answer's labels (its series -/// identity's and each non-empty label column's) and value. +/// [`SERIES`]: each answer's labels and value. fn execute(query: &str) -> 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(); - let scan = dag - .nodes + let samples: Vec = SERIES .iter() - .find(|node| { - matches!( - node.payload, - PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) - ) - }) - .unwrap(); - let schema = Arc::new(scan.output_schema.clone()); - let rows = SERIES - .iter() - .map(|(job, instance, value)| { - let labels = labels(&[("__name__", "x"), ("job", job), ("instance", instance)]); - promql_rows::series_row(&schema, &labels, EVALUATION_MS - 500, *value).unwrap() + .map(|(job, instance, value)| RawDataSample { + labels: labels(&[("__name__", "x"), ("job", job), ("instance", instance)]), + timestamp_ms: EVALUATION_MS - 500, + value: *value, }) .collect(); - let batch = Batch::try_new(schema.clone(), rows).unwrap(); - let store = InMemoryDataStore::default() - .with_raw_data( - Source::TimeSeries { metric: "x".into() }, - schema, - vec![batch], - ) - .unwrap(); - let roots = [dag.roots[0] as u64]; - let bound = compile(&dag, &[], &roots) - .unwrap_or_else(|error| panic!("{query}: {error}")) - .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 - }); - let mut answers: Vec = batches + let mut answers: Vec = execute_series(&root, &samples, EVALUATION_MS) .iter() .flat_map(|batch| { batch.to_rows().into_iter().map(|row| { - let mut labels = BTreeMap::new(); - let mut value = None; - for (field, cell) in batch.schema().fields.iter().zip(&row) { - match cell { - Value::Utf8(text) if field.name == PROMQL_SERIES_IDENTITY => { - labels.extend(promql_rows::decode_series_identity(text).unwrap()) - } - Value::Utf8(text) if !text.is_empty() => { - labels.insert(field.name.clone(), text.to_string()); - } - Value::Float64(v) => value = Some(*v), - Value::Int64(v) => value = Some(*v as f64), - _ => {} - } - } - (labels, value.expect("a sample value")) + let value = row.iter().rev().find_map(|cell| match cell { + Value::Float64(v) => Some(*v), + Value::Int64(v) => Some(*v as f64), + _ => None, + }); + ( + series_labels(batch.schema(), &row), + value.expect("a sample value"), + ) }) }) .collect(); @@ -123,66 +65,6 @@ fn execute(query: &str) -> Vec { answers } -/// The frontend's exact `topk` is an `Aggregate` of `TopK`, which the -/// executor runs: the 2 largest job sums, with their jobs. -#[test] -fn exact_topk_of_a_grouped_sum_runs() { - let root = lower_promql("topk(2, sum by (job) (x))", AccuracyTarget::exact()).unwrap(); - assert!( - matches!( - root.non_asap(), - Some(NonASAPOp::Aggregate { measures, .. }) - if matches!(measures.as_slice(), [AggIntent::TopK { .. }]) - ), - "{:?}", - root.operator - ); - assert_eq!( - execute("topk(2, sum by (job) (x))"), - vec![ - (labels(&[("job", "a")]), 7.0), - (labels(&[("job", "b")]), 10.0) - ] - ); -} - -/// `topk by (job)` ranks series within each job and returns them as they -/// are, metric name included. -#[test] -fn exact_topk_per_partition_keeps_series_labels() { - assert_eq!( - execute("topk by (job) (1, x)"), - vec![ - ( - labels(&[("__name__", "x"), ("instance", "2"), ("job", "b")]), - 8.0 - ), - ( - labels(&[("__name__", "x"), ("instance", "3"), ("job", "a")]), - 4.0 - ), - ] - ); -} - -/// `without` groups by the labels it keeps, `__name__` dropped, through -/// the exact `Aggregate` (the planned workload test reaches it through an -/// exact accumulator). -#[test] -fn without_grouping_keeps_every_other_label() { - assert_eq!( - execute("max without (instance) (x)"), - vec![ - (labels(&[("job", "a")]), 4.0), - (labels(&[("job", "b")]), 8.0) - ] - ); - assert_eq!( - execute("count without (job, instance) (x)"), - vec![(labels(&[]), 5.0)] - ); -} - /// A `count_values` label named like a grouping label replaces it, as in /// Prometheus: the groups are the values alone. #[test] diff --git a/crates/integration-tests/tests/per_series_exact_execution.rs b/crates/integration-tests/tests/per_series_exact_execution.rs index d4510a74e..af18d7919 100644 --- a/crates/integration-tests/tests/per_series_exact_execution.rs +++ b/crates/integration-tests/tests/per_series_exact_execution.rs @@ -5,27 +5,15 @@ use std::collections::BTreeMap; use std::rc::Rc; -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_executor::physical_planner::promql_rows; +use asap_executor::values::Value; use asap_integration_tests::fixtures::lower_promql; use asap_types::accuracy::AccuracyTarget; -use asap_types::ir::operator::{Reduction, Source}; -use asap_types::ir::schema::{Schema, PROMQL_SERIES_IDENTITY}; +use asap_types::ir::operator::Reduction; use asap_types::ir::{NonASAPOp, OperatorNode}; -use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; -use futures::{executor::block_on, StreamExt}; -/// One raw data sample: every label of its series, its time and value. -#[derive(Clone)] -struct RawDataSample { - labels: BTreeMap, - timestamp_ms: i64, - value: f64, -} +use crate::physical_common::{execute_series, series_labels, RawDataSample}; /// Evaluation time; every query's window is `(240 s, 300 s]`. const EVALUATION_MS: i64 = 300_000; @@ -74,77 +62,10 @@ fn series(instance: &str, values: &[f64]) -> Vec { .collect() } -/// The labels of an answer row, from the output schema's label columns: -/// its series identity's labels and every other non-empty text column. -fn series_labels(schema: &Schema, row: &[Value]) -> BTreeMap { - let mut labels = BTreeMap::new(); - for (field, cell) in schema.fields.iter().zip(row) { - let Value::Utf8(text) = cell else { continue }; - if field.name == PROMQL_SERIES_IDENTITY { - labels.extend(promql_rows::decode_series_identity(text).unwrap()); - } else if !text.is_empty() { - labels.insert(field.name.clone(), text.to_string()); - } - } - labels -} - /// Run `query`'s exact per-entity plan at query time over `samples`, bound /// at its `Scan` of `x`: each answered series' `instance` and value. fn execute(query: &str, samples: &[RawDataSample]) -> Vec<(String, f64)> { - let plan = per_entity_plan(query); - let dag = asap_types::test_support::forwarders::physical_asap_dag_at_query_time(&plan).unwrap(); - let node = |matches: fn(&PhysicalASAPOperatorPayload) -> bool| { - let found: Vec<_> = dag - .nodes - .iter() - .filter(|node| matches(&node.payload)) - .collect(); - let [node] = found[..] else { - panic!("{query}: one node expected, found {}", found.len()); - }; - node - }; - let scan = node(|payload| { - matches!( - payload, - PhysicalASAPOperatorPayload::NonASAP(NonASAPOp::Scan { .. }) - ) - }); - // The runtime's `x` provider over every sample it received. - let schema = Arc::new(scan.output_schema.clone()); - let rows = samples - .iter() - .map(|s| promql_rows::series_row(&schema, &s.labels, s.timestamp_ms, s.value).unwrap()) - .collect(); - let batch = Batch::try_new(schema.clone(), rows).unwrap(); - let store = InMemoryDataStore::default() - .with_raw_data( - Source::TimeSeries { metric: "x".into() }, - 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 - }); + let batches = execute_series(&per_entity_plan(query), samples, EVALUATION_MS); let mut answers: Vec<(String, f64)> = batches .iter() .flat_map(|batch| { diff --git a/crates/integration-tests/tests/physical_common/mod.rs b/crates/integration-tests/tests/physical_common/mod.rs index d29a3ff36..0f8b39835 100644 --- a/crates/integration-tests/tests/physical_common/mod.rs +++ b/crates/integration-tests/tests/physical_common/mod.rs @@ -120,3 +120,67 @@ pub fn raw_data_store( } store } + +/// One raw data sample: every label of its series (`__name__` included), +/// its time and value. +#[derive(Clone)] +#[allow(dead_code)] +pub struct RawDataSample { + pub labels: std::collections::BTreeMap, + pub timestamp_ms: i64, + pub value: f64, +} + +/// `pairs` as a label set: `labels(&[("job", "a")])` is `{job="a"}`. +#[allow(dead_code)] +pub fn labels(pairs: &[(&str, &str)]) -> std::collections::BTreeMap { + pairs + .iter() + .map(|(name, value)| (name.to_string(), value.to_string())) + .collect() +} + +/// Run `root`, lowered with its series identity +/// (`promql_rows::with_series_identity`), at query time +/// `evaluation_time_ms` over `samples`, bound at each of its `Scan`s as a +/// runtime binds them: the root's batches. +#[allow(dead_code)] +pub fn execute_series( + root: &std::rc::Rc, + samples: &[RawDataSample], + evaluation_time_ms: i64, +) -> Vec { + use asap_executor::physical_planner::{compile, promql_rows}; + let dag = asap_types::test_support::forwarders::physical_asap_dag_at_query_time(root).unwrap(); + let store = raw_data_store(&dag, |_, schema| { + samples + .iter() + .map(|s| promql_rows::series_row(schema, &s.labels, s.timestamp_ms, s.value).unwrap()) + .collect() + }); + let roots = [dag.roots[0] as u64]; + let plan = compile(&dag, &[], &roots).unwrap_or_else(|error| panic!("compile: {error}")); + execute(&plan, store, Scope::Query { evaluation_time_ms }).remove(0) +} + +/// The labels of an answer row, from `schema`'s label columns: its series +/// identity's labels and every other non-empty text column (a label the +/// query grouped by or added). +#[allow(dead_code)] +pub fn series_labels( + schema: &asap_types::ir::schema::Schema, + row: &[asap_executor::values::Value], +) -> std::collections::BTreeMap { + use asap_executor::physical_planner::promql_rows; + use asap_executor::values::Value; + let mut labels = std::collections::BTreeMap::new(); + for (field, cell) in schema.fields.iter().zip(row) { + let Value::Utf8(text) = cell else { continue }; + if field.name == asap_types::ir::schema::PROMQL_SERIES_IDENTITY { + labels.extend(promql_rows::decode_series_identity(text).unwrap()); + } else if !text.is_empty() { + labels.insert(field.name.clone(), text.to_string()); + } + } + labels +} diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs index 9ee5f90ef..f42de40c4 100644 --- a/crates/planner/tests/reduce_aggregates.rs +++ b/crates/planner/tests/reduce_aggregates.rs @@ -8,8 +8,7 @@ use std::collections::BTreeMap; use std::sync::Arc; -use asap_executor::physical_planner::compile; -use asap_executor::runtime::{Limits, RunContext, Scope}; +use asap_executor::runtime::Scope; use asap_executor::sources::InMemoryDataStore; use asap_executor::values::{Batch, Value}; use asap_planner::{ @@ -20,9 +19,8 @@ use asap_types::ir::operator::Source; use asap_types::ir::schema::{DataType, Field, Schema}; use asap_types::ir::NonASAPOp; use asap_types::plan::physical_asap_dag::PhysicalASAPOperatorPayload; -use futures::{executor::block_on, StreamExt}; -use crate::time_series::{series_labels, time_series_tables, RawDataSample}; +use crate::time_series::{labels, run, sample, series_labels, time_series_tables, RawDataSample}; /// The evaluation time of every run. const EVALUATION_MS: i64 = 1_800_000_000_000; @@ -47,55 +45,27 @@ fn workload(language: &str, query: &str, source: &str, arrival: &str) -> Plannin .unwrap() } -fn query_scope() -> RunContext { - RunContext::new( - Scope::Query { - evaluation_time_ms: EVALUATION_MS, - }, - Limits::default(), - ) - .unwrap() -} - -/// The batches of `plan`'s one query, run at query time over `store`: the -/// whole DAG computed from raw data, no materialized view read as stored. -fn run( +/// The batches of `plan`'s one query, run at query time over the store +/// `store` builds for its DAG: the whole DAG computed from raw data, no +/// materialized view read as stored. +fn run_query( plan: &InstallablePlan, store: impl FnOnce(&asap_planner::PhysicalASAPDAG) -> InMemoryDataStore, ) -> Vec { - let dag = &plan.dag; - let roots = [dag.roots[0] as u64]; - let compiled = compile(dag, &[], &roots).expect("the executor compiles the plan"); - let instance = compiled.instantiate(Arc::new(store(dag))).unwrap(); - block_on(async { - let mut output = instance - .execute(compiled.roots(), query_scope()) - .unwrap() - .remove(0); - let mut batches = Vec::new(); - while let Some(batch) = output.next().await { - batches.push((*batch.expect("the query runs")).clone()); - } - batches - }) + let scope = Scope::Query { + evaluation_time_ms: EVALUATION_MS, + }; + run( + &plan.dag, + store(&plan.dag), + &[plan.dag.roots[0] as u64], + scope, + ) + .remove(0) } // ── PromQL ─────────────────────────────────────────────────────────────── -/// One sample of `metric` with `labels` at `timestamp_ms`. -fn sample(metric: &str, labels: &[(&str, &str)], timestamp_ms: i64, value: f64) -> RawDataSample { - let mut all: BTreeMap = labels - .iter() - .map(|(name, value)| (name.to_string(), value.to_string())) - .collect(); - all.insert("__name__".into(), metric.into()); - RawDataSample { - labels: all, - timestamp_ms, - value, - } -} - /// The gauge `m`, one sample per series 5 s before the evaluation: job /// `a` holds 1, 2 and 4, job `b` holds 2 and (with a `zone` label) 2.5. fn gauge_samples() -> Vec { @@ -157,7 +127,7 @@ async fn promql( samples: &[RawDataSample], ) -> Vec<(BTreeMap, f64)> { let plan = plan_promql(query, metric).await; - let mut answers: Vec<_> = run(&plan, |dag| time_series_tables(dag, samples)) + let mut answers: Vec<_> = run_query(&plan, |dag| time_series_tables(dag, samples)) .iter() .flat_map(|batch| { batch.to_rows().into_iter().map(|row| { @@ -178,13 +148,6 @@ async fn promql( answers } -fn labels(pairs: &[(&str, &str)]) -> BTreeMap { - pairs - .iter() - .map(|(name, value)| (name.to_string(), value.to_string())) - .collect() -} - fn assert_close( got: &[(BTreeMap, f64)], expected: &[(BTreeMap, f64)], @@ -247,6 +210,63 @@ async fn promql_count_values() { ); } +/// The gauge `m` for `count_values ... without (instance)`: job `a` holds +/// 1 twice, job `b` holds 2 twice, once with a `zone` label. +fn count_values_samples() -> Vec { + let at = EVALUATION_MS - 5_000; + vec![ + sample("m", &[("job", "a"), ("instance", "1")], at, 1.0), + sample("m", &[("job", "a"), ("instance", "2")], at, 1.0), + sample("m", &[("job", "b"), ("instance", "1")], at, 2.0), + sample( + "m", + &[("job", "b"), ("instance", "2"), ("zone", "z")], + at, + 2.0, + ), + ] +} + +/// `count_values ... without (instance)` groups by every other label and +/// the value label: `{job="b"}` and `{job="b", zone="z"}` stay apart. +#[tokio::test] +async fn promql_count_values_without_instance() { + let answers = promql( + r#"count_values("v", m) without (instance)"#, + "m", + &count_values_samples(), + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a"), ("v", "1")]), 2.0), + (labels(&[("job", "b"), ("v", "2")]), 1.0), + (labels(&[("job", "b"), ("v", "2"), ("zone", "z")]), 1.0), + ], + ); +} + +/// A `count_values` label named like a kept label overwrites it before +/// grouping, as in Prometheus: `zone="z"` becomes `zone="2"`, so both of +/// job `b`'s series count in one group. +#[tokio::test] +async fn promql_count_values_without_overwrites_a_kept_label() { + let answers = promql( + r#"count_values("zone", m) without (instance)"#, + "m", + &count_values_samples(), + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a"), ("zone", "1")]), 2.0), + (labels(&[("job", "b"), ("zone", "2")]), 2.0), + ], + ); +} + /// `sum without (instance)` groups by every other label, `__name__` /// dropped: `{job="b"}` and `{job="b", zone="z"}` are different groups. #[tokio::test] @@ -347,7 +367,8 @@ fn table_rows() -> Vec { } /// Each answer row of the SQL `query` over [`table_rows`], a table at -/// rest (an ingesting table hits #1320). +/// rest: over an ingesting table, Stage 3 may select an ingestion-time +/// view over the whole table, which installation refuses. async fn sql(query: &str) -> Vec> { let catalog = SqlCatalog::new().with_table("t", table_schema()); let plan = plan_installable(UserInput::new( @@ -358,7 +379,7 @@ async fn sql(query: &str) -> Vec> { .await .unwrap_or_else(|error| panic!("{query}: {error}")) .0; - let mut rows: Vec<_> = run(&plan, |dag| { + let mut rows: Vec<_> = run_query(&plan, |dag| { let schema = dag .nodes .iter() diff --git a/crates/planner/tests/runtime_install.rs b/crates/planner/tests/runtime_install.rs index c4a2ca803..d4d85ebe8 100644 --- a/crates/planner/tests/runtime_install.rs +++ b/crates/planner/tests/runtime_install.rs @@ -6,11 +6,9 @@ use std::cell::Cell; use std::collections::{BTreeMap, BTreeSet}; -use std::sync::Arc; use crate::time_series; -use asap_executor::physical_planner::compile; -use asap_executor::runtime::{Limits, OutputStream, RunContext, Scope}; +use asap_executor::runtime::{OutputStream, RunContext, Scope}; use asap_executor::sources::{ DataStore, InMemoryDataStore, MaterializedViewRequest, RawDataRequest, }; @@ -18,15 +16,13 @@ use asap_executor::values::{Batch, Value}; use asap_executor::Error as ExecutorError; use asap_planner::{ plan_installable, CostInputs, DeploymentCapabilities, FrontendInput, InstallablePlan, - MaterializedView, OperatorKind, PhysicalASAPDAG, PlanningModels, PlanningWorkload, - SelectedPlanReport, UserInput, + MaterializedView, OperatorKind, PlanningModels, PlanningWorkload, SelectedPlanReport, + UserInput, }; use asap_types::ir::operator::Source; use asap_types::ir::{ASAPOp, NonASAPOp, Operator}; -use futures::{executor::block_on, StreamExt}; - -use time_series::{series_labels, time_series_tables, RawDataSample}; +use time_series::{run, sample, series_labels, time_series_tables, RawDataSample}; /// Example 4b's evaluation time: the end of the 60-minute window. const EVALUATION_MS: i64 = 3_600_000; @@ -64,18 +60,6 @@ fn workload_every(queries: &[(&str, u64)]) -> PlanningWorkload { .unwrap() } -/// One sample of `metric` for series `instance`. -fn sample(metric: &str, instance: &str, timestamp_ms: i64, value: f64) -> RawDataSample { - RawDataSample { - labels: BTreeMap::from([ - ("__name__".to_string(), metric.to_string()), - ("instance".to_string(), instance.to_string()), - ]), - timestamp_ms, - value, - } -} - thread_local! { /// How many `read_raw_data` calls the runs on this thread made. static RAW_DATA_READS: Cell = const { Cell::new(0) }; @@ -105,39 +89,6 @@ impl DataStore for CountingStore { } } -/// Compile `ids` of `dag` and run them in `scope` over the raw `samples`, -/// as a runtime does with the executor: each id's output batches. -fn run( - dag: &PhysicalASAPDAG, - samples: &[RawDataSample], - ids: &[u64], - scope: Scope, -) -> Vec> { - // The executor's plan for `ids`; every `Scan` it reaches is a raw data - // slot. - let compiled = compile(dag, &[], ids).expect("the executor compiles the plan"); - // The plan's raw samples at the `Scan`s by metric. - let store = time_series_tables(dag, samples); - let instance = compiled - .instantiate(Arc::new(CountingStore(store))) - .unwrap(); - block_on(async { - // The run's scope (ingestion window or query time) and limits. - let context = RunContext::new(scope, Limits::default()).unwrap(); - // One output stream per id. - let outputs = instance.execute(compiled.roots(), context).unwrap(); - futures::future::join_all(outputs.into_iter().map(|mut output| async move { - // Its batches, collected. - let mut batches = Vec::new(); - while let Some(batch) = output.next().await { - batches.push((*batch.unwrap()).clone()); - } - batches - })) - .await - }) -} - /// One answer row: its series labels and its value. type Answer = (BTreeMap, f64); @@ -328,14 +279,25 @@ async fn an_instant_selector_reads_the_latest_sample_per_series() { }); assert_eq!(retention.lookback_ms(), Some(1_000)); let samples = [ - sample("latency_ms", "a", EVALUATION_MS - 900, 1.0), - sample("latency_ms", "a", EVALUATION_MS - 100, 10.0), - sample("latency_ms", "b", EVALUATION_MS - 200, 100.0), + sample("latency_ms", &[("instance", "a")], EVALUATION_MS - 900, 1.0), + sample( + "latency_ms", + &[("instance", "a")], + EVALUATION_MS - 100, + 10.0, + ), + sample( + "latency_ms", + &[("instance", "b")], + EVALUATION_MS - 200, + 100.0, + ), ]; let query = Scope::Query { evaluation_time_ms: EVALUATION_MS, }; - let answered = answers(&run(dag, &samples, &query_roots(&plan), query)[0]); + let store = CountingStore(time_series_tables(dag, &samples)); + let answered = answers(&run(dag, store, &query_roots(&plan), query)[0]); assert_eq!(answered.len(), 1); assert_eq!(answered[0].1, 110.0); } @@ -496,19 +458,6 @@ async fn plan_every_minute(queries: &[(&str, &str)]) -> InstallablePlan { planned.unwrap().0 } -/// One sample of `m` for series `(instance, job)`. -fn m_sample(instance: &str, job: &str, timestamp_ms: i64, value: f64) -> RawDataSample { - RawDataSample { - labels: BTreeMap::from([ - ("__name__".to_string(), "m".to_string()), - ("instance".to_string(), instance.to_string()), - ("job".to_string(), job.to_string()), - ]), - timestamp_ms, - value, - } -} - /// Samples of `m` for four series (instances `a`..`d`, jobs `x` and `y`) /// every 15 s from 8 minutes before [`EVALUATION_MS`] through it, so some /// lie exactly on the minute boundaries that tumbling windows end at. @@ -519,7 +468,12 @@ fn m_samples() -> Vec { for step in 0..=32i64 { let timestamp_ms = EVALUATION_MS - 480_000 + step * 15_000; let value = (index as f64 + 1.0) * 10.0 + step as f64; - samples.push(m_sample(instance, job, timestamp_ms, value)); + samples.push(sample( + "m", + &[("instance", instance), ("job", job)], + timestamp_ms, + value, + )); } } samples @@ -558,7 +512,8 @@ fn query_answers(plan: &InstallablePlan, samples: &[RawDataSample]) -> Vec BTreeMap { + pairs + .iter() + .map(|(name, value)| (name.to_string(), value.to_string())) + .collect() +} + +/// One sample of `metric` for the series with `labels` (and `__name__` +/// `metric`) at `timestamp_ms`: `sample("m", &[("job", "a")], 0, 1.0)` is +/// `m{job="a"} 1` at time 0. +pub fn sample( + metric: &str, + labels: &[(&str, &str)], + timestamp_ms: i64, + value: f64, +) -> RawDataSample { + let mut all = self::labels(labels); + all.insert("__name__".into(), metric.into()); + RawDataSample { + labels: all, + timestamp_ms, + value, + } +} + +/// Compile `ids` of `dag` and run them in `scope` over `store`, as a +/// runtime does with the executor: each id's output batches. +pub fn run( + dag: &PhysicalASAPDAG, + store: impl DataStore + 'static, + ids: &[u64], + scope: Scope, +) -> Vec> { + // The executor's plan for `ids`; every `Scan` it reaches is a raw data + // slot. + let compiled = compile(dag, &[], ids).expect("the executor compiles the plan"); + let instance = compiled.instantiate(Arc::new(store)).unwrap(); + block_on(async { + // The run's scope (ingestion window or query time) and limits. + let context = RunContext::new(scope, Limits::default()).unwrap(); + // One output stream per id. + let outputs = instance.execute(compiled.roots(), context).unwrap(); + futures::future::join_all(outputs.into_iter().map(|mut output| async move { + // Its batches, collected. + let mut batches = Vec::new(); + while let Some(batch) = output.next().await { + batches.push((*batch.expect("the query runs")).clone()); + } + batches + })) + .await + }) +} + /// The series identity column's text: the canonical JSON of every label /// ([`PROMQL_SERIES_IDENTITY`]). pub fn series_identity(labels: &BTreeMap) -> String { diff --git a/crates/types/src/ir/schema/aggregate_schema.rs b/crates/types/src/ir/schema/aggregate_schema.rs index b571e872b..133a85506 100644 --- a/crates/types/src/ir/schema/aggregate_schema.rs +++ b/crates/types/src/ir/schema/aggregate_schema.rs @@ -14,7 +14,9 @@ use crate::ir::SchemaDerivationError; /// Where one output column of an aggregate comes from: an input column it /// keeps, a measure's value (with the input column the schema types it by, /// the measure's own or the probe value column), or a label column the -/// measure adds (`count_values`' and `absent`'s). `sum by (job) (x)` → +/// aggregate computes (`count_values`' and `absent`'s, and a series identity +/// a `without` grouping keeps, its labels less the excluded ones and +/// `__name__`). `sum by (job) (x)` → /// `[InputColumn(job), Measure { index: 0, input_column: Some(value) }]`. The one layout the output schema /// ([`aggregate_output_schema`]) and the group properties read. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -252,7 +254,9 @@ fn measure_input_column(in_schema: &Schema, intent: &AggIntent) -> Option Vec { let value = crate::ir::scalar::column_resolution::resolve_column_ref( &ColumnRef::SampleValue, @@ -266,7 +270,15 @@ pub fn without_kept_columns(in_schema: &Schema, excluded: &[ColumnId]) -> Vec = kept.iter().map(|&i| in_schema.fields[i].clone()).collect(); let mut sources: Vec = kept .into_iter() - .map(OutputColumnSource::InputColumn) + .map(|i| { + let field = &in_schema.fields[i]; + let label_set = field.name == super::PROMQL_SERIES_IDENTITY + || matches!(field.dtype, FieldDataType::Plain(DataType::Map { .. })); + if label_set { + OutputColumnSource::AddedLabel + } else { + OutputColumnSource::InputColumn(i) + } + }) .collect(); let probe = in_schema .column_id("value") @@ -296,6 +317,14 @@ fn without_output_schema( .cloned() .unwrap_or_else(|| Field::plain("value", DataType::Float64, false)); for (i, intent) in measures.iter().enumerate() { + // `count_values("l", v)`'s label `l`, unless a kept column of that + // name is replaced by it, as on the `by` path. + if let AggIntent::CountValues { label } = intent { + if !out_cols.iter().any(|c| c.name == *label) { + out_cols.push(Field::plain(label.clone(), DataType::Utf8, false)); + sources.push(OutputColumnSource::AddedLabel); + } + } // Only the output *type* is read from here, so the leading column is // enough for the multi-column intents: `Cardinality` and `PearsonCorr` // both have a fixed output type that ignores it. @@ -392,10 +421,10 @@ mod tests { ); } - /// `count_values by (job) ("version", v)` adds the `version` label before - /// the count; `count_values without (host)` adds none, like its schema. + /// `count_values by (job) ("version", v)` and `count_values without + /// (host) ("version", v)` both add the `version` label before the count. #[test] - fn count_values_adds_its_label_only_by_keys() { + fn count_values_adds_its_label_by_and_without_keys() { use OutputColumnSource::*; let count_values = AggIntent::CountValues { label: "version".into(), @@ -418,6 +447,43 @@ mod tests { ), vec![ InputColumn(2), + AddedLabel, + Measure { + index: 0, + input_column: Some(1) + } + ] + ); + } + + /// `sum without (host)` over `[ts, value, job, host, identity]` computes + /// the kept series identity (its labels less `host` and `__name__`), so + /// its source is an added label, not the input identity. + #[test] + fn a_without_aggregate_computes_its_kept_series_identity() { + use OutputColumnSource::*; + let mut input = series(); + input.fields.push(col( + crate::ir::schema::PROMQL_SERIES_IDENTITY, + DataType::Utf8, + false, + )); + let (schema, sources) = aggregate_output( + &input, + &Reduction::Reduce(GroupKeys::without(vec![3])), + &[AggIntent::Sum { col: None }], + &[], + ) + .unwrap(); + assert_eq!( + schema.fields[1].name, + crate::ir::schema::PROMQL_SERIES_IDENTITY + ); + assert_eq!( + sources, + vec![ + InputColumn(2), + AddedLabel, Measure { index: 0, input_column: Some(1) diff --git a/crates/types/src/ir/schema/series_identity.rs b/crates/types/src/ir/schema/series_identity.rs index 3edf058b7..8b3ba851f 100644 --- a/crates/types/src/ir/schema/series_identity.rs +++ b/crates/types/src/ir/schema/series_identity.rs @@ -5,10 +5,16 @@ use crate::ir::schema::*; /// column contains every dynamic source label. It does not assert that the /// query's projected labels are the full label set. /// -/// This realization supports explicit `by` grouping and per-series computation. -/// Operators that rewrite or implicitly match dynamic label sets require their -/// own realization; they must not accidentally treat the opaque identity as a -/// user label or silently discard it. +/// This realization supports explicit `by` grouping, `without` grouping and +/// per-series computation. A `without` grouping keeps the identity with its +/// excluded labels and `__name__` removed, computed when the plan is +/// compiled: `sum without (instance) (m)` turns `{__name__=m, job=a, +/// instance=1, zone=z}` into `{job=a, zone=z}`, and the aggregate's output +/// schema marks that column as computed, not copied +/// (`aggregate_schema::without_output_schema`). Operators that rewrite or +/// implicitly match dynamic label sets require their own realization; they +/// must not accidentally treat the opaque identity as a user label or +/// silently discard it. pub fn with_promql_series_identity( root: &std::rc::Rc, ) -> Result, String> { diff --git a/docs/develop_docs/physical-compile-coverage.md b/docs/develop_docs/physical-compile-coverage.md index a0b221ddf..09d49ec7e 100644 --- a/docs/develop_docs/physical-compile-coverage.md +++ b/docs/develop_docs/physical-compile-coverage.md @@ -113,14 +113,16 @@ Totals after this change: 19 Supported, 5 Partial, 5 Missing, 2 Backend. | Row | Change | |---|---| -| 1 | Vector-vector arithmetic with one-to-one matching, `on`, and `ignoring`. Each side is reduced to its matching labels, then matched on equal label sets. A duplicate match group is an error, as in Prometheus. The result drops `__name__`. `without` aggregation. `irate`, `idelta`, `changes`, `resets`, `last_over_time`, and exact `quantile_over_time`. `@ ` on selectors. Still Partial. | +| 1 | Vector-vector arithmetic with one-to-one matching, `on`, and `ignoring`. Each side is reduced to its matching labels, then matched on equal label sets. A duplicate match group is an error, as in Prometheus. The result drops `__name__`. `irate`, `idelta`, `changes`, `resets`, `last_over_time`, and exact `quantile_over_time`. `@ ` on selectors. Still Partial. | | 11 | The range functions above, over raw selector rows. | | 12 | `@ ` on subqueries anchors the step grid. An inner selector's `@` pins every step. Still Partial. | `@` fixes the instant a window ends at, before `offset`; the output keeps the query's evaluation time. The raw rows must cover the window at that instant. -Vector matching and `without` rewrite the series identity, so their results -already lack `__name__`. Other results keep it; the query adapter still drops +Vector matching rewrites the series identity, so its results already lack +`__name__`. (This section once listed `without` aggregation too, but +`compile` refused it with "grouping without requires resolved label +columns"; it is covered in the reduce aggregates section below.) Other results keep it; the query adapter still drops it. (Range functions drop it too since the comparison change below.) Totals are unchanged: 19 Supported, 5 Partial, 5 Missing, 2 Backend. @@ -202,6 +204,16 @@ quantile's is named `histogram_quantile`. Both are tracked in issue #911. Totals after this change: 21 Supported, 5 Partial, 3 Missing, 2 Backend. +## Covered by exact reduce aggregates + +| Row | Change | +|---|---| +| 1 | PromQL `stddev`, `stdvar`, `group` and `count_values`, with `by` or `without`. `stddev`/`stdvar` use Welford's update, as Prometheus does: `stddev by (job) (m)` over job `a`'s 1, 2 and 4 is the square root of 14/9. `group` is 1 per group. `count_values("v", m)` writes each value as Go's `FormatFloat(v, 'f', -1, 64)` does (`0.30000000000000004`, `1000000000000000000000`, `NaN`, `+Inf`), and its label replaces a grouping or kept label of that name, as in Prometheus. Still Partial. | +| 2 | `without` grouping over the series identity: `sum without (instance) (m)` groups by every label but `instance` and `__name__`, so `{job=a, instance=1, zone=z}` answers as `{job=a, zone=z}`. The IR marks that kept identity as computed, not copied. A ranking (`topk`, `Sort`, `Limit`) partitioned `without` over the identity is refused, since it keeps each row's labels. SQL `STDDEV`, `STDDEV_SAMP`, `STDDEV_POP`, `VAR_SAMP`, `VAR_POP` and `corr` over Float64 or Int64 columns: NULL for fewer than two values (sample forms) or none (population forms), `corr` NULL for a constant input. | +| 3 | An exact `topk`, `Aggregate{Reduce, [TopK]}`, lowers to a `Sort` that keeps the first k rows per partition (value descending, NULLs and NaN last; `fetch: Some(k)`, a bounded heap of k rows per partition) and a `Project` of the ranked rows. The current-series top-k (row 21) lowers to the same sort. `topk(2, sum by (job) (rate(c[5m])))` answers the two largest job sums with their jobs; `topk by (job) (1, m)` keeps each series' full labels, `__name__` included. | + +Totals are unchanged: 21 Supported, 5 Partial, 3 Missing, 2 Backend. + ## Remaining In order of backend usage: From 2b25afc2b8270deaaf0f5d3fbfa25e2fa0507cdb Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 08:11:50 +0000 Subject: [PATCH 3/8] fix(executor): round-2 review fixes for reduce aggregates - count_values: a label that collides with a by or kept column makes that position a computed Utf8 label; a label the without list names is removed again, counting per kept group (IR schema and lowering). - One label-set predicate, ir::schema::is_label_set_column, read by the IR and resolve_without; one count_values label push for both paths. - compile_internal binds keys, filters, k and label once; the top-k and count_values lowerings return their operators. - promql_rows::series_identity_without: streaming rewrite of the canonical identity JSON, byte-equal to decode/filter/encode. - Sort with fetch: candidates compared with the heap's last row straight from the Arrow arrays, keys decoded only for kept rows, replace-top on a Vec heap, HeldRow without the sort-key slice, chunks gathered with one interleave (Batch::interleave). - OutputColumnSource::AddedLabel renamed ComputedLabel; nits. --- crates/executor/src/expressions/mod.rs | 24 +- .../src/operators/aggregate/columnar.rs | 25 +- crates/executor/src/operators/sort.rs | 434 +++++++++++++----- crates/executor/src/physical_planner/mod.rs | 200 ++++---- .../src/physical_planner/promql_rows.rs | 162 +++++++ crates/executor/src/values/batch/mod.rs | 53 +++ crates/executor/tests/physical_semantics.rs | 73 +++ crates/planner/tests/reduce_aggregates.rs | 73 +++ .../types/src/ir/schema/aggregate_schema.rs | 131 ++++-- crates/types/src/ir/schema/mod.rs | 10 + crates/types/src/ir/schema/series_identity.rs | 4 +- .../stages/logical/group/column_properties.rs | 2 +- .../develop_docs/physical-compile-coverage.md | 5 +- 13 files changed, 900 insertions(+), 296 deletions(-) diff --git a/crates/executor/src/expressions/mod.rs b/crates/executor/src/expressions/mod.rs index fc97cff31..2934d347c 100644 --- a/crates/executor/src/expressions/mod.rs +++ b/crates/executor/src/expressions/mod.rs @@ -104,23 +104,6 @@ pub enum Expression { Not(Box), IsNull(Box), } -/// The series identity `identity` (`promql_rows::encode_series_identity`'s -/// JSON of every label) less the labels `excluding`, encoded the same way: -/// `{"__name__":"m","instance":"1","job":"a"}` less `instance` and -/// `__name__` is `{"job":"a"}`. The labels are borrowed from `identity` -/// (copied only when JSON escapes them), and `identity` is not checked to be -/// canonical: the executor wrote it. -fn identity_less(identity: &str, excluding: &[String]) -> Result { - use std::borrow::Cow; - /// One label name or value, borrowed unless JSON escapes it. - #[derive(serde::Deserialize, serde::Serialize, PartialEq, Eq, PartialOrd, Ord)] - struct Text<'a>(#[serde(borrow)] Cow<'a, str>); - let mut labels: std::collections::BTreeMap = - serde_json::from_str(identity).map_err(|error| invalid(&error.to_string()))?; - labels.retain(|name, _| !excluding.iter().any(|label| label.as_str() == name.0)); - serde_json::to_string(&labels).map_err(|error| invalid(&error.to_string())) -} - impl Expression { pub fn planner(expression: CompiledExpression) -> Self { Self::Planner(Box::new(expression)) @@ -465,7 +448,12 @@ impl Expression { let mut labels = std::collections::BTreeMap::new(); match &row[*column] { Value::Utf8(identity) => { - return Ok(Value::Utf8(identity_less(identity, excluding)?.into())); + return Ok(Value::Utf8( + crate::physical_planner::promql_rows::series_identity_without( + identity, excluding, + )? + .into(), + )); } Value::Map(entries) => { for (key, value) in entries.iter() { diff --git a/crates/executor/src/operators/aggregate/columnar.rs b/crates/executor/src/operators/aggregate/columnar.rs index 351cbdd32..8bf04a1e9 100644 --- a/crates/executor/src/operators/aggregate/columnar.rs +++ b/crates/executor/src/operators/aggregate/columnar.rs @@ -284,17 +284,16 @@ fn update_measure( // A numeric column's values as Float64, Int64 converted: the // variance and correlation kernels read both. let float64_values = |column: usize| -> Result { - let array = batch.column(column); - if let Some(floats) = array.as_any().downcast_ref::() { - return Ok(floats.clone()); - } - let integers = array - .as_any() - .downcast_ref::() - .ok_or_else(|| invalid("numeric aggregate value required"))?; - Ok(arrow::compute::kernels::arity::unary(integers, |v| { - v as f64 - })) + floats(column).or_else(|_| { + let integers = batch + .column(column) + .as_any() + .downcast_ref::() + .ok_or_else(|| invalid("numeric aggregate value required"))?; + Ok(arrow::compute::kernels::arity::unary(integers, |v| { + v as f64 + })) + }) }; match (states, measure) { (MeasureStates::Count(counts), _) => { @@ -749,7 +748,9 @@ mod tests { /// The correlation state as two [`Moments`] beside its own count and /// means, the layout [`CoMoments`] replaced: the reference its result - /// must equal bit for bit. + /// must equal bit for bit. A one-time proof that flattening the state + /// changed no result; the lasting checks are the documented examples + /// in `physical_semantics::correlation_follows_datafusion`. #[derive(Default)] struct TwoMoments { count: u64, diff --git a/crates/executor/src/operators/sort.rs b/crates/executor/src/operators/sort.rs index a8513be34..fa5b8d01e 100644 --- a/crates/executor/src/operators/sort.rs +++ b/crates/executor/src/operators/sort.rs @@ -1,4 +1,6 @@ use super::*; +use arrow::array::{Array, ArrayRef}; +use std::cmp::Ordering; impl Operator { /// Sort `input`'s rows by `keys` within each group of the `groups` /// columns, groups in key order. `fetch` keeps only the first `fetch` @@ -133,51 +135,210 @@ fn compare_rows<'v>( .unwrap_or(std::cmp::Ordering::Equal) } +/// The sort order of two rows, given the order of their key values: ties +/// by input position, as the stable sort keeps input order. A row that +/// sorts later is greater. +fn sort_order(key_order: Ordering, a_position: u64, b_position: u64) -> Ordering { + key_order.then(a_position.cmp(&b_position)) +} + /// One row a sort with `fetch` holds: its key values, its position in the -/// input (which breaks ties, as the stable sort keeps input order) and -/// where its row is held: a chunk of held rows, and the row of the input -/// batch that chunk was taken from. -struct HeldRow<'k> { - keys: Vec, +/// input (which breaks ties) and where its row is held: row `row` of chunk +/// `chunk`, or, while `chunk` is [`READING`], row `row` of the batch being +/// read. +struct HeldRow { + keys: Box<[Value]>, input_position: u64, chunk: u32, row: u32, - sort: &'k [SortKey], } -impl PartialEq for HeldRow<'_> { - fn eq(&self, other: &Self) -> bool { - self.cmp(other).is_eq() +/// The chunk of a held row from the batch being read, until that batch's +/// kept rows are taken into a chunk. +const READING: u32 = u32::MAX; + +/// Add `row` to `heap`, a max-heap in a `Vec` by `order` (the row that sorts +/// last on top). +fn heap_push( + heap: &mut Vec, + row: HeldRow, + order: &impl Fn(&HeldRow, &HeldRow) -> Ordering, +) { + heap.push(row); + let mut child = heap.len() - 1; + while child > 0 { + let parent = (child - 1) / 2; + if order(&heap[child], &heap[parent]).is_le() { + break; + } + heap.swap(child, parent); + child = parent; } } -impl Eq for HeldRow<'_> {} -impl PartialOrd for HeldRow<'_> { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) + +/// Restore `heap` (see [`heap_push`]) after its top row was overwritten in +/// place by a row that sorts earlier: one sift down, where a push then a pop +/// reorder twice and allocate a new row. +fn heap_sift_down_top(heap: &mut [HeldRow], order: &impl Fn(&HeldRow, &HeldRow) -> Ordering) { + let mut parent = 0; + loop { + let (left, right) = (2 * parent + 1, 2 * parent + 2); + let mut largest = parent; + if left < heap.len() && order(&heap[left], &heap[largest]).is_gt() { + largest = left; + } + if right < heap.len() && order(&heap[right], &heap[largest]).is_gt() { + largest = right; + } + if largest == parent { + return; + } + heap.swap(parent, largest); + parent = largest; } } -/// Sort order: a row that sorts later is greater, so a group's max-heap -/// has its last kept row on top. -impl Ord for HeldRow<'_> { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - compare_rows(self.sort, self.keys.iter(), other.keys.iter()) - .then(self.input_position.cmp(&other.input_position)) - } + +/// Row `i` of `left` against row `j` of `right` by one sort key, both arrays +/// of the key's column, in [`compare_key`]'s order read straight from the +/// arrays: NULLs first or last, NaN after every number in either direction, +/// `-0` equal to `0`. A type Arrow cannot compare (a map) is decoded and +/// compared by value. +fn key_comparator( + left: &ArrayRef, + right: &ArrayRef, + dtype: &DataType, + key: &SortKey, +) -> Result { + use arrow::array::{AsArray, DynComparator}; + use arrow::datatypes::Float64Type; + let Ok(values) = arrow::array::make_comparator(left, right, Default::default()) else { + let depth = crate::values::arrow_columns::ColumnDepth::BatchColumn; + let left = crate::values::arrow_columns::decode(left.as_ref(), dtype, depth); + let right = crate::values::arrow_columns::decode(right.as_ref(), dtype, depth); + let key = key.clone(); + return Ok(Box::new(move |i, j| compare_key(&left[i], &right[j], &key))); + }; + let (left_nulls, right_nulls) = (left.logical_nulls(), right.logical_nulls()); + let floats = left + .as_primitive_opt::() + .cloned() + .zip(right.as_primitive_opt::().cloned()); + let SortKey { + descending, + nulls_first, + .. + } = *key; + let compare: DynComparator = Box::new(move |i, j| { + let left_null = left_nulls.as_ref().is_some_and(|nulls| nulls.is_null(i)); + let right_null = right_nulls.as_ref().is_some_and(|nulls| nulls.is_null(j)); + match (left_null, right_null) { + (true, true) => return Ordering::Equal, + (true, false) => { + return if nulls_first { + Ordering::Less + } else { + Ordering::Greater + } + } + (false, true) => { + return if nulls_first { + Ordering::Greater + } else { + Ordering::Less + } + } + (false, false) => {} + } + let order = match &floats { + Some((left, right)) => { + let (a, b) = (left.value(i), right.value(j)); + if a.is_nan() || b.is_nan() { + return a.is_nan().cmp(&b.is_nan()); + } + a.partial_cmp(&b).expect("neither is NaN") + } + None => values(i, j), + }; + if descending { + order.reverse() + } else { + order + } + }); + Ok(compare) } -/// Rows a sort with `fetch` holds, taken from one input batch: `rows` are -/// that batch's rows `input_rows` (ascending). A row a later batch -/// displaces stays until [`compact`] rewrites the kept rows into one chunk. -struct HeldChunk { - rows: Batch, - input_rows: Vec, +/// The sort-key values of the rows of one batch a sort with `fetch` keeps, +/// decoded only for those rows: a number, time, date or bool key reads its +/// cell, another type decodes its column once, at the first kept row. An +/// error for a NaN inside a collection key, which has no order. +struct KeptKeyValues<'b> { + batch: &'b Batch, + keys: &'b [SortKey], + decoded: Vec>>, +} + +impl<'b> KeptKeyValues<'b> { + fn new(batch: &'b Batch, keys: &'b [SortKey]) -> Self { + Self { + batch, + keys, + decoded: vec![None; keys.len()], + } + } + + /// Row `row`'s key values, in a new allocation. + fn row(&mut self, row: usize) -> Result, Error> { + let mut values = vec![Value::Null; self.keys.len()].into_boxed_slice(); + self.write(row, &mut values)?; + Ok(values) + } + + /// Write row `row`'s key values over `values`, one per key: a held row + /// that is displaced passes its own, so a full group allocates nothing. + fn write(&mut self, row: usize, values: &mut [Value]) -> Result<(), Error> { + use arrow::array::AsArray; + use arrow::datatypes::{Date32Type, Float64Type, Int64Type, TimestampMillisecondType}; + for ((index, key), slot) in self.keys.iter().enumerate().zip(values) { + let array = self.batch.column(key.column); + let value = if array.is_null(row) { + Value::Null + } else { + match plain(self.batch.schema(), key.column)?.0 { + DataType::Float64 => { + Value::Float64(array.as_primitive::().value(row)) + } + DataType::Int64 => Value::Int64(array.as_primitive::().value(row)), + DataType::Timestamp => Value::Timestamp( + array.as_primitive::().value(row), + ), + DataType::Date => Value::Date(array.as_primitive::().value(row)), + DataType::Bool => Value::Bool(array.as_boolean().value(row)), + _ => { + let column = match &mut self.decoded[index] { + Some(column) => column, + empty => empty.insert(decoded(self.batch, key.column)?), + }; + column[row].clone() + } + } + }; + if matches!(value, Value::Map(_)) && nested_nan(&value) { + return Err(invalid("NaN in collection sort key")); + } + *slot = value; + } + Ok(()) + } } /// The first `fetch` rows of each group of `input` by `keys`, groups in key /// order, ties in input order, as a full sort and a per-group limit give /// them. Each group keeps a heap of at most `fetch` rows, read a batch at a /// time, so it holds `groups × fetch` rows (plus displaced rows, until they -/// are compacted away) instead of its whole input. +/// are compacted away) instead of its whole input. A row is tested against +/// its group's last kept row straight from the Arrow arrays; only a row +/// that is kept has its key values decoded. async fn sort_with_fetch( mut input: Input<'_, Batch>, schema: &SchemaRef, @@ -186,23 +347,31 @@ async fn sort_with_fetch( fetch: u64, context: RunContext, ) -> Result { - use std::collections::BinaryHeap; let mut table = super::group_table::GroupTable::new(groups.to_vec()); - let mut heaps: Vec> = Vec::new(); - let mut chunks: Vec = Vec::new(); + let mut heaps: Vec> = Vec::new(); + // The batches of held rows; row `r` of chunk `c` is `chunks[c]`'s row `r`. + let mut chunks: Vec = Vec::new(); let mut workspace = Workspace::new(&context)?; let mut work = Cooperative::new(&context); let mut input_position = 0u64; let mut kept_rows = 0usize; let mut key_bytes = 0usize; - let held_bytes = |chunks: &[HeldChunk], key_bytes: usize, kept_rows: usize| { - chunks - .iter() - .map(|chunk| chunk.rows.memory_bytes() + 4 * chunk.input_rows.len()) - .sum::() + let held_bytes = |chunks: &[Batch], key_bytes: usize, kept_rows: usize| { + chunks.iter().map(Batch::memory_bytes).sum::() + key_bytes + kept_rows * std::mem::size_of::() }; + let order = |a: &HeldRow, b: &HeldRow| { + sort_order( + compare_rows(keys, a.keys.iter(), b.keys.iter()), + a.input_position, + b.input_position, + ) + }; + let dtypes = keys + .iter() + .map(|key| plain(schema, key.column).map(|(dtype, _)| dtype.clone())) + .collect::, _>>()?; let fetch = usize::try_from(fetch).unwrap_or(usize::MAX); while let Some(batch) = input.next().await { let batch = batch?; @@ -212,126 +381,137 @@ async fn sort_with_fetch( } let held = held_bytes(&chunks, key_bytes, kept_rows); let ids = chunked_group_ids(&mut table, &batch, &mut workspace, held, &mut work).await?; - heaps.resize_with(table.len(), BinaryHeap::new); - let key_values = sort_key_values(&batch, keys)?; - let chunk = chunks.len() as u32; - // Whether each row of `batch` is still kept. + heaps.resize_with(table.len(), Vec::new); + let mut kept_keys = KeptKeyValues::new(&batch, keys); + // Per held chunk, then the batch being read (last), the key + // comparators of this batch's rows against its rows, built when a + // heap's last row first lies there. + let mut comparators: Vec>> = + (0..=chunks.len()).map(|_| None).collect(); + // Whether each row of `batch` is still kept, and the groups it went to. let mut kept = vec![false; batch.num_rows()]; + let mut touched: Vec = Vec::new(); for (row, &group) in ids.iter().enumerate() { let heap = &mut heaps[group as usize]; let position = input_position + row as u64; - let sorts_before = |last: &HeldRow| { - compare_rows( - keys, - key_values.iter().map(|values| &values[row]), - last.keys.iter(), - ) - .then(position.cmp(&last.input_position)) - .is_lt() - }; - if heap.len() >= fetch && !heap.peek().is_some_and(sorts_before) { - continue; - } - let held = HeldRow { - keys: key_values + let full = heap.len() >= fetch; + if full { + let last = &heap[0]; + let slot = if last.chunk == READING { + chunks.len() + } else { + last.chunk as usize + }; + let compare = match &mut comparators[slot] { + Some(compare) => compare, + empty => { + let held = chunks.get(slot).unwrap_or(&batch); + empty.insert( + keys.iter() + .zip(&dtypes) + .map(|(key, dtype)| { + key_comparator( + batch.column(key.column), + held.column(key.column), + dtype, + key, + ) + }) + .collect::>()?, + ) + } + }; + let key_order = compare .iter() - .map(|values| values[row].clone()) - .collect(), - input_position: position, - chunk, - row: row as u32, - sort: keys, - }; - key_bytes += held.keys.iter().map(Value::bytes).sum::(); - heap.push(held); + .map(|compare| compare(row, last.row as usize)) + .find(|order| order.is_ne()) + .unwrap_or(Ordering::Equal); + if !sort_order(key_order, position, last.input_position).is_lt() { + continue; + } + } kept[row] = true; - kept_rows += 1; - if heap.len() > fetch { - let displaced = heap.pop().expect("a full heap"); - key_bytes -= displaced.keys.iter().map(Value::bytes).sum::(); - kept_rows -= 1; - if displaced.chunk == chunk { - kept[displaced.row as usize] = false; + touched.push(group); + if full { + // The row displaces the group's last kept row: overwrite + // that row in place, its key allocation reused. + let last = &mut heap[0]; + key_bytes -= last.keys.iter().map(Value::bytes).sum::(); + if last.chunk == READING { + kept[last.row as usize] = false; } + kept_keys.write(row, &mut last.keys)?; + key_bytes += last.keys.iter().map(Value::bytes).sum::(); + (last.input_position, last.chunk, last.row) = (position, READING, row as u32); + heap_sift_down_top(heap, &order); + } else { + let held = HeldRow { + keys: kept_keys.row(row)?, + input_position: position, + chunk: READING, + row: row as u32, + }; + key_bytes += held.keys.iter().map(Value::bytes).sum::(); + heap_push(heap, held, &order); + kept_rows += 1; } } input_position += batch.num_rows() as u64; - let input_rows: Vec = (0..batch.num_rows() as u32) - .filter(|&row| kept[row as usize]) - .collect(); - if !input_rows.is_empty() { - chunks.push(HeldChunk { - rows: batch.take(&arrow::array::UInt32Array::from(input_rows.clone()))?, - input_rows, - }); + // Take the batch's kept rows into a chunk, and point their held rows + // there. + let mut chunk_rows = vec![0u32; batch.num_rows()]; + let mut taken = Vec::new(); + for (row, _) in kept.iter().enumerate().filter(|(_, &kept)| kept) { + chunk_rows[row] = taken.len() as u32; + taken.push(row as u32); } - let chunk_rows = chunks.iter().map(|c| c.input_rows.len()).sum::(); - if chunk_rows > 2 * kept_rows.max(1) { + if !taken.is_empty() { + let chunk = chunks.len() as u32; + for &group in &touched { + for held in heaps[group as usize].iter_mut() { + if held.chunk == READING { + held.chunk = chunk; + held.row = chunk_rows[held.row as usize]; + } + } + } + chunks.push(batch.take(&arrow::array::UInt32Array::from(taken))?); + } + let held_rows = chunks.iter().map(Batch::num_rows).sum::(); + if held_rows > 2 * kept_rows.max(1) { compact(schema, &mut chunks, &mut heaps)?; } workspace.set_bytes(table.memory_bytes() + held_bytes(&chunks, key_bytes, kept_rows))?; } - compact(schema, &mut chunks, &mut heaps)?; - // Groups in key order, each group's rows in sort order. - let mut order = Vec::with_capacity(kept_rows); + // Groups in key order, each group's rows in sort order, gathered from + // the chunks in one copy. + let mut rows = Vec::with_capacity(kept_rows); for group in table.groups_in_key_order() { if let Some(heap) = heaps.get_mut(group as usize) { - order.extend( - std::mem::take(heap) - .into_sorted_vec() - .iter() - .map(|held| held.row), + heap.sort_unstable_by(order); + rows.extend( + heap.iter() + .map(|held| (held.chunk as usize, held.row as usize)), ); } } - match chunks.pop() { - Some(kept) => kept.rows.take(&arrow::array::UInt32Array::from(order)), - None => Batch::try_new(schema.clone(), Vec::new()), - } + Batch::interleave(schema, &chunks, &rows) } -/// Rewrite the rows `heaps` keep into one chunk, in heap order, dropping +/// Gather the rows `heaps` keep into one chunk, in one copy, dropping /// displaced rows, and point each kept row at its row there. fn compact( schema: &SchemaRef, - chunks: &mut Vec, - heaps: &mut [std::collections::BinaryHeap], + chunks: &mut Vec, + heaps: &mut [Vec], ) -> Result<(), Error> { - if chunks.is_empty() { - return Ok(()); - } - let mut offsets = Vec::with_capacity(chunks.len()); - let mut total = 0u32; - for chunk in chunks.iter() { - offsets.push(total); - total += chunk.input_rows.len() as u32; - } let mut rows = Vec::new(); - for heap in heaps.iter_mut() { - let mut held = std::mem::take(heap).into_vec(); - for row in &mut held { - let chunk = &chunks[row.chunk as usize]; - let index = chunk - .input_rows - .binary_search(&row.row) - .expect("a kept row is in its chunk"); - rows.push(offsets[row.chunk as usize] + index as u32); - row.chunk = 0; - row.row = rows.len() as u32 - 1; - } - *heap = std::collections::BinaryHeap::from(held); + for held in heaps.iter_mut().flatten() { + rows.push((held.chunk as usize, held.row as usize)); + held.chunk = 0; + held.row = rows.len() as u32 - 1; } - let all = Batch::concat( - schema, - &chunks - .iter() - .map(|chunk| chunk.rows.clone()) - .collect::>(), - )?; - *chunks = vec![HeldChunk { - rows: all.take(&arrow::array::UInt32Array::from(rows.clone()))?, - input_rows: (0..rows.len() as u32).collect(), - }]; + *chunks = vec![Batch::interleave(schema, chunks, &rows)?]; Ok(()) } diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index 4fcb35bc0..ad9bd925e 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -283,22 +283,48 @@ fn compile_internal( } None => node, }; + // An exact top-k or `count_values` lowers to a helper operator + // (none for a `count_values` whose label `without` removes) and + // the node's own. if let Payload::NonASAP(NonASAPOp::Aggregate { - reduction: PlannerReduction::Reduce(_), + reduction: PlannerReduction::Reduce(keys), measures, + filters, having: None, .. }) = &node.payload { - if let [AggIntent::TopK { .. }] = measures.as_slice() { - let Some(NonASAPOp::Aggregate { child, .. }) = restored[&id].non_asap() else { - unreachable!("an aggregate restores as an aggregate") + if let [measure @ (AggIntent::TopK { .. } | AggIntent::CountValues { .. })] = + measures.as_slice() + { + let [input] = schemas.as_slice() else { + return Err(invalid(format!("node {id}: an aggregate reads one input"))); }; - lower_exact_topk(&mut physical_dag, node, child, inputs, &schemas, &output)?; - continue; - } - if let [AggIntent::CountValues { .. }] = measures.as_slice() { - lower_count_values(&mut physical_dag, node, inputs, &schemas, &output)?; + if filters.iter().any(Option::is_some) { + return Err(invalid(format!( + "node {id}: a filtered top-k or count_values" + ))); + } + let (helper_operator, operator) = match measure { + AggIntent::TopK { k, .. } => { + let Some(NonASAPOp::Aggregate { child, .. }) = restored[&id].non_asap() + else { + unreachable!("an aggregate restores as an aggregate") + }; + lower_exact_topk(child, keys, *k as u64, input, &output) + } + AggIntent::CountValues { label } => { + lower_count_values(keys, label, input, &output) + } + _ => unreachable!("matched a top-k or count_values"), + } + .map_err(|error| invalid(format!("node {id}: {error}")))?; + if let Some(helper_operator) = helper_operator { + let helper = helper_id(id, 0)?; + physical_dag.add(helper, inputs, helper_operator)?; + inputs = vec![helper]; + } + physical_dag.add(id, inputs, operator)?; continue; } } @@ -1506,39 +1532,19 @@ fn top_k_sort( ) } -/// Lower `node`, an exact top-k `Aggregate{Reduce(keys), [TopK{k}]}` over -/// `child` (PromQL `topk`, SQL's heavy hitter), into `physical_dag`: the -/// first `k` rows of its input per `keys` partition by the ranked value -/// ([`top_k_sort`]), then its ranked rows' columns. `topk(2, sum by (job) +/// An exact top-k `Aggregate{Reduce(keys), [TopK{k}]}` over `child` (PromQL +/// `topk`, SQL's heavy hitter) reading `input`, lowered: the first `k` rows +/// of its input per `keys` partition by the ranked value ([`top_k_sort`]), +/// then a projection of its ranked rows' columns. `topk(2, sum by (job) /// (x))` over job sums `a=7, b=10, c=3` answers `b=10` and `a=7`. fn lower_exact_topk( - physical_dag: &mut CompiledPhysicalDAG, - node: &PhysicalASAPDAGNode, child: &std::rc::Rc, - inputs: Vec, - schemas: &[SchemaRef], + keys: &GroupKeys, + k: u64, + input: &SchemaRef, output: &SchemaRef, -) -> Result<(), Error> { - let id = node.id as NodeId; - let Payload::NonASAP(NonASAPOp::Aggregate { - reduction: PlannerReduction::Reduce(keys), - measures, - filters, - .. - }) = &node.payload - else { - return Err(invalid(format!("node {id}: a top-k is an aggregate"))); - }; - let [AggIntent::TopK { k, .. }] = measures.as_slice() else { - return Err(invalid(format!("node {id}: a top-k has one measure"))); - }; - let [input] = schemas else { - return Err(invalid(format!("node {id}: an aggregate reads one input"))); - }; - if filters.iter().any(Option::is_some) { - return Err(invalid(format!("node {id}: a filtered top-k"))); - } - let ranked = |error: String| invalid(format!("node {id}: top-k: {error}")); +) -> Result<(Option, Operator), Error> { + let ranked = |error: String| invalid(format!("top-k: {error}")); let value = planner_types::ir::schema::topk::ranked_column(child) .map_err(|error| ranked(error.to_string()))?; let columns = planner_types::ir::schema::topk::ranked_row_projection( @@ -1555,50 +1561,34 @@ fn lower_exact_topk( _ => Err(ranked("a ranked row projects columns".into())), }) .collect::>()?; - let sort = helper_id(id, 0)?; - physical_dag.add( - sort, - inputs, - top_k_sort(input, value, groups(input, keys)?, *k as u64)?, - )?; - physical_dag.add( - id, - vec![sort], + Ok(( + Some(top_k_sort(input, value, groups(input, keys)?, k)?), Operator::project(input.clone(), columns)?.with_output_schema(output.clone())?, - ) + )) } -/// Lower `node`, PromQL `count_values("l", v)` as -/// `Aggregate{Reduce(keys), [CountValues{l}]}`, into `physical_dag`: a -/// projection adding the value as text in the label `l` -/// ([`Expression::FloatText`]), then a count per group and value text. `l` -/// replaces a grouping label of that name: `count_values by (job) ("job", -/// v)` over `{job=a} 2` and `{job=b} 2` answers `{job="2"} 2`. +/// PromQL `count_values("l", v)` as `Aggregate{Reduce(keys), +/// [CountValues{l}]}` reading `input`, lowered: a projection adding the +/// value as text in the label `l` ([`Expression::FloatText`]), then a count +/// per group and value text. `l` replaces a grouping label of that name: +/// `count_values by (job) ("job", v)` over `{job=a} 2` and `{job=b} 2` +/// answers `{job="2"} 2`. A `without` grouping that excludes `l` removes it +/// again (the IR's `output` has no `l`), so the count is per group only: +/// `count_values("zone", v) without (instance, zone)` adds no projection. fn lower_count_values( - physical_dag: &mut CompiledPhysicalDAG, - node: &PhysicalASAPDAGNode, - inputs: Vec, - schemas: &[SchemaRef], + keys: &GroupKeys, + label: &str, + input: &SchemaRef, output: &SchemaRef, -) -> Result<(), Error> { - let id = node.id as NodeId; - let Payload::NonASAP(NonASAPOp::Aggregate { - reduction: PlannerReduction::Reduce(keys), - measures, - filters, - .. - }) = &node.payload - else { - return Err(invalid(format!("node {id}: count_values is an aggregate"))); - }; - let [AggIntent::CountValues { label }] = measures.as_slice() else { - return Err(invalid(format!("node {id}: count_values has one measure"))); - }; - let [input] = schemas else { - return Err(invalid(format!("node {id}: an aggregate reads one input"))); - }; - if filters.iter().any(Option::is_some) { - return Err(invalid(format!("node {id}: a filtered count_values"))); +) -> Result<(Option, Operator), Error> { + if !output.fields.iter().any(|field| field.name == label) { + let count = Operator::aggregate( + input.clone(), + groups(input, keys)?, + vec![(String::new(), Reduction::Count)], + )? + .with_output_schema(output.clone())?; + return Ok((None, count)); } let value = named_column(input, &ColumnRef::SampleValue)?; let mut columns: Vec<_> = input @@ -1607,12 +1597,12 @@ fn lower_count_values( .enumerate() .map(|(i, field)| (field.name.clone(), Expression::Column(i))) .collect(); - columns.push((label.clone(), Expression::FloatText(value))); + columns.push((label.to_string(), Expression::FloatText(value))); let text = input.fields.len(); let mut partitions = groups(input, keys)?; match partitions .iter() - .position(|&key| input.fields[key].name == *label) + .position(|&key| input.fields[key].name == label) { Some(key) => partitions[key] = text, None => partitions.push(text), @@ -1624,9 +1614,7 @@ fn lower_count_values( vec![(String::new(), Reduction::Count)], )? .with_output_schema(output.clone())?; - let projection = helper_id(id, 0)?; - physical_dag.add(projection, inputs, project)?; - physical_dag.add(id, vec![projection], aggregate) + Ok((Some(project), aggregate)) } /// `node` with its `without` grouping (PromQL `sum without (instance) (m)`) @@ -1681,10 +1669,10 @@ fn resolve_without( // `count_values("zone", v)`'s label replaces a kept label `zone`, as // Prometheus overwrites it before grouping: the kept label set drops it // too, so `{job=b, zone=z}` and `{job=b}` with the value 2 are one group - // `{job=b, zone="2"}`. + // `{job=b, zone="2"}`. An excluded label is removed already. let replaced = match &node.payload { Payload::NonASAP(NonASAPOp::Aggregate { measures, .. }) => match measures.as_slice() { - [AggIntent::CountValues { label }] => Some(label.clone()), + [AggIntent::CountValues { label }] if !excluded.contains(label) => Some(label.clone()), _ => None, }, _ => None, @@ -1696,29 +1684,27 @@ fn resolve_without( let columns = (0..input.fields.len()) .map(|i| { let field = &input.fields[i]; - let label_map = matches!(field.dtype, FieldDataType::Plain(DataType::Map { .. })); - let expression = if !kept.contains(&i) { - Expression::Column(i) - } else if field.name == promql_rows::SERIES_IDENTITY_COLUMN { - rewrites = true; - Expression::LabelIdentity { - column: i, - excluding: removed - .iter() - .cloned() - .chain(["__name__".to_string()]) - .collect(), - } - } else if label_map { - rewrites = true; - Expression::LabelSet { - column: i, - labels: removed.clone(), - without: true, - } - } else { - Expression::Column(i) - }; + let expression = + if !kept.contains(&i) || !planner_types::ir::schema::is_label_set_column(field) { + Expression::Column(i) + } else if field.name == promql_rows::SERIES_IDENTITY_COLUMN { + rewrites = true; + Expression::LabelIdentity { + column: i, + excluding: removed + .iter() + .cloned() + .chain(["__name__".to_string()]) + .collect(), + } + } else { + rewrites = true; + Expression::LabelSet { + column: i, + labels: removed.clone(), + without: true, + } + }; (field.name.clone(), expression) }) .collect(); diff --git a/crates/executor/src/physical_planner/promql_rows.rs b/crates/executor/src/physical_planner/promql_rows.rs index d08070667..4308a5f36 100644 --- a/crates/executor/src/physical_planner/promql_rows.rs +++ b/crates/executor/src/physical_planner/promql_rows.rs @@ -28,6 +28,79 @@ pub fn decode_series_identity(encoded: &str) -> Result, Ok(labels) } +/// The series identity `identity` less the labels `excluding`, encoded as +/// [`encode_series_identity`] writes it: `{"__name__":"m","instance":"1", +/// "job":"a"}` less `instance` and `__name__` is `{"job":"a"}`. The encoding +/// is canonical (names sorted, one way to escape each character), so this +/// copies the kept `"name":"value"` pairs' bytes in order, decoding and +/// encoding nothing. A name token without an escape is the name itself +/// (JSON escapes only `"`, `\` and control characters), so it is compared +/// with the excluded names byte for byte; a token with one is compared with +/// each excluded name's encoding. An error for input that is not a JSON +/// object of string labels. +pub fn series_identity_without(identity: &str, excluding: &[String]) -> Result { + let bytes = identity.as_bytes(); + let malformed = || invalid("series identity is not canonically encoded"); + // The index after the JSON string token that starts at `start`. + let string_end = |start: usize| -> Option { + if bytes.get(start) != Some(&b'"') { + return None; + } + let mut i = start + 1; + while let Some(&byte) = bytes.get(i) { + match byte { + b'\\' => i += 2, + b'"' => return Some(i + 1), + _ => i += 1, + } + } + None + }; + // Whether the name token `token`, quotes included, names an excluded + // label. + let excluded = |token: &str| { + let name = &token[1..token.len() - 1]; + if name.contains('\\') { + excluding + .iter() + .any(|label| serde_json::to_string(label).is_ok_and(|encoded| encoded == token)) + } else { + excluding.iter().any(|label| label == name) + } + }; + if bytes.first() != Some(&b'{') { + return Err(malformed()); + } + let mut out = String::with_capacity(identity.len()); + out.push('{'); + let mut i = 1; + if bytes.get(i) == Some(&b'"') { + loop { + let name_end = string_end(i).ok_or_else(malformed)?; + if bytes.get(name_end) != Some(&b':') { + return Err(malformed()); + } + let value_end = string_end(name_end + 1).ok_or_else(malformed)?; + if !excluded(&identity[i..name_end]) { + if out.len() > 1 { + out.push(','); + } + out.push_str(&identity[i..value_end]); + } + i = value_end; + if bytes.get(i) != Some(&b',') { + break; + } + i += 1; + } + } + if bytes.get(i) != Some(&b'}') || i + 1 != bytes.len() { + return Err(malformed()); + } + out.push('}'); + Ok(out) +} + /// Resolve the row representation before candidate search; see /// [`planner_types::ir::schema::series_identity::with_promql_series_identity`]. pub fn with_series_identity(root: &Rc) -> Result, Error> { @@ -80,3 +153,92 @@ pub fn series_row( } Ok(row) } + +#[cfg(test)] +mod tests { + use super::*; + + /// The identity less `excluding` by decoding, filtering and encoding + /// again: the reference [`series_identity_without`] must equal byte for + /// byte. + fn decoded_without(identity: &str, excluding: &[String]) -> String { + let mut labels = decode_series_identity(identity).unwrap(); + labels.retain(|name, _| !excluding.contains(name)); + encode_series_identity(&labels).unwrap() + } + + /// Over label sets with escaped quotes, backslashes, control + /// characters, unicode and empty values, excluding none, some or every + /// label (and labels that are absent), the streaming rewrite equals the + /// decoded one byte for byte. + #[test] + fn series_identity_without_equals_decode_filter_encode() { + let texts = [ + "", + "a", + "job", + "q\"uote", + "back\\slash", + "\\\"", + "tab\tnew\nline\u{1}", + "ünïcødé ☃ 𝄞", + "\"", + "\\", + "comma,colon:brace}{", + "__name__", + ]; + let mut identities = Vec::new(); + for (i, first) in texts.iter().enumerate() { + for (j, second) in texts.iter().enumerate() { + let mut labels = BTreeMap::new(); + labels.insert((*first).to_string(), (*second).to_string()); + labels.insert((*second).to_string(), format!("{first}{i}")); + labels.insert(format!("{second}{j}"), String::new()); + labels.insert("__name__".to_string(), "m".to_string()); + identities.push(encode_series_identity(&labels).unwrap()); + } + } + identities.push(encode_series_identity(&BTreeMap::new()).unwrap()); + let mut exclusions: Vec> = vec![vec![], vec!["absent".into()]]; + exclusions.extend(texts.iter().map(|text| vec![(*text).to_string()])); + exclusions.push(texts.iter().map(|text| (*text).to_string()).collect()); + for identity in &identities { + let every: Vec = decode_series_identity(identity) + .unwrap() + .into_keys() + .collect(); + for excluding in exclusions.iter().chain([&every]) { + assert_eq!( + series_identity_without(identity, excluding).unwrap(), + decoded_without(identity, excluding), + "{identity} less {excluding:?}" + ); + } + assert_eq!(series_identity_without(identity, &every).unwrap(), "{}"); + assert_eq!(series_identity_without(identity, &[]).unwrap(), *identity); + } + } + + /// Input that is not a JSON object of string labels is an error. + #[test] + fn series_identity_without_refuses_a_malformed_identity() { + for identity in [ + "", + "{", + "}", + "[]", + r#"{"a"}"#, + r#"{"a":1}"#, + r#"{"a":"b",}"#, + r#"{"a":"b""c":"d"}"#, + r#"{"a":"b"} "#, + r#"{"a":"b"#, + r#"{"a\":"b"}"#, + ] { + assert!( + series_identity_without(identity, &["x".into()]).is_err(), + "{identity}" + ); + } + } +} diff --git a/crates/executor/src/values/batch/mod.rs b/crates/executor/src/values/batch/mod.rs index 07b07ddec..247bfb014 100644 --- a/crates/executor/src/values/batch/mod.rs +++ b/crates/executor/src/values/batch/mod.rs @@ -510,6 +510,59 @@ impl Batch { Self::from_parts(schema.clone(), arrays, states, rows) } + /// The rows `indices` of `batches` (all in `schema`), each a (batch, + /// row) pair, in that order: one copy of each row, where [`Self::concat`] + /// then [`Self::take`] copy it twice. `[(1, 0), (0, 2)]` is the first row + /// of `batches[1]`, then the third of `batches[0]`. A state column the + /// batches do not share is joined as `concat` joins it. + pub(crate) fn interleave( + schema: &SchemaRef, + batches: &[Self], + indices: &[(usize, usize)], + ) -> Result { + let Some(first) = batches.first() else { + return Self::try_new(schema.clone(), Vec::new()); + }; + let shared_states = (0..schema.fields.len()).all(|column| { + batches.iter().all(|batch| { + match (batch.state_column(column), first.state_column(column)) { + (None, None) => true, + (Some(states), Some(first)) => Arc::ptr_eq(states, first), + _ => false, + } + }) + }); + if !shared_states { + let mut offsets = Vec::with_capacity(batches.len()); + let mut rows = 0u32; + for batch in batches { + offsets.push(rows); + rows += batch.num_rows() as u32; + } + return Self::concat(schema, batches)?.take(&UInt32Array::from_iter_values( + indices + .iter() + .map(|&(batch, row)| offsets[batch] + row as u32), + )); + } + let arrays = (0..schema.fields.len()) + .map(|column| { + let pieces = batches + .iter() + .map(|batch| batch.column(column).as_ref()) + .collect::>(); + arrow::compute::interleave(&pieces, indices) + .map_err(|error| Error::Invalid(format!("columnar interleave: {error}"))) + }) + .collect::, _>>()?; + Self::from_parts( + schema.clone(), + arrays, + first.state_columns.to_vec(), + indices.len(), + ) + } + /// The Planner schema of the columns. pub fn schema(&self) -> &SchemaRef { &self.schema diff --git a/crates/executor/tests/physical_semantics.rs b/crates/executor/tests/physical_semantics.rs index 39c475f49..0731631ef 100644 --- a/crates/executor/tests/physical_semantics.rs +++ b/crates/executor/tests/physical_semantics.rs @@ -209,6 +209,79 @@ fn sort_direction_null_placement_and_ties() { } } +// Over ascending input, where every row displaces its group's last kept +// row, and with a text key before the number (`ORDER BY label, v`), a sort +// that keeps the first `fetch` rows of each group answers the same rows, in +// the same order, as a full sort and a per-group limit. +#[test] +fn sort_with_fetch_over_ascending_input_equals_sort_then_limit() { + let input = schema(&[ + ("g", DataType::Int64, false), + ("label", DataType::Utf8, true), + ("v", DataType::Float64, false), + ]); + let rows: Vec> = (0..300i64) + .map(|i| { + let label = match i % 5 { + 0 => Value::Null, + k => Value::Utf8(format!("l{}", k % 2).into()), + }; + vec![Value::Int64(i % 4), label, Value::Float64(i as f64 / 3.)] + }) + .collect(); + let batches = || rows.chunks(17).map(<[_]>::to_vec).collect::>(); + for groups in [vec![], vec![0]] { + for (label_descending, value_descending) in + [(false, false), (false, true), (true, false), (true, true)] + { + let key = vec![ + SortKey { + column: 1, + descending: label_descending, + nulls_first: label_descending, + }, + SortKey { + column: 2, + descending: value_descending, + nulls_first: false, + }, + ]; + for fetch in [1, 3, 7, 1000] { + let fetched = unary( + input.clone(), + batches(), + Operator::sort(input.clone(), key.clone(), groups.clone(), Some(fetch)) + .unwrap(), + ); + let mut dag = PhysicalDAG::default(); + let source = batches() + .into_iter() + .map(|rows| Batch::try_new(input.clone(), rows).unwrap()) + .collect(); + dag.add(0, vec![], Operator::source(input.clone(), source).unwrap()) + .unwrap(); + dag.add( + 1, + vec![0], + Operator::sort(input.clone(), key.clone(), groups.clone(), None).unwrap(), + ) + .unwrap(); + dag.add( + 2, + vec![1], + Operator::limit(input.clone(), fetch, 0, groups.clone()).unwrap(), + ) + .unwrap(); + assert_eq!( + format!("{fetched:?}"), + format!("{:?}", collect(&dag, 2)), + "groups {groups:?}, descending {label_descending}/{value_descending}, fetch {fetch}" + ); + } + } + } +} + // A sort that keeps the first `fetch` rows of each group answers the same // rows, in the same order, as a full sort and a per-group limit: ties in // input order, NaN after every number, NULLs where the key puts them, also diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs index f42de40c4..6092ea737 100644 --- a/crates/planner/tests/reduce_aggregates.rs +++ b/crates/planner/tests/reduce_aggregates.rs @@ -267,6 +267,79 @@ async fn promql_count_values_without_overwrites_a_kept_label() { ); } +/// A `count_values` label that is also excluded is set and then removed, +/// as Prometheus's `without` deletes it after overwriting: the values do not +/// split the groups, so `count_values("zone", h) without (instance, zone)` +/// counts job `a`'s four series in one group, and `count_values("instance", +/// m) without (instance)` outputs no `instance` label. +#[tokio::test] +async fn promql_count_values_label_excluded_by_without_is_dropped() { + let at = EVALUATION_MS - 5_000; + let zones = vec![ + sample( + "h", + &[("job", "a"), ("instance", "1"), ("zone", "z")], + at, + 2.0, + ), + sample("h", &[("job", "a"), ("instance", "2")], at, 2.0), + sample( + "h", + &[("job", "a"), ("instance", "3"), ("zone", "y")], + at, + 2.0, + ), + sample( + "h", + &[("job", "a"), ("instance", "4"), ("zone", "z")], + at, + 5.0, + ), + ]; + let answers = promql( + r#"count_values("zone", h) without (instance, zone)"#, + "h", + &zones, + ) + .await; + assert_close(&answers, &[(labels(&[("job", "a")]), 4.0)]); + let answers = promql( + r#"count_values("instance", m) without (instance)"#, + "m", + &count_values_samples(), + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 2.0), + (labels(&[("job", "b")]), 1.0), + (labels(&[("job", "b"), ("zone", "z")]), 1.0), + ], + ); +} + +/// A `count_values` label named like a kept label column (here `job`, +/// a column since the selector names it) replaces that column with the +/// value text: job `b`'s series both read `job="2"`, apart by `zone`. +#[tokio::test] +async fn promql_count_values_without_replaces_a_kept_label_column() { + let answers = promql( + r#"count_values("job", m{job=~"a|b"}) without (instance)"#, + "m", + &count_values_samples(), + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "1")]), 2.0), + (labels(&[("job", "2")]), 1.0), + (labels(&[("job", "2"), ("zone", "z")]), 1.0), + ], + ); +} + /// `sum without (instance)` groups by every other label, `__name__` /// dropped: `{job="b"}` and `{job="b", zone="z"}` are different groups. #[tokio::test] diff --git a/crates/types/src/ir/schema/aggregate_schema.rs b/crates/types/src/ir/schema/aggregate_schema.rs index 133a85506..bd68bf3f0 100644 --- a/crates/types/src/ir/schema/aggregate_schema.rs +++ b/crates/types/src/ir/schema/aggregate_schema.rs @@ -26,7 +26,7 @@ pub(crate) enum OutputColumnSource { index: usize, input_column: Option, }, - AddedLabel, + ComputedLabel, } /// Output schema of a *per-series* window/range reduction (`rate`/`increase`, /// or an `*_over_time` reducer under a time `Window`). Such a reduction emits @@ -161,14 +161,9 @@ pub(crate) fn aggregate_output( for (i, intent) in measures.iter().enumerate() { // `count_values("l", v)` emits TWO columns: the synthesized `Utf8` label // `l` (the stringified sample value it groups by) and the per-value - // count. If `l` collides with a group-by key of the same name, PromQL's - // synthesized label takes precedence — emit a single column, never a - // duplicate. + // count. if let AggIntent::CountValues { label } = intent { - if !out_cols.iter().any(|c| c.name == *label) { - out_cols.push(Field::plain(label.clone(), DataType::Utf8, false)); - sources.push(OutputColumnSource::AddedLabel); - } + push_count_values_label(&mut out_cols, &mut sources, label); let mut cnt = intent.output_column(&probe); if let Some(name) = output_names.get(i).filter(|s| !s.is_empty()) { cnt.name = name.clone(); @@ -188,7 +183,7 @@ pub(crate) fn aggregate_output( .keys() .map(|name| Field::plain(name.clone(), DataType::Utf8, false)), ); - sources.extend(labels.keys().map(|_| OutputColumnSource::AddedLabel)); + sources.extend(labels.keys().map(|_| OutputColumnSource::ComputedLabel)); } // Only the output *type* is read from here, so the leading column is // enough for the multi-column intents: `Cardinality` and `PearsonCorr` @@ -241,6 +236,29 @@ pub(crate) fn aggregate_output( )) } +/// Add `count_values("l", v)`'s label column `l`, the value text it groups +/// by, to an aggregate's output columns so far. A kept or `by` column named +/// `l` is replaced by it, as Prometheus overwrites that label: `count_values +/// by (job) ("job", v)` outputs one `job` column, holding the value text, a +/// label the aggregate computes. +fn push_count_values_label( + out_cols: &mut Vec, + sources: &mut Vec, + label: &str, +) { + let field = Field::plain(label, DataType::Utf8, false); + match out_cols.iter().position(|column| column.name == label) { + Some(position) => { + out_cols[position] = field; + sources[position] = OutputColumnSource::ComputedLabel; + } + None => { + out_cols.push(field); + sources.push(OutputColumnSource::ComputedLabel); + } + } +} + /// The input column a measure's output is typed by when it names one: its /// leading input column (the multi-column intents' output type ignores the /// rest). @@ -256,7 +274,8 @@ fn measure_input_column(in_schema: &Schema, intent: &AggIntent) -> Option Vec { let value = crate::ir::scalar::column_resolution::resolve_column_ref( &ColumnRef::SampleValue, @@ -275,10 +294,11 @@ pub fn without_kept_columns(in_schema: &Schema, excluded: &[ColumnId]) -> Vec = kept .into_iter() .map(|i| { - let field = &in_schema.fields[i]; - let label_set = field.name == super::PROMQL_SERIES_IDENTITY - || matches!(field.dtype, FieldDataType::Plain(DataType::Map { .. })); - if label_set { - OutputColumnSource::AddedLabel + if super::is_label_set_column(&in_schema.fields[i]) { + OutputColumnSource::ComputedLabel } else { OutputColumnSource::InputColumn(i) } @@ -317,12 +334,15 @@ fn without_output_schema( .cloned() .unwrap_or_else(|| Field::plain("value", DataType::Float64, false)); for (i, intent) in measures.iter().enumerate() { - // `count_values("l", v)`'s label `l`, unless a kept column of that - // name is replaced by it, as on the `by` path. + // A label the grouping also excludes is set and then removed, as in + // Prometheus: `count_values("zone", v) without (instance, zone)` + // counts per kept group, with no `zone` label. if let AggIntent::CountValues { label } = intent { - if !out_cols.iter().any(|c| c.name == *label) { - out_cols.push(Field::plain(label.clone(), DataType::Utf8, false)); - sources.push(OutputColumnSource::AddedLabel); + if !excluded + .iter() + .any(|&id| in_schema.fields[id].name == *label) + { + push_count_values_label(&mut out_cols, &mut sources, label); } } // Only the output *type* is read from here, so the leading column is @@ -433,7 +453,7 @@ mod tests { columns(Reduction::by(vec![2]), std::slice::from_ref(&count_values)), vec![ InputColumn(2), - AddedLabel, + ComputedLabel, Measure { index: 0, input_column: Some(1) @@ -447,7 +467,64 @@ mod tests { ), vec![ InputColumn(2), - AddedLabel, + ComputedLabel, + Measure { + index: 0, + input_column: Some(1) + } + ] + ); + } + + /// `count_values("job", v)` by `job` and `without (host)`: the label + /// replaces the kept `job` column with the value text it computes, so + /// that position is an added `Utf8` label, not a copy of the input `job` + /// (here nullable; the value text never is). + #[test] + fn a_count_values_label_replaces_a_kept_column_of_its_name() { + use OutputColumnSource::*; + let mut input = series(); + input.fields[2].nullable = true; + let count_values = AggIntent::CountValues { + label: "job".into(), + }; + for reduction in [ + Reduction::by(vec![2]), + Reduction::Reduce(GroupKeys::without(vec![3])), + ] { + let (schema, sources) = + aggregate_output(&input, &reduction, std::slice::from_ref(&count_values), &[]) + .unwrap(); + assert_eq!( + sources, + vec![ + ComputedLabel, + Measure { + index: 0, + input_column: Some(1) + } + ], + "{reduction:?}" + ); + assert_eq!(schema.fields[0], col("job", DataType::Utf8, false)); + } + } + + /// `count_values("host", v) without (host)`: Prometheus sets the label + /// and then removes it with the excluded ones, so no label is added and + /// the count is per kept group. + #[test] + fn a_count_values_label_excluded_by_without_is_not_added() { + use OutputColumnSource::*; + assert_eq!( + columns( + Reduction::Reduce(GroupKeys::without(vec![3])), + &[AggIntent::CountValues { + label: "host".into() + }] + ), + vec![ + InputColumn(2), Measure { index: 0, input_column: Some(1) @@ -483,7 +560,7 @@ mod tests { sources, vec![ InputColumn(2), - AddedLabel, + ComputedLabel, Measure { index: 0, input_column: Some(1) @@ -506,8 +583,8 @@ mod tests { assert_eq!( columns(Reduction::by(vec![]), &[absent]), vec![ - AddedLabel, - AddedLabel, + ComputedLabel, + ComputedLabel, Measure { index: 0, input_column: Some(1) diff --git a/crates/types/src/ir/schema/mod.rs b/crates/types/src/ir/schema/mod.rs index 77df2e67e..47ed0bbd8 100644 --- a/crates/types/src/ir/schema/mod.rs +++ b/crates/types/src/ir/schema/mod.rs @@ -278,6 +278,16 @@ pub struct Schema { /// label map. `$` cannot occur in a user PromQL label name. pub const PROMQL_SERIES_IDENTITY: &str = "$promql_series_identity"; +/// Whether `field` holds a whole label set: the series identity or a label +/// map. A `without` grouping computes such a kept column, its labels less +/// the excluded ones and `__name__`, instead of copying it: under +/// `without (instance)` the identity `{__name__=m, job=a, instance=1}` +/// becomes `{job=a}`, while a kept `job` column is copied. +pub fn is_label_set_column(field: &Field) -> bool { + field.name == PROMQL_SERIES_IDENTITY + || matches!(field.dtype, FieldDataType::Plain(DataType::Map { .. })) +} + impl Schema { pub fn has_promql_series_identity(&self) -> bool { self.closed diff --git a/crates/types/src/ir/schema/series_identity.rs b/crates/types/src/ir/schema/series_identity.rs index 8b3ba851f..bd9b02b68 100644 --- a/crates/types/src/ir/schema/series_identity.rs +++ b/crates/types/src/ir/schema/series_identity.rs @@ -7,8 +7,8 @@ use crate::ir::schema::*; /// /// This realization supports explicit `by` grouping, `without` grouping and /// per-series computation. A `without` grouping keeps the identity with its -/// excluded labels and `__name__` removed, computed when the plan is -/// compiled: `sum without (instance) (m)` turns `{__name__=m, job=a, +/// excluded labels and `__name__` removed, computed by the aggregate: +/// `sum without (instance) (m)` turns `{__name__=m, job=a, /// instance=1, zone=z}` into `{job=a, zone=z}`, and the aggregate's output /// schema marks that column as computed, not copied /// (`aggregate_schema::without_output_schema`). Operators that rewrite or diff --git a/crates/types/src/stages/logical/group/column_properties.rs b/crates/types/src/stages/logical/group/column_properties.rs index 82be7197e..fde7f218d 100644 --- a/crates/types/src/stages/logical/group/column_properties.rs +++ b/crates/types/src/stages/logical/group/column_properties.rs @@ -585,7 +585,7 @@ fn columns_from_sources( index, input_column, } => ColumnProperties::computed(measure(*index, column_at(*input_column).sign)), - OutputColumnSource::AddedLabel => ColumnProperties::default(), + OutputColumnSource::ComputedLabel => ColumnProperties::default(), }) .collect() } diff --git a/docs/develop_docs/physical-compile-coverage.md b/docs/develop_docs/physical-compile-coverage.md index 09d49ec7e..5aa444fbd 100644 --- a/docs/develop_docs/physical-compile-coverage.md +++ b/docs/develop_docs/physical-compile-coverage.md @@ -208,9 +208,10 @@ Totals after this change: 21 Supported, 5 Partial, 3 Missing, 2 Backend. | Row | Change | |---|---| -| 1 | PromQL `stddev`, `stdvar`, `group` and `count_values`, with `by` or `without`. `stddev`/`stdvar` use Welford's update, as Prometheus does: `stddev by (job) (m)` over job `a`'s 1, 2 and 4 is the square root of 14/9. `group` is 1 per group. `count_values("v", m)` writes each value as Go's `FormatFloat(v, 'f', -1, 64)` does (`0.30000000000000004`, `1000000000000000000000`, `NaN`, `+Inf`), and its label replaces a grouping or kept label of that name, as in Prometheus. Still Partial. | -| 2 | `without` grouping over the series identity: `sum without (instance) (m)` groups by every label but `instance` and `__name__`, so `{job=a, instance=1, zone=z}` answers as `{job=a, zone=z}`. The IR marks that kept identity as computed, not copied. A ranking (`topk`, `Sort`, `Limit`) partitioned `without` over the identity is refused, since it keeps each row's labels. SQL `STDDEV`, `STDDEV_SAMP`, `STDDEV_POP`, `VAR_SAMP`, `VAR_POP` and `corr` over Float64 or Int64 columns: NULL for fewer than two values (sample forms) or none (population forms), `corr` NULL for a constant input. | +| 1 | PromQL `stddev`, `stdvar`, `group` and `count_values`, with `by` or `without`. `stddev`/`stdvar` use Welford's update, as Prometheus does: `stddev by (job) (m)` over job `a`'s 1, 2 and 4 is the square root of 14/9. `group` is 1 per group. `count_values("v", m)` writes each value as Go's `FormatFloat(v, 'f', -1, 64)` does (`0.30000000000000004`, `1000000000000000000000`, `NaN`, `+Inf`), and its label replaces a grouping or kept label of that name, as in Prometheus. A label the `without` list also names is removed again, so `count_values("zone", m) without (instance, zone)` counts per kept group with no `zone` label. Still Partial. | +| 2 | `without` grouping over the series identity: `sum without (instance) (m)` groups by every label but `instance` and `__name__`, so `{job=a, instance=1, zone=z}` answers as `{job=a, zone=z}`. The IR marks that kept identity as computed, not copied. A ranking (`topk`, `Sort`, `Limit`) partitioned `without` over the identity is refused, since it keeps each row's labels. | | 3 | An exact `topk`, `Aggregate{Reduce, [TopK]}`, lowers to a `Sort` that keeps the first k rows per partition (value descending, NULLs and NaN last; `fetch: Some(k)`, a bounded heap of k rows per partition) and a `Project` of the ranked rows. The current-series top-k (row 21) lowers to the same sort. `topk(2, sum by (job) (rate(c[5m])))` answers the two largest job sums with their jobs; `topk by (job) (1, m)` keeps each series' full labels, `__name__` included. | +| 29 | SQL `STDDEV`, `STDDEV_SAMP`, `STDDEV_POP`, `VAR_SAMP`, `VAR_POP` and `corr` over Float64 or Int64 columns: NULL for fewer than two values (sample forms) or none (population forms), `corr` NULL for a constant input. | Totals are unchanged: 21 Supported, 5 Partial, 3 Missing, 2 Backend. From 878fd82cf4bc0eb2b6127a89e48ea325cb9ace76 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:00:05 +0000 Subject: [PATCH 4/8] fix(executor): round-3 review fixes for reduce aggregates - Sort{fetch}: a map sort key is compared by decoded value, chosen by type (Arrow's map order differs); a map key column is NaN-checked when its batch is read, as the full sort does. Randomized test of Sort{fetch} against sort then limit, with map keys. - count_values("__name__", v) without (...) drops __name__, as Prometheus does (removed_by_without in the IR schema). - series_identity_without: doc says the input must be canonical (a canonical check cost 18-34 %); fuzz test against decode. - Each group is listed once per batch (per-group stamp); the heap compares numbers and times inline (compare_held_key). - LabelIdentity over a dictionary rewrites the dictionary's texts once per entry and keeps the keys. - decode_cell shared by decode and KeptKeyValues; one NaN check (key_column); count_values aggregate built once; redundant guard in resolve_without dropped; READING sentinel replaced by chunks.len(). - OutputColumnSource doc lists the replaced-column case. --- crates/executor/src/expressions/columnar.rs | 57 +++++ crates/executor/src/operators/sort.rs | 180 ++++++++------ crates/executor/src/physical_planner/mod.rs | 70 +++--- .../src/physical_planner/promql_rows.rs | 71 +++++- crates/executor/src/values/arrow_columns.rs | 57 ++++- crates/executor/tests/physical_semantics.rs | 223 ++++++++++++++++++ crates/planner/tests/reduce_aggregates.rs | 44 ++++ .../types/src/ir/schema/aggregate_schema.rs | 53 ++++- 8 files changed, 620 insertions(+), 135 deletions(-) diff --git a/crates/executor/src/expressions/columnar.rs b/crates/executor/src/expressions/columnar.rs index d844f27f5..7423eae79 100644 --- a/crates/executor/src/expressions/columnar.rs +++ b/crates/executor/src/expressions/columnar.rs @@ -14,6 +14,10 @@ //! distinct combination of dictionary keys, in order of first appearance, //! so a label matcher is evaluated once per series, not once per sample. //! Otherwise it runs once per row over the decoded columns it reads. +//! - **Per dictionary entry:** a series identity less some labels +//! (`sum without (instance)`'s [`Expression::LabelIdentity`]) over a +//! dictionary column rewrites each dictionary text once, as text, and +//! keeps the column's keys. //! //! Errors are those of row-by-row evaluation: vectorized operations cannot //! fail, and the per-input path visits inputs in row order. @@ -737,6 +741,54 @@ fn evaluate_per_key_combination( Ok((results, indices)) } +/// Column `column` of `batch`, a dictionary of series identities, less the +/// labels `excluding`: each dictionary text rewritten once by +/// [`series_identity_without`](crate::physical_planner::promql_rows::series_identity_without) +/// into a new dictionary over the same keys, with no decoded [`Value`]. Two +/// texts can rewrite to one identity (`{"instance":"1","job":"a"}` and +/// `{"instance":"2","job":"a"}` less `instance`), so the new dictionary may +/// repeat a text; its readers compare texts, not keys (the group table +/// resolves each entry by its text). `None` when the column is not a +/// dictionary, when the dictionary is much larger than the batch (a slice +/// of a large batch), or when a text is malformed, so the per-row path +/// reads only the rows' entries and reports its error. +fn identity_without_per_entry( + batch: &Batch, + column: usize, + excluding: &[String], +) -> Option { + let dictionary = batch + .column(column) + .as_any() + .downcast_ref::>()?; + let texts = dictionary + .values() + .as_any() + .downcast_ref::()?; + if !arrow_columns::dense_dictionary(texts.len(), batch.num_rows()) { + return None; + } + let mut rewritten = + arrow::array::StringBuilder::with_capacity(texts.len(), texts.value_data().len()); + for entry in 0..texts.len() { + if texts.is_null(entry) { + rewritten.append_null(); + } else { + rewritten.append_value( + crate::physical_planner::promql_rows::series_identity_without( + texts.value(entry), + excluding, + ) + .ok()?, + ); + } + } + let rewritten = + DictionaryArray::try_new(dictionary.keys().clone(), Arc::new(rewritten.finish())) + .expect("the keys index the same number of texts"); + Some(Arc::new(rewritten)) +} + fn expression_columns(expression: &Expression, columns: &mut Vec) { use Expression as E; match expression { @@ -779,6 +831,11 @@ impl Expression { if let Some((vector, _)) = lower_expression(self, batch.schema()) { return evaluate_vector(&vector, batch)?.into_array(batch.num_rows()); } + if let Expression::LabelIdentity { column, excluding } = self { + if let Some(array) = identity_without_per_entry(batch, *column, excluding) { + return Ok(array); + } + } let mut columns = Vec::new(); expression_columns(self, &mut columns); evaluate_per_input(batch, &distinct(columns), dtype, |row| self.evaluate(row)) diff --git a/crates/executor/src/operators/sort.rs b/crates/executor/src/operators/sort.rs index fa5b8d01e..d1e44ff2d 100644 --- a/crates/executor/src/operators/sort.rs +++ b/crates/executor/src/operators/sort.rs @@ -106,19 +106,22 @@ pub(super) fn execute<'a>( /// The sort keys' values of `batch`, column by column; an error for a NaN /// inside a collection key, which has no order. fn sort_key_values(batch: &Batch, keys: &[SortKey]) -> Result>, Error> { - let key_values = keys + keys.iter() + .map(|key| key_column(batch, key.column)) + .collect() +} + +/// The values of sort key column `column` of `batch`; an error for a NaN +/// inside a collection key (`{a: NaN}`), which has no order. +fn key_column(batch: &Batch, column: usize) -> Result, Error> { + let values = decoded(batch, column)?; + if values .iter() - .map(|key| decoded(batch, key.column)) - .collect::, _>>()?; - for values in &key_values { - if values - .iter() - .any(|value| matches!(value, Value::Map(_)) && nested_nan(value)) - { - return Err(invalid("NaN in collection sort key")); - } + .any(|value| matches!(value, Value::Map(_)) && nested_nan(value)) + { + return Err(invalid("NaN in collection sort key")); } - Ok(key_values) + Ok(values) } /// The order of two rows by `keys`, given each row's key values in order: @@ -144,8 +147,8 @@ fn sort_order(key_order: Ordering, a_position: u64, b_position: u64) -> Ordering /// One row a sort with `fetch` holds: its key values, its position in the /// input (which breaks ties) and where its row is held: row `row` of chunk -/// `chunk`, or, while `chunk` is [`READING`], row `row` of the batch being -/// read. +/// `chunk`, or, while `chunk` is the number of chunks (the index the batch +/// being read will take), row `row` of that batch. struct HeldRow { keys: Box<[Value]>, input_position: u64, @@ -153,10 +156,6 @@ struct HeldRow { row: u32, } -/// The chunk of a held row from the batch being read, until that batch's -/// kept rows are taken into a chunk. -const READING: u32 = u32::MAX; - /// Add `row` to `heap`, a max-heap in a `Vec` by `order` (the row that sorts /// last on top). fn heap_push( @@ -201,8 +200,10 @@ fn heap_sift_down_top(heap: &mut [HeldRow], order: &impl Fn(&HeldRow, &HeldRow) /// Row `i` of `left` against row `j` of `right` by one sort key, both arrays /// of the key's column, in [`compare_key`]'s order read straight from the /// arrays: NULLs first or last, NaN after every number in either direction, -/// `-0` equal to `0`. A type Arrow cannot compare (a map) is decoded and -/// compared by value. +/// `-0` equal to `0`. A map key is decoded and compared by value, as Arrow's +/// map order differs inside the map: Arrow puts `{a: NULL}` before `{a: 1}` +/// and `{a: -0}` before `{a: 0}`; [`Value::compare`] puts a NULL value last +/// and `-0` equal to `0`. fn key_comparator( left: &ArrayRef, right: &ArrayRef, @@ -211,13 +212,15 @@ fn key_comparator( ) -> Result { use arrow::array::{AsArray, DynComparator}; use arrow::datatypes::Float64Type; - let Ok(values) = arrow::array::make_comparator(left, right, Default::default()) else { + if matches!(dtype, DataType::Map { .. }) { let depth = crate::values::arrow_columns::ColumnDepth::BatchColumn; let left = crate::values::arrow_columns::decode(left.as_ref(), dtype, depth); let right = crate::values::arrow_columns::decode(right.as_ref(), dtype, depth); let key = key.clone(); return Ok(Box::new(move |i, j| compare_key(&left[i], &right[j], &key))); - }; + } + let values = arrow::array::make_comparator(left, right, Default::default()) + .map_err(|error| invalid(&error.to_string()))?; let (left_nulls, right_nulls) = (left.logical_nulls(), right.logical_nulls()); let floats = left .as_primitive_opt::() @@ -270,8 +273,9 @@ fn key_comparator( /// The sort-key values of the rows of one batch a sort with `fetch` keeps, /// decoded only for those rows: a number, time, date or bool key reads its -/// cell, another type decodes its column once, at the first kept row. An -/// error for a NaN inside a collection key, which has no order. +/// cell, a text key decodes its column once, at the first kept row. A map +/// key decodes its column when the batch is read, as the full sort does, so +/// a NaN inside any row's map is an error, kept or not. struct KeptKeyValues<'b> { batch: &'b Batch, keys: &'b [SortKey], @@ -279,12 +283,19 @@ struct KeptKeyValues<'b> { } impl<'b> KeptKeyValues<'b> { - fn new(batch: &'b Batch, keys: &'b [SortKey]) -> Self { - Self { + fn new(batch: &'b Batch, keys: &'b [SortKey]) -> Result { + let decoded = keys + .iter() + .map(|key| match plain(batch.schema(), key.column)?.0 { + DataType::Map { .. } => key_column(batch, key.column).map(Some), + _ => Ok(None), + }) + .collect::>()?; + Ok(Self { batch, keys, - decoded: vec![None; keys.len()], - } + decoded, + }) } /// Row `row`'s key values, in a new allocation. @@ -297,36 +308,19 @@ impl<'b> KeptKeyValues<'b> { /// Write row `row`'s key values over `values`, one per key: a held row /// that is displaced passes its own, so a full group allocates nothing. fn write(&mut self, row: usize, values: &mut [Value]) -> Result<(), Error> { - use arrow::array::AsArray; - use arrow::datatypes::{Date32Type, Float64Type, Int64Type, TimestampMillisecondType}; for ((index, key), slot) in self.keys.iter().enumerate().zip(values) { + let dtype = plain(self.batch.schema(), key.column)?.0; let array = self.batch.column(key.column); - let value = if array.is_null(row) { - Value::Null - } else { - match plain(self.batch.schema(), key.column)?.0 { - DataType::Float64 => { - Value::Float64(array.as_primitive::().value(row)) - } - DataType::Int64 => Value::Int64(array.as_primitive::().value(row)), - DataType::Timestamp => Value::Timestamp( - array.as_primitive::().value(row), - ), - DataType::Date => Value::Date(array.as_primitive::().value(row)), - DataType::Bool => Value::Bool(array.as_boolean().value(row)), - _ => { - let column = match &mut self.decoded[index] { - Some(column) => column, - empty => empty.insert(decoded(self.batch, key.column)?), - }; - column[row].clone() - } + *slot = match crate::values::arrow_columns::decode_cell(array.as_ref(), dtype, row) { + Some(value) => value, + None => { + let column = match &mut self.decoded[index] { + Some(column) => column, + empty => empty.insert(key_column(self.batch, key.column)?), + }; + column[row].clone() } }; - if matches!(value, Value::Map(_)) && nested_nan(&value) { - return Err(invalid("NaN in collection sort key")); - } - *slot = value; } Ok(()) } @@ -362,46 +356,53 @@ async fn sort_with_fetch( + kept_rows * std::mem::size_of::() }; let order = |a: &HeldRow, b: &HeldRow| { - sort_order( - compare_rows(keys, a.keys.iter(), b.keys.iter()), - a.input_position, - b.input_position, - ) + let key_order = keys + .iter() + .zip(a.keys.iter().zip(b.keys.iter())) + .map(|(key, (a, b))| compare_held_key(a, b, key)) + .find(|order| order.is_ne()) + .unwrap_or(Ordering::Equal); + sort_order(key_order, a.input_position, b.input_position) }; let dtypes = keys .iter() .map(|key| plain(schema, key.column).map(|(dtype, _)| dtype.clone())) .collect::, _>>()?; let fetch = usize::try_from(fetch).unwrap_or(usize::MAX); + // The groups the batch being read kept rows in, each once: a group is + // added when its stamp is not yet that batch's number. + let mut touched: Vec = Vec::new(); + let mut touched_in_batch: Vec = Vec::new(); + let mut batch_number = 0u64; while let Some(batch) = input.next().await { let batch = batch?; work.checkpoint_rows(batch.num_rows()).await?; if fetch == 0 { continue; } + let mut kept_keys = KeptKeyValues::new(&batch, keys)?; + batch_number += 1; let held = held_bytes(&chunks, key_bytes, kept_rows); let ids = chunked_group_ids(&mut table, &batch, &mut workspace, held, &mut work).await?; heaps.resize_with(table.len(), Vec::new); - let mut kept_keys = KeptKeyValues::new(&batch, keys); + touched_in_batch.resize(table.len(), 0); + touched.clear(); + // The chunk index this batch's kept rows will take. + let reading = chunks.len() as u32; // Per held chunk, then the batch being read (last), the key // comparators of this batch's rows against its rows, built when a // heap's last row first lies there. let mut comparators: Vec>> = (0..=chunks.len()).map(|_| None).collect(); - // Whether each row of `batch` is still kept, and the groups it went to. + // Whether each row of `batch` is still kept. let mut kept = vec![false; batch.num_rows()]; - let mut touched: Vec = Vec::new(); for (row, &group) in ids.iter().enumerate() { let heap = &mut heaps[group as usize]; let position = input_position + row as u64; let full = heap.len() >= fetch; if full { let last = &heap[0]; - let slot = if last.chunk == READING { - chunks.len() - } else { - last.chunk as usize - }; + let slot = last.chunk as usize; let compare = match &mut comparators[slot] { Some(compare) => compare, empty => { @@ -431,24 +432,27 @@ async fn sort_with_fetch( } } kept[row] = true; - touched.push(group); + if touched_in_batch[group as usize] != batch_number { + touched_in_batch[group as usize] = batch_number; + touched.push(group); + } if full { // The row displaces the group's last kept row: overwrite // that row in place, its key allocation reused. let last = &mut heap[0]; key_bytes -= last.keys.iter().map(Value::bytes).sum::(); - if last.chunk == READING { + if last.chunk == reading { kept[last.row as usize] = false; } kept_keys.write(row, &mut last.keys)?; key_bytes += last.keys.iter().map(Value::bytes).sum::(); - (last.input_position, last.chunk, last.row) = (position, READING, row as u32); + (last.input_position, last.chunk, last.row) = (position, reading, row as u32); heap_sift_down_top(heap, &order); } else { let held = HeldRow { keys: kept_keys.row(row)?, input_position: position, - chunk: READING, + chunk: reading, row: row as u32, }; key_bytes += held.keys.iter().map(Value::bytes).sum::(); @@ -457,8 +461,8 @@ async fn sort_with_fetch( } } input_position += batch.num_rows() as u64; - // Take the batch's kept rows into a chunk, and point their held rows - // there. + // Take the batch's kept rows into chunk `reading`, and point their + // held rows at their rows there. let mut chunk_rows = vec![0u32; batch.num_rows()]; let mut taken = Vec::new(); for (row, _) in kept.iter().enumerate().filter(|(_, &kept)| kept) { @@ -466,11 +470,9 @@ async fn sort_with_fetch( taken.push(row as u32); } if !taken.is_empty() { - let chunk = chunks.len() as u32; for &group in &touched { for held in heaps[group as usize].iter_mut() { - if held.chunk == READING { - held.chunk = chunk; + if held.chunk == reading { held.row = chunk_rows[held.row as usize]; } } @@ -481,7 +483,11 @@ async fn sort_with_fetch( if held_rows > 2 * kept_rows.max(1) { compact(schema, &mut chunks, &mut heaps)?; } - workspace.set_bytes(table.memory_bytes() + held_bytes(&chunks, key_bytes, kept_rows))?; + workspace.set_bytes( + table.memory_bytes() + + touched_in_batch.len() * std::mem::size_of::() + + held_bytes(&chunks, key_bytes, kept_rows), + )?; } // Groups in key order, each group's rows in sort order, gathered from // the chunks in one copy. @@ -515,6 +521,28 @@ fn compact( Ok(()) } +/// [`compare_key`] for the key values of rows a sort with `fetch` holds, +/// with the common keys inline: two numbers, neither NaN (`-0` equal to +/// `0`), or two integers or times. NULL, NaN and every other type take +/// [`compare_key`]. The heap compares held rows about four times per kept +/// row; the full sort keeps [`compare_key`], which its merge runs faster +/// (inlining these arms there slowed a sort of 10^6 random numbers by a +/// sixth). +fn compare_held_key(a: &Value, b: &Value, key: &SortKey) -> Ordering { + let order = match (a, b) { + (Value::Float64(a), Value::Float64(b)) if !a.is_nan() && !b.is_nan() => { + a.partial_cmp(b).expect("neither is NaN") + } + (Value::Int64(a), Value::Int64(b)) | (Value::Timestamp(a), Value::Timestamp(b)) => a.cmp(b), + _ => return compare_key(a, b, key), + }; + if key.descending { + order.reverse() + } else { + order + } +} + /// The order of two values of one sort key: NULLs first or last, NaN after /// every number, then the key's direction. fn compare_key(a: &Value, b: &Value, key: &SortKey) -> std::cmp::Ordering { diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index ad9bd925e..df4f77564 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -1581,40 +1581,35 @@ fn lower_count_values( input: &SchemaRef, output: &SchemaRef, ) -> Result<(Option, Operator), Error> { - if !output.fields.iter().any(|field| field.name == label) { - let count = Operator::aggregate( - input.clone(), - groups(input, keys)?, - vec![(String::new(), Reduction::Count)], - )? - .with_output_schema(output.clone())?; - return Ok((None, count)); - } - let value = named_column(input, &ColumnRef::SampleValue)?; - let mut columns: Vec<_> = input - .fields - .iter() - .enumerate() - .map(|(i, field)| (field.name.clone(), Expression::Column(i))) - .collect(); - columns.push((label.to_string(), Expression::FloatText(value))); - let text = input.fields.len(); - let mut partitions = groups(input, keys)?; - match partitions - .iter() - .position(|&key| input.fields[key].name == label) - { - Some(key) => partitions[key] = text, - None => partitions.push(text), - } - let project = Operator::project(input.clone(), columns)?; - let aggregate = Operator::aggregate( - project.schema(), - partitions, - vec![(String::new(), Reduction::Count)], - )? - .with_output_schema(output.clone())?; - Ok((Some(project), aggregate)) + let (project, partitions) = if output.fields.iter().any(|field| field.name == label) { + let value = named_column(input, &ColumnRef::SampleValue)?; + let mut columns: Vec<_> = input + .fields + .iter() + .enumerate() + .map(|(i, field)| (field.name.clone(), Expression::Column(i))) + .collect(); + columns.push((label.to_string(), Expression::FloatText(value))); + let text = input.fields.len(); + let mut partitions = groups(input, keys)?; + match partitions + .iter() + .position(|&key| input.fields[key].name == label) + { + Some(key) => partitions[key] = text, + None => partitions.push(text), + } + (Some(Operator::project(input.clone(), columns)?), partitions) + } else { + (None, groups(input, keys)?) + }; + let counted = project + .as_ref() + .map_or_else(|| input.clone(), Operator::schema); + let aggregate = + Operator::aggregate(counted, partitions, vec![(String::new(), Reduction::Count)])? + .with_output_schema(output.clone())?; + Ok((project, aggregate)) } /// `node` with its `without` grouping (PromQL `sum without (instance) (m)`) @@ -1669,10 +1664,13 @@ fn resolve_without( // `count_values("zone", v)`'s label replaces a kept label `zone`, as // Prometheus overwrites it before grouping: the kept label set drops it // too, so `{job=b, zone=z}` and `{job=b}` with the value 2 are one group - // `{job=b, zone="2"}`. An excluded label is removed already. + // `{job=b, zone="2"}`. Whether the label itself is output (not when the + // grouping removes it: an excluded label or `__name__`) is the IR's + // `output`, which `lower_count_values` reads; removing such a label + // twice here changes nothing. let replaced = match &node.payload { Payload::NonASAP(NonASAPOp::Aggregate { measures, .. }) => match measures.as_slice() { - [AggIntent::CountValues { label }] if !excluded.contains(label) => Some(label.clone()), + [AggIntent::CountValues { label }] => Some(label.clone()), _ => None, }, _ => None, diff --git a/crates/executor/src/physical_planner/promql_rows.rs b/crates/executor/src/physical_planner/promql_rows.rs index 4308a5f36..b5cf91207 100644 --- a/crates/executor/src/physical_planner/promql_rows.rs +++ b/crates/executor/src/physical_planner/promql_rows.rs @@ -36,8 +36,13 @@ pub fn decode_series_identity(encoded: &str) -> Result, /// encoding nothing. A name token without an escape is the name itself /// (JSON escapes only `"`, `\` and control characters), so it is compared /// with the excluded names byte for byte; a token with one is compared with -/// each excluded name's encoding. An error for input that is not a JSON -/// object of string labels. +/// each excluded name's encoding. The input must be canonical, as every +/// identity the executor writes is (through `series_row`): only its +/// structure is checked, so input that is not a JSON object of string +/// labels is an error, but a non-canonical one (`{"b":"1","a":"2"}`, +/// `{"a":"\/"}`) or an invalid escape inside a string is copied as it is: +/// checking the canonical form would add a fifth to a third to the +/// rewrite's time, on every identity of every batch. pub fn series_identity_without(identity: &str, excluding: &[String]) -> Result { let bytes = identity.as_bytes(); let malformed = || invalid("series identity is not canonically encoded"); @@ -241,4 +246,66 @@ mod tests { ); } } + + /// Over identities mutated a character at a time (replaced, inserted, + /// removed, or two swapped), the rewrite never panics, and equals + /// decode, filter, encode wherever [`decode_series_identity`] accepts + /// the input. Input decode refuses may be copied (see + /// [`series_identity_without`]). + #[test] + fn series_identity_without_equals_decode_on_mutated_identities() { + const ALPHABET: [char; 18] = [ + '"', '\\', 'u', '0', '1', 'a', 'b', 'f', 'F', 'n', 't', '{', '}', ':', ',', '/', + '\u{1}', '☃', + ]; + let mut state = 7u64; + let mut below = |n: usize| { + state = state + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + (state >> 33) as usize % n + }; + let seeds = [ + r#"{}"#, + r#"{"a":"1"}"#, + r#"{"__name__":"m","instance":"1","job":"a"}"#, + r#"{"\"":"\\","a\nb":"\u0001\t","z":""}"#, + r#"{"a":"\b\f\r","b":"☃\u001f"}"#, + ]; + let exclusions: [Vec; 3] = [ + vec![], + vec!["a".into()], + vec!["instance".into(), "\"".into()], + ]; + let (mut accepted, mut refused) = (0, 0); + for case in 0..20_000 { + let mut identity: Vec = seeds[case % seeds.len()].chars().collect(); + for _ in 0..1 + below(3) { + let at = below(identity.len() + 1); + let letter = ALPHABET[below(ALPHABET.len())]; + match below(4) { + 0 if at < identity.len() => identity[at] = letter, + 1 => identity.insert(at, letter), + 2 if at < identity.len() => drop(identity.remove(at)), + _ if at + 1 < identity.len() => identity.swap(at, at + 1), + _ => {} + } + } + let identity: String = identity.into_iter().collect(); + let excluding = &exclusions[case % exclusions.len()]; + let rewritten = series_identity_without(&identity, excluding); + match decode_series_identity(&identity) { + Ok(_) => { + accepted += 1; + assert_eq!( + rewritten.unwrap(), + decoded_without(&identity, excluding), + "{identity}" + ); + } + Err(_) => refused += 1, + } + } + assert!(accepted > 1_000 && refused > 1_000, "{accepted}/{refused}"); + } } diff --git a/crates/executor/src/values/arrow_columns.rs b/crates/executor/src/values/arrow_columns.rs index 5528433d6..daa9b77c8 100644 --- a/crates/executor/src/values/arrow_columns.rs +++ b/crates/executor/src/values/arrow_columns.rs @@ -359,6 +359,43 @@ impl<'a> DictionaryTexts<'a> { } } +/// The value of row `row` of `array`, a column of Planner type `dtype`, +/// read from that one cell: `Float64(2.5)` from a `Float64Array` holding +/// `2.5`. `None` for a type [`decode`] reads a column at a time: text (a +/// batch holds it as a dictionary) and the nested types. +pub(crate) fn decode_cell(array: &dyn Array, dtype: &DataType, row: usize) -> Option { + fn typed(array: &dyn Array) -> &A { + array + .as_any() + .downcast_ref::() + .expect("column has its declared layout") + } + let null = array.is_null(row); + Some(match dtype { + DataType::Null => Value::Null, + DataType::Bool | DataType::Int64 | DataType::Float64 if null => Value::Null, + DataType::Timestamp | DataType::Date | DataType::Interval if null => Value::Null, + DataType::Bool => Value::Bool(typed::(array).value(row)), + DataType::Int64 => Value::Int64(typed::(array).value(row)), + DataType::Float64 => Value::Float64(typed::(array).value(row)), + DataType::Timestamp => { + Value::Timestamp(typed::(array).value(row)) + } + DataType::Date => Value::Date(typed::(array).value(row)), + DataType::Interval => { + let value: IntervalMonthDayNano = typed::(array).value(row); + Value::Interval { + months: value.months, + days: value.days, + nanos: value.nanoseconds, + } + } + DataType::Utf8 | DataType::List { .. } | DataType::Struct { .. } | DataType::Map { .. } => { + return None + } + }) +} + /// Every value of `array`, a column of Planner type `dtype`. pub(crate) fn decode(array: &dyn Array, dtype: &DataType, depth: ColumnDepth) -> Vec { let length = array.len(); @@ -383,18 +420,14 @@ pub(crate) fn decode(array: &dyn Array, dtype: &DataType, depth: ColumnDepth) -> } match dtype { DataType::Null => vec![Value::Null; length], - DataType::Bool => primitive!(BooleanArray, Value::Bool), - DataType::Int64 => primitive!(Int64Array, Value::Int64), - DataType::Float64 => primitive!(Float64Array, Value::Float64), - DataType::Timestamp => primitive!(TimestampMillisecondArray, Value::Timestamp), - DataType::Date => primitive!(Date32Array, Value::Date), - DataType::Interval => primitive!(IntervalMonthDayNanoArray, |v: IntervalMonthDayNano| { - Value::Interval { - months: v.months, - days: v.days, - nanos: v.nanoseconds, - } - }), + DataType::Bool + | DataType::Int64 + | DataType::Float64 + | DataType::Timestamp + | DataType::Date + | DataType::Interval => (0..length) + .map(|row| decode_cell(array, dtype, row).expect("a type read by the cell")) + .collect(), DataType::Utf8 if depth == ColumnDepth::BatchColumn => { let typed = array .as_any() diff --git a/crates/executor/tests/physical_semantics.rs b/crates/executor/tests/physical_semantics.rs index 0731631ef..8d8937204 100644 --- a/crates/executor/tests/physical_semantics.rs +++ b/crates/executor/tests/physical_semantics.rs @@ -384,6 +384,229 @@ fn sort_with_fetch_equals_sort_then_limit() { } } +/// The rows of `ops` run in a chain over `batches` of `input`, or the +/// error the run returns. +fn try_chain( + input: &SchemaRef, + batches: Vec, + ops: Vec, +) -> Result>, String> { + let mut dag = PhysicalDAG::default(); + dag.add(0, vec![], Operator::source(input.clone(), batches).unwrap()) + .unwrap(); + let root = ops.len() as u64; + for (index, op) in ops.into_iter().enumerate() { + dag.add(index as u64 + 1, vec![index as u64], op).unwrap(); + } + block_on(async { + let mut stream = dag + .execute(&[root], context()) + .map_err(|error| error.to_string())? + .remove(0); + let mut rows = vec![]; + while let Some(batch) = stream.next().await { + rows.extend(batch.map_err(|error| error.to_string())?.to_rows()); + } + Ok(rows) + }) +} + +/// A linear congruential generator: the same cases on every run. +struct Lcg(u64); +impl Lcg { + fn below(&mut self, n: u64) -> u64 { + self.0 = self + .0 + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + (self.0 >> 33) % n + } +} + +// Over random batches, groups, keys, directions and `fetch`, a sort that +// keeps the first `fetch` rows of each group answers the same rows, or the +// same error, as a full sort and a per-group limit. The keys mix numbers +// (NULL, NaN, ±inf, -0), nullable text (empty, case, unicode, NUL) and maps +// of nullable numbers (a NULL, -0 or NaN value inside), so the test of a +// row against its group's last kept row agrees with the heap's order. +#[test] +fn sort_with_fetch_equals_sort_then_limit_over_random_rows() { + let input = schema(&[ + ("g", DataType::Utf8, false), + ("h", DataType::Int64, false), + ("v", DataType::Float64, true), + ("w", DataType::Int64, true), + ("id", DataType::Int64, false), + ("t", DataType::Utf8, true), + ( + "m", + DataType::Map { + key: Box::new(DataType::Utf8), + value: Box::new(DataType::Float64), + value_nullable: true, + }, + true, + ), + ]); + const TEXTS: [&str; 8] = ["", "a", "A", "b", "ä", "a\u{0}", "zz", "☃"]; + let map = |random: &mut Lcg, nan: bool| { + if random.below(8) == 0 { + return Value::Null; + } + let mut entries = Vec::new(); + for name in ["a", "b"] { + if random.below(2) == 0 { + continue; + } + let value = match random.below(7) { + 0 => Value::Null, + 1 => Value::Float64(-0.), + 2 => Value::Float64(0.), + 3 if nan => Value::Float64(f64::NAN), + _ => Value::Float64(random.below(3) as f64), + }; + entries.push((Value::Utf8(name.into()), value)); + } + Value::Map(entries.into()) + }; + for (seed, key_columns, nan_in_map) in [ + (42, vec![2, 3], false), + (7, vec![2, 3, 5], false), + (8, vec![5], false), + (9, vec![6], false), + (10, vec![2, 5, 6], false), + (11, vec![6], true), + ] { + let mut random = Lcg(seed); + for case in 0..400 { + let group_count = 1 + random.below(6); + let distinct_values = 1 + random.below(8); + let mut id = 0; + let batches: Vec = (0..1 + random.below(30)) + .map(|_| { + let rows = (0..random.below(40)) + .map(|_| { + id += 1; + vec![ + Value::Utf8(format!("g{}", random.below(group_count)).into()), + Value::Int64(random.below(2) as i64), + match random.below(12) { + 0 => Value::Null, + 1 => Value::Float64(f64::NAN), + 2 => Value::Float64(f64::INFINITY), + 3 => Value::Float64(f64::NEG_INFINITY), + 4 => Value::Float64(-0.), + _ => Value::Float64(random.below(distinct_values) as f64), + }, + if random.below(10) == 0 { + Value::Null + } else { + Value::Int64(random.below(3) as i64) + }, + Value::Int64(id), + if random.below(5) == 0 { + Value::Null + } else { + Value::Utf8(TEXTS[random.below(8) as usize].into()) + }, + map(&mut random, nan_in_map), + ] + }) + .collect(); + Batch::try_new(input.clone(), rows).unwrap() + }) + .collect(); + let groups = [vec![], vec![0], vec![0, 1], vec![5]][case % 4].clone(); + let keys: Vec = (0..1 + random.below(3)) + .map(|_| SortKey { + column: key_columns[random.below(key_columns.len() as u64) as usize], + descending: random.below(2) == 0, + nulls_first: random.below(2) == 0, + }) + .collect(); + let fetch = [0, 1, 2, 3, 5, 1000][random.below(6) as usize]; + let sort = |fetch| Operator::sort(input.clone(), keys.clone(), groups.clone(), fetch); + let fetched = try_chain(&input, batches.clone(), vec![sort(Some(fetch)).unwrap()]); + let limited = try_chain( + &input, + batches, + vec![ + sort(None).unwrap(), + Operator::limit(input.clone(), fetch, 0, groups.clone()).unwrap(), + ], + ); + assert_eq!( + format!("{fetched:?}"), + format!("{limited:?}"), + "seed {seed}, case {case}: groups {groups:?}, keys {keys:?}, fetch {fetch}" + ); + } + } +} + +// Two series identities that differ only in an excluded label rewrite to +// the same identity, so `sum without (instance)` adds them in one group, +// within a batch and across batches. +#[test] +fn identities_equal_but_for_an_excluded_label_aggregate_in_one_group() { + let input = schema(&[ + ("identity", DataType::Utf8, false), + ("v", DataType::Float64, false), + ]); + let row = + |identity: &str, value: f64| vec![Value::Utf8(identity.into()), Value::Float64(value)]; + let batches = vec![ + vec![ + row(r#"{"instance":"1","job":"a"}"#, 1.), + row(r#"{"instance":"2","job":"a"}"#, 2.), + row(r#"{"instance":"1","job":"b"}"#, 4.), + row(r#"{"instance":"1","job":"a"}"#, 8.), + ], + vec![row(r#"{"instance":"3","job":"a"}"#, 16.)], + ]; + let project = Operator::project( + input.clone(), + vec![ + ( + "identity".into(), + Expression::LabelIdentity { + column: 0, + excluding: vec!["instance".into()], + }, + ), + ("v".into(), Expression::Column(1)), + ], + ) + .unwrap(); + let projected = project.schema(); + let mut dag = PhysicalDAG::default(); + let source = batches + .into_iter() + .map(|rows| Batch::try_new(input.clone(), rows).unwrap()) + .collect(); + dag.add(0, vec![], Operator::source(input.clone(), source).unwrap()) + .unwrap(); + dag.add(1, vec![0], project).unwrap(); + dag.add( + 2, + vec![1], + Operator::aggregate(projected, vec![0], vec![("sum".into(), Reduction::Sum(1))]).unwrap(), + ) + .unwrap(); + let mut sums = collect(&dag, 2) + .into_iter() + .map(|row| format!("{row:?}")) + .collect::>(); + sums.sort(); + assert_eq!( + sums, + [ + format!("{:?}", row(r#"{"job":"a"}"#, 27.)), + format!("{:?}", row(r#"{"job":"b"}"#, 4.)), + ] + ); +} + // Outer joins preserve unmatched NULLs, while semi/anti joins preserve left multiplicity. #[test] fn joins_nulls_duplicates_and_empty_sides() { diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs index 6092ea737..0c79d1b36 100644 --- a/crates/planner/tests/reduce_aggregates.rs +++ b/crates/planner/tests/reduce_aggregates.rs @@ -319,6 +319,50 @@ async fn promql_count_values_label_excluded_by_without_is_dropped() { ); } +/// A `count_values` label `__name__` under `without` is set and then +/// removed, as a `without` grouping always drops `__name__` in Prometheus: +/// the values do not split the groups, so the two `zone="z"` series count +/// in one group. +#[tokio::test] +async fn promql_count_values_name_label_under_without_is_dropped() { + let at = EVALUATION_MS - 5_000; + let samples = vec![ + sample( + "h", + &[("job", "a"), ("instance", "1"), ("zone", "z")], + at, + 2.0, + ), + sample("h", &[("job", "a"), ("instance", "2")], at, 2.0), + sample( + "h", + &[("job", "a"), ("instance", "3"), ("zone", "y")], + at, + 2.0, + ), + sample( + "h", + &[("job", "a"), ("instance", "4"), ("zone", "z")], + at, + 5.0, + ), + ]; + let answers = promql( + r#"count_values("__name__", h) without (instance)"#, + "h", + &samples, + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 1.0), + (labels(&[("job", "a"), ("zone", "y")]), 1.0), + (labels(&[("job", "a"), ("zone", "z")]), 2.0), + ], + ); +} + /// A `count_values` label named like a kept label column (here `job`, /// a column since the selector names it) replaces that column with the /// value text: job `b`'s series both read `job="2"`, apart by `zone`. diff --git a/crates/types/src/ir/schema/aggregate_schema.rs b/crates/types/src/ir/schema/aggregate_schema.rs index bd68bf3f0..d65a7e0f7 100644 --- a/crates/types/src/ir/schema/aggregate_schema.rs +++ b/crates/types/src/ir/schema/aggregate_schema.rs @@ -14,9 +14,11 @@ use crate::ir::SchemaDerivationError; /// Where one output column of an aggregate comes from: an input column it /// keeps, a measure's value (with the input column the schema types it by, /// the measure's own or the probe value column), or a label column the -/// aggregate computes (`count_values`' and `absent`'s, and a series identity +/// aggregate computes (`count_values`' and `absent`'s; a series identity /// a `without` grouping keeps, its labels less the excluded ones and -/// `__name__`). `sum by (job) (x)` → +/// `__name__`; and a `by` or kept column that a `count_values` label of the +/// same name replaces: `count_values by (job) ("job", v)` → +/// `[ComputedLabel, Measure { .. }]`). `sum by (job) (x)` → /// `[InputColumn(job), Measure { index: 0, input_column: Some(value) }]`. The one layout the output schema /// ([`aggregate_output_schema`]) and the group properties read. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -287,6 +289,17 @@ pub fn without_kept_columns(in_schema: &Schema, excluded: &[ColumnId]) -> Vec bool { + label == "__name__" + || excluded + .iter() + .any(|&id| in_schema.fields[id].name == label) +} + /// Output schema of a `without(excluded)` aggregate: the kept labels (every /// input label column except the `excluded` positions, the time axis, and the /// sample-value column) followed by the aggregate output column(s), a @@ -298,7 +311,8 @@ pub fn without_kept_columns(in_schema: &Schema, excluded: &[ColumnId]) -> Vec Date: Sun, 15 Nov 2026 10:43:24 +0000 Subject: [PATCH 5/8] fix(executor): count without installs; fewer random sort cases in debug `count without (instance) (m)` failed to install with `PromQL vector requires labels, time and one value`: its output keeps a series identity, so the lowering adds the label rewrite that drops `__name__`, and that rewrite's layout accepted only a Float64 value, while a cross-series count is Int64 (as `count by` answers it). The layout now takes the one numeric column as the value; operators that compute on the value (binary operators, `histogram_quantile`, `label_replace`) read it through `float_layout`, which still requires Float64. Tests: planner `count without (instance)` and `(instance, zone)`, `group without` and `stddev without` with hand-computed answers; an executor test that a label rewrite carries an Int64 count and a binary operator refuses it. `sort_with_fetch_equals_sort_then_limit_over_random_rows` runs every seed with 40 cases per seed in a debug build (400 in release): 63 s to 2.4 s. --- .../executor/src/operators/series_labels.rs | 59 ++++++++++++++++--- crates/executor/tests/physical_semantics.rs | 5 +- crates/planner/tests/reduce_aggregates.rs | 53 +++++++++++++++++ .../develop_docs/physical-compile-coverage.md | 2 +- 4 files changed, 110 insertions(+), 9 deletions(-) diff --git a/crates/executor/src/operators/series_labels.rs b/crates/executor/src/operators/series_labels.rs index fdf8b8b73..2bce411a4 100644 --- a/crates/executor/src/operators/series_labels.rs +++ b/crates/executor/src/operators/series_labels.rs @@ -9,8 +9,11 @@ use planner_types::ir::schema::PROMQL_SERIES_IDENTITY; pub(super) type Labels = BTreeMap; /// Where a row's label set lives: the encoded series identity when present, -/// otherwise the non-empty Utf8 label columns. The one Float64 column is the -/// sample value, whatever an aggregate named it. +/// otherwise the non-empty Utf8 label columns. The one numeric column is the +/// sample value, whatever an aggregate named it: Float64, or Int64 for a +/// count (`count without (instance) (m)` answers `{job="a"} 3` as Int64, +/// as `count by (job) (m)` does). A label rewrite carries the value as it +/// is; an operator that computes on it reads it with [`float_layout`]. #[derive(Clone, Debug)] pub(super) struct Layout { pub(super) identity: Option, @@ -26,7 +29,7 @@ pub(super) fn layout(input: &SchemaRef) -> Result { match plain(input, i)? { (DataType::Utf8, false) if field.name == PROMQL_SERIES_IDENTITY => identity = Some(i), (DataType::Utf8, _) if field.name != PROMQL_SERIES_IDENTITY => labels.push(i), - (DataType::Float64, false) if value.is_none() => value = Some(i), + (DataType::Float64 | DataType::Int64, false) if value.is_none() => value = Some(i), (DataType::Timestamp, false) if input.time_index == Some(i) => {} _ => return Err(invalid("PromQL vector requires labels, time and one value")), } @@ -38,6 +41,17 @@ pub(super) fn layout(input: &SchemaRef) -> Result { }) } +/// [`layout`] for an operator that computes on the sample value (a binary +/// operator, `histogram_quantile`, `label_replace`): it reads and writes +/// the value as Float64, so an Int64 count is refused. +pub(super) fn float_layout(input: &SchemaRef) -> Result { + let layout = layout(input)?; + if plain(input, layout.value)?.0 != &DataType::Float64 { + return Err(invalid("PromQL vector value must be Float64 here")); + } + Ok(layout) +} + impl Layout { #[cfg(test)] pub(super) fn read(&self, input: &SchemaRef, row: &[Value]) -> Result { @@ -96,8 +110,8 @@ impl Operator { if destination.is_empty() || destination == PROMQL_SERIES_IDENTITY { return Err(invalid("invalid or reserved destination label name")); } - layout(&input)?; - let out = layout(&output)?; + float_layout(&input)?; + let out = float_layout(&output)?; if input .fields .iter() @@ -171,7 +185,7 @@ impl Operator { /// label set other than the `le` column's label. The result drops `le`, /// `__name__` and the time column; its value is the quantile. pub fn series_histogram_quantile(input: SchemaRef, q: f64, le: usize) -> Result { - let layout = layout(&input)?; + let layout = float_layout(&input)?; if !layout.labels.contains(&le) { return Err(invalid("histogram bucket bound must be a label column")); } @@ -235,7 +249,7 @@ impl Operator { return Err(invalid("PromQL scalar operand must be one Float64 value")); } } else { - layout(schema)?; + float_layout(schema)?; } } let mut output = if scalars == [true, false] { @@ -1268,4 +1282,35 @@ mod columnar_tests { } assert!(errors > 0, "the repeated-label-set error is exercised"); } + + /// `count without (instance) (m)` outputs `[identity, count: Int64]`: a + /// label rewrite carries the Int64 count as it is, while an operator + /// that computes on the value (`* on() ...`, `histogram_quantile`) + /// requires a Float64 sample. + #[test] + fn a_label_rewrite_carries_an_int64_count_and_a_computation_refuses_it() { + let counted = Arc::new(Schema { + fields: vec![ + field(PROMQL_SERIES_IDENTITY, DataType::Utf8, false), + field("count", DataType::Int64, false), + ], + time_index: None, + unique_keys: vec![], + closed: false, + }); + Operator::series_without_name(counted.clone()).unwrap(); + Operator::series_labels(counted.clone(), VectorMatchKind::On, vec!["job".into()]).unwrap(); + let multiply = BinaryOperator { + kind: BinaryOpKind::Arithmetic(planner_types::ir::scalar::ArithmeticOpKind::Mul), + vector_match: None, + checked_relative_division: false, + checked_finite_division: false, + }; + let Err(error) = + Operator::series_binary(counted.clone(), counted, multiply, [false, false]) + else { + panic!("a binary operator over an Int64 count is refused"); + }; + assert!(error.to_string().contains("must be Float64"), "{error}"); + } } diff --git a/crates/executor/tests/physical_semantics.rs b/crates/executor/tests/physical_semantics.rs index 8d8937204..c13d0ab70 100644 --- a/crates/executor/tests/physical_semantics.rs +++ b/crates/executor/tests/physical_semantics.rs @@ -429,8 +429,11 @@ impl Lcg { // (NULL, NaN, ±inf, -0), nullable text (empty, case, unicode, NUL) and maps // of nullable numbers (a NULL, -0 or NaN value inside), so the test of a // row against its group's last kept row agrees with the heap's order. +// Every seed (each key kind) runs in every build; a debug build runs 40 +// cases per seed, a release build 400. #[test] fn sort_with_fetch_equals_sort_then_limit_over_random_rows() { + const CASES_PER_SEED: usize = if cfg!(debug_assertions) { 40 } else { 400 }; let input = schema(&[ ("g", DataType::Utf8, false), ("h", DataType::Int64, false), @@ -478,7 +481,7 @@ fn sort_with_fetch_equals_sort_then_limit_over_random_rows() { (11, vec![6], true), ] { let mut random = Lcg(seed); - for case in 0..400 { + for case in 0..CASES_PER_SEED { let group_count = 1 + random.below(6); let distinct_values = 1 + random.below(8); let mut id = 0; diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs index 0c79d1b36..6b9de2c46 100644 --- a/crates/planner/tests/reduce_aggregates.rs +++ b/crates/planner/tests/reduce_aggregates.rs @@ -399,6 +399,59 @@ async fn promql_sum_without_instance() { ); } +/// `count without (instance)` counts the series of each group of every +/// other label: job `a` has 3 series, `{job="b"}` and `{job="b", zone="z"}` +/// one each. Dropping `zone` as well merges job `b`'s two series. +#[tokio::test] +async fn promql_count_without_instance() { + let answers = promql("count without (instance) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 3.0), + (labels(&[("job", "b")]), 1.0), + (labels(&[("job", "b"), ("zone", "z")]), 1.0), + ], + ); + let answers = promql("count without (instance, zone) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 3.0), + (labels(&[("job", "b")]), 2.0), + ], + ); +} + +/// `group without (instance)` answers 1 per group of every other label. +#[tokio::test] +async fn promql_group_without_instance() { + let answers = promql("group without (instance) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 1.0), + (labels(&[("job", "b")]), 1.0), + (labels(&[("job", "b"), ("zone", "z")]), 1.0), + ], + ); +} + +/// `stddev without (instance)`: job `a`'s 1, 2, 4 have variance 14/9; the +/// two single-series groups of job `b` have deviation 0. +#[tokio::test] +async fn promql_stddev_without_instance() { + let answers = promql("stddev without (instance) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), (14.0f64 / 9.0).sqrt()), + (labels(&[("job", "b")]), 0.0), + (labels(&[("job", "b"), ("zone", "z")]), 0.0), + ], + ); +} + /// An exact `topk` over a grouped sum of rates answers the 3 largest of /// the grouped sum's answers, with their jobs. #[tokio::test] diff --git a/docs/develop_docs/physical-compile-coverage.md b/docs/develop_docs/physical-compile-coverage.md index 5aa444fbd..1f66091a0 100644 --- a/docs/develop_docs/physical-compile-coverage.md +++ b/docs/develop_docs/physical-compile-coverage.md @@ -209,7 +209,7 @@ Totals after this change: 21 Supported, 5 Partial, 3 Missing, 2 Backend. | Row | Change | |---|---| | 1 | PromQL `stddev`, `stdvar`, `group` and `count_values`, with `by` or `without`. `stddev`/`stdvar` use Welford's update, as Prometheus does: `stddev by (job) (m)` over job `a`'s 1, 2 and 4 is the square root of 14/9. `group` is 1 per group. `count_values("v", m)` writes each value as Go's `FormatFloat(v, 'f', -1, 64)` does (`0.30000000000000004`, `1000000000000000000000`, `NaN`, `+Inf`), and its label replaces a grouping or kept label of that name, as in Prometheus. A label the `without` list also names is removed again, so `count_values("zone", m) without (instance, zone)` counts per kept group with no `zone` label. Still Partial. | -| 2 | `without` grouping over the series identity: `sum without (instance) (m)` groups by every label but `instance` and `__name__`, so `{job=a, instance=1, zone=z}` answers as `{job=a, zone=z}`. The IR marks that kept identity as computed, not copied. A ranking (`topk`, `Sort`, `Limit`) partitioned `without` over the identity is refused, since it keeps each row's labels. | +| 2 | `without` grouping over the series identity: `sum without (instance) (m)` groups by every label but `instance` and `__name__`, so `{job=a, instance=1, zone=z}` answers as `{job=a, zone=z}`. The IR marks that kept identity as computed, not copied. `count without (instance) (m)` answers its count as Int64, as `count by` does: the label rewrite that drops `__name__` carries any numeric sample, while an operator that computes on the sample (a binary operator between vectors, `histogram_quantile`, `label_replace`) requires Float64. A ranking (`topk`, `Sort`, `Limit`) partitioned `without` over the identity is refused, since it keeps each row's labels. | | 3 | An exact `topk`, `Aggregate{Reduce, [TopK]}`, lowers to a `Sort` that keeps the first k rows per partition (value descending, NULLs and NaN last; `fetch: Some(k)`, a bounded heap of k rows per partition) and a `Project` of the ranked rows. The current-series top-k (row 21) lowers to the same sort. `topk(2, sum by (job) (rate(c[5m])))` answers the two largest job sums with their jobs; `topk by (job) (1, m)` keeps each series' full labels, `__name__` included. | | 29 | SQL `STDDEV`, `STDDEV_SAMP`, `STDDEV_POP`, `VAR_SAMP`, `VAR_POP` and `corr` over Float64 or Int64 columns: NULL for fewer than two values (sample forms) or none (population forms), `corr` NULL for a constant input. | From 0be01c5db4d5580f6269fc201e554f55b5482f72 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:10:13 +0000 Subject: [PATCH 6/8] fix(ir): renumber parent column references when realizing series identity topk(1, sum without (instance) (m)) answered {job="b"} 2 instead of {job="a"} 7: the frontend binds the sort key to Column(0) of the aggregate's [sum] output, and identity realization then inserts the identity column ahead of the measure without updating the parent. sort, sort_desc and bottomk ordered by the identity text the same way. with_promql_series_identity now compares each node's input scope before and after realization and, when a column moved, renumbers every column reference into that scope (NonASAPOp::map_input_columns, with map_columns on GroupKeys, Reduction and AggIntent). A reference to a column realization does not keep is an error. --- crates/planner/tests/reduce_aggregates.rs | 123 +++++++++++- crates/types/src/ir/operator/agg_intent.rs | 36 ++++ crates/types/src/ir/operator/arguments.rs | 23 +++ crates/types/src/ir/operator/non_asap.rs | 113 ++++++++++- crates/types/src/ir/schema/series_identity.rs | 175 +++++++++++++++++- 5 files changed, 455 insertions(+), 15 deletions(-) diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs index 6b9de2c46..b55eb66b2 100644 --- a/crates/planner/tests/reduce_aggregates.rs +++ b/crates/planner/tests/reduce_aggregates.rs @@ -119,15 +119,28 @@ async fn plan_promql(query: &str, metric: &str) -> InstallablePlan { .0 } -/// Each answer of the PromQL `query` over `samples`: its labels (the -/// series identity's and each non-empty label column's) and its value. +/// Each answer of the PromQL `query` over `samples`, sorted by labels: its +/// labels (the series identity's and each non-empty label column's) and +/// its value. async fn promql( query: &str, metric: &str, samples: &[RawDataSample], +) -> Vec<(BTreeMap, f64)> { + let mut answers = promql_in_output_order(query, metric, samples).await; + answers.sort_by(|a, b| a.0.cmp(&b.0)); + answers +} + +/// Each answer of the PromQL `query` over `samples`, in the order the +/// query outputs them (`sort` and `sort_desc` order their answers). +async fn promql_in_output_order( + query: &str, + metric: &str, + samples: &[RawDataSample], ) -> Vec<(BTreeMap, f64)> { let plan = plan_promql(query, metric).await; - let mut answers: Vec<_> = run_query(&plan, |dag| time_series_tables(dag, samples)) + run_query(&plan, |dag| time_series_tables(dag, samples)) .iter() .flat_map(|batch| { batch.to_rows().into_iter().map(|row| { @@ -143,9 +156,7 @@ async fn promql( (series_labels(batch.schema(), &row), value) }) }) - .collect(); - answers.sort_by(|a, b| a.0.cmp(&b.0)); - answers + .collect() } fn assert_close( @@ -452,6 +463,106 @@ async fn promql_stddev_without_instance() { ); } +/// `topk`, `bottomk`, `sort` and `sort_desc` over a `without` aggregate +/// rank by the aggregate's value, not by its series identity: `sum without +/// (instance) (m)` answers `{job="a"}` 7, `{job="b"}` 2 and `{job="b", +/// zone="z"}` 2.5, so the largest is job `a`'s 7 and the smallest job `b`'s +/// 2. +#[tokio::test] +async fn promql_rankings_over_sum_without_rank_by_value() { + let samples = gauge_samples(); + let a = || labels(&[("job", "a")]); + let b = || labels(&[("job", "b")]); + let bz = || labels(&[("job", "b"), ("zone", "z")]); + let top = promql("topk(1, sum without (instance) (m))", "m", &samples).await; + assert_close(&top, &[(a(), 7.0)]); + let bottom = promql("bottomk(1, sum without (instance) (m))", "m", &samples).await; + assert_close(&bottom, &[(b(), 2.0)]); + let top_two = promql("topk(2, sum without (instance) (m))", "m", &samples).await; + assert_close(&top_two, &[(a(), 7.0), (bz(), 2.5)]); + // A matcher on `job` binds `job` before realization: the aggregate's + // output starts `[job, sum]`, and the ranking reads the value there. + let top = promql( + r#"topk(1, sum without (instance) (m{job=~"a|b"}))"#, + "m", + &samples, + ) + .await; + assert_close(&top, &[(a(), 7.0)]); + let sorted = promql_in_output_order("sort(sum without (instance) (m))", "m", &samples).await; + assert_close(&sorted, &[(b(), 2.0), (bz(), 2.5), (a(), 7.0)]); + let sorted_desc = + promql_in_output_order("sort_desc(sum without (instance) (m))", "m", &samples).await; + assert_close(&sorted_desc, &[(a(), 7.0), (bz(), 2.5), (b(), 2.0)]); +} + +/// The same over `count without (instance) (m)`: job `a` counts 3 series, +/// `{job="b"}` and `{job="b", zone="z"}` one each. The two 1s tie, so the +/// sorted answers are checked as values in order and labels as a set. +#[tokio::test] +async fn promql_rankings_over_count_without_rank_by_value() { + let samples = gauge_samples(); + let a = || labels(&[("job", "a")]); + let b = || labels(&[("job", "b")]); + let bz = || labels(&[("job", "b"), ("zone", "z")]); + let top = promql("topk(1, count without (instance) (m))", "m", &samples).await; + assert_close(&top, &[(a(), 3.0)]); + let bottom = promql("bottomk(2, count without (instance) (m))", "m", &samples).await; + assert_close(&bottom, &[(b(), 1.0), (bz(), 1.0)]); + let values = |answers: &[(BTreeMap, f64)]| -> Vec { + answers.iter().map(|(_, value)| *value).collect() + }; + let sorted = promql_in_output_order("sort(count without (instance) (m))", "m", &samples).await; + assert_eq!(values(&sorted), [1.0, 1.0, 3.0], "{sorted:?}"); + assert_eq!(sorted[2].0, a(), "{sorted:?}"); + let sorted_desc = + promql_in_output_order("sort_desc(count without (instance) (m))", "m", &samples).await; + assert_eq!(values(&sorted_desc), [3.0, 1.0, 1.0], "{sorted_desc:?}"); + assert_eq!(sorted_desc[0].0, a(), "{sorted_desc:?}"); +} + +/// Control: a grouped `topk by (job)` ranks within each job, over the raw +/// series and over a `without` aggregate. Job `a`'s largest series is +/// instance 3's 4 and its only `without` group sums to 7; job `b`'s are +/// `{instance="2", zone="z"}`'s 2.5 and the `{job="b", zone="z"}` group's +/// 2.5. +#[tokio::test] +async fn promql_topk_by_job_ranks_within_each_job() { + let samples = gauge_samples(); + let top = promql("topk by (job) (1, m)", "m", &samples).await; + assert_close( + &top, + &[ + ( + labels(&[ + ("__name__", "m"), + ("instance", "2"), + ("job", "b"), + ("zone", "z"), + ]), + 2.5, + ), + ( + labels(&[("__name__", "m"), ("instance", "3"), ("job", "a")]), + 4.0, + ), + ], + ); + let top = promql( + "topk by (job) (1, sum without (instance) (m))", + "m", + &samples, + ) + .await; + assert_close( + &top, + &[ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b"), ("zone", "z")]), 2.5), + ], + ); +} + /// An exact `topk` over a grouped sum of rates answers the 3 largest of /// the grouped sum's answers, with their jobs. #[tokio::test] diff --git a/crates/types/src/ir/operator/agg_intent.rs b/crates/types/src/ir/operator/agg_intent.rs index d17b2aab3..e814876f4 100644 --- a/crates/types/src/ir/operator/agg_intent.rs +++ b/crates/types/src/ir/operator/agg_intent.rs @@ -372,6 +372,42 @@ pub enum MathFunc { // `asap_frontend_promql::promql::reduction_for`) — so they stay generic // alongside `input_col`, in one `impl` block. impl AggIntent { + /// This intent with every input column renumbered by `column`: the + /// value columns of [`input_cols`](AggIntent::input_cols) and + /// `histogram_quantile`'s `le`. `Sum { col: Some(1) }` under a renumbering + /// that moves column 1 to 2 is `Sum { col: Some(2) }`. An `Extension` + /// names its columns, so it is unchanged. + pub(crate) fn map_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self { + let mut intent = self.clone(); + match &mut intent { + AggIntent::Sum { col } + | AggIntent::Min { col } + | AggIntent::Max { col } + | AggIntent::Avg { col } + | AggIntent::Quantile { col, .. } + | AggIntent::FrequencyL2 { col } + | AggIntent::FrequencyEntropy { col } + | AggIntent::StdDev { col, .. } + | AggIntent::Variance { col, .. } => { + if let Some(col) = col { + *col = column(*col); + } + } + AggIntent::Cardinality { cols } => { + for col in cols { + *col = column(*col); + } + } + AggIntent::PearsonCorr { left, right } => { + *left = column(*left); + *right = column(*right); + } + AggIntent::HistogramQuantile { le, .. } => *le = column(*le), + _ => {} + } + intent + } + /// Resolve the existing SQL arg-selector extension using its child schema. /// The tuple is (selected value column, ordering column). /// Unknown extensions remain owned by their deployment model. Recognized diff --git a/crates/types/src/ir/operator/arguments.rs b/crates/types/src/ir/operator/arguments.rs index e00962a0a..78c8ee556 100644 --- a/crates/types/src/ir/operator/arguments.rs +++ b/crates/types/src/ir/operator/arguments.rs @@ -91,6 +91,18 @@ impl GroupKeys { } } +impl GroupKeys { + /// These keys with every column renumbered by `column`, `by` or + /// `without` kept: `by (1)` under a renumbering that moves column 1 to 2 + /// is `by (2)`. + pub(crate) fn map_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self { + Self { + keys: self.keys.iter().map(|&key| column(key)).collect(), + without: self.without, + } + } +} + impl std::ops::Deref for GroupKeys { type Target = [C]; fn deref(&self) -> &Self::Target { @@ -500,6 +512,17 @@ impl Reduction { } } +impl Reduction { + /// This reduction with every grouping key renumbered by `column`; + /// `PerEntity` is unchanged. + pub(crate) fn map_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self { + match self { + Self::Reduce(keys) => Self::Reduce(keys.map_columns(column)), + Self::PerEntity => Self::PerEntity, + } + } +} + impl Reduction { /// Shorthand for the common case — group by these (possibly empty) /// keys, kept rather than excluded. diff --git a/crates/types/src/ir/operator/non_asap.rs b/crates/types/src/ir/operator/non_asap.rs index ff5103d77..2d7f68f9b 100644 --- a/crates/types/src/ir/operator/non_asap.rs +++ b/crates/types/src/ir/operator/non_asap.rs @@ -185,11 +185,23 @@ pub enum NonASAPOp> { } impl NonASAPOp { - /// The direct operator inputs, in field order, followed by the operator - /// nodes referenced from this operator's scalar expressions. + /// The direct operator inputs, followed by the operator nodes referenced + /// from this operator's scalar expressions. pub(crate) fn children(&self) -> Vec<&C> { + let mut out = self.inputs(); + for expr in self.scalar_exprs() { + out.extend(expr.operator_refs()); + } + out + } + + /// The direct operator inputs, in field order: a `Join`'s left then + /// right. Their output columns, in this order, are the operator's input + /// scope, which its column references index (see + /// [`NonASAPOp::map_input_columns`]). + pub(crate) fn inputs(&self) -> Vec<&C> { use NonASAPOp::*; - let mut out: Vec<&C> = match self { + match self { Scan { .. } | Values { .. } | PromqlVectorFromScalar(_) => vec![], Filter { child, .. } | Project { child, .. } @@ -207,11 +219,7 @@ impl NonASAPOp { Join { left, right, .. } | SetOp { left, right, .. } => vec![left, right], BinaryOp { lhs, rhs, .. } => vec![lhs, rhs], Concat { children, .. } => children.iter().collect(), - }; - for expr in self.scalar_exprs() { - out.extend(expr.operator_refs()); } - out } /// Every scalar expression this operator owns. @@ -457,6 +465,97 @@ impl NonASAPOp { } } + /// This operator with every column reference into its input scope + /// renumbered by `column`: the columns of its direct inputs, in + /// [`inputs`](NonASAPOp::inputs) order. A `Sort` by `Column(0)` over + /// `[sum]` whose input becomes `[$promql_series_identity, sum]` must sort + /// by `Column(1)`; the renumbering `0 → 1` gives that. Two references are + /// in another scope and kept: a `Scan`'s predicates index its own + /// schema, and an `Aggregate`'s `having` indexes its output. Operator + /// nodes read from scalar expressions keep their own scope. + pub(crate) fn map_input_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self + where + C: Clone, + { + use NonASAPOp::*; + let pred = |p: &Predicate, column: &mut dyn FnMut(ColumnId) -> ColumnId| { + Predicate(p.0.map_columns(&mut |c| column(c))) + }; + let keys = |keys: &[SortKey], column: &mut dyn FnMut(ColumnId) -> ColumnId| { + keys.iter() + .map(|k| SortKey { + expr: k.expr.map_columns(&mut |c| column(c)), + ascending: k.ascending, + nulls_first: k.nulls_first, + }) + .collect() + }; + let mut op = self.clone(); + match &mut op { + Scan { .. } + | Values { .. } + | PromqlVectorFromScalar(_) + | SetOp { .. } + | Concat { .. } + | BinaryOp { .. } + | TimeRange { .. } + | TimeShift { .. } + | PromqlInfoEnrich { .. } + | PromqlSubquery { .. } => {} + Filter { pred: p, .. } | Join { pred: p, .. } => *p = pred(p, column), + Project { cols, .. } => { + for item in cols { + item.expr = item.expr.map_columns(column); + } + } + Aggregate { + reduction, + measures, + filters, + .. + } => { + *reduction = reduction.map_columns(column); + for measure in measures { + *measure = measure.map_columns(column); + } + for filter in filters.iter_mut().flatten() { + *filter = pred(filter, column); + } + } + Dedup { cols, .. } => { + for col in cols { + *col = column(*col); + } + } + Sort { + keys: k, + partition_by, + .. + } => { + *k = keys(k, column); + *partition_by = partition_by.map_columns(column); + } + Limit { partition_by, .. } + | PromqlSeriesSample { + by: partition_by, .. + } => *partition_by = partition_by.map_columns(column), + SQLWindowFunc { + args, + partition_by, + order_by, + .. + } => { + for arg in args { + *arg = arg.map_columns(column); + } + *partition_by = partition_by.map_columns(column); + *order_by = keys(order_by, column); + } + PromqlRelabel { value, .. } => *value = value.map_columns(column), + } + op + } + /// The variant name, for diagnostics and export. pub(crate) fn kind_name(&self) -> &'static str { use NonASAPOp::*; diff --git a/crates/types/src/ir/schema/series_identity.rs b/crates/types/src/ir/schema/series_identity.rs index bd9b02b68..78c164b2c 100644 --- a/crates/types/src/ir/schema/series_identity.rs +++ b/crates/types/src/ir/schema/series_identity.rs @@ -15,6 +15,14 @@ use crate::ir::schema::*; /// implicitly match dynamic label sets require their own realization; they /// must not accidentally treat the opaque identity as a user label or /// silently discard it. +/// +/// Realization inserts the identity column into nodes whose parents were +/// bound before it, so every parent's column references are renumbered to +/// match: `topk(1, sum without (instance) (m))` binds its sort key to +/// `Column(0)` of `[sum]`; realization makes the aggregate's output +/// `[$promql_series_identity, sum]` and the key `Column(1)`. A reference to +/// a column realization does not keep fails here rather than read another +/// column. pub fn with_promql_series_identity( root: &std::rc::Rc, ) -> Result, String> { @@ -41,6 +49,33 @@ pub fn with_promql_series_identity( if let Some(error) = error { return Err(error); } + if let (Operator::NonASAP(before), Operator::NonASAP(after)) = (&node.operator, &operator) { + let positions = positions_after_realization( + &input_scope(&before.inputs()), + &input_scope(&after.inputs()), + ); + if positions + .iter() + .enumerate() + .any(|(column, position)| *position != Some(column)) + { + let mut dropped = None; + let renumbered = after.map_input_columns(&mut |column| { + positions.get(column).copied().flatten().unwrap_or_else(|| { + dropped = Some(column); + column + }) + }); + if let Some(column) = dropped { + return Err(format!( + "series identity realization does not keep input column {column} \ + that {} reads", + before.kind_name() + )); + } + operator = Operator::NonASAP(renumbered); + } + } match &mut operator { Operator::NonASAP(NonASAPOp::Scan { source: Source::TimeSeries { .. }, @@ -97,17 +132,153 @@ pub fn with_promql_series_identity( _ => return Err("operator has no dynamic series-identity realization".into()), } let rebuilt = Rc::new(OperatorNode::new(operator).map_err(|e| e.to_string())?); + // `having` indexes the aggregate's own output, which the + // renumbering above does not follow; PromQL emits none. + if matches!( + rebuilt.non_asap(), + Some(NonASAPOp::Aggregate { + having: Some(_), + .. + }) + ) && rebuilt.schema.fields.len() != node.schema.fields.len() + { + return Err("series identity realization moves the columns a HAVING reads".into()); + } cache.insert(Rc::as_ptr(node), Rc::clone(&rebuilt)); Ok(rebuilt) } visit(root, &mut HashMap::new()) } +/// The input scope of an operator over `inputs`: their output columns, in +/// order. +fn input_scope<'a>(inputs: &[&'a std::rc::Rc]) -> Vec<&'a Field> { + inputs + .iter() + .flat_map(|input| input.schema.fields.iter()) + .collect() +} + +/// Where each column of the input scope `before` realization sits in the +/// scope `after` it: the same columns in the same order, with +/// [`PROMQL_SERIES_IDENTITY`] columns inserted. `[sum]` before and +/// `[$promql_series_identity, sum]` after give `[Some(1)]`. `None` marks a +/// column `after` does not keep. +fn positions_after_realization(before: &[&Field], after: &[&Field]) -> Vec> { + let mut next = 0; + before + .iter() + .map(|field| { + while after + .get(next) + .is_some_and(|f| f.name != field.name && f.name == PROMQL_SERIES_IDENTITY) + { + next += 1; + } + let kept = after.get(next).is_some_and(|f| f.name == field.name); + kept.then(|| { + next += 1; + next - 1 + }) + }) + .collect() +} + #[cfg(test)] mod tests { + use std::rc::Rc; + use std::time::Duration; + use super::with_promql_series_identity; - use crate::ir::scalar::ScalarValue; - use crate::ir::{NonASAPOp, Operator, OperatorNode, ScalarExpr}; + use crate::ir::operator::arguments::{GroupKeys, Reduction, Source}; + use crate::ir::operator::AggIntent; + use crate::ir::scalar::{Predicate, ScalarValue, SortKey}; + use crate::ir::schema::{DataType, Field, Schema, PROMQL_SERIES_IDENTITY}; + use crate::ir::{NonASAPOp, Operator, OperatorNode, ProjectItem, ScalarExpr, TimeRangeKind}; + + fn node(operator: NonASAPOp) -> Rc { + OperatorNode::new_shared(Operator::NonASAP(operator)).unwrap() + } + + /// `sort_desc(sum without (instance) (m))` with a filter and a + /// projection above the aggregate, bound over its output `[sum]` before + /// realization: every reference to `sum`, `Column(0)`, reads + /// `Column(1)` of the realized `[$promql_series_identity, sum]`. + #[test] + fn realization_renumbers_parent_references_into_a_without_aggregate() { + let scan = node(NonASAPOp::Scan { + source: Source::TimeSeries { metric: "m".into() }, + predicates: vec![], + schema: Schema::with_time_index( + vec![ + Field::plain("ts", DataType::Timestamp, false), + Field::plain("value", DataType::Float64, false), + Field::plain("instance", DataType::Utf8, true), + ], + 0, + vec![], + ), + }); + let instant = node(NonASAPOp::TimeRange { + range: Duration::from_secs(300), + kind: TimeRangeKind::Instant, + child: scan, + }); + let sum = node(NonASAPOp::Aggregate { + reduction: Reduction::Reduce(GroupKeys::without(vec![2])), + measures: vec![AggIntent::Sum { col: None }], + output_names: vec![String::new()], + filters: vec![None], + having: None, + child: instant, + }); + let sum_column = |node: &OperatorNode| { + node.schema + .fields + .iter() + .position(|field| field.name == "sum") + .unwrap() + }; + assert_eq!(sum_column(&sum), 0); + let filtered = node(NonASAPOp::Filter { + pred: Predicate(ScalarExpr::IsNotNull(Box::new(ScalarExpr::Column(0)))), + child: sum, + }); + let projected = node(NonASAPOp::Project { + cols: vec![ProjectItem { + alias: Some("sum".into()), + expr: ScalarExpr::Column(0), + }], + child: Rc::clone(&filtered), + }); + let sorted = node(NonASAPOp::Sort { + keys: vec![SortKey { + expr: ScalarExpr::Column(0), + ascending: false, + nulls_first: false, + }], + partition_by: GroupKeys::none(), + child: filtered, + }); + let sorted = with_promql_series_identity(&sorted).unwrap(); + let Some(NonASAPOp::Sort { keys, child, .. }) = sorted.non_asap() else { + panic!("a sort"); + }; + assert_eq!(child.schema.fields[0].name, PROMQL_SERIES_IDENTITY); + assert_eq!(keys[0].expr, ScalarExpr::Column(sum_column(child))); + let Some(NonASAPOp::Filter { pred, child: sum }) = child.non_asap() else { + panic!("a filter"); + }; + assert_eq!( + pred.0, + ScalarExpr::IsNotNull(Box::new(ScalarExpr::Column(sum_column(sum)))) + ); + let projected = with_promql_series_identity(&projected).unwrap(); + let Some(NonASAPOp::Project { cols, .. }) = projected.non_asap() else { + panic!("a projection"); + }; + assert_eq!(cols[0].expr, ScalarExpr::Column(1)); + } // A direct scalar literal (`vector(1)`) stays a valid vector input when // series identity is realized. From 9ccabdee7107207cf3d60e06761ed62393156dc3 Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:33:50 +0000 Subject: [PATCH 7/8] fix: round-5 review fixes for reduce aggregates - One generic column walk per IR argument type, with no catch-all arm: AggIntent::try_map_cols (map_columns, input_cols and the frontend's resolve_agg_intent use it) and GroupKeys::try_map_keys. A new column-carrying intent now fails to compile instead of being skipped. - NonASAPOp::map_parts walks every variant once; map_children and map_input_columns are thin wrappers. NonASAPOp::input_scope sits next to inputs and serves realization and Join validation. - Series identity realization renumbers an Aggregate's HAVING by how its output moves, and returns a typed SeriesIdentityError. The planner falls back to the unrealized root only when realization does not apply (limitk, info()); a dropped column is OptimizeError::SeriesIdentity. - Nested aggregates over a without aggregate install: the executor resolves a summary update's sample value as the planner does (the one numeric column after an aggregate renamed it). - avg without two or more labels plans: Pass 1's avg casts the last column, whatever the number of kept labels. - A grouping key that a without aggregate below removed is absent, as in Prometheus; a closed schema that carries the series identity no longer proves a label absent (it may be in the identity). --- crates/executor/src/physical_planner/mod.rs | 29 +- .../src/physical_planner/promql_rows.rs | 3 +- crates/frontend-common/src/resolve.rs | 301 ++++++++++-------- crates/planner/src/pass.rs | 5 + crates/planner/src/run.rs | 77 ++++- crates/planner/tests/e2e_plan.rs | 32 ++ crates/planner/tests/reduce_aggregates.rs | 80 +++++ .../src/candidates/measure_output.rs | 26 +- crates/types/src/ir/operator/agg_intent.rs | 150 ++++++--- crates/types/src/ir/operator/arguments.rs | 22 +- crates/types/src/ir/operator/non_asap.rs | 228 ++++++------- crates/types/src/ir/scalar/mod.rs | 4 +- crates/types/src/ir/schema/series_identity.rs | 234 ++++++++++---- 13 files changed, 763 insertions(+), 428 deletions(-) diff --git a/crates/executor/src/physical_planner/mod.rs b/crates/executor/src/physical_planner/mod.rs index df4f77564..f61dd7d9f 100644 --- a/crates/executor/src/physical_planner/mod.rs +++ b/crates/executor/src/physical_planner/mod.rs @@ -1487,29 +1487,14 @@ fn summary_column(input: &SchemaRef) -> Result { _ => Err(invalid("one summary state column required")), } } +/// The column of `input` that `column` names, resolved as the planner +/// resolves it (`resolve_column_ref`): a name must be unique, and the +/// sample value is the `value` column or, after an aggregate renamed it, the +/// one numeric column. An outer `sum by (job)` over `sum without (instance) +/// (m)` updates on `sum` of `[job, $promql_series_identity, sum]`. fn named_column(input: &SchemaRef, column: &ColumnRef) -> Result { - let name = match column { - // This summary-update lookup matches column names without qualifiers. - // Reject ambiguous names rather than guessing a join side. - ColumnRef::Named(name) | ColumnRef::Qualified { name, .. } => name.as_str(), - ColumnRef::SampleValue => "value", - _ => { - return Err(invalid( - "summary update requires an unambiguous bound column", - )) - } - }; - let matches = input - .fields - .iter() - .enumerate() - .filter(|(_, field)| field.name == name) - .map(|(i, _)| i) - .collect::>(); - match matches.as_slice() { - [column] => Ok(*column), - _ => Err(invalid("summary update column missing or ambiguous")), - } + planner_types::ir::scalar::resolve_column_ref(column, input) + .map_err(|error| invalid(format!("summary update column: {error}"))) } /// The `Sort` an exact top-k lowers to: the first `k` rows of each /// `partitions` group by the column `value`, largest first, NULL and NaN diff --git a/crates/executor/src/physical_planner/promql_rows.rs b/crates/executor/src/physical_planner/promql_rows.rs index b5cf91207..ff01a7b18 100644 --- a/crates/executor/src/physical_planner/promql_rows.rs +++ b/crates/executor/src/physical_planner/promql_rows.rs @@ -109,7 +109,8 @@ pub fn series_identity_without(identity: &str, excluding: &[String]) -> Result) -> Result, Error> { - planner_types::ir::schema::series_identity::with_promql_series_identity(root).map_err(invalid) + planner_types::ir::schema::series_identity::with_promql_series_identity(root) + .map_err(|error| invalid(error.to_string())) } /// Construct source rows only from full identities. The named label columns diff --git a/crates/frontend-common/src/resolve.rs b/crates/frontend-common/src/resolve.rs index 8d400c361..0606e49fa 100644 --- a/crates/frontend-common/src/resolve.rs +++ b/crates/frontend-common/src/resolve.rs @@ -212,7 +212,7 @@ fn resolve(tree: &UnresolvedOp, usage_derived_schema: &Schema) -> Result Result { let child = resolve(child, usage_derived_schema)?; let scope = child.scope(); - let reduction = resolve_reduction(reduction, scope)?; + let reduction = resolve_reduction(reduction, &child)?; let measures = measures .iter() .map(|m| resolve_agg_intent(m, scope)) @@ -799,152 +799,132 @@ fn resolve_group_keys( keys: &GroupKeys, scope: Scope, ) -> Result, ResolveError> { - let ids = scope.resolve_all(keys.keys())?; - Ok(if keys.is_without() { - GroupKeys::without(ids) - } else { - GroupKeys::by(ids) - }) + keys.try_map_keys(&mut |key| scope.resolve(key)) } /// Resolve PromQL aggregation grouping keys with the language's absent-label -/// semantics (issue #53): a key not present in a **closed** schema is provably -/// absent from every row, so all rows carry its empty value, grouping by it is -/// the identity partition, and Prometheus omits the (empty) label from the -/// aggregation output — so the key is **dropped** rather than rejected. This -/// is what makes `sum(sum by (k) (m)) by (j)` lower: the inner cross-series -/// aggregate freezes the schema to the closed `[k, sum]`, which provably lacks -/// `j`. +/// semantics (issue #53): a key not present in the input is dropped rather +/// than rejected when it is provably absent from every row. +/// +/// - In a **closed** schema every row carries the key's empty value, so +/// grouping `by` it is the identity partition, and Prometheus omits the +/// (empty) label from the output. This is what makes `sum(sum by (k) (m)) +/// by (j)` lower: the inner cross-series aggregate freezes the schema to +/// the closed `[k, sum]`, which provably lacks `j`. A closed schema that +/// carries the series identity proves nothing: in `max without (instance) +/// ((10 * min without (zone) (m)) unless n)` the left side's realized +/// `[$promql_series_identity, min]` has no `instance` column, but its +/// identity has `instance`. +/// - A key that a `without` aggregate below already removed +/// (`removed_below`, see [`labels_removed_by_without`]) is absent from +/// every row, as in Prometheus. As a `without` key it removes nothing: +/// in `sum without (instance) (sum without (instance) (m))` the inner +/// output `[$promql_series_identity, sum]` has no `instance`, and the +/// outer grouping is `without ()`. As a `by` key it is the empty label: +/// `sum by (instance) (sum without (instance) (m))` is `sum by ()`. /// -/// Against an **open** schema an unresolved key is still an error: the label -/// may exist at runtime, and PromQL leaves seed every referenced label via the -/// schema resolver, so an unresolved key over an open schema indicates a resolution bug, -/// not an absent label. (SQL is unaffected — its `GROUP BY` resolves through -/// the strict [`resolve_group_keys`], and DataFusion has already validated -/// the columns anyway.) +/// Any other key unresolved over an open schema is still an error: the +/// label may exist at runtime, in the series identity, without a column of +/// its own (the output of `m and n` carries `m`'s labels in its identity +/// only), so it indicates a resolution gap, not an absent label. (SQL is +/// unaffected — its `GROUP BY` resolves through the strict +/// [`resolve_group_keys`], and DataFusion has already validated the columns +/// anyway.) fn resolve_group_keys_promql( - cols: &[ColumnRef], + keys: &GroupKeys, scope: Scope, -) -> Result, ResolveError> { - cols.iter() - .filter_map(|c| match scope.resolve(c) { - Err(ResolveError::NotFound { .. }) if scope.schema.closed => None, + removed_below: &[&str], +) -> Result, ResolveError> { + // A closed schema with a series identity column lists only the labels + // its side of the query named; any other label may be in the identity. + let provably_absent = scope.schema.closed + && !scope + .schema + .fields + .iter() + .any(|field| field.name == asap_types::ir::schema::PROMQL_SERIES_IDENTITY); + let removed = |key: &ColumnRef| match key { + ColumnRef::Named(name) | ColumnRef::Qualified { name, .. } => { + removed_below.contains(&name.as_str()) + } + ColumnRef::SampleValue | ColumnRef::Wildcard => false, + }; + let ids = keys + .keys() + .iter() + .filter_map(|key| match scope.resolve(key) { + Err(ResolveError::NotFound { .. }) if provably_absent || removed(key) => None, other => Some(other), }) - .collect() + .collect::>()?; + Ok(if keys.is_without() { + GroupKeys::without(ids) + } else { + GroupKeys::by(ids) + }) } -/// Resolve a name-based reduction. Uses [`resolve_group_keys_promql`] rather -/// than the strict [`resolve_group_keys`]: a key absent from a **closed** -/// schema (the output of a nested cross-series aggregate that collapsed the -/// label) is provably absent from every row, so PromQL drops it from the -/// grouping rather than rejecting the query (issue #53) — `sum(sum by (group) -/// (m)) by (job)`. SQL `GROUP BY` keys are always present, so the lenient -/// path is a no-op difference there. +/// The labels removed from every series of `input` by a `without` +/// aggregate: the ones it excludes, seen through the filters and +/// projections above it that keep every label. `sum without (instance) +/// (m)` and `sum without (instance) (m) > 1` remove `instance`; any other +/// operator, none. +fn labels_removed_by_without(input: &OperatorNode) -> Vec<&str> { + match input.non_asap() { + Some(NonASAPOp::Aggregate { + reduction: Reduction::Reduce(keys), + child, + .. + }) if keys.is_without() => keys + .keys() + .iter() + .filter_map(|&key| child.schema.fields.get(key)) + .map(|field| field.name.as_str()) + .collect(), + Some(NonASAPOp::Filter { child, .. }) => labels_removed_by_without(child), + // `10 * x` and `abs(x)` project every column of `x` (less + // `__name__`) under its name. + Some(NonASAPOp::Project { child, .. }) + if child.schema.fields.iter().all(|field| { + field.name == "__name__" + || input + .schema + .fields + .iter() + .any(|kept| kept.name == field.name) + }) => + { + labels_removed_by_without(child) + } + _ => Vec::new(), + } +} + +/// Resolve a name-based reduction over `input`. Uses +/// [`resolve_group_keys_promql`] rather than the strict +/// [`resolve_group_keys`]: a key provably absent from every row is dropped +/// rather than rejected — `sum(sum by (group) (m)) by (job)` (issue #53). +/// SQL `GROUP BY` keys are always present, so the lenient path is a no-op +/// difference there. fn resolve_reduction( reduction: &Reduction, - scope: Scope, + input: &Bound, ) -> Result, ResolveError> { Ok(match reduction { - Reduction::Reduce(by) => { - let ids = resolve_group_keys_promql(by.keys(), scope)?; - Reduction::Reduce(if by.is_without() { - GroupKeys::without(ids) - } else { - GroupKeys::by(ids) - }) - } + Reduction::Reduce(keys) => Reduction::Reduce(resolve_group_keys_promql( + keys, + input.scope(), + &labels_removed_by_without(&input.node), + )?), Reduction::PerEntity => Reduction::PerEntity, }) } -/// Resolve a name-based aggregate intent: every `col: Option` -/// resolves to `Option` (`None` stays `None`, the sample-value -/// convention); every other field carries through unchanged. fn resolve_agg_intent( intent: &AggIntent, scope: Scope, ) -> Result, ResolveError> { - let col = |c: &Option| -> Result, ResolveError> { - c.as_ref().map(|r| scope.resolve(r)).transpose() - }; - Ok(match intent { - AggIntent::Count => AggIntent::Count, - AggIntent::PearsonCorr { left, right } => AggIntent::PearsonCorr { - left: scope.resolve(left)?, - right: scope.resolve(right)?, - }, - AggIntent::Sum { col: c } => AggIntent::Sum { col: col(c)? }, - AggIntent::Min { col: c } => AggIntent::Min { col: col(c)? }, - AggIntent::Max { col: c } => AggIntent::Max { col: col(c)? }, - AggIntent::Avg { col: c } => AggIntent::Avg { col: col(c)? }, - AggIntent::StdDev { col: c, population } => AggIntent::StdDev { - col: col(c)?, - population: *population, - }, - AggIntent::Variance { col: c, population } => AggIntent::Variance { - col: col(c)?, - population: *population, - }, - AggIntent::Quantile { col: c, q } => AggIntent::Quantile { - col: col(c)?, - q: *q, - }, - AggIntent::TopK { k } => AggIntent::TopK { k: *k }, - AggIntent::Cardinality { cols } => AggIntent::Cardinality { - cols: scope.resolve_all(cols)?, - }, - AggIntent::FrequencyL2 { col: c } => AggIntent::FrequencyL2 { col: col(c)? }, - AggIntent::FrequencyEntropy { col: c } => AggIntent::FrequencyEntropy { col: col(c)? }, - AggIntent::Rate => AggIntent::Rate, - AggIntent::IRate => AggIntent::IRate, - AggIntent::Increase => AggIntent::Increase, - AggIntent::Changes => AggIntent::Changes, - AggIntent::Delta => AggIntent::Delta, - AggIntent::IDelta => AggIntent::IDelta, - AggIntent::Deriv => AggIntent::Deriv, - AggIntent::Resets => AggIntent::Resets, - AggIntent::PredictLinear { seconds } => AggIntent::PredictLinear { seconds: *seconds }, - AggIntent::DoubleExpSmoothing { smoothing, trend } => AggIntent::DoubleExpSmoothing { - smoothing: *smoothing, - trend: *trend, - }, - AggIntent::HistogramCount => AggIntent::HistogramCount, - AggIntent::HistogramSum => AggIntent::HistogramSum, - AggIntent::HistogramAvg => AggIntent::HistogramAvg, - AggIntent::HistogramStdDev => AggIntent::HistogramStdDev, - AggIntent::HistogramStdVar => AggIntent::HistogramStdVar, - AggIntent::HistogramFraction { lower, upper } => AggIntent::HistogramFraction { - lower: *lower, - upper: *upper, - }, - AggIntent::HistogramQuantile { q, le } => AggIntent::HistogramQuantile { - q: *q, - le: scope.resolve(le)?, - }, - AggIntent::Math(f) => AggIntent::Math(f.clone()), - AggIntent::Absent { labels } => AggIntent::Absent { - labels: labels.clone(), - }, - AggIntent::AbsentOverTime => AggIntent::AbsentOverTime, - AggIntent::PresentOverTime => AggIntent::PresentOverTime, - AggIntent::TimeFn(f) => AggIntent::TimeFn(*f), - AggIntent::Group => AggIntent::Group, - AggIntent::CountValues { label } => AggIntent::CountValues { - label: label.clone(), - }, - AggIntent::LastOverTime => AggIntent::LastOverTime, - AggIntent::FirstOverTime => AggIntent::FirstOverTime, - AggIntent::MadOverTime => AggIntent::MadOverTime, - AggIntent::TsOfMinOverTime => AggIntent::TsOfMinOverTime, - AggIntent::TsOfMaxOverTime => AggIntent::TsOfMaxOverTime, - AggIntent::TsOfFirstOverTime => AggIntent::TsOfFirstOverTime, - AggIntent::TsOfLastOverTime => AggIntent::TsOfLastOverTime, - AggIntent::Extension { ext_kind, payload } => AggIntent::Extension { - ext_kind: ext_kind.clone(), - payload: payload.clone(), - }, - }) + intent.try_map_cols(&mut |column| scope.resolve(column)) } /// Resolve a standalone scalar in an empty column scope; plan reads retain their own scope. @@ -1458,32 +1438,77 @@ mod tests { unique_keys: vec![], closed: true, }; + let job = || ColumnRef::Named("job".into()); assert_eq!( - resolve_group_keys_promql(&[ColumnRef::Named("job".into())], Scope::unqualified(&s)), - Ok(vec![]) + resolve_group_keys_promql(&GroupKeys::by(vec![job()]), Scope::unqualified(&s), &[]), + Ok(GroupKeys::by(vec![])) ); // Present keys still resolve positionally; absent ones drop around them. assert_eq!( resolve_group_keys_promql( - &[ - ColumnRef::Named("job".into()), - ColumnRef::Named("group".into()) - ], - Scope::unqualified(&s) + &GroupKeys::by(vec![job(), ColumnRef::Named("group".into())]), + Scope::unqualified(&s), + &[] ), - Ok(vec![0]) + Ok(GroupKeys::by(vec![0])) ); } + #[test] + fn group_keys_promql_keeps_an_absent_key_of_a_closed_identity_schema() { + // A realized side of a binary operator: closed, but its labels live in + // the identity, so `instance` may be there. Not dropped. + let s = Schema { + fields: vec![ + Field::plain( + asap_types::ir::schema::PROMQL_SERIES_IDENTITY, + DataType::Utf8, + false, + ), + Field::plain("min", DataType::Float64, false), + ], + time_index: None, + unique_keys: vec![], + closed: true, + }; + let instance = || ColumnRef::Named("instance".into()); + for keys in [ + GroupKeys::by(vec![instance()]), + GroupKeys::without(vec![instance()]), + ] { + assert!(matches!( + resolve_group_keys_promql(&keys, Scope::unqualified(&s), &[]), + Err(ResolveError::NotFound { .. }) + )); + } + } + #[test] fn group_keys_promql_still_errors_on_open_schema() { - // An open schema can't prove absence — an unresolved key there is a - // resolution bug (the schema resolver seeds every referenced label), not an - // absent label. Keep the strict error. + // An open schema can't prove a `by` key absent — an unresolved key there + // is a resolution bug (the schema resolver seeds every referenced label), + // not an absent label. Keep the strict error. let open = ts_value_schema(); // closed: false + let job = || ColumnRef::Named("job".into()); + let open_scope = || Scope::unqualified(&open); assert!(matches!( - resolve_group_keys_promql(&[ColumnRef::Named("job".into())], Scope::unqualified(&open)), + resolve_group_keys_promql(&GroupKeys::by(vec![job()]), open_scope(), &[]), Err(ResolveError::NotFound { .. }) )); + // A `by` key a `without` below removed groups by the empty label. + assert_eq!( + resolve_group_keys_promql(&GroupKeys::by(vec![job()]), open_scope(), &["job"]), + Ok(GroupKeys::by(vec![])) + ); + // A `without` key may still be in the series identity, unless the + // input `without` aggregate removed it: then it removes nothing. + assert!(matches!( + resolve_group_keys_promql(&GroupKeys::without(vec![job()]), open_scope(), &[]), + Err(ResolveError::NotFound { .. }) + )); + assert_eq!( + resolve_group_keys_promql(&GroupKeys::without(vec![job()]), open_scope(), &["job"]), + Ok(GroupKeys::without(vec![])) + ); } } diff --git a/crates/planner/src/pass.rs b/crates/planner/src/pass.rs index 2bc70b697..0c56f9c38 100644 --- a/crates/planner/src/pass.rs +++ b/crates/planner/src/pass.rs @@ -14,6 +14,7 @@ use std::collections::BTreeMap; +use asap_types::ir::schema::series_identity::SeriesIdentityError; use asap_types::workload::parsed_workload::ParsedWorkload; use asap_types::workload::WorkloadError; @@ -77,6 +78,10 @@ pub enum OptimizeError { PerComputationCandidates(#[from] PerComputationCandidateError), #[error("plan selection: {0}")] Selection(#[from] SelectionError), + /// Series identity realization would make an operator read a column it + /// does not keep: a defect in the realization, not in the query. + #[error("PromQL series identity realization: {0}")] + SeriesIdentity(SeriesIdentityError), /// The pass returned something the downstream contract forbids. This is a /// defect in the pass, not in its input. #[error("pass `{pass}` violated the output contract: {detail}")] diff --git a/crates/planner/src/run.rs b/crates/planner/src/run.rs index c18adcca6..b41fce908 100644 --- a/crates/planner/src/run.rs +++ b/crates/planner/src/run.rs @@ -13,8 +13,8 @@ use asap_stage1_pass2_reuse_candidates::generate_logical_asap_memo_candidates; use asap_stage2_physical_optimization::generate_physical_asap_memo_candidates; use asap_stage3_plan_selection::{select_plan, ErrorSelectingPlan, Selection}; use asap_types::deployment_capabilities::DeploymentCapabilitiesError; -use asap_types::ir::schema::series_identity::with_promql_series_identity; -use asap_types::ir::QueryRoot; +use asap_types::ir::schema::series_identity::{with_promql_series_identity, SeriesIdentityError}; +use asap_types::ir::{OperatorNode, QueryRoot}; use asap_types::plan::physical_asap_dag::PhysicalASAPDAG; use asap_types::stages::{LogicalASAPMemoCandidates, Memo, PhysicalASAPMemoCandidates}; use asap_types::workload::{DataWorkload, QueryLanguage, QueryWorkloadEntry}; @@ -192,18 +192,18 @@ impl OptimizationPass for Stage1ToStage3Pass { .zip(workload.exprs()) .map(|(&index, node)| { let node = match promql { - true => with_promql_series_identity(node).unwrap_or_else(|_| Rc::clone(node)), + true => promql_root(node, with_promql_series_identity(node))?, false => Rc::clone(node), }; - (index, QueryRoot::Operator(node)) + Ok((index, QueryRoot::Operator(node))) }) - .chain( - workload - .scalar_roots() - .iter() - .map(|(index, expr)| (*index, QueryRoot::Scalar(expr.clone()))), - ) - .collect(); + .collect::>()?; + roots.extend( + workload + .scalar_roots() + .iter() + .map(|(index, expr)| (*index, QueryRoot::Scalar(expr.clone()))), + ); roots.sort_by_key(|(index, _)| *index); let roots = roots.into_iter().map(|(_, root)| root).collect(); let entries: Vec<_> = workload.query_workload().entries().collect(); @@ -220,3 +220,58 @@ impl OptimizationPass for Stage1ToStage3Pass { Ok(output) } } + +/// The PromQL root `node` planned: `realized`, its series identity +/// realization (`with_promql_series_identity`), or `node` itself, over its +/// label columns, when the realization does not apply to one of its +/// operators (`limitk(2, m)`'s `PromqlSeriesSample`, `info(m)`'s +/// `PromqlInfoEnrich`). A realization that drops a column an operator +/// reads is a planning error: falling back would hide it. +fn promql_root( + node: &Rc, + realized: Result, SeriesIdentityError>, +) -> Result, OptimizeError> { + match realized { + Ok(realized) => Ok(realized), + Err(SeriesIdentityError::NotApplicable(_)) => Ok(Rc::clone(node)), + Err(error) => Err(OptimizeError::SeriesIdentity(error)), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use asap_types::ir::scalar::ScalarExpr; + use asap_types::ir::{NonASAPOp, Operator}; + + fn vector_one() -> Rc { + OperatorNode::new_shared(Operator::NonASAP(NonASAPOp::PromqlVectorFromScalar( + ScalarExpr::Literal(asap_types::ir::scalar::ScalarValue::Float64(1.0)), + ))) + .unwrap() + } + + /// A realization that does not apply plans the root as it is. + #[test] + fn a_promql_root_realization_does_not_apply_to_is_planned_as_is() { + let node = vector_one(); + let realized = Err(SeriesIdentityError::NotApplicable( + "PromqlSeriesSample has no dynamic series-identity realization".into(), + )); + assert!(Rc::ptr_eq(&promql_root(&node, realized).unwrap(), &node)); + } + + /// A realization that drops a column an operator reads fails planning + /// rather than plan the unrealized root. + #[test] + fn a_realization_that_drops_a_read_column_fails_planning() { + let error = SeriesIdentityError::DroppedColumn { + operator: "Sort", + column: 1, + }; + assert!(matches!( + promql_root(&vector_one(), Err(error.clone())), + Err(OptimizeError::SeriesIdentity(dropped)) if dropped == error + )); + } +} diff --git a/crates/planner/tests/e2e_plan.rs b/crates/planner/tests/e2e_plan.rs index 0cc51af1b..2fbddff15 100644 --- a/crates/planner/tests/e2e_plan.rs +++ b/crates/planner/tests/e2e_plan.rs @@ -370,3 +370,35 @@ async fn scalar_roots_survive_planning_in_workload_order() { } } } + +/// A PromQL root holding an operator series identity realization does not +/// cover is planned over its label columns: `limitk(2, up)` lowers to a +/// `PromqlSeriesSample` and `info(up)` to a `PromqlInfoEnrich`, and each +/// plans with one root. +#[tokio::test] +async fn plans_a_promql_root_series_identity_does_not_cover_over_its_labels() { + for query in ["limitk(2, up)", "info(up)"] { + let workload = PlanningWorkload { + query_workload: QueryWorkload { + language: QueryLanguage::PromQL, + query_batch: Some(vec![batch(query)]), + repeating_queries: None, + }, + data_workload: Some( + DataWorkload::sampled_every( + asap_frontend_promql::promql_metric_names(query).unwrap(), + DurationMs(1000), + ) + .with_series_count(1_000), + ), + }; + let output = e2e_plan(UserInput::new( + &workload, + FrontendInput::Promql, + PlanningModels::builtin(), + )) + .await + .unwrap_or_else(|error| panic!("{query}: {error}")); + assert_eq!(output.plan.roots.len(), 1, "{query}"); + } +} diff --git a/crates/planner/tests/reduce_aggregates.rs b/crates/planner/tests/reduce_aggregates.rs index b55eb66b2..8ca2683c1 100644 --- a/crates/planner/tests/reduce_aggregates.rs +++ b/crates/planner/tests/reduce_aggregates.rs @@ -463,6 +463,86 @@ async fn promql_stddev_without_instance() { ); } +/// An aggregate over a `without` aggregate reads its value: `sum without +/// (instance) (m)` answers `{job="a"}` 7, `{job="b"}` 2 and `{job="b", +/// zone="z"}` 2.5, so summing that by `job` gives job `a` 7 and job `b` +/// 4.5, as `sum by (job) (m)` does. +#[tokio::test] +async fn promql_aggregate_over_sum_without() { + let samples = gauge_samples(); + let answers = promql("sum by (job) (sum without (instance) (m))", "m", &samples).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b")]), 4.5), + ], + ); + let answers = promql( + "max without (zone) (sum without (instance) (m))", + "m", + &samples, + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b")]), 2.5), + ], + ); +} + +/// `avg without` two labels averages each group of the labels left: job +/// `a`'s 1, 2 and 4 average 7/3, job `b`'s 2 and 2.5 average 2.25. +#[tokio::test] +async fn promql_avg_without_two_labels() { + let answers = promql("avg without (instance, zone) (m)", "m", &gauge_samples()).await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 7.0 / 3.0), + (labels(&[("job", "b")]), 2.25), + ], + ); +} + +/// A label an inner `without` removed is absent, as in Prometheus: the +/// outer `without (instance)` has no `instance` left to drop, so the query +/// answers as `sum without (instance) (m)`; `by (instance, job)` groups +/// by `job` alone (job `b`: 2 + 2.5). +#[tokio::test] +async fn promql_without_an_absent_label_is_a_no_op() { + let answers = promql( + "sum without (instance) (sum without (instance) (m))", + "m", + &gauge_samples(), + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b")]), 2.0), + (labels(&[("job", "b"), ("zone", "z")]), 2.5), + ], + ); + // As a `by` key the removed label is the empty label. + let answers = promql( + "sum by (instance, job) (sum without (instance) (m) > 1)", + "m", + &gauge_samples(), + ) + .await; + assert_close( + &answers, + &[ + (labels(&[("job", "a")]), 7.0), + (labels(&[("job", "b")]), 4.5), + ], + ); +} + /// `topk`, `bottomk`, `sort` and `sort_desc` over a `without` aggregate /// rank by the aggregate's value, not by its series identity: `sum without /// (instance) (m)` answers `{job="a"}` 7, `{job="b"}` 2 and `{job="b", diff --git a/crates/stage1-pass1-per-computation-candidates/src/candidates/measure_output.rs b/crates/stage1-pass1-per-computation-candidates/src/candidates/measure_output.rs index 34358eb1a..963d1791c 100644 --- a/crates/stage1-pass1-per-computation-candidates/src/candidates/measure_output.rs +++ b/crates/stage1-pass1-per-computation-candidates/src/candidates/measure_output.rs @@ -123,9 +123,9 @@ pub(super) fn measure_output( // so its value is the sample-value column; a per-series count is // Float64. let (sum, count) = match measure.reduction { - Reduction::Reduce(by) => ( - last_as_float64(memo, pending, by.len(), sum)?, - last_as_float64(memo, pending, by.len(), count)?, + Reduction::Reduce(_) => ( + last_as_float64(memo, pending, sum)?, + last_as_float64(memo, pending, count)?, ), Reduction::PerEntity => { let sum_schema = pending.schema(memo, sum); @@ -356,24 +356,22 @@ fn ranked_as_topk( })) } -/// `group` (a memo group or pending), a grouped aggregate's `keys` group -/// keys and then its value, with the value cast to Float64 unless it -/// already is one (a pending `Project` over it). +/// `group` (a memo group or pending), a grouped aggregate's label columns +/// and then its value, with the value cast to Float64 unless it already is +/// one (a pending `Project` over it). fn last_as_float64( memo: &Memo, pending: &mut Pending, - keys: usize, group: MemoGroupId, ) -> Result { - if pending - .schema(memo, group) - .fields - .last() - .map(|field| &field.dtype) - == Some(&FieldDataType::Plain(DataType::Float64)) - { + let fields = &pending.schema(memo, group).fields; + if fields.last().map(|field| &field.dtype) == Some(&FieldDataType::Plain(DataType::Float64)) { return Ok(group); } + // The columns before the value: `by`'s keys, or the labels a `without` + // keeps (`avg without (instance, zone)` over `[.., zone, instance, + // $promql_series_identity]` keeps only the identity). + let keys = fields.len() - 1; let mut cols = key_items(keys); cols.push(ProjectItem { alias: None, diff --git a/crates/types/src/ir/operator/agg_intent.rs b/crates/types/src/ir/operator/agg_intent.rs index e814876f4..2a432b764 100644 --- a/crates/types/src/ir/operator/agg_intent.rs +++ b/crates/types/src/ir/operator/agg_intent.rs @@ -372,40 +372,16 @@ pub enum MathFunc { // `asap_frontend_promql::promql::reduction_for`) — so they stay generic // alongside `input_col`, in one `impl` block. impl AggIntent { - /// This intent with every input column renumbered by `column`: the - /// value columns of [`input_cols`](AggIntent::input_cols) and + /// This intent with every column renumbered by `column`: the value + /// columns of [`input_cols`](AggIntent::input_cols) and /// `histogram_quantile`'s `le`. `Sum { col: Some(1) }` under a renumbering /// that moves column 1 to 2 is `Sum { col: Some(2) }`. An `Extension` /// names its columns, so it is unchanged. pub(crate) fn map_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self { - let mut intent = self.clone(); - match &mut intent { - AggIntent::Sum { col } - | AggIntent::Min { col } - | AggIntent::Max { col } - | AggIntent::Avg { col } - | AggIntent::Quantile { col, .. } - | AggIntent::FrequencyL2 { col } - | AggIntent::FrequencyEntropy { col } - | AggIntent::StdDev { col, .. } - | AggIntent::Variance { col, .. } => { - if let Some(col) = col { - *col = column(*col); - } - } - AggIntent::Cardinality { cols } => { - for col in cols { - *col = column(*col); - } - } - AggIntent::PearsonCorr { left, right } => { - *left = column(*left); - *right = column(*right); - } - AggIntent::HistogramQuantile { le, .. } => *le = column(*le), - _ => {} - } - intent + let mapped: Result<_, std::convert::Infallible> = + self.try_map_cols(&mut |&c| Ok(column(c))); + let Ok(mapped) = mapped; + mapped } /// Resolve the existing SQL arg-selector extension using its child schema. @@ -495,20 +471,110 @@ impl AggIntent { /// is built over from it. pub fn input_cols(&self) -> Vec { match self { - AggIntent::Sum { col } - | AggIntent::Min { col } - | AggIntent::Max { col } - | AggIntent::Avg { col } - | AggIntent::Quantile { col, .. } - | AggIntent::FrequencyL2 { col, .. } - | AggIntent::FrequencyEntropy { col, .. } - | AggIntent::StdDev { col, .. } - | AggIntent::Variance { col, .. } => col.clone().into_iter().collect(), - AggIntent::Cardinality { cols, .. } => cols.clone(), - AggIntent::PearsonCorr { left, right } => vec![left.clone(), right.clone()], - _ => vec![], + // `le` is the bucket bound each value is ranked by, not a value. + AggIntent::HistogramQuantile { .. } => vec![], + _ => { + let mut cols = Vec::new(); + let walked: Result<_, std::convert::Infallible> = self.try_map_cols(&mut |col| { + cols.push(col.clone()); + Ok(()) + }); + let Ok(_) = walked; + cols + } } } + + /// This intent with every column it holds mapped by `f`, in argument + /// order; the first error stops the walk. Every variant is listed, so a + /// new variant that holds a column must be mapped here: a front end + /// resolves `Sum { col: Some("value") }` to `Sum { col: Some(1) }` with + /// it, and series identity realization renumbers `Some(1)` to + /// `Some(2)` when it inserts a column before it. + pub fn try_map_cols( + &self, + f: &mut impl FnMut(&C) -> Result, + ) -> Result, E> { + let mut col = |c: &Option| c.as_ref().map(&mut *f).transpose(); + Ok(match self { + AggIntent::Count => AggIntent::Count, + AggIntent::Sum { col: c } => AggIntent::Sum { col: col(c)? }, + AggIntent::Min { col: c } => AggIntent::Min { col: col(c)? }, + AggIntent::Max { col: c } => AggIntent::Max { col: col(c)? }, + AggIntent::Avg { col: c } => AggIntent::Avg { col: col(c)? }, + AggIntent::StdDev { col: c, population } => AggIntent::StdDev { + col: col(c)?, + population: *population, + }, + AggIntent::Variance { col: c, population } => AggIntent::Variance { + col: col(c)?, + population: *population, + }, + AggIntent::Quantile { col: c, q } => AggIntent::Quantile { + col: col(c)?, + q: *q, + }, + AggIntent::FrequencyL2 { col: c } => AggIntent::FrequencyL2 { col: col(c)? }, + AggIntent::FrequencyEntropy { col: c } => AggIntent::FrequencyEntropy { col: col(c)? }, + AggIntent::PearsonCorr { left, right } => AggIntent::PearsonCorr { + left: f(left)?, + right: f(right)?, + }, + AggIntent::Cardinality { cols } => AggIntent::Cardinality { + cols: cols.iter().map(&mut *f).collect::>()?, + }, + AggIntent::HistogramQuantile { q, le } => { + AggIntent::HistogramQuantile { q: *q, le: f(le)? } + } + AggIntent::TopK { k } => AggIntent::TopK { k: *k }, + AggIntent::Rate => AggIntent::Rate, + AggIntent::IRate => AggIntent::IRate, + AggIntent::Increase => AggIntent::Increase, + AggIntent::Changes => AggIntent::Changes, + AggIntent::Delta => AggIntent::Delta, + AggIntent::IDelta => AggIntent::IDelta, + AggIntent::Deriv => AggIntent::Deriv, + AggIntent::Resets => AggIntent::Resets, + AggIntent::PredictLinear { seconds } => AggIntent::PredictLinear { seconds: *seconds }, + AggIntent::DoubleExpSmoothing { smoothing, trend } => AggIntent::DoubleExpSmoothing { + smoothing: *smoothing, + trend: *trend, + }, + AggIntent::HistogramCount => AggIntent::HistogramCount, + AggIntent::HistogramSum => AggIntent::HistogramSum, + AggIntent::HistogramAvg => AggIntent::HistogramAvg, + AggIntent::HistogramStdDev => AggIntent::HistogramStdDev, + AggIntent::HistogramStdVar => AggIntent::HistogramStdVar, + AggIntent::HistogramFraction { lower, upper } => AggIntent::HistogramFraction { + lower: *lower, + upper: *upper, + }, + AggIntent::Math(func) => AggIntent::Math(func.clone()), + AggIntent::Absent { labels } => AggIntent::Absent { + labels: labels.clone(), + }, + AggIntent::AbsentOverTime => AggIntent::AbsentOverTime, + AggIntent::PresentOverTime => AggIntent::PresentOverTime, + AggIntent::TimeFn(func) => AggIntent::TimeFn(*func), + AggIntent::Group => AggIntent::Group, + AggIntent::CountValues { label } => AggIntent::CountValues { + label: label.clone(), + }, + AggIntent::LastOverTime => AggIntent::LastOverTime, + AggIntent::FirstOverTime => AggIntent::FirstOverTime, + AggIntent::MadOverTime => AggIntent::MadOverTime, + AggIntent::TsOfMinOverTime => AggIntent::TsOfMinOverTime, + AggIntent::TsOfMaxOverTime => AggIntent::TsOfMaxOverTime, + AggIntent::TsOfFirstOverTime => AggIntent::TsOfFirstOverTime, + AggIntent::TsOfLastOverTime => AggIntent::TsOfLastOverTime, + // An extension names its columns in its payload, resolved + // against the child schema (`arg_selector_columns`). + AggIntent::Extension { ext_kind, payload } => AggIntent::Extension { + ext_kind: ext_kind.clone(), + payload: payload.clone(), + }, + }) + } } impl AggIntent { diff --git a/crates/types/src/ir/operator/arguments.rs b/crates/types/src/ir/operator/arguments.rs index 78c8ee556..e574f1d9a 100644 --- a/crates/types/src/ir/operator/arguments.rs +++ b/crates/types/src/ir/operator/arguments.rs @@ -89,6 +89,18 @@ impl GroupKeys { pub fn keys(&self) -> &[C] { &self.keys } + /// These keys with every key mapped by `f`, `by` or `without` kept; the + /// first error stops the walk. A front end resolves `by ("job")` to + /// `by (2)` with it. + pub fn try_map_keys( + &self, + f: &mut impl FnMut(&C) -> Result, + ) -> Result, E> { + Ok(GroupKeys { + keys: self.keys.iter().map(f).collect::>()?, + without: self.without, + }) + } } impl GroupKeys { @@ -96,10 +108,10 @@ impl GroupKeys { /// `without` kept: `by (1)` under a renumbering that moves column 1 to 2 /// is `by (2)`. pub(crate) fn map_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self { - Self { - keys: self.keys.iter().map(|&key| column(key)).collect(), - without: self.without, - } + let mapped: Result<_, std::convert::Infallible> = + self.try_map_keys(&mut |&key| Ok(column(key))); + let Ok(mapped) = mapped; + mapped } } @@ -510,9 +522,7 @@ impl Reduction { .collect::>()?; Some(Reduction::by(keys)) } -} -impl Reduction { /// This reduction with every grouping key renumbered by `column`; /// `PerEntity` is unchanged. pub(crate) fn map_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self { diff --git a/crates/types/src/ir/operator/non_asap.rs b/crates/types/src/ir/operator/non_asap.rs index 2d7f68f9b..847ff8bfd 100644 --- a/crates/types/src/ir/operator/non_asap.rs +++ b/crates/types/src/ir/operator/non_asap.rs @@ -196,9 +196,8 @@ impl NonASAPOp { } /// The direct operator inputs, in field order: a `Join`'s left then - /// right. Their output columns, in this order, are the operator's input - /// scope, which its column references index (see - /// [`NonASAPOp::map_input_columns`]). + /// right. Their output columns, in this order, are the operator's + /// [`input_scope`](NonASAPOp::input_scope). pub(crate) fn inputs(&self) -> Vec<&C> { use NonASAPOp::*; match self { @@ -222,6 +221,21 @@ impl NonASAPOp { } } + /// The operator's input scope: the output columns of its direct + /// [`inputs`](NonASAPOp::inputs), in order, which its column references + /// index. A `Join` of `[a, b]` and `[c]` reads `c` as `Column(2)`; + /// [`map_input_columns`](NonASAPOp::map_input_columns) renumbers these + /// references. + pub(crate) fn input_scope(&self) -> Vec<&Field> + where + C: OperatorInput, + { + self.inputs() + .into_iter() + .flat_map(|input| input.schema().fields.iter()) + .collect() + } + /// Every scalar expression this operator owns. pub fn scalar_exprs(&self) -> Vec<&ScalarExpr> { use NonASAPOp::*; @@ -259,22 +273,65 @@ impl NonASAPOp { } /// Rebuild this operator with `f` applied to every child, including the - /// operator nodes referenced from scalar expressions, in - /// [`Self::children`] order (direct inputs first), so a caller may hand - /// out replacements by position. Every other field is cloned. `f` may - /// change the reference type, e.g. from `Rc` to a node id. - /// - /// Example: `Project { child: q, cols: [q.value + scalar(c)] }` calls `f` - /// on `q`, then on `c`. + /// operator nodes referenced from scalar expressions, in the order + /// [`map_parts`](NonASAPOp::map_parts) visits them. Every other field is + /// cloned. `f` may change the reference type, e.g. from + /// `Rc` to a node id. pub(crate) fn map_children(&self, mut f: impl FnMut(&C) -> D) -> NonASAPOp { + self.map_parts(&mut |column| column, &mut f) + } + + /// This operator with every column reference into its input scope + /// renumbered by `column`: the columns of its direct inputs, in + /// [`inputs`](NonASAPOp::inputs) order. A `Sort` by `Column(0)` over + /// `[sum]` whose input becomes `[$promql_series_identity, sum]` must sort + /// by `Column(1)`; the renumbering `0 → 1` gives that. Two references are + /// in another scope and kept: a `Scan`'s predicates index its own + /// schema, and an `Aggregate`'s `having` indexes its output. Operator + /// nodes read from scalar expressions keep their own scope. + pub(crate) fn map_input_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self + where + C: Clone, + { + self.map_parts(column, &mut C::clone) + } + + /// One walk over every field of this operator: each column reference + /// into its input scope renumbered by `column` (see + /// [`map_input_columns`](NonASAPOp::map_input_columns)), each child and + /// each operator node read from a scalar expression mapped by `f`, and + /// every other field cloned. A `Scan`'s predicates, an `Aggregate`'s + /// `having` and the expressions of an operator without inputs index + /// another scope, so their columns are kept. + /// + /// `f` runs in [`Self::children`] order (direct inputs first), so a + /// caller may hand out replacements by position. Example: + /// `Project { child: q, cols: [q.value + scalar(c)] }` calls `f` on `q`, + /// then on `c`. + fn map_parts( + &self, + column: &mut impl FnMut(ColumnId) -> ColumnId, + f: &mut impl FnMut(&C) -> D, + ) -> NonASAPOp { use NonASAPOp::*; - fn map_pred(p: &Predicate, f: &mut impl FnMut(&C) -> D) -> Predicate { - Predicate(p.0.map_operator_refs(f)) + fn kept(column: ColumnId) -> ColumnId { + column } - fn map_keys(keys: &[SortKey], f: &mut impl FnMut(&C) -> D) -> Vec> { + fn map_pred( + p: &Predicate, + column: &mut impl FnMut(ColumnId) -> ColumnId, + f: &mut impl FnMut(&C) -> D, + ) -> Predicate { + Predicate(p.0.map_parts(column, f)) + } + fn map_keys( + keys: &[SortKey], + column: &mut impl FnMut(ColumnId) -> ColumnId, + f: &mut impl FnMut(&C) -> D, + ) -> Vec> { keys.iter() .map(|k| SortKey { - expr: k.expr.map_operator_refs(f), + expr: k.expr.map_parts(column, f), ascending: k.ascending, nulls_first: k.nulls_first, }) @@ -287,21 +344,24 @@ impl NonASAPOp { schema, } => Scan { source: source.clone(), - predicates: predicates.iter().map(|p| map_pred(p, &mut f)).collect(), + predicates: predicates + .iter() + .map(|p| map_pred(p, &mut kept, f)) + .collect(), schema: schema.clone(), }, Values { rows, schema } => Values { rows: rows .iter() - .map(|row| row.iter().map(|e| e.map_operator_refs(&mut f)).collect()) + .map(|row| row.iter().map(|e| e.map_parts(&mut kept, f)).collect()) .collect(), schema: schema.clone(), }, - PromqlVectorFromScalar(e) => PromqlVectorFromScalar(e.map_operator_refs(&mut f)), + PromqlVectorFromScalar(e) => PromqlVectorFromScalar(e.map_parts(&mut kept, f)), Filter { pred, child } => { let child = f(child); Filter { - pred: map_pred(pred, &mut f), + pred: map_pred(pred, column, f), child, } } @@ -311,7 +371,7 @@ impl NonASAPOp { .iter() .map(|c| ProjectItem { alias: c.alias.clone(), - expr: c.expr.map_operator_refs(&mut f), + expr: c.expr.map_parts(column, f), }) .collect(); Project { cols, child } @@ -327,12 +387,12 @@ impl NonASAPOp { let child = f(child); let filters = filters .iter() - .map(|p| p.as_ref().map(|p| map_pred(p, &mut f))) + .map(|p| p.as_ref().map(|p| map_pred(p, column, f))) .collect(); - let having = having.as_ref().map(|p| map_pred(p, &mut f)); + let having = having.as_ref().map(|p| map_pred(p, &mut kept, f)); Aggregate { - reduction: reduction.clone(), - measures: measures.clone(), + reduction: reduction.map_columns(column), + measures: measures.iter().map(|m| m.map_columns(column)).collect(), output_names: output_names.clone(), filters, having, @@ -348,7 +408,7 @@ impl NonASAPOp { let (left, right) = (f(left), f(right)); Join { kind: kind.clone(), - pred: map_pred(pred, &mut f), + pred: map_pred(pred, column, f), left, right, } @@ -365,10 +425,10 @@ impl NonASAPOp { right: f(right), }, Concat { children } => Concat { - children: children.iter().map(&mut f).collect(), + children: children.iter().map(f).collect(), }, Dedup { cols, child } => Dedup { - cols: cols.clone(), + cols: cols.iter().map(|&c| column(c)).collect(), child: f(child), }, Sort { @@ -378,8 +438,8 @@ impl NonASAPOp { } => { let child = f(child); Sort { - keys: map_keys(keys, &mut f), - partition_by: partition_by.clone(), + keys: map_keys(keys, column, f), + partition_by: partition_by.map_columns(column), child, } } @@ -391,7 +451,7 @@ impl NonASAPOp { } => Limit { n: *n, offset: *offset, - partition_by: partition_by.clone(), + partition_by: partition_by.map_columns(column), child: f(child), }, BinaryOp { @@ -415,12 +475,12 @@ impl NonASAPOp { child, } => { let child = f(child); - let args = args.iter().map(|e| e.map_operator_refs(&mut f)).collect(); - let order_by = map_keys(order_by, &mut f); + let args = args.iter().map(|e| e.map_parts(column, f)).collect(); + let order_by = map_keys(order_by, column, f); SQLWindowFunc { func: func.clone(), args, - partition_by: partition_by.clone(), + partition_by: partition_by.map_columns(column), order_by, frame: frame.clone(), output_name: output_name.clone(), @@ -440,7 +500,7 @@ impl NonASAPOp { let child = f(child); PromqlRelabel { dst: dst.clone(), - value: value.map_operator_refs(&mut f), + value: value.map_parts(column, f), child, } } @@ -449,7 +509,7 @@ impl NonASAPOp { child: f(child), }, PromqlSeriesSample { by, kind, child } => PromqlSeriesSample { - by: by.clone(), + by: by.map_columns(column), kind: *kind, child: f(child), }, @@ -465,97 +525,6 @@ impl NonASAPOp { } } - /// This operator with every column reference into its input scope - /// renumbered by `column`: the columns of its direct inputs, in - /// [`inputs`](NonASAPOp::inputs) order. A `Sort` by `Column(0)` over - /// `[sum]` whose input becomes `[$promql_series_identity, sum]` must sort - /// by `Column(1)`; the renumbering `0 → 1` gives that. Two references are - /// in another scope and kept: a `Scan`'s predicates index its own - /// schema, and an `Aggregate`'s `having` indexes its output. Operator - /// nodes read from scalar expressions keep their own scope. - pub(crate) fn map_input_columns(&self, column: &mut impl FnMut(ColumnId) -> ColumnId) -> Self - where - C: Clone, - { - use NonASAPOp::*; - let pred = |p: &Predicate, column: &mut dyn FnMut(ColumnId) -> ColumnId| { - Predicate(p.0.map_columns(&mut |c| column(c))) - }; - let keys = |keys: &[SortKey], column: &mut dyn FnMut(ColumnId) -> ColumnId| { - keys.iter() - .map(|k| SortKey { - expr: k.expr.map_columns(&mut |c| column(c)), - ascending: k.ascending, - nulls_first: k.nulls_first, - }) - .collect() - }; - let mut op = self.clone(); - match &mut op { - Scan { .. } - | Values { .. } - | PromqlVectorFromScalar(_) - | SetOp { .. } - | Concat { .. } - | BinaryOp { .. } - | TimeRange { .. } - | TimeShift { .. } - | PromqlInfoEnrich { .. } - | PromqlSubquery { .. } => {} - Filter { pred: p, .. } | Join { pred: p, .. } => *p = pred(p, column), - Project { cols, .. } => { - for item in cols { - item.expr = item.expr.map_columns(column); - } - } - Aggregate { - reduction, - measures, - filters, - .. - } => { - *reduction = reduction.map_columns(column); - for measure in measures { - *measure = measure.map_columns(column); - } - for filter in filters.iter_mut().flatten() { - *filter = pred(filter, column); - } - } - Dedup { cols, .. } => { - for col in cols { - *col = column(*col); - } - } - Sort { - keys: k, - partition_by, - .. - } => { - *k = keys(k, column); - *partition_by = partition_by.map_columns(column); - } - Limit { partition_by, .. } - | PromqlSeriesSample { - by: partition_by, .. - } => *partition_by = partition_by.map_columns(column), - SQLWindowFunc { - args, - partition_by, - order_by, - .. - } => { - for arg in args { - *arg = arg.map_columns(column); - } - *partition_by = partition_by.map_columns(column); - *order_by = keys(order_by, column); - } - PromqlRelabel { value, .. } => *value = value.map_columns(column), - } - op - } - /// The variant name, for diagnostics and export. pub(crate) fn kind_name(&self) -> &'static str { use NonASAPOp::*; @@ -1171,14 +1140,7 @@ impl NonASAPOp { { return Err(invalid("SQL join requires relations")); } - let scope = Schema::new( - left.schema() - .fields - .iter() - .chain(&right.schema().fields) - .cloned() - .collect(), - ); + let scope = Schema::new(self.input_scope().into_iter().cloned().collect()); predicate(pred, &scope)?; } SetOp { left, right, .. } => { diff --git a/crates/types/src/ir/scalar/mod.rs b/crates/types/src/ir/scalar/mod.rs index fe128ffc4..bee4020da 100644 --- a/crates/types/src/ir/scalar/mod.rs +++ b/crates/types/src/ir/scalar/mod.rs @@ -236,7 +236,9 @@ impl ScalarExpr { self.map_parts(column, &mut C::clone) } - fn map_parts( + /// One walk over this expression: every column reference renumbered by + /// `column`, every operator node it reads mapped by `f`. + pub(crate) fn map_parts( &self, column: &mut impl FnMut(ColumnId) -> ColumnId, f: &mut impl FnMut(&C) -> D, diff --git a/crates/types/src/ir/schema/series_identity.rs b/crates/types/src/ir/schema/series_identity.rs index 78c164b2c..b93e93d42 100644 --- a/crates/types/src/ir/schema/series_identity.rs +++ b/crates/types/src/ir/schema/series_identity.rs @@ -20,19 +20,30 @@ use crate::ir::schema::*; /// bound before it, so every parent's column references are renumbered to /// match: `topk(1, sum without (instance) (m))` binds its sort key to /// `Column(0)` of `[sum]`; realization makes the aggregate's output -/// `[$promql_series_identity, sum]` and the key `Column(1)`. A reference to -/// a column realization does not keep fails here rather than read another -/// column. +/// `[$promql_series_identity, sum]` and the key `Column(1)`. An +/// `Aggregate`'s `having` indexes the aggregate's own output, so it is +/// renumbered by how that output moves (PromQL emits no `having`; an IR +/// tree may). +/// +/// Errors: +/// - [`SeriesIdentityError::NotApplicable`]: the root holds an operator +/// this realization does not cover, such as `limitk` or `info()`; a +/// caller plans the root over its label columns instead. +/// - [`SeriesIdentityError::DroppedColumn`]: a reference to a column +/// realization does not keep. Reading another column would give a wrong +/// answer, so a caller must not fall back. pub fn with_promql_series_identity( root: &std::rc::Rc, -) -> Result, String> { +) -> Result, SeriesIdentityError> { use crate::ir::operator::Source; + use crate::ir::scalar::Predicate; use crate::ir::{NonASAPOp, Operator, OperatorNode}; use std::{collections::HashMap, rc::Rc}; + use SeriesIdentityError::NotApplicable; fn visit( node: &Rc, cache: &mut HashMap<*const OperatorNode, Rc>, - ) -> Result, String> { + ) -> Result, SeriesIdentityError> { if let Some(found) = cache.get(&Rc::as_ptr(node)) { return Ok(Rc::clone(found)); } @@ -50,29 +61,12 @@ pub fn with_promql_series_identity( return Err(error); } if let (Operator::NonASAP(before), Operator::NonASAP(after)) = (&node.operator, &operator) { - let positions = positions_after_realization( - &input_scope(&before.inputs()), - &input_scope(&after.inputs()), - ); - if positions - .iter() - .enumerate() - .any(|(column, position)| *position != Some(column)) - { - let mut dropped = None; - let renumbered = after.map_input_columns(&mut |column| { - positions.get(column).copied().flatten().unwrap_or_else(|| { - dropped = Some(column); - column - }) - }); - if let Some(column) = dropped { - return Err(format!( - "series identity realization does not keep input column {column} \ - that {} reads", - before.kind_name() - )); - } + let positions = + positions_after_realization(&before.input_scope(), &after.input_scope()); + if moves(&positions) { + let renumbered = renumber(&positions, before.kind_name(), |column| { + after.map_input_columns(&mut |c| column(c)) + })?; operator = Operator::NonASAP(renumbered); } } @@ -88,13 +82,15 @@ pub fn with_promql_series_identity( .any(|field| field.name == PROMQL_SERIES_IDENTITY) { if !schema.has_promql_series_identity() { - return Err("invalid physical series identity".into()); + return Err(NotApplicable("invalid physical series identity".into())); } cache.insert(Rc::as_ptr(node), Rc::clone(node)); return Ok(Rc::clone(node)); } if schema.closed { - return Err("dynamic series identity requires an open PromQL source".into()); + return Err(NotApplicable( + "dynamic series identity requires an open PromQL source".into(), + )); } schema.fields.push(Field::new( PROMQL_SERIES_IDENTITY, @@ -113,7 +109,9 @@ pub fn with_promql_series_identity( Operator::NonASAP(NonASAPOp::Sort { partition_by, .. }) if partition_by.is_without() => { - return Err("dynamic without ranking requires label-set projection".into()); + return Err(NotApplicable( + "dynamic without ranking requires label-set projection".into(), + )); } Operator::NonASAP( NonASAPOp::TimeRange { .. } @@ -129,20 +127,45 @@ pub fn with_promql_series_identity( | NonASAPOp::Aggregate { .. } | NonASAPOp::Sort { .. }, ) => {} - _ => return Err("operator has no dynamic series-identity realization".into()), + Operator::NonASAP(op) => { + return Err(NotApplicable(format!( + "{} has no dynamic series-identity realization", + op.kind_name() + ))) + } + Operator::ASAP(_) => { + return Err(NotApplicable( + "an ASAP operator has no dynamic series-identity realization".into(), + )) + } } - let rebuilt = Rc::new(OperatorNode::new(operator).map_err(|e| e.to_string())?); - // `having` indexes the aggregate's own output, which the - // renumbering above does not follow; PromQL emits none. - if matches!( - rebuilt.non_asap(), - Some(NonASAPOp::Aggregate { - having: Some(_), - .. - }) - ) && rebuilt.schema.fields.len() != node.schema.fields.len() + let new = |operator| { + OperatorNode::new(operator) + .map(Rc::new) + .map_err(|e| NotApplicable(e.to_string())) + }; + let mut rebuilt = new(operator)?; + // `having` indexes the aggregate's own output, which the input + // renumbering above does not follow. + if let Some(NonASAPOp::Aggregate { + having: Some(having), + .. + }) = rebuilt.non_asap() { - return Err("series identity realization moves the columns a HAVING reads".into()); + let positions = positions_after_realization( + &node.schema.fields.iter().collect::>(), + &rebuilt.schema.fields.iter().collect::>(), + ); + if moves(&positions) { + let having = renumber(&positions, "Aggregate's HAVING", |column| { + Predicate(having.0.map_columns(&mut |c| column(c))) + })?; + let mut aggregate = rebuilt.non_asap().cloned().expect("an aggregate"); + if let NonASAPOp::Aggregate { having: slot, .. } = &mut aggregate { + *slot = Some(having); + } + rebuilt = new(Operator::NonASAP(aggregate))?; + } } cache.insert(Rc::as_ptr(node), Rc::clone(&rebuilt)); Ok(rebuilt) @@ -150,13 +173,56 @@ pub fn with_promql_series_identity( visit(root, &mut HashMap::new()) } -/// The input scope of an operator over `inputs`: their output columns, in -/// order. -fn input_scope<'a>(inputs: &[&'a std::rc::Rc]) -> Vec<&'a Field> { - inputs +/// Why [`with_promql_series_identity`] gives no root carrying +/// [`PROMQL_SERIES_IDENTITY`]. +#[derive(Debug, Clone, PartialEq, thiserror::Error)] +pub enum SeriesIdentityError { + /// The root holds an operator this realization does not cover: + /// `limitk(2, m)` lowers to a `PromqlSeriesSample`, `info(m)` to a + /// `PromqlInfoEnrich`. The root is planned over its label columns + /// instead. + #[error("{0}")] + NotApplicable(String), + /// `operator` reads input column `column`, which realization does not + /// keep. Renumbering it would read another column and give a wrong + /// answer. + #[error("series identity realization does not keep column {column} that {operator} reads")] + DroppedColumn { + operator: &'static str, + column: ColumnId, + }, +} + +/// Whether some column of `positions` (see [`positions_after_realization`]) +/// moved or is not kept. +fn moves(positions: &[Option]) -> bool { + positions .iter() - .flat_map(|input| input.schema.fields.iter()) - .collect() + .enumerate() + .any(|(column, position)| *position != Some(column)) +} + +/// `map` run with the renumbering `positions` gives (see +/// [`positions_after_realization`]); [`SeriesIdentityError::DroppedColumn`] +/// when `operator` reads a column `positions` does not keep. Over +/// `[Some(1)]`, `Column(0)` becomes `Column(1)` and `Column(1)` is +/// dropped. +fn renumber( + positions: &[Option], + operator: &'static str, + map: impl FnOnce(&mut dyn FnMut(ColumnId) -> ColumnId) -> T, +) -> Result { + let mut dropped = None; + let mapped = map(&mut |column| { + positions.get(column).copied().flatten().unwrap_or_else(|| { + dropped = Some(column); + column + }) + }); + match dropped { + Some(column) => Err(SeriesIdentityError::DroppedColumn { operator, column }), + None => Ok(mapped), + } } /// Where each column of the input scope `before` realization sits in the @@ -189,10 +255,10 @@ mod tests { use std::rc::Rc; use std::time::Duration; - use super::with_promql_series_identity; + use super::{renumber, with_promql_series_identity, SeriesIdentityError}; use crate::ir::operator::arguments::{GroupKeys, Reduction, Source}; use crate::ir::operator::AggIntent; - use crate::ir::scalar::{Predicate, ScalarValue, SortKey}; + use crate::ir::scalar::{CompareOpKind, ExprSemantics, Predicate, ScalarValue, SortKey}; use crate::ir::schema::{DataType, Field, Schema, PROMQL_SERIES_IDENTITY}; use crate::ir::{NonASAPOp, Operator, OperatorNode, ProjectItem, ScalarExpr, TimeRangeKind}; @@ -200,12 +266,9 @@ mod tests { OperatorNode::new_shared(Operator::NonASAP(operator)).unwrap() } - /// `sort_desc(sum without (instance) (m))` with a filter and a - /// projection above the aggregate, bound over its output `[sum]` before - /// realization: every reference to `sum`, `Column(0)`, reads - /// `Column(1)` of the realized `[$promql_series_identity, sum]`. - #[test] - fn realization_renumbers_parent_references_into_a_without_aggregate() { + /// `sum without (instance) (m)` over `[ts, value, instance]` with + /// `having`, which indexes its output: `[sum]` before realization. + fn sum_without_instance(having: Option) -> Rc { let scan = node(NonASAPOp::Scan { source: Source::TimeSeries { metric: "m".into() }, predicates: vec![], @@ -224,14 +287,23 @@ mod tests { kind: TimeRangeKind::Instant, child: scan, }); - let sum = node(NonASAPOp::Aggregate { + node(NonASAPOp::Aggregate { reduction: Reduction::Reduce(GroupKeys::without(vec![2])), measures: vec![AggIntent::Sum { col: None }], output_names: vec![String::new()], filters: vec![None], - having: None, + having, child: instant, - }); + }) + } + + /// `sort_desc(sum without (instance) (m))` with a filter and a + /// projection above the aggregate, bound over its output `[sum]` before + /// realization: every reference to `sum`, `Column(0)`, reads + /// `Column(1)` of the realized `[$promql_series_identity, sum]`. + #[test] + fn realization_renumbers_parent_references_into_a_without_aggregate() { + let sum = sum_without_instance(None); let sum_column = |node: &OperatorNode| { node.schema .fields @@ -280,6 +352,48 @@ mod tests { assert_eq!(cols[0].expr, ScalarExpr::Column(1)); } + /// `sum without (instance) (m) HAVING sum > 1`: the `having` reads + /// `sum`, `Column(0)` of the output `[sum]`, and after realization + /// `Column(1)` of `[$promql_series_identity, sum]`. + #[test] + fn realization_renumbers_a_having_over_the_aggregate_output() { + let sum_over_one = |column| { + Predicate(ScalarExpr::Compare { + left: Box::new(ScalarExpr::Column(column)), + op: CompareOpKind::Gt, + right: Box::new(ScalarExpr::Literal(ScalarValue::Float64(1.0))), + semantics: ExprSemantics::Promql, + }) + }; + let realized = + with_promql_series_identity(&sum_without_instance(Some(sum_over_one(0)))).unwrap(); + let Some(NonASAPOp::Aggregate { + having: Some(having), + .. + }) = realized.non_asap() + else { + panic!("an aggregate with a having"); + }; + assert_eq!(realized.schema.fields[1].name, "sum"); + assert_eq!(*having, sum_over_one(1)); + } + + /// A reference to a column realization does not keep is an error, not a + /// read of another column: over the positions `[Some(1)]`, `Column(0)` + /// moves to `Column(1)` and `Column(1)` is dropped. + #[test] + fn renumbering_a_dropped_column_is_an_error() { + let positions = [Some(1)]; + assert_eq!(renumber(&positions, "Sort", |column| column(0)), Ok(1)); + assert_eq!( + renumber(&positions, "Sort", |column| column(1)), + Err(SeriesIdentityError::DroppedColumn { + operator: "Sort", + column: 1 + }) + ); + } + // A direct scalar literal (`vector(1)`) stays a valid vector input when // series identity is realized. #[test] From 0b0e03491f737071ee9535cb9a1ff5851c95023f Mon Sep 17 00:00:00 2001 From: zzylol <50204836+zzylol@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:12:38 +0000 Subject: [PATCH 8/8] fix(ir): fail planning when a realized operator's schema does not derive A schema error from rebuilding an operator after series identity renumbering was wrapped as NotApplicable, so promql_root silently planned the unrealized root. It is now SeriesIdentityError::InvalidRealizedOperator, a planning error like DroppedColumn. NotApplicable keeps only operators realization does not cover and sources that are not open PromQL sources. --- crates/planner/src/pass.rs | 3 ++- crates/planner/src/run.rs | 19 +++++++++++++-- crates/types/src/ir/schema/error.rs | 2 +- crates/types/src/ir/schema/series_identity.rs | 24 +++++++++++++++---- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/crates/planner/src/pass.rs b/crates/planner/src/pass.rs index 0c56f9c38..207228c3b 100644 --- a/crates/planner/src/pass.rs +++ b/crates/planner/src/pass.rs @@ -79,7 +79,8 @@ pub enum OptimizeError { #[error("plan selection: {0}")] Selection(#[from] SelectionError), /// Series identity realization would make an operator read a column it - /// does not keep: a defect in the realization, not in the query. + /// does not keep, or rebuilt an operator whose schema no longer derives: + /// a defect in the realization, not in the query. #[error("PromQL series identity realization: {0}")] SeriesIdentity(SeriesIdentityError), /// The pass returned something the downstream contract forbids. This is a diff --git a/crates/planner/src/run.rs b/crates/planner/src/run.rs index b41fce908..1f69236bc 100644 --- a/crates/planner/src/run.rs +++ b/crates/planner/src/run.rs @@ -225,8 +225,10 @@ impl OptimizationPass for Stage1ToStage3Pass { /// realization (`with_promql_series_identity`), or `node` itself, over its /// label columns, when the realization does not apply to one of its /// operators (`limitk(2, m)`'s `PromqlSeriesSample`, `info(m)`'s -/// `PromqlInfoEnrich`). A realization that drops a column an operator -/// reads is a planning error: falling back would hide it. +/// `PromqlInfoEnrich`). A realization defect is a planning error, since +/// falling back would hide it: one that drops a column an operator reads +/// (`DroppedColumn`) or rebuilds an operator whose schema no longer derives +/// (`InvalidRealizedOperator`). fn promql_root( node: &Rc, realized: Result, SeriesIdentityError>, @@ -274,4 +276,17 @@ mod tests { Err(OptimizeError::SeriesIdentity(dropped)) if dropped == error )); } + + /// A realization that rebuilds an operator whose schema no longer + /// derives fails planning rather than plan the unrealized root. + #[test] + fn a_realization_that_builds_an_invalid_operator_fails_planning() { + let error = SeriesIdentityError::InvalidRealizedOperator( + asap_types::ir::schema::error::SchemaDerivationError::EmptyConcat, + ); + assert!(matches!( + promql_root(&vector_one(), Err(error.clone())), + Err(OptimizeError::SeriesIdentity(invalid)) if invalid == error + )); + } } diff --git a/crates/types/src/ir/schema/error.rs b/crates/types/src/ir/schema/error.rs index 570759916..5b3783845 100644 --- a/crates/types/src/ir/schema/error.rs +++ b/crates/types/src/ir/schema/error.rs @@ -6,7 +6,7 @@ use crate::ir::schema::ColumnId; use thiserror::Error; /// Errors from schema and type derivation over an operator DAG. -#[derive(Debug, Error)] +#[derive(Debug, Clone, PartialEq, Error)] pub enum SchemaDerivationError { #[error("invalid scalar function signature: {0}")] InvalidScalarSignature(String), diff --git a/crates/types/src/ir/schema/series_identity.rs b/crates/types/src/ir/schema/series_identity.rs index b93e93d42..1ba06d4f0 100644 --- a/crates/types/src/ir/schema/series_identity.rs +++ b/crates/types/src/ir/schema/series_identity.rs @@ -1,4 +1,5 @@ //! Series-identity realization for the unified dag. +use crate::ir::schema::error::SchemaDerivationError; use crate::ir::schema::*; /// Resolve a PromQL root to rows carrying [`PROMQL_SERIES_IDENTITY`] before /// candidate search. `closed` describes physical columns here: the final @@ -32,6 +33,9 @@ use crate::ir::schema::*; /// - [`SeriesIdentityError::DroppedColumn`]: a reference to a column /// realization does not keep. Reading another column would give a wrong /// answer, so a caller must not fall back. +/// - [`SeriesIdentityError::InvalidRealizedOperator`]: a renumbered +/// operator's schema no longer derives. Its input was valid, so this is +/// a defect in realization and a caller must not fall back either. pub fn with_promql_series_identity( root: &std::rc::Rc, ) -> Result, SeriesIdentityError> { @@ -142,7 +146,7 @@ pub fn with_promql_series_identity( let new = |operator| { OperatorNode::new(operator) .map(Rc::new) - .map_err(|e| NotApplicable(e.to_string())) + .map_err(SeriesIdentityError::InvalidRealizedOperator) }; let mut rebuilt = new(operator)?; // `having` indexes the aggregate's own output, which the input @@ -177,10 +181,11 @@ pub fn with_promql_series_identity( /// [`PROMQL_SERIES_IDENTITY`]. #[derive(Debug, Clone, PartialEq, thiserror::Error)] pub enum SeriesIdentityError { - /// The root holds an operator this realization does not cover: - /// `limitk(2, m)` lowers to a `PromqlSeriesSample`, `info(m)` to a - /// `PromqlInfoEnrich`. The root is planned over its label columns - /// instead. + /// The root holds an operator this realization does not cover, or a + /// source that is not an open PromQL source: `limitk(2, m)` lowers to + /// a `PromqlSeriesSample`, `info(m)` to a `PromqlInfoEnrich`, and a + /// closed scan has no dynamic labels to carry. The root is planned + /// over its label columns instead. #[error("{0}")] NotApplicable(String), /// `operator` reads input column `column`, which realization does not @@ -191,6 +196,15 @@ pub enum SeriesIdentityError { operator: &'static str, column: ColumnId, }, + /// An operator rebuilt over its realized children, with its column + /// references renumbered, has a schema that no longer derives. Its + /// unrealized form derived, and renumbering moves `Column(0)` of + /// `[sum]` to the same `sum` at `Column(1)` of + /// `[$promql_series_identity, sum]`, so this is a defect in + /// realization, not in the query. Planning the unrealized root would + /// hide it. + #[error("series identity realization built an invalid operator: {0}")] + InvalidRealizedOperator(SchemaDerivationError), } /// Whether some column of `positions` (see [`positions_after_realization`])