From 353149cf63dc60f244bf056c4db44655d2617495 Mon Sep 17 00:00:00 2001 From: Dennis Zhuang Date: Thu, 22 Oct 2026 04:55:03 +0800 Subject: [PATCH] feat(promql): run sort and absent in the native select node Signed-off-by: Dennis Zhuang --- src/promql/src/native/display.rs | 28 +++++- src/promql/src/native/display/tests.rs | 20 +++- src/promql/src/native/execute.rs | 121 +++++++++++++++++++------ src/promql/src/native/execute/tests.rs | 109 ++++++++++++++++++++++ src/promql/src/native/plan.rs | 10 +- src/promql/src/select.rs | 38 +++++++- src/promql/src/select/tests.rs | 45 +++++++++ 7 files changed, 338 insertions(+), 33 deletions(-) diff --git a/src/promql/src/native/display.rs b/src/promql/src/native/display.rs index 35ccb27542f..2124f692aec 100644 --- a/src/promql/src/native/display.rs +++ b/src/promql/src/native/display.rs @@ -22,8 +22,8 @@ use std::fmt::{self, Write}; use std::sync::Arc; -use datatypes::series::SeriesOrdering; use datatypes::series::time::EvalGrid; +use datatypes::series::{MetricName, SeriesOrdering}; use crate::map::MathOp; use crate::native::metrics::{self, NodePath}; @@ -32,6 +32,7 @@ use crate::native::plan::{ NativeTree, NativeType, }; use crate::range_eval::RangeFunction; +use crate::select::SortOrder; use crate::vector_match::{BinaryOp, Cardinality, FillValues, MatchingLabels}; /// The header line of every tree. @@ -110,6 +111,15 @@ fn describe(node: &NativeNode) -> String { ), BoundSelect::LimitK { k, group } => format!("limitk {k} {}", grouping(group)), BoundSelect::LimitRatio { ratio } => format!("limit_ratio {ratio}"), + BoundSelect::Sort(order) => match order { + SortOrder::Asc => "sort".to_string(), + SortOrder::Desc => "sort_desc".to_string(), + SortOrder::ByLabel(labels) => format!("sort_by_label [{}]", label_list(labels)), + SortOrder::ByLabelDesc(labels) => { + format!("sort_by_label_desc [{}]", label_list(labels)) + } + }, + BoundSelect::Absent { labels } => format!("absent {{{}}}", label_pairs(labels)), }, NativeExpr::Aggregate { op, .. } => format!( "{} {} {}", @@ -255,6 +265,22 @@ fn binary_op(op: BinaryOp) -> &'static str { } } +/// The labels of `metric` as `name="value"` pairs, the metric name first. +fn label_pairs(metric: &MetricName) -> String { + metric + .name() + .map(|name| format!("__name__={name:?}")) + .into_iter() + .chain( + metric + .tags() + .iter() + .map(|tag| format!("{}={:?}", tag.name, tag.value)), + ) + .collect::>() + .join(", ") +} + /// A grouping as `by [labels]` or `without [labels]`. fn grouping(group: &Grouping) -> String { match group { diff --git a/src/promql/src/native/display/tests.rs b/src/promql/src/native/display/tests.rs index 46e9098cf5f..d259ce28eee 100644 --- a/src/promql/src/native/display/tests.rs +++ b/src/promql/src/native/display/tests.rs @@ -14,9 +14,9 @@ use std::sync::Arc; -use datatypes::series::SeriesOrdering; use datatypes::series::samples::{FieldSpec, SeriesSchema}; use datatypes::series::time::{EvalGrid, GridSpec}; +use datatypes::series::{Label, MetricName, SeriesOrdering}; use super::super::plan::{ AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar, @@ -24,6 +24,7 @@ use super::super::plan::{ }; use crate::map::MathOp; use crate::range_eval::{RangeFunction, RangeWindow}; +use crate::select::SortOrder; use crate::vector_match::{BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels}; fn grid(start: i64, step: i64, len: u64) -> Arc { @@ -427,6 +428,23 @@ fn a_selection_names_its_binding() { BoundSelect::LimitRatio { ratio: -0.5 }, " root Select limit_ratio -0.5", ), + ( + BoundSelect::Sort(SortOrder::ByLabelDesc(vec!["job".into(), "le".into()])), + " root Select sort_by_label_desc [job, le]", + ), + ( + BoundSelect::Absent { + labels: MetricName::try_new( + None, + vec![Label { + name: "job".into(), + value: "x".into(), + }], + ) + .unwrap(), + }, + r#" root Select absent {job="x"}"#, + ), ]; 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 3e379c1bdb6..66b17c63f97 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -102,7 +102,7 @@ use crate::native::plan::{ BoundInstant, BoundSelect, Grouping, NativeExpr, NativeNode, NativeTree, }; use crate::range_eval::RangeEvaluator; -use crate::select; +use crate::select::{self, SortOrder}; use crate::vector_match::{ BinaryMatching, binary_vectors_with_reservation, validate_binary_operand, }; @@ -1835,15 +1835,15 @@ impl SelectRuntime { assembled.sort_by(|a, b| select::compare_labels(&a.metric, &b.metric)); } buffer.grow(selection_workspace_bytes( + &self.op, + &self.schema, &assembled, - self.schema.fields.len(), ))?; let metrics = Arc::clone(&buffer.metrics); let output = { let _timer = metrics.timer(); - select_series(&self.op, &self.schema, &assembled)? + select_series(&self.op, &self.schema, assembled)? }; - drop(assembled); self.input.release(); self.computed = true; Ok(Some(output)) @@ -1862,7 +1862,7 @@ fn select_overflow(path: &NodePath) -> DataFusionError { fn select_series( op: &BoundSelect, schema: &SeriesSchema, - series: &[Series], + series: Vec, ) -> Result> { let grouping = |group: &Grouping| match group { Grouping::By(labels) => select::Grouping::By(labels.to_vec()), @@ -1870,22 +1870,61 @@ fn select_series( }; match op { BoundSelect::TopK { k, top, group } => { - select::topk(*k, *top, &grouping(group), schema, series) + select::topk(*k, *top, &grouping(group), schema, &series) } - BoundSelect::LimitK { k, group } => select::limitk(*k, &grouping(group), schema, series), - BoundSelect::LimitRatio { ratio } => select::limit_ratio(*ratio, schema, series), + BoundSelect::LimitK { k, group } => select::limitk(*k, &grouping(group), schema, &series), + BoundSelect::LimitRatio { ratio } => select::limit_ratio(*ratio, schema, &series), + // A sort hands back the series it was given, rebuilding only those `sort` and `sort_desc` + // drop histogram samples from. + BoundSelect::Sort(order) => select::sort_instant(order, schema, series), + BoundSelect::Absent { labels } => Ok(select::absent(schema, &series, labels.clone())? + .into_iter() + .collect()), } } -/// A rough estimate of what a selection kernel allocates over `series`, beside the input it reads. +/// A rough estimate of what the selection `op` allocates over `series`, beside the input it reads. +/// Like [`merge_workspace_bytes`] it is not an allocation bound: vector slack and the hash table's +/// own layout are not measured. /// -/// Per row, the `(point, row)` a kept sample is recorded as, the candidate it is ranked as and the -/// value each field rebuilds it into; per series, its place in the label order and its group, its -/// label pairs in the group key, its cursor and the series and field descriptors of its result; and -/// the histograms `limitk` and `limit_ratio` copy into their results, at the widest histogram of -/// each field. Like [`merge_workspace_bytes`] it is not an allocation bound: vector slack and the -/// hash table's own layout are not measured. -fn selection_workspace_bytes(series: &[Series], fields: usize) -> usize { +/// - `topk`, `bottomk`, `limitk` and `limit_ratio`, per row: the `(point, row)` a kept sample is +/// recorded as, the candidate it is ranked as and the value each field rebuilds it into; per +/// series: its place in the label order and its group, its label pairs in the group key, its +/// cursor and the series and field descriptors of its result; and the histograms `limitk` and +/// `limit_ratio` copy into their results, at the widest histogram of each field. +/// - 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. +fn selection_workspace_bytes(op: &BoundSelect, schema: &SeriesSchema, series: &[Series]) -> usize { + let fields = schema.fields.len(); + match op { + BoundSelect::Sort(order) => { + let rebuilds = matches!(order, SortOrder::Asc | SortOrder::Desc); + return series.iter().fold(0usize, |total, item| { + let floats = if rebuilds + && item + .fields + .iter() + .any(|field| histogram_row_bytes(field).is_some()) + { + item.time.len().saturating_mul(std::mem::size_of::()) + } else { + 0 + }; + total + .saturating_add(std::mem::size_of::<(f64, Series)>()) + .saturating_add(floats) + }); + } + BoundSelect::Absent { .. } => { + let points = schema + .eval_grid + .as_ref() + .map_or(0, |grid| grid.timestamps().len()); + return points.saturating_mul(fields.saturating_mul(1 + std::mem::size_of::())); + } + BoundSelect::TopK { .. } | BoundSelect::LimitK { .. } | BoundSelect::LimitRatio { .. } => {} + } let row = std::mem::size_of::<(usize, usize)>() + std::mem::size_of::<(usize, usize, f64)>() + fields * std::mem::size_of::(); @@ -2326,7 +2365,7 @@ impl NodeRuntime { let Some(output) = output else { return Ok(()); }; - self.queue_results(output) + self.queue_results(output, false) } /// Retains one chunk of a selection's input, reserved for before it is retained, as @@ -2363,29 +2402,42 @@ impl NodeRuntime { /// Runs this node's selection kernel and queues the series it kept. fn compute_select(&mut self, path: &NodePath) -> Result<()> { - let output = { + let (output, shares_input) = { let select = self.select.as_mut().ok_or_else(|| no_select(path))?; - select.compute(path, &mut self.buffer)? + let shares_input = matches!(select.op, BoundSelect::Sort(_)); + (select.compute(path, &mut self.buffer)?, shares_input) }; let Some(output) = output else { return Ok(()); }; - self.queue_results(output) + self.queue_results(output, shares_input) } /// Queues the series a kernel that reads its whole input produced, and settles the reservation /// on the queue. - fn queue_results(&mut self, output: Vec) -> Result<()> { + /// + /// `shares_input` says the series are the assembled input itself, as a sort hands it back: a + /// field there is one slice per input chunk of buffers and histogram tables the chunks shared, + /// so each series is measured the way the input was ([`operand_chunk_bytes`], each table billed + /// once) rather than by the allocations its slices belong to, which would count a buffer once + /// per chunk. + fn queue_results(&mut self, output: Vec, shares_input: bool) -> Result<()> { + let mut tables = HashSet::new(); // The results are queued the way a node that produces a chunk per input queues what it // hands on: the queue is the one source of chunk order and of the bytes queued chunks hold. let mut closed = 0usize; for series in output { // The kernel decided every point of a result series at once, so the one chunk it is // handed over as closes it. + let shared = shares_input.then(|| { + let (fresh, table_bytes) = unbilled_tables(&series, &tables); + tables.extend(fresh); + operand_chunk_bytes(&series).saturating_add(table_bytes) + }); let chunk = NativeChunk::Evaluated(EvaluatedChunk::try_new(series, true)?); // The result is already admitted, within the workspace reserved for the kernel. What the // queue keeps is the estimate of the chunk that holds it. - let bytes = chunk.bytes(); + let bytes = shared.unwrap_or_else(|| chunk.bytes()); self.queued.push_back((chunk, bytes)); closed += 1; } @@ -2792,9 +2844,16 @@ impl NativeExecutionState { "the selection at {path} does not produce samples" )) })?; - // `topk`, `bottomk` and `limitk` rank one value per sample, so their kernels - // take one field; `limit_ratio` keeps whole series and takes any number. - if !matches!(op, BoundSelect::LimitRatio { .. }) && schema.fields.len() != 1 { + // `topk`, `bottomk`, `limitk` and the value sorts read one value per sample, + // so their kernels take one field; the others keep or test whole samples and + // take any number. + let one_field = matches!( + op, + BoundSelect::TopK { .. } + | BoundSelect::LimitK { .. } + | BoundSelect::Sort(SortOrder::Asc | SortOrder::Desc) + ); + if one_field && schema.fields.len() != 1 { return Err(not_implemented("multi-field Select", &path)); } runtime.select = Some(SelectRuntime::new(op.clone(), schema)); @@ -2887,8 +2946,16 @@ impl NativeExecutionState { // A selection keeps samples of the series it read, so pieces of one label // 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. - holds_shared[index] = input_holds_shared; + // it treats a map's. `absent` is the exception: it answers with one series + // under the labels it was bound to. + let absent = matches!( + expr, + NativeExpr::Select { + op: BoundSelect::Absent { .. }, + .. + } + ); + holds_shared[index] = input_holds_shared && !absent; } NodeTag::Window => { let (drops_metric_name, input_schema) = match expr { diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index a9d762ad816..b77e35e5685 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -48,6 +48,7 @@ use crate::native::plan::{ BoundSelect, BoundWindow, Grouping, NativeExpr, NativeNode, NativeTree, NativeType, }; use crate::range_eval::{RangeEvaluator, RangeFunction, RangeWindow}; +use crate::select::SortOrder; use crate::vector_match::{ BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels, reset_visited_pairs, visited_pairs, @@ -6599,3 +6600,111 @@ fn a_binary_node_reads_what_a_selection_kept() { } assert_eq!(decided(&run(&mut execution)), vec![(1, Some(15.0))]); } + +/// `absent` answers where its input holds nothing, under the labels it was bound to, and answers +/// at every point when no series arrives at all. +#[test] +fn absent_answers_where_the_input_holds_nothing() { + let grid = grid(0, 60_000, 3); + let schema = evaluated_schema(Arc::clone(&grid)); + // What `absent_labels` derives from `up{job="x"}`: the name is not one of them. + let labels = MetricName::try_new( + None, + vec![Label { + name: "job".into(), + value: "x".into(), + }], + ) + .unwrap(); + let tree = NativeTree::try_new(select( + evaluated_input(Arc::clone(&grid)), + BoundSelect::Absent { + labels: labels.clone(), + }, + )) + .unwrap(); + + let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new()); + execution + .push_evaluated(evaluated_chunk_of( + &schema, + named_metric("up", "y"), + 1, + vec![Some(0.0)], + true, + )) + .unwrap(); + let chunks = run(&mut execution); + assert_eq!(chunks.len(), 1); + assert_eq!(chunks[0].series.metric, labels); + assert_eq!(decided(&chunks), vec![(0, Some(1.0)), (2, Some(1.0))]); + + let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new()); + assert_eq!( + decided(&run(&mut execution)), + vec![(0, Some(1.0)), (1, Some(1.0)), (2, Some(1.0))] + ); +} + +/// A sort orders the series of an instant result, and the node hands them over in that order. +#[test] +fn sort_desc_hands_over_series_in_value_order() { + let grid = grid(0, 60_000, 1); + let schema = evaluated_schema(Arc::clone(&grid)); + let tree = NativeTree::try_new(select( + evaluated_input(Arc::clone(&grid)), + BoundSelect::Sort(SortOrder::Desc), + )) + .unwrap(); + let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new()); + for (job, value) in [("b", 1.0), ("a", 3.0), ("c", 2.0)] { + execution + .push_evaluated(evaluated_chunk_of( + &schema, + named_metric("m", job), + 0, + vec![Some(value)], + true, + )) + .unwrap(); + } + let jobs: Vec = run(&mut execution) + .iter() + .map(|chunk| chunk.series.metric.get("job").unwrap().to_string()) + .collect(); + assert_eq!(jobs, ["a", "c", "b"]); +} + +/// A sort hands back the assembled input, whose field is one slice per input chunk of one shared +/// buffer: it is queued at what the slices hold, as the input was retained, not at the buffer once +/// per chunk, which here would be ten times the input and more than the pool. +#[test] +fn a_sorted_series_is_queued_at_what_its_slices_hold() { + let points = 1_000; + let grid = grid(0, 60_000, points); + let schema = evaluated_schema(Arc::clone(&grid)); + let tree = NativeTree::try_new(select( + evaluated_input(Arc::clone(&grid)), + BoundSelect::Sort(SortOrder::ByLabel(vec!["job".into()])), + )) + .unwrap(); + let pool = bounded_pool(48 * 1024); + let mut execution = executor(&tree, Arc::clone(&pool), &ExecutionPlanMetricsSet::new()); + let values = Float64Array::from((0..points).map(|v| v as f64).collect::>()); + for start in (0..points as usize).step_by(100) { + execution + .push_evaluated(evaluated_array_chunk_of( + &schema, + named_metric("m", "a"), + start, + values.slice(start, 100), + start + 100 == points as usize, + )) + .unwrap(); + // The selection retains each chunk as it arrives, at what its slice holds. + assert!(execution.next_chunk().unwrap().is_none()); + } + let chunks = run(&mut execution); + assert_eq!(decided(&chunks).len(), points as usize); + assert_eq!(pool.reserved(), 0); +} diff --git a/src/promql/src/native/plan.rs b/src/promql/src/native/plan.rs index 42bd6a0f892..8e827b78819 100644 --- a/src/promql/src/native/plan.rs +++ b/src/promql/src/native/plan.rs @@ -31,12 +31,13 @@ use std::sync::Arc; use datafusion::error::{DataFusionError, Result}; -use datatypes::series::SeriesOrdering; use datatypes::series::samples::{FieldSpec, SeriesSchema}; use datatypes::series::time::EvalGrid; +use datatypes::series::{MetricName, SeriesOrdering}; use crate::map::MathOp; use crate::range_eval::{RangeFunction, RangeWindow}; +use crate::select::SortOrder; use crate::vector_match::BinaryMatching; /// What one node produces. @@ -236,6 +237,13 @@ pub enum BoundSelect { /// `limit_ratio`: the series whose label hash falls in the ratio; it keeps whole series, so it /// has no grouping. LimitRatio { ratio: f64 }, + /// `sort`, `sort_desc`, `sort_by_label` and `sort_by_label_desc`: the input's series in another + /// order, which only an instant result shows. `sort` and `sort_desc` also drop histogram + /// samples, in a range result too. + Sort(SortOrder), + /// `absent`: `1` under `labels` at every point where the input holds no sample. The labels are + /// what the compiler derives from the argument's matchers. + Absent { labels: MetricName }, } /// What a node computes. Every variant holds a bound operation, never a name to resolve. diff --git a/src/promql/src/select.rs b/src/promql/src/select.rs index 6645862e097..e88de344778 100644 --- a/src/promql/src/select.rs +++ b/src/promql/src/select.rs @@ -424,20 +424,30 @@ pub fn absent_labels(matchers: &[LabelMatcher]) -> Result { MetricName::try_new(None, labels).map_err(external) } -/// Orders the series of an instant result. A range result is returned unchanged: Prometheus -/// does not sort matrices. +/// Orders the series of an instant result. A range result keeps its order: Prometheus does not +/// sort matrices. +/// +/// `sort` and `sort_desc` drop histogram samples, in a range result too: Prometheus filters them +/// out at every step (`filterFloats`) before it sorts. The label sorts keep every sample. pub fn sort_instant( order: &SortOrder, schema: &SeriesSchema, mut series: Vec, ) -> Result> { let points = grid_points(schema, &series)?; + if matches!(order, SortOrder::Asc | SortOrder::Desc) { + single_field(schema, "sort")?; + let mut floats = Vec::with_capacity(series.len()); + for item in series { + floats.extend(float_samples(schema, item)?); + } + series = floats; + } if points != 1 { return Ok(series); } match order { SortOrder::Asc | SortOrder::Desc => { - single_field(schema, "sort")?; let mut keyed = Vec::with_capacity(series.len()); for item in series { let value = value_at(&item, 0)?; @@ -476,6 +486,28 @@ pub fn sort_instant( } } +/// `series` without its histogram samples, `None` when nothing else is left. One field, as `sort` +/// reads. +fn float_samples(schema: &SeriesSchema, series: Series) -> Result> { + let field = &series.fields.as_slice()[0]; + if !field + .iter() + .any(|sample| matches!(sample, SampleRef::Histogram(_))) + { + return Ok(Some(series)); + } + let mut builder = GridSeriesBuilder::try_new(Arc::new(schema.clone()))?; + let mut cursor = field.cursor(); + for (point, row) in series.time.positions() { + let value = match cursor.at(row).expect("the axis and the fields are aligned") { + SampleRef::Float(value) => Some(value), + SampleRef::Missing | SampleRef::Histogram(_) => None, + }; + builder.push(point, &[value])?; + } + builder.finish(series.metric) +} + /// Go's `math.NaN()` bit pattern, which Prometheus returns for an undefined scalar. const GO_NAN: f64 = f64::from_bits(0x7FF8_0000_0000_0001); diff --git a/src/promql/src/select/tests.rs b/src/promql/src/select/tests.rs index 4433020e2c2..b2492aba577 100644 --- a/src/promql/src/select/tests.rs +++ b/src/promql/src/select/tests.rs @@ -294,6 +294,51 @@ fn topk_skips_histogram_samples() { assert_eq!(kept[0].metric.name(), Some("b")); } +/// `sort` drops histogram samples at every step, as Prometheus' `filterFloats` does, while +/// `sort_by_label` keeps them. +#[test] +fn sort_drops_histogram_samples() { + let histogram = + OwnedHistogram::parse("0/0/0/3ff0000000000000/3ff0000000000000/0/1_1/3ff0000000000000///"); + let mixed = |schema: &SeriesSchema, name: &str| { + let mut builder = GridSeriesBuilder::try_new(Arc::new(schema.clone())).unwrap(); + builder + .push_samples( + 0, + &[Some(GridSample::Histogram(Box::new( + FloatHistogram::try_from_view(&histogram.view()).unwrap(), + )))], + ) + .unwrap(); + if schema.eval_grid.as_ref().unwrap().timestamps().len() > 1 { + builder + .push_samples(1, &[Some(GridSample::Float(2.0))]) + .unwrap(); + } + builder + .finish(metric(&format!("__name__={name}"))) + .unwrap() + .unwrap() + }; + + let instant = schema(1, 1); + let series = vec![ + mixed(&instant, "h"), + build(&instant, metric("__name__=f"), &vec![vec![Some(1.0)]]).unwrap(), + ]; + let sorted = sort_instant(&SortOrder::Asc, &instant, series.clone()).unwrap(); + assert_eq!(sorted.len(), 1); + assert_eq!(sorted[0].metric.name(), Some("f")); + let by_name = sort_instant(&SortOrder::ByLabel(names("__name__")), &instant, series).unwrap(); + assert_eq!(by_name.len(), 2); + + let range = schema(2, 1); + let sorted = sort_instant(&SortOrder::Desc, &range, vec![mixed(&range, "h")]).unwrap(); + assert_eq!(sorted.len(), 1); + let positions: Vec = sorted[0].time.positions().map(|(point, _)| point).collect(); + assert_eq!(positions, [1]); +} + /// A selection hands back the sample it selected, start timestamp included. /// /// `limitk` and `limit_ratio` choose samples rather than compute them, so a histogram that arrives