From 2d3feee67b10723a59fc71bda6de2561e04f5f6e Mon Sep 17 00:00:00 2001 From: discord9 Date: Sun, 18 Oct 2026 22:17:09 +0800 Subject: [PATCH] perf(promql): skip the per-row occupancy scan for a single dense float field `occupied_runs` inspected every row through `has_value`, once per field, to find the grid positions holding a sample. The fused evaluator's ordinary output is one dense, null-free float field, where every row holds a value by construction, so that scan can only reproduce the whole range. A series with exactly that shape now reports one run covering the range, using absolute grid indices, instead of walking its rows. Every other shape keeps the scan, including a null field, a histogram or mixed field, and several fields. The runs feed the duplicate-label-set check, so this changes how the runs are found and not what they mean: an overlap is still an error and disjoint points are still emitted separately. Tests pin the fast path against the whole range and the scan against a split range; shortening the fast path's run fails the first, which a value-only comparison would not catch. Signed-off-by: discord9 --- src/promql/src/extension_plan/fused_series.rs | 19 ++++- .../src/extension_plan/fused_series/tests.rs | 69 ++++++++++++++++++- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/promql/src/extension_plan/fused_series.rs b/src/promql/src/extension_plan/fused_series.rs index 5ae1635140d..35fb93be717 100644 --- a/src/promql/src/extension_plan/fused_series.rs +++ b/src/promql/src/extension_plan/fused_series.rs @@ -670,14 +670,29 @@ fn render_labels(metric: &MetricName) -> String { /// Each run is `(start, end_exclusive)` over absolute grid indices. A row whose every field is /// missing is not occupied: Prometheus has no point there, so it cannot collide with another /// series. -fn occupied_runs(series: &Series) -> Result> { +pub(crate) fn occupied_runs(series: &Series) -> Result> { let TimeAxis::Grid(range) = &series.time else { return Err(DataFusionError::Execution( "FusedSeriesExec requires evaluated series on a time grid".into(), )); }; + let rows = range.len(); + // A series with one dense, null-free float field holds a value on every row, so the whole + // range is one occupied run and no row has to be inspected. This is the shape the fused + // evaluator produces for a range function over ordinary float samples. + if series.fields.len() == 1 + && let Some(DenseSamplesRef::Float(values)) = + series.fields.iter().next().and_then(Samples::dense) + && values.len() == rows + && values.null_count() == 0 + { + return Ok((rows > 0) + .then(|| (range.start(), range.start() + rows)) + .into_iter() + .collect()); + } let mut runs: Vec<(usize, usize)> = Vec::new(); - for offset in 0..range.len() { + for offset in 0..rows { if !series .fields .iter() diff --git a/src/promql/src/extension_plan/fused_series/tests.rs b/src/promql/src/extension_plan/fused_series/tests.rs index 7d09d38df25..478798dda6a 100644 --- a/src/promql/src/extension_plan/fused_series/tests.rs +++ b/src/promql/src/extension_plan/fused_series/tests.rs @@ -50,7 +50,8 @@ use futures::{Stream, StreamExt}; use store_api::region_engine::RawLabelSource; use super::{ - FusedSeriesExec, MAX_CHUNKS_PER_POLL, apply_maps, classify_output, series_to_record_batch, + FusedSeriesExec, MAX_CHUNKS_PER_POLL, apply_maps, classify_output, occupied_runs, + series_to_record_batch, }; use crate::map::MathOp; use crate::range_eval::{RangeEvaluator, RangeFunction, RangeWindow}; @@ -414,6 +415,72 @@ fn grid_range(series: &Series) -> (usize, usize) { (range.start(), range.len()) } +/// A single dense, null-free float field covers every row of the range, so the fast path must +/// report exactly one run spanning it, in absolute grid indices. +#[test] +fn a_single_dense_float_field_occupies_the_whole_range() { + let chunks = vec![chunk( + 1, + "a", + &[ + (300_000, Some(1.0)), + (360_000, Some(2.0)), + (420_000, Some(3.0)), + ], + true, + )]; + let case = Case::new( + &["host"], + RangeWindow { + range_ms: 60_000, + offset_ms: 0, + anchor_ms: None, + }, + RangeFunction::SumOverTime, + vec![], + ); + let reference = case.reference(&chunks); + let (series, _) = &reference[0]; + let (start, len) = grid_range(series); + assert_eq!(len, 3, "the fixture must cover three grid points"); + assert_eq!( + occupied_runs(series).unwrap(), + vec![(start, start + len)], + "one dense float field occupies the whole range" + ); +} + +/// A field with a missing sample does not cover the range, so the scan reports the runs it finds +/// rather than the whole range. +#[test] +fn a_field_with_a_missing_sample_occupies_only_its_runs() { + let chunks = vec![chunk( + 1, + "a", + &[(300_000, Some(1.0)), (360_000, None), (420_000, Some(3.0))], + true, + )]; + let case = Case::new( + &["host"], + RangeWindow { + range_ms: 60_000, + offset_ms: 0, + anchor_ms: None, + }, + RangeFunction::SumOverTime, + vec![], + ); + let reference = case.reference(&chunks); + let (series, _) = &reference[0]; + let (start, len) = grid_range(series); + assert_eq!(len, 3); + assert_eq!( + occupied_runs(series).unwrap(), + vec![(start, start + 1), (start + 2, start + 3)], + "the missing point splits the range into two runs" + ); +} + /// Asserts the node emitted exactly the reference batches, comparing values bit for bit. fn assert_matches_reference( actual: &[RecordBatch],