fix(promql): drop the start timestamp a computed histogram never had

A window holding one histogram became the result as it arrived, while two or more
went through an add that clears the timestamp, so whether a summed histogram carried
the payload said how many samples the window held rather than what the function did.
Clear it where the fold returns, leaving the bucket layout to the caller.

Also correct two comments that described behaviour this branch does not have: the
selection kernel carries no point limit of its own, and the interleaved map sits
above the selection rather than between it and the input.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-21 21:23:20 +08:00
parent b49bbc0d16
commit bbd740a685
5 changed files with 72 additions and 30 deletions
+8 -1
View File
@@ -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<Item = Result<FloatHistogram>>,
@@ -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))
}
+3 -3
View File
@@ -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.
+10 -8
View File
@@ -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);
+51
View File
@@ -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
-18
View File
@@ -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.