diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 334dba01c49..3c06d24eeb1 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -319,6 +319,13 @@ impl Emitted { .iter() .map(|field| occupied_runs(field, base)) .collect(); + // A series that produced no value at all owns nothing here: it hands out no sample under + // this label set, so recording it would refuse the next series that produces none either, + // and the answer to both is simply empty. Only a label set already holding values has to + // be marked closed when its last chunk arrives. + if occupied.iter().all(Vec::is_empty) && !self.sets.contains_key(&labels) { + return Ok(()); + } if let Some(history) = self.sets.get(&labels) { if history.closed { return Err(closed_label_set(path, &labels)); diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index f0278cc179b..53c4a4cbd12 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -1622,3 +1622,51 @@ fn the_emitted_charge_is_the_same_folded_in_as_walked() { ); } } + +/// Two series that each produce no value at all are not a collision: both answer empty, and the +/// node hands nothing on rather than refusing the second one. +/// +/// `rate` over a five-minute window with one sample in it has no result, in the single-point and +/// the batch kernel alike. Both series then arrive as a closing chunk of missing values under the +/// same label set -- `rate` drops the name -- but neither holds a sample, so there is nothing to +/// report twice and nothing to merge. +#[test] +fn two_series_with_no_result_do_not_collide() { + let out = grid(0, 60_000, 1); + let tree = NativeTree::try_new(window_with( + input(), + RangeFunction::Rate, + 300_000, + None, + Arc::clone(&out), + )) + .unwrap(); + let schema = raw_schema(); + let mut execution = executor(&tree, pool(), &ExecutionPlanMetricsSet::new()); + + // One sample each: rate over a 5m window with a single sample has no result. + for name in ["a", "b"] { + execution + .push_raw(raw_chunk_of( + &schema, + name.as_bytes(), + MetricName::try_new(Some(name.into()), vec![]).unwrap(), + &[0], + &[1.0], + true, + )) + .unwrap(); + } + let mut handed = Vec::new(); + loop { + match execution.next_chunk() { + Ok(Some(chunk)) => handed.push(decided(&[chunk])), + Ok(None) => break, + Err(error) => panic!("two empty series were refused: {error}"), + } + } + assert!( + handed.iter().flatten().all(|(_, value)| value.is_none()), + "a series with no result handed a value on: {handed:?}" + ); +}