From d808ae66cbb551abfc2aaf402bca0e972f448b78 Mon Sep 17 00:00:00 2001 From: discord9 <55937128+discord9@users.noreply.github.com> Date: Mon, 19 Oct 2026 18:42:49 +0800 Subject: [PATCH] fix(promql): relabel the closing chunk the way the kernels relabel the one before it A map chain with at least one kernel rewrites the labels of every chunk it maps, and the zero-length chunk that closes a series is one of them. The kernels drop the metadata labels and, like Prometheus' simpleFloatFunc, every label whose value is empty -- an ordinary label such as `empty=""` goes too. The closing chunk took a separate branch that removed only the metadata labels, so it kept `empty=""` while the chunk before it lost it. A consumer of the series then read the two chunks as two series and rejected the second as a change of label set before the last chunk. Both paths now go through `drop_metric_name`, which is what the kernels already use, so a chunk without positions and a chunk whose values were all trimmed away are relabelled exactly like a chunk that was mapped. Signed-off-by: discord9 Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --- src/promql/src/map.rs | 15 +++--- src/promql/src/range_eval/tests.rs | 82 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/promql/src/map.rs b/src/promql/src/map.rs index 87523a86176..a63e396f869 100644 --- a/src/promql/src/map.rs +++ b/src/promql/src/map.rs @@ -39,7 +39,7 @@ use datatypes::series::time::{GridRange, TimeAxis}; use crate::evaluated::EvaluatedChunk; use crate::functions::Round; -use crate::grid_series::{GridSeriesBuilder, METADATA_LABELS}; +use crate::grid_series::GridSeriesBuilder; /// Float math supported by the evaluated-series kernel. #[derive(Clone, Copy, Debug)] @@ -142,11 +142,11 @@ pub fn map_math(schema: &SeriesSchema, series: &Series, op: MathOp) -> Result>, is_last: bool| { + let range = GridRange::try_new(grid.clone(), start, values.len()).unwrap(); + let fields = + SeriesFields::try_from_vec(vec![Samples::from_float(Float64Array::from(values))]) + .unwrap(); + let series = + Series::try_new(&schema, labelled.clone(), TimeAxis::Grid(range), fields).unwrap(); + EvaluatedChunk::try_new(series, is_last).unwrap() + }; + + // The valued chunk fills the whole grid, so the closing one decides nothing new and reaches + // a consumer with no positions at all. + let valued = with_metric(0, vec![Some(-1.0), Some(-2.0), Some(-3.0)], false); + let closing = with_metric(3, vec![], true); + + let ops = [MathOp::Abs]; + let mapped_valued = map_chunk(&schema, &valued, &ops).unwrap(); + let mapped_closing = map_chunk(&schema, &closing, &ops).unwrap(); + + let names: Vec<_> = mapped_valued + .series + .metric + .tags() + .iter() + .map(|label| label.name.to_string()) + .collect(); + assert_eq!( + names, + vec!["host".to_string()], + "the kernel did not drop the name, the metadata labels and the empty-valued label" + ); + assert_eq!( + mapped_closing.series.metric, mapped_valued.series.metric, + "the closing chunk carries labels the chunk before it does not" + ); + + // Both chunks are one series to the consumer. + let mut downstream = outer_layer(&schema); + downstream + .push_evaluated(&mapped_valued) + .expect("the valued chunk opens the series"); + downstream + .push_evaluated(&mapped_closing) + .expect("the closing chunk carries the labels of the chunk before it") + .expect("a series closes"); + downstream.finish().expect("the series closed"); + } + /// A prefix reaches the outer layer before the inner series ends: the composed pipeline streams /// instead of buffering a whole series behind the map chain. #[test]