diff --git a/src/promql/src/histogram_eval/aggregate.rs b/src/promql/src/histogram_eval/aggregate.rs index 9e1721c8b59..0ffcca5e8e8 100644 --- a/src/promql/src/histogram_eval/aggregate.rs +++ b/src/promql/src/histogram_eval/aggregate.rs @@ -156,7 +156,8 @@ fn aggregate_point<'a>( /// The Kahan sum or the mean of `histograms` in order, without compaction; `None` when there is /// none or they mix exponential and custom buckets. Prometheus `sum_over_time` and /// `avg_over_time` run the same steps as the aggregations. Histograms are consumed one at a -/// time, so the first error ends the fold. +/// time, so the first error ends the fold. The result is derived, so it carries no start +/// timestamp. pub(crate) fn sum_or_avg( op: HistogramAggregate, histograms: impl IntoIterator>, @@ -236,5 +237,11 @@ pub(crate) fn sum_or_avg( } }, } + // The result was derived from its inputs, so it carries no start timestamp of its own, like + // every other operation that builds one. Clearing it here covers the fold that added nothing: + // the first histogram of a window becomes the result as it arrived, and the adds above are + // what drop the field otherwise, which would make the payload depend on the sample count. + // The bucket layout is left as the fold built it -- compaction belongs to the caller. + value.start_timestamp = None; Ok(Some(value)) } diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 332e6c7e91e..84bc07aa752 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -987,9 +987,9 @@ impl InstantRuntime { /// refuses the gather. The clipping below is [`select_instants`]' own, repeated rather than /// approximated: an estimate that disagrees with the kernel is a bug here rather than a /// conservative bound. It is the kernel's own candidate count -- the one it sizes its arrays - /// with and refuses against a point limit -- so it bounds the rows it can report even though a - /// candidate at the lookback edge itself selects nothing. Both ends come from chunks the node - /// already holds, so the count reads two timestamps rather than the rows. + /// with -- so it bounds the rows it can report even though a candidate at the lookback edge + /// itself selects nothing. Both ends come from chunks the node already holds, so the count + /// reads two timestamps rather than the rows. fn selected_points(&self, closing: &RawSeriesChunk) -> usize { let spec = self.selection.output_grid.spec(); // The selection refuses these itself, before it can decide anything. diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index d027d93f2da..67e9f7466be 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -2600,8 +2600,8 @@ fn an_interleaved_input_is_refused_without_a_window() { /// never reaches [`NativeExecutionState::try_new`]. The ordering check there stays for the shape /// the binding cannot see through: it only looks at a window's own input, so a window over a map /// over the source still reads an interleaved stream, which is -/// [`a_window_over_an_interleaved_input_is_rejected`]. The shape with a map between the input and -/// the selection is [`an_interleaved_input_is_refused_under_a_map_over_a_selection`]. +/// [`a_window_over_an_interleaved_input_is_rejected`]. The shape with a map above the selection is +/// [`an_interleaved_input_is_refused_under_a_map_over_a_selection`]. #[test] fn an_interleaved_input_is_refused_under_a_selection() { let out_grid = grid(0, 60_000, 2); @@ -2623,13 +2623,15 @@ fn an_interleaved_input_is_refused_under_a_selection() { ); } -/// The same refusal with a map between the input and the selection. +/// The same refusal with a map above the selection. /// -/// A map passes chunks through in the order they arrive, so the selection still reads an -/// interleaved stream even though the node below it is a map: the ordering is a property of the -/// input, and the binding records it there rather than at the node that reads it. `validate` -/// descends through the map to the selection, whose own arm then checks the input it reads, so the -/// tree is refused at the same point as [`an_interleaved_input_is_refused_under_a_selection`]. +/// The tree here is `Input -> Instant -> Map`: the map sits above the selection, and the selection +/// reads the interleaved input directly. A map between the input and the selection is not a tree +/// this compiler can build at all -- a `Map` produces an evaluated vector while an instant +/// selection requires raw samples, and the plan's `Instant` arm rejects evaluated values -- so what +/// this test pins is the recursion: `validate` descends through the outer map to the selection +/// below it, whose own arm then refuses the input it reads, and the tree is refused at the same +/// point as [`an_interleaved_input_is_refused_under_a_selection`]. #[test] fn an_interleaved_input_is_refused_under_a_map_over_a_selection() { let out_grid = grid(0, 60_000, 2); diff --git a/src/promql/src/range_eval/tests.rs b/src/promql/src/range_eval/tests.rs index 5b0f72d40c0..56c830841a0 100644 --- a/src/promql/src/range_eval/tests.rs +++ b/src/promql/src/range_eval/tests.rs @@ -1133,6 +1133,57 @@ fn a_selected_histogram_keeps_its_start_timestamp() { ); } +/// A histogram computed from a window carries no start timestamp, even when the window holds one. +/// +/// `sum_over_time` computes a histogram rather than selecting one, so the timestamp the sample was +/// read with is not part of the result -- the rule the histogram operations themselves follow, as +/// `histogram_eval::tests::a_start_timestamp_belongs_to_a_read_histogram_and_not_to_a_computed_one` +/// pins. The fold clears the field while adding, so a window holding two histograms already loses +/// it; a window holding one must not depend on that, or whether a result carries the field would +/// say how many samples the window held rather than what the function did. +#[test] +fn a_computed_histogram_drops_its_start_timestamp() { + let schema = raw_schema(1); + let start: i64 = 1_500; + // One histogram sample inside the window of both grid points, so the fold adds nothing to it + // and the single input is the whole result. + let chunks = [histogram_chunk_from(&schema, 1, 0, Some(start), true)]; + let mut evaluator = RangeEvaluator::try_new( + &schema, + grid(0, 10_000, 2), + RangeWindow { + range_ms: 20_000, + offset_ms: 0, + anchor_ms: None, + }, + RangeFunction::SumOverTime, + ) + .unwrap(); + let out = collect_chunks(&mut evaluator, &chunks); + let mut computed = Vec::new(); + for chunk in &out { + let mut cursor = chunk.series.fields.as_slice()[0].cursor(); + for (position, row) in chunk.series.time.positions() { + match cursor.at(row).expect("the axis and the fields are aligned") { + SampleRef::Histogram(view) => { + computed.push((position, view.count, view.start_timestamp)); + } + SampleRef::Float(_) => panic!("sum_over_time answered a float for a histogram"), + SampleRef::Missing => { + panic!("sum_over_time handed out a row of its own axis without a sample") + } + } + } + } + // The sum of one histogram is that histogram, so the count says the sample was summed rather + // than passed over; only the start timestamp has to be gone. + assert_eq!( + computed, + [(0, 1.0, None), (1, 1.0, None)], + "sum_over_time kept the start timestamp of the histogram it summed" + ); +} + /// A window retaining a native histogram is charged for the payload it owns, not only the slot. /// /// The histogram sample below stays ahead of the second grid point's window, so the evaluator keeps diff --git a/src/promql/src/vector_match/tests.rs b/src/promql/src/vector_match/tests.rs index adb65beac38..9b830462c8b 100644 --- a/src/promql/src/vector_match/tests.rs +++ b/src/promql/src/vector_match/tests.rs @@ -1592,24 +1592,6 @@ fn a_histogram_comparison_filter_keeps_the_payload_of_the_operand_it_keeps() { "`!=` did not hand back the left histogram it kept: {ne:?}" ); - // The right operand's start timestamp is never what comes out, at any point of either - // comparison. - for series in eq.iter().chain(&ne) { - let mut cursor = series.fields.as_slice()[0].cursor(); - for (point, row) in series.time.positions() { - let SampleRef::Histogram(view) = - cursor.at(row).expect("the axis and the fields are aligned") - else { - panic!("a comparison handed out a sample that is not a histogram"); - }; - assert_ne!( - view.start_timestamp, - Some(RIGHT_START), - "the right operand's start timestamp came out at {point}: {series:?}" - ); - } - } - // A `bool` comparison answers in floats, so nothing it hands out is a histogram and no start // timestamp leaves it: `job=eq` holds equal histograms, so it answers 1, and `job=ne` differs // in count and sum, so it answers 0.