From e8d7c06a0293bcff07094d564b742eeae2252d31 Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:33:14 +0800 Subject: [PATCH] fix(promql): let an evaluated series skip positions no chunk covers A position no chunk covers holds no sample. That is the same state as a position whose chunk carries a missing value, so a window over either takes no sample, and the consumer never has to tell the two apart. `push_evaluated` nevertheless required every chunk after the first to begin exactly where the previous one ended, while letting the first begin anywhere. The asymmetry has no semantic basis: both spellings evaluate identically, as `a_series_may_begin_past_the_first_position_of_its_grid` asserts, so the check only refused a legal way to write the same output. `EvaluatedChunk::range` documents that ranges never go backwards and never overlap -- not that they tile the grid. The check, `SeriesState::input_end` (which existed only for it), and the test that pinned its error are removed. `map_chunk` now refuses an empty kernel chain instead of giving it an identity meaning no plan can reach. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/promql/src/evaluated.rs | 5 + src/promql/src/map.rs | 22 +-- src/promql/src/range_eval.rs | 47 +----- src/promql/src/range_eval/tests.rs | 223 +---------------------------- 4 files changed, 25 insertions(+), 272 deletions(-) diff --git a/src/promql/src/evaluated.rs b/src/promql/src/evaluated.rs index fd7888c6ae5..20e06bf162c 100644 --- a/src/promql/src/evaluated.rs +++ b/src/promql/src/evaluated.rs @@ -33,6 +33,11 @@ use datatypes::series::time::{GridRange, TimeAxis}; /// A series closes exactly once, on the chunk that carries `is_last`. A closed prefix is always /// followed by a closing chunk, even when that chunk holds no values, so a consumer can tell /// "nothing more here" from "no more input". +/// +/// The chunks of a series need not tile the grid: a position no chunk covers holds no sample, which +/// is an ordinary state for a PromQL series rather than a missing report. A window over such a +/// position reads no sample there, exactly as it does for a position whose chunk carries a missing +/// value, so a consumer never has to tell the two apart. #[derive(Clone, Debug)] pub struct EvaluatedChunk { /// The evaluated values, addressed on their absolute grid positions. diff --git a/src/promql/src/map.rs b/src/promql/src/map.rs index 3e5948f29f7..6f8dcfbd1da 100644 --- a/src/promql/src/map.rs +++ b/src/promql/src/map.rs @@ -143,10 +143,11 @@ pub fn map_math(schema: &SeriesSchema, series: &Series, op: MathOp) -> Result, metric: MetricName, last_ts: i64, - /// Grid index after the last input position consumed. The chunks of one evaluated series tile - /// the input grid, so this is where the next one has to start. Unlike `completed`, it counts - /// positions of the *input* grid, which is a different grid whenever the input is itself - /// evaluated. - input_end: usize, /// Next grid index to evaluate. cursor: usize, /// Grid index after the last position the evaluator has decided, whether it holds a value or @@ -576,14 +571,10 @@ impl RangeEvaluator { /// the value at each grid point is a sample at that time. Stale markers are kept as values, /// since the input is computed. /// - /// The first chunk opens the series with its labels, each later one starts exactly where the - /// previous one ended, and the one marked `is_last` closes it. The first chunk may begin at any - /// position of the input grid: a producer that trims its output starts at its first row - /// holding a value, and the positions before it carry no sample rather than a missing one. A - /// consumer therefore cannot tell a dropped leading chunk from a trimmed prefix -- both leave - /// the same positions without a sample and are reported as decided. A gap after the first - /// chunk is rejected instead, because there it would claim positions between two chunks of one - /// series are settled while a sample may still arrive in them. + /// The first chunk opens the series with its labels and the one marked `is_last` closes it. The + /// chunks need not tile the input grid: a position no chunk covers holds no sample, which is + /// the same state as a position whose chunk carries a missing value, so the positions a chunk + /// jumps over are simply reported as decided without a sample. /// /// A chunk reports the positions it decided, whether they hold a value or not, so a position /// an inner evaluation settled without a result crosses this layer too; as on the raw path, @@ -620,29 +611,6 @@ impl RangeEvaluator { range.start() ))); } - // Every chunk after the first has to continue exactly where the previous one ended, so the - // positions of one series never repeat or leave a gap. The first chunk is not constrained: - // a producer that trims its output to the rows holding a value begins at its first such - // row, and the positions before it carry no sample rather than a missing one. Reporting - // them as decided is still correct, because no input ever arrives there, so a window over - // them can never gain a sample. - // - // The concession is deliberately leading-only, and it costs the consumer no information: - // `emit`, the only producer here, begins a series at position 0 and tiles every chunk after - // it, so a leading gap can only mean an outside producer trimmed the series head -- a - // dropped leading chunk and a trimmed prefix are the same thing to this layer, since both - // leave the positions before the start without a sample. A gap in the middle is not the - // same: the positions in it lie between two chunks of one series, so they would read as - // decided-without-a-sample while a sample may still arrive there, and it is rejected. - if let Some(state) = &self.series - && range.start() != state.input_end - { - return Err(DataFusionError::Execution(format!( - "evaluated chunks are out of order: {} does not continue from {}", - range.start(), - state.input_end - ))); - } if let Some(state) = &self.series { if state.metric != chunk.series.metric { return Err(DataFusionError::Execution(format!( @@ -655,7 +623,6 @@ impl RangeEvaluator { primary_key: None, metric: chunk.series.metric.clone(), last_ts: 0, - input_end: 0, cursor: 0, completed: 0, emitted_end: 0, @@ -665,11 +632,6 @@ impl RangeEvaluator { // on its timestamps, so the next sample can only arrive there. let input_end = range.start() + range.len(); self.ingest(&range.timestamps(), &chunk.series.fields, false)?; - // Advanced only now: `ingest` may fail on a sample it cannot shift or convert. - if let Some(state) = self.series.as_mut() { - // Open, because the first chunk above opens the series and `ingest` never closes it. - state.input_end = input_end; - } if !chunk.is_last { // A point is decided once no sample that can still arrive falls in its window, and the // next sample can only sit at `grid[input_end]`. `ready` is inclusive and in the @@ -787,7 +749,6 @@ impl RangeEvaluator { primary_key: Some(chunk.primary_key.clone()), metric, last_ts: time[time.len() - 1], - input_end: 0, cursor: 0, completed: 0, emitted_end: 0, diff --git a/src/promql/src/range_eval/tests.rs b/src/promql/src/range_eval/tests.rs index c1f30002deb..481feeb0419 100644 --- a/src/promql/src/range_eval/tests.rs +++ b/src/promql/src/range_eval/tests.rs @@ -2370,98 +2370,6 @@ fn evaluated_chunk( EvaluatedChunk::try_new(series, is_last).unwrap() } -/// Pushes `chunks` into a fresh consumer on the 5/20 grid and returns what it handed out together -/// with the series those chunks fold into. -fn consume( - input: &Arc, - chunks: &[EvaluatedChunk], -) -> (Vec, Option) { - let mut consumer = outer_layer(input); - let schema = consumer.output_schema().clone(); - let handed_out: Vec<_> = chunks - .iter() - .flat_map(|chunk| consumer.push_evaluated(chunk).unwrap()) - .collect(); - consumer.finish().expect("the series closed"); - let series = fold_chunks(&handed_out, &schema); - (handed_out, series) -} - -/// The chunks of one evaluated series pushed one at a time and the whole series pushed as one -/// chunk reach the same consumer result, on a grid the series is not indexed on. -/// -/// This is chunking invariance, not an independent oracle: both sides run the same outer layer, -/// so it is the properties the other tests of this module name that pin the protocol itself. -#[test] -fn a_series_pushed_in_chunks_matches_the_whole_series() { - let mut inner = inner_layer(); - let schema = inner.output_schema().clone(); - let inner_chunks = collect_chunks(&mut inner, &consumer_chunks()); - inner.finish().expect("the inner series closed"); - assert!( - inner_chunks.len() > 1, - "the fixture hands the inner series out as one chunk" - ); - - let (handed_out, chunked) = consume(&schema, &inner_chunks); - - // The whole inner series as one chunk that closes it, the way a producer that never chunked - // would hand it over. - let whole = fold_chunks(&inner_chunks, &schema).expect("the fixture inner series holds values"); - let whole = EvaluatedChunk::try_new(whole, true).unwrap(); - let (_, at_once) = consume(&schema, std::slice::from_ref(&whole)); - - assert!( - handed_out.len() > 1, - "the chunked path handed out one chunk, exactly like the whole-series path" - ); - assert_series_eq( - chunked.as_ref(), - &expand(at_once.as_ref().expect("both paths produce a value")), - &metric(0), - ); -} - -/// A chunk leaving the outer layer is addressed on the outer grid, and the starts of those chunks -/// strictly increase: no row number of the inner grid leaks into the outer addressing. -#[test] -fn a_chunk_indexed_on_the_inner_grid_is_absolute_on_the_outer_one() { - let mut inner = inner_layer(); - let schema = inner.output_schema().clone(); - let inner_grid = schema.eval_grid.clone().unwrap(); - let inner_chunks = collect_chunks(&mut inner, &consumer_chunks()); - inner.finish().expect("the inner series closed"); - - assert_ne!( - inner_grid.spec(), - outer_grid().spec(), - "the fixture evaluates both layers on one grid" - ); - let mut consumer = outer_layer(&schema); - let mut starts = Vec::new(); - for chunk in &inner_chunks { - let Some(handed_out) = consumer.push_evaluated(chunk).unwrap() else { - continue; - }; - assert_eq!( - handed_out.range().grid().spec(), - outer_grid().spec(), - "a chunk is addressed on a grid other than the one its layer was asked for" - ); - starts.push(handed_out.range().start()); - } - consumer.finish().expect("the outer series closed"); - - assert!( - starts.len() > 1, - "one chunk makes the strict monotonicity below free" - ); - assert!( - starts.windows(2).all(|pair| pair[0] < pair[1]), - "the outer chunk starts {starts:?} do not strictly increase" - ); -} - /// A point is decided by the chunk after which no sample can reach its window, so it leaves with /// that chunk: the outer point at 5 ms is settled by the inner chunk covering position 0, and it /// must not wait for the chunk that follows it. @@ -2643,36 +2551,10 @@ fn a_map_chain_yielding_no_value_keeps_progress_and_the_closing_chunk() { ); } -/// The chunks of one series are handed over once, in order: a chunk starting behind the end of the -/// previous one is rejected, and the error names both positions. -#[test] -fn an_out_of_order_chunk_is_rejected() { - let mut inner = inner_layer(); - let schema = inner.output_schema().clone(); - let inner_chunks = collect_chunks(&mut inner, &consumer_chunks()); - inner.finish().expect("the inner series closed"); - - let mut consumer = evaluated_layer(&schema, grid(0, 10, 5), 10); - // The first two chunks cover 0..1 and 1..3, so the series has reached position 3. - for chunk in &inner_chunks[..2] { - consumer.push_evaluated(chunk).unwrap(); - } - let overlapping = evaluated_chunk(&schema, 2, vec![Some(1.0)], false); - let error = consumer.push_evaluated(&overlapping).unwrap_err(); - assert!( - matches!(error, DataFusionError::Execution(_)), - "not an execution error: {error}" - ); - let message = error.to_string(); - assert!( - message.contains("2 does not continue from 3"), - "the message does not name both positions: {message}" - ); -} - -/// A producer that trims its output starts at its first row holding a value. The consumer accepts -/// that series and reports the positions before it as decided, so the undelivered prefix is not -/// left waiting for input. +/// A position no chunk covers reads exactly like one whose chunk carries a missing value: both say +/// there is no sample there, so a window over either takes no sample. The consumer does not ask +/// which of the two it was given, and reports the position as decided either way rather than +/// leaving it waiting for input. #[test] fn a_series_may_begin_past_the_first_position_of_its_grid() { let grid = grid(0, 10, 4); @@ -2869,95 +2751,6 @@ mod composition { } } - /// An empty map chain is the identity, and it has to be the identity on every chunk of one - /// series: the valued chunk and the zero-length closing one must carry the same labels, or - /// the consumer of the series rejects it as two. - /// - /// `last_over_time` keeps `__name__` -- it is one of the two range functions that do -- so this - /// series carries the metadata label a map chain rewrites, and the identity below is not - /// vacuous. Before `map_chunk` treated a chain without kernels as the identity it dropped the - /// metadata labels from the zero-length chunk alone, so the chunk that closed the series was - /// labelled differently from the chunk before it. - /// - /// No plan reaches this state: a `Map` is only built around at least one kernel, and - /// `map_chunk` is the only caller-side contract here. The empty slice is therefore pinned as - /// what `map_chunk` documents it to be -- the identity -- rather than as a case a planner - /// produces. Rejecting the empty slice instead would drop this test and nothing else. - #[test] - fn an_empty_map_chain_keeps_the_chunk_identity() { - let window = RangeWindow { - range_ms: 10, - offset_ms: 0, - anchor_ms: None, - }; - let inner_grid = grid(0, 10, 2); - let mut inner = RangeEvaluator::try_new( - &raw_schema(1), - inner_grid.clone(), - window, - RangeFunction::LastOverTime, - ) - .unwrap(); - let schema = inner.output_schema().clone(); - - // The sample at 10 ms settles both points of the two-point grid, and the point at 10 ms - // reads it, so this push hands out the whole grid with a value in it. - let first = chunk(&[(10, vec![Some(2.0)])], 1, 0, false); - let valued = inner - .push(&first) - .unwrap() - .expect("the windows of both points are complete"); - assert!(!valued.is_last); - assert_eq!( - (valued.range().start(), valued.range().len()), - (0, inner_grid.timestamps().len()), - "the fixture does not decide the whole grid before the last chunk" - ); - - // The last sample lies past every window, so the closing chunk decides nothing new and - // reaches a consumer with no positions at all. - let last = chunk(&[(100, vec![Some(3.0)])], 1, 0, true); - let closing = inner.push(&last).unwrap().expect("a series closes"); - assert!(closing.is_last); - assert!( - closing.range().is_empty(), - "the closing chunk decided a position: {} values", - closing.range().len() - ); - inner.finish().expect("the inner series closed"); - - // Without a metadata label in the fixture both assertions below would hold for free. - assert_eq!(valued.series.metric.name(), Some("http_requests")); - assert_eq!(closing.series.metric.name(), Some("http_requests")); - - // An empty chain maps nothing, so each chunk comes back exactly as it went in. - let mapped_valued = map_chunk(&schema, &valued, &[]).unwrap(); - assert_eq!( - mapped_valued.series.metric, valued.series.metric, - "an empty chain rewrote the valued chunk's labels" - ); - let mapped_closing = map_chunk(&schema, &closing, &[]).unwrap(); - assert_eq!( - mapped_closing.series.metric, closing.series.metric, - "an empty chain rewrote the closing chunk's labels" - ); - - // Both chunks are one series to the consumer: the valued one opens it, the closing one is - // accepted after it, and the series ends. - let mut downstream = - RangeEvaluator::try_new(&schema, inner_grid, window, RangeFunction::LastOverTime) - .unwrap(); - downstream - .push_evaluated(&mapped_valued) - .expect("the valued chunk opens the series"); - let closed = downstream - .push_evaluated(&mapped_closing) - .expect("the closing chunk carries the labels of the chunk before it") - .expect("a series closes"); - assert!(closed.is_last); - downstream.finish().expect("the series closed"); - } - /// A chain with a kernel rewrites the labels of every chunk it maps, and it has to rewrite them /// the same way on the zero-length closing chunk as on the chunk before it. /// @@ -3179,14 +2972,6 @@ mod composition { "the chunk starts {starts:?} of the {layer} layer do not increase" ); } - for chunk in &pipeline.outer_chunks { - for ×tamp in &chunk.range().timestamps() { - assert!( - !inner_grid.timestamps().contains(×tamp), - "the outer layer used the inner timestamp {timestamp}" - ); - } - } pipeline.finish(); } }