diff --git a/src/promql/src/native/display.rs b/src/promql/src/native/display.rs index 9fd4d98fa41..1ba5241bf8e 100644 --- a/src/promql/src/native/display.rs +++ b/src/promql/src/native/display.rs @@ -127,6 +127,9 @@ fn describe(node: &NativeNode) -> String { BoundSelect::Histogram(HistogramFunction::Fraction { lower, upper }) => { format!("histogram_fraction {lower} {upper}") } + BoundSelect::CountValues { label, group } => { + format!("count_values {label:?} {}", grouping(group)) + } }, NativeExpr::Aggregate { op, .. } => format!( "{} {} {}", diff --git a/src/promql/src/native/display/tests.rs b/src/promql/src/native/display/tests.rs index d259ce28eee..ac1ca128827 100644 --- a/src/promql/src/native/display/tests.rs +++ b/src/promql/src/native/display/tests.rs @@ -445,6 +445,13 @@ fn a_selection_names_its_binding() { }, r#" root Select absent {job="x"}"#, ), + ( + BoundSelect::CountValues { + label: "value".into(), + group: Grouping::Without(Arc::new(["job".into()])), + }, + r#" root Select count_values "value" without [job]"#, + ), ]; for (op, line) in lines { let tree = NativeTree::try_new(NativeNode { diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 9616c248d33..b6e878b44ab 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -91,6 +91,7 @@ use datatypes::series::samples::{ use datatypes::series::time::{EvalGrid, GridRange, GridSpan, GridSpec, TimeAxis}; use datatypes::series::{MetricName, SeriesOrdering}; +use crate::aggregate; use crate::grid_series::{ GridSample, GridSeriesBuilder, LabelSet, label_set, merge_same_labelset, owned, }; @@ -1884,6 +1885,19 @@ fn select_series( BoundSelect::Histogram(function) => { evaluate_histograms(*function, schema, &merge_same_labelset(schema, series)?) } + BoundSelect::CountValues { label, group } => { + let (by, labels) = match group { + Grouping::By(labels) => (true, labels), + Grouping::Without(labels) => (false, labels), + }; + let labels: Vec<&str> = labels.iter().map(|label| &**label).collect(); + let grouping = if by { + aggregate::Grouping::By(&labels) + } else { + aggregate::Grouping::Without(&labels) + }; + aggregate::count_values(schema, &series, label, grouping) + } BoundSelect::Absent { labels } => Ok(select::absent(schema, &series, labels.clone())? .into_iter() .collect()), @@ -1902,6 +1916,9 @@ fn select_series( /// - A sort: the value each series is keyed by, and the floats `sort` and `sort_desc` rebuild a /// series into when it holds histograms; the label sorts move the series they were given. /// - `absent`: a presence flag and a result value per grid point and field. +/// - `count_values`, per row, since a gauge with distinct values makes about one group per sample: +/// the group's label set twice (its metric and its key) with the value written into it, its +/// count per field, and the result series it becomes. /// - `histogram_quantile` and `histogram_fraction`: the merge of label-set pieces before the kernel /// ([`merge_workspace_bytes`]); per series, its group entry and label pairs and a cursor per /// field; per row, the bucket it contributes and a result value per field. @@ -1949,6 +1966,20 @@ fn selection_workspace_bytes(op: &BoundSelect, schema: &SeriesSchema, series: &[ }, ); } + BoundSelect::CountValues { .. } => { + let label = std::mem::size_of::<(Arc, Arc)>(); + // The value label at the length of a 17-digit value with sign, point and exponent; + // the `'f'` form writes a large magnitude out in full, which this does not follow. + let value = 24; + let group = std::mem::size_of::>() + + fields * std::mem::size_of::() + + std::mem::size_of::() + + fields * std::mem::size_of::(); + return series.iter().fold(0usize, |total, item| { + let labels = 2 * (item.metric.tags().len() + 2) * label + value; + total.saturating_add(item.time.len().saturating_mul(group + labels)) + }); + } BoundSelect::TopK { .. } | BoundSelect::LimitK { .. } | BoundSelect::LimitRatio { .. } => {} } let row = std::mem::size_of::<(usize, usize)>() @@ -2973,12 +3004,14 @@ impl NativeExecutionState { // set stay pieces: they hold disjoint points, so ranking them apart picks // what ranking the merged series would, and what is above treats them as // it treats a map's. `absent` answers with one series under the labels it - // was bound to, and the histogram functions merge the label sets they - // answer with, so neither hands over pieces. + // was bound to, and the histogram functions and `count_values` answer with + // one series per label set they make, so none of them hands over pieces. let whole = matches!( expr, NativeExpr::Select { - op: BoundSelect::Absent { .. } | BoundSelect::Histogram(_), + op: BoundSelect::Absent { .. } + | BoundSelect::Histogram(_) + | BoundSelect::CountValues { .. }, .. } ); diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index 792ced48d15..0e577616c8c 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -6805,3 +6805,53 @@ fn histogram_quantile_reads_the_pieces_of_one_label_set_as_one_series() { .collect(); assert_eq!(positions, [0, 1]); } + +/// `count_values` counts the samples that share a value across the whole vector at a point, under +/// a label set per value; `a` arrives in two chunks and counts at both points. +#[test] +fn count_values_counts_the_samples_sharing_a_value() { + let grid = grid(0, 60_000, 2); + let schema = evaluated_schema(Arc::clone(&grid)); + let tree = NativeTree::try_new(select( + evaluated_input(Arc::clone(&grid)), + BoundSelect::CountValues { + label: "v".into(), + group: Grouping::By(Arc::from([])), + }, + )) + .unwrap(); + let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new()); + for (job, start, value, is_last) in [ + ("a", 0, 1.0, false), + ("a", 1, 1.0, true), + ("b", 0, 2.0, true), + ("c", 0, 1.0, true), + ] { + execution + .push_evaluated(evaluated_chunk_of( + &schema, + named_metric("m", job), + start, + vec![Some(value)], + is_last, + )) + .unwrap(); + } + let mut counts: Vec<(String, Decided)> = run(&mut execution) + .iter() + .map(|chunk| { + ( + chunk.series.metric.get("v").unwrap().to_string(), + decided(std::slice::from_ref(chunk)), + ) + }) + .collect(); + counts.sort_by(|a, b| a.0.cmp(&b.0)); + assert_eq!( + counts, + [ + ("1".to_string(), vec![(0, Some(2.0)), (1, Some(1.0))]), + ("2".to_string(), vec![(0, Some(1.0))]), + ] + ); +} diff --git a/src/promql/src/native/plan.rs b/src/promql/src/native/plan.rs index 92a816dc748..6301a95c314 100644 --- a/src/promql/src/native/plan.rs +++ b/src/promql/src/native/plan.rs @@ -226,7 +226,7 @@ pub struct BoundAggregate { /// An operator over the whole input vector at each grid point: one that keeps some of the input's /// samples with their series' labels and values, orders the series, reports where none is present, -/// or reads a classic histogram from its bucket series. +/// reads a classic histogram from its bucket series, or counts the samples sharing a value. /// /// Its constants are bound here, as a window's are: a `k` or a ratio only known at run time has no /// field. @@ -249,6 +249,9 @@ pub enum BoundSelect { /// `histogram_quantile` or `histogram_fraction`: each classic histogram is read from the bucket /// series that share its labels, so this too reads the whole vector at a point. Histogram(HistogramFunction), + /// `count_values(label, v)`: how many samples of each group share a value, under a label set + /// per value. It writes a label per distinct value, which an aggregate's fixed groups cannot. + CountValues { label: Arc, group: Grouping }, } /// What a node computes. Every variant holds a bound operation, never a name to resolve.