test(promql): check the label and position of the chunk that closes a series

The fold helper checked the ordering of a chunk only through the rows holding a
value, which the grid builder sees. A closing chunk has no rows, so an empty
chunk at a wrong position passed. It now compares every chunk's range against
the end of the previous one directly, and compares every chunk's metric, since
a consumer keys series by their labels and a later chunk with different ones
would look like a different series.

`an_empty_closing_chunk_is_labelled_like_the_chunks_before_it` covers the label
half for `rate`, which drops `__name__`: this also fails if the empty closing
chunk stops applying the metadata removal.

Also corrects the docs: the range spans the first and last position holding a
value, so it is an envelope that may contain missing values, not a report of
every position evaluated. `is_last`, not the empty endpoint, says whether the
positions after the range are missing or not evaluated yet.

Signed-off-by: discord9 <discord9@users.noreply.github.com>
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-19 20:03:38 +08:00
parent 67bf30e6c7
commit 6be213b836
3 changed files with 61 additions and 13 deletions
+14 -9
View File
@@ -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,
+4 -3
View File
@@ -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<Option<EvaluatedChunk>> {
+43 -1
View File
@@ -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<SeriesSchema>) -> Option<Series> {
let metric = chunks
.first()
@@ -231,10 +236,18 @@ fn fold_chunks(chunks: &[EvaluatedChunk], schema: &Arc<SeriesSchema>) -> 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 {