diff --git a/src/promql/src/evaluated.rs b/src/promql/src/evaluated.rs index af5c7d1bd34..5ff8c666123 100644 --- a/src/promql/src/evaluated.rs +++ b/src/promql/src/evaluated.rs @@ -20,13 +20,18 @@ use datatypes::series::time::{GridRange, TimeAxis}; /// One slice of one evaluated series. /// -/// The series' [`TimeAxis::Grid`] covers the positions this chunk has finished evaluating. Those -/// positions are absolute on the grid: a consumer that concatenates the chunks of a series never -/// reorders them, and never has to translate a chunk-local row number into a grid position. +/// The series' [`TimeAxis::Grid`] covers the positions this chunk finished evaluating and found a +/// value in, from the first such position to the last. Those positions are absolute on the grid: a +/// consumer that concatenates the chunks of a series never reorders them, and never has to +/// translate a chunk-local row number into a grid position. /// -/// A series closes exactly once, on the chunk that carries [`EvaluatedChunk::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 at these positions" from "no more input". +/// The range is an envelope, not a census: a position inside it whose window has no result is a +/// missing value, and the positions after the range either hold no value in this chunk or have not +/// been evaluated yet. Only [`EvaluatedChunk::is_last`] tells those two apart. +/// +/// 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". #[derive(Debug)] pub struct EvaluatedChunk { /// The evaluated values, addressed on their absolute grid positions. @@ -48,9 +53,9 @@ impl EvaluatedChunk { /// The absolute grid range this chunk covers. /// - /// Ranges of one series increase and never overlap. An empty range is meaningful: it reports - /// that the chunk closed a prefix of the grid without producing new values, which a consumer - /// needs in order to advance. + /// Ranges of one series never go backwards and never overlap. An empty range is meaningful: it + /// reports that the chunk closed a prefix of the grid without producing new values, and its + /// position is where that prefix ended, so a consumer can still tell how far the series got. pub fn range(&self) -> &GridRange { match &self.series.time { TimeAxis::Grid(range) => range, diff --git a/src/promql/src/range_eval.rs b/src/promql/src/range_eval.rs index f72e7344331..e1d054c244f 100644 --- a/src/promql/src/range_eval.rs +++ b/src/promql/src/range_eval.rs @@ -537,11 +537,12 @@ impl RangeEvaluator { /// /// Returns the values of the grid points this chunk finished evaluating, as one chunk whose /// range is absolute on the evaluation grid; `None` when it finished no point holding a value. - /// A grid point whose window has no result is simply absent from the range, as it is from a - /// whole series. + /// The range spans the first and last such point, so a point between them whose window has no + /// result is a missing value, as it is in a whole series. /// /// The last chunk of a series is always returned, empty when it finished none, because that - /// chunk is what closes the series for the consumer. + /// chunk is what closes the series for the consumer. The empty range is addressed where the + /// series got to, so a consumer can still tell how far it advanced. /// /// The evaluator must not be reused after an error. pub fn push(&mut self, chunk: &RawSeriesChunk) -> Result> { diff --git a/src/promql/src/range_eval/tests.rs b/src/promql/src/range_eval/tests.rs index d6ef5827d50..c36fab758fb 100644 --- a/src/promql/src/range_eval/tests.rs +++ b/src/promql/src/range_eval/tests.rs @@ -221,7 +221,12 @@ fn collect_chunks( } /// Folds the chunks of one series back into one series, the way a consumer that keeps every value -/// would, and checks the chunk protocol: absolute increasing ranges, and one closing chunk last. +/// would, and checks the chunk protocol: absolute ranges, never going backwards, all of the same +/// series, and one closing chunk last. +/// +/// The range check is explicit rather than left to the builder, whose own ordering check only sees +/// the rows holding a value: a closing chunk with no rows must still start at or after the end of +/// the previous chunk, and an empty chunk at a wrong position is exactly what would pass otherwise. fn fold_chunks(chunks: &[EvaluatedChunk], schema: &Arc) -> Option { let metric = chunks .first() @@ -231,10 +236,18 @@ fn fold_chunks(chunks: &[EvaluatedChunk], schema: &Arc) -> Option< .clone(); let mut builder = GridSeriesBuilder::try_new(schema.clone()).unwrap(); let mut closing = false; + let mut previous_end = 0; for chunk in chunks { assert!(!closing, "a chunk follows the closing chunk"); closing = chunk.is_last; + assert_eq!(chunk.series.metric, metric, "a chunk changed the series"); let range = chunk.range(); + assert!( + range.start() >= previous_end, + "chunk range {} goes back before the previous end {previous_end}", + range.start() + ); + previous_end = range.start() + range.len(); let mut fields: Vec<_> = chunk .series .fields @@ -864,6 +877,35 @@ fn a_series_that_ends_early_still_closes() { assert_eq!(closed.range().start(), 2); } +/// The closing chunk of a series is labelled like the chunks before it, including the metadata +/// labels a function drops: `rate` removes `__name__`, and a consumer that keyed series by label +/// set would see the same series split in two if the empty chunk kept it. +#[test] +fn an_empty_closing_chunk_is_labelled_like_the_chunks_before_it() { + let window = RangeWindow { + range_ms: 20, + offset_ms: 0, + anchor_ms: None, + }; + let mut evaluator = + RangeEvaluator::try_new(&raw_schema(1), grid(0, 10, 6), window, RangeFunction::Rate) + .unwrap(); + let first = chunk(&[(10, vec![Some(1.0)]), (20, vec![Some(3.0)])], 1, 0, false); + let handed = evaluator + .push(&first) + .unwrap() + .expect("the windows of the first point are complete"); + assert!(!handed.is_last); + assert_eq!(handed.series.metric.name(), None); + + let last = chunk(&[(45, vec![Some(5.0)])], 1, 0, true); + let closed = evaluator.push(&last).unwrap().expect("a series closes"); + assert!(closed.is_last); + assert!(closed.range().is_empty()); + assert_eq!(closed.series.metric, handed.series.metric); + assert_eq!(closed.series.metric.name(), None); +} + #[test] fn rejects_invalid_parameters() { let zero_range = RangeWindow {