diff --git a/src/common/query/src/native_histogram.rs b/src/common/query/src/native_histogram.rs index 05527b02cc8..9aadc8d71e4 100644 --- a/src/common/query/src/native_histogram.rs +++ b/src/common/query/src/native_histogram.rs @@ -597,28 +597,14 @@ impl NativeHistogram { } } - fn detect_start_timestamp_reset( - &self, - previous: &Self, - previous_ts: i64, - current_ts: i64, - ) -> bool { - let current_start = self.start_timestamp.unwrap_or_default(); - if current_start == 0 || current_start >= current_ts || current_start < previous_ts { - return false; - } - if current_start > previous_ts { - return true; - } - - let previous_start = previous.start_timestamp.unwrap_or_default(); - previous_start <= previous_ts && previous_start != 0 && previous_start != previous_ts - } - /// Detects a counter reset using start timestamps, hints, and payload values. pub fn detect_counter_reset(&self, previous: &Self, previous_ts: i64, current_ts: i64) -> bool { - self.detect_start_timestamp_reset(previous, previous_ts, current_ts) - || self.detect_reset(previous) + is_start_timestamp_reset( + previous.start_timestamp.unwrap_or_default(), + previous_ts, + self.start_timestamp.unwrap_or_default(), + current_ts, + ) || self.detect_reset(previous) } fn all_buckets(&self) -> Option> { @@ -832,6 +818,32 @@ impl NativeHistogram { } } +/// Whether a pair of start timestamps witnesses a counter reset, as Prometheus +/// `isStartTimestampReset` decides it. A start timestamp of `0` means none was stated. +pub fn is_start_timestamp_reset( + previous_start: i64, + previous_ts: i64, + current_start: i64, + current_ts: i64, +) -> bool { + // An unchanged start never restarted, even when it is later than the previous sample. + if previous_start == current_start + || current_start == 0 + || current_start >= current_ts + || current_start < previous_ts + { + return false; + } + if current_start > previous_ts { + return true; + } + + // The current start points at the previous sample: a reset for a delta series, but not for + // a cumulative one whose start is unknown, which is what an unset or invalid previous start + // (later than its own sample) says. + previous_start <= previous_ts && previous_start != 0 && previous_start != previous_ts +} + fn reconcile_exponential( left: &NativeHistogram, right: &NativeHistogram, diff --git a/src/promql/src/functions/native_histogram.rs b/src/promql/src/functions/native_histogram.rs index 45445ce3644..e75302b7df9 100644 --- a/src/promql/src/functions/native_histogram.rs +++ b/src/promql/src/functions/native_histogram.rs @@ -4672,6 +4672,26 @@ mod tests { assert_eq!(idelta.sum, 12.0); } + /// Same samples as the native route's `equal_histogram_start_timestamps_are_not_a_reset`: + /// both routes must agree that a shared start timestamp is no reset, and that a bucket drop + /// under that start still is one. + #[test] + fn equal_start_timestamps_are_not_a_reset() { + let mut earlier = sample_histogram(2.0, 2.0, vec![1.0, 1.0]); + earlier.start_timestamp = Some(1500); + let mut dropped = sample_histogram(1.0, 1.0, vec![1.0]); + dropped.start_timestamp = Some(1500); + for (later, expected) in [(earlier.clone(), 0.0), (dropped, 1.0)] { + assert_eq!( + run_float_range_udf( + NativeHistogramResets::scalar_udf(), + vec![Some(earlier.clone()), Some(later)], + ), + Some(expected) + ); + } + } + #[test] fn extrapolated_rate_uses_start_timestamp_synthetic_zero() { let mut single = sample_histogram(1.0, 1.0, vec![1.0]); diff --git a/src/promql/src/histogram_eval/float_histogram.rs b/src/promql/src/histogram_eval/float_histogram.rs index dae0c444d33..42ed96216e1 100644 --- a/src/promql/src/histogram_eval/float_histogram.rs +++ b/src/promql/src/histogram_eval/float_histogram.rs @@ -663,40 +663,6 @@ impl FloatHistogram { bound(idx, self.schema, &self.custom_values) } - /// The reset a pair of start timestamps witnesses, given the window timestamps they are - /// compared against. - /// - /// Separate from the payload so a caller whose window timestamps have been moved by an - /// `offset` can shift the stored timestamps onto the same axis without cloning either - /// histogram: `range_eval` keeps its samples at `timestamp + offset_ms` while the stored start - /// timestamp stays where the sender put it, and comparing across those two axes reports a - /// reset for a pair that never restarted. - pub(crate) fn start_timestamp_reset( - current_start: i64, - previous_start: i64, - previous_ts: i64, - current_ts: i64, - ) -> bool { - if current_start == 0 || current_start >= current_ts || current_start < previous_ts { - return false; - } - if current_start > previous_ts { - return true; - } - - // The previous sample's own start timestamp witnesses a restart between it and the one - // before, which the current sample inherits. Requiring it to be at or before `previous_ts` - // keeps a start later than its own sample — which no sender can have observed — from being - // read as one. - // - // What this does NOT implement is the rule that two adjacent samples sharing the same - // non-zero start timestamp never restarted between them: here only the previous sample's - // start is compared with its own time, so equal starts are not excluded. The existing - // planner's `detect_start_timestamp_reset` has the same gap, so the two routes agree, which - // is what this route owes it; closing the gap is a change to both. - previous_start <= previous_ts && previous_start != 0 && previous_start != previous_ts - } - /// Detects a counter reset for a window whose timestamps an `offset` has already moved. /// /// `range_eval` hands its windows at `timestamp + offset_ms` while a histogram's start @@ -717,10 +683,10 @@ impl FloatHistogram { current_ts: i64, offset_ms: i64, ) -> bool { - Self::start_timestamp_reset( - self.start_timestamp.unwrap_or_default(), + common_query::native_histogram::is_start_timestamp_reset( previous.start_timestamp.unwrap_or_default(), previous_ts.wrapping_sub(offset_ms), + self.start_timestamp.unwrap_or_default(), current_ts.wrapping_sub(offset_ms), ) || self.detect_reset(previous).unwrap_or(false) } diff --git a/src/promql/src/range_eval/tests.rs b/src/promql/src/range_eval/tests.rs index de6d3fcf269..562b10b0a75 100644 --- a/src/promql/src/range_eval/tests.rs +++ b/src/promql/src/range_eval/tests.rs @@ -1397,6 +1397,44 @@ fn single_histogram_start_timestamp_is_compared_on_shifted_axis() { } } +/// Two samples sharing a start timestamp never restarted between them, even when that start is +/// later than the earlier sample: Prometheus `isStartTimestampReset` answers false on +/// `prevStartTimestamp == currStartTimestamp` before any other check (promql/functions.go:761). +/// That only removes the start-timestamp evidence: a bucket drop under the same start is still a +/// reset, because `resets()` ORs in `DetectReset`. +#[test] +fn equal_histogram_start_timestamps_are_not_a_reset() { + let schema = raw_schema(1); + for (later_buckets, expected) in [(2, 0.0), (1, 1.0)] { + let mut evaluator = RangeEvaluator::try_new( + &schema, + grid(2_000, 2_000, 1), + RangeWindow { + range_ms: 3_000, + offset_ms: 0, + anchor_ms: None, + modifier: None, + }, + RangeFunction::Resets, + ) + .unwrap(); + let chunks = collect_chunks( + &mut evaluator, + &[ + histogram_chunk_from(&schema, 2, 1_000, Some(1_500), false), + histogram_chunk_from(&schema, later_buckets, 2_000, Some(1_500), true), + ], + ); + let series = fold_chunks(&chunks, evaluator.output_schema()).unwrap(); + match series.fields.as_slice()[0].cursor().at(0).unwrap() { + SampleRef::Float(resets) => { + assert_eq!(resets, expected, "later sample has {later_buckets} buckets") + } + other => panic!("expected resets, got {}", describe_sample(Some(other))), + } + } +} + /// A first histogram whose count is negative is not extrapolated towards zero. /// /// The zero point is `first.count / increase`, and a negative one would pull the sampled start