fix(promql): refuse a chunk reaching backwards, accept one that skips ahead

Two different kinds of non-adjacency were treated as one. A chunk that
starts *past* the end of the previous one leaves positions no chunk covers,
and those hold no sample -- an ordinary state, identical to a chunk carrying
a missing value, which `a_series_may_begin_past_the_first_position_of_its_grid`
already asserts. A chunk that starts *behind* it is not the same: samples of
one series are stored in arrival order and read back by binary search
(`evaluate` uses `partition_point`), so an out-of-order one makes the search
stop at the wrong sample and report a wrong value with no error to show for it.

The check now rejects only `range.start() < reached_end`, before anything is
stored. `SeriesState::input_end` becomes `reached_end`, advanced with `max` so
a skipped span can still be covered by a later chunk.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-20 00:37:28 +08:00
parent e8d7c06a02
commit a349e745f6
2 changed files with 110 additions and 4 deletions
+30 -4
View File
@@ -264,6 +264,13 @@ struct SeriesState {
primary_key: Option<Buffer>,
metric: MetricName,
last_ts: i64,
/// Grid index one past the last position any chunk of this series covered.
///
/// A later chunk must not start behind it: the samples of one series are stored in the order
/// they arrive and read back by binary search, so a chunk reaching backwards would store
/// timestamps out of order and the search would stop at the wrong one -- a wrong value, with no
/// error. A chunk starting *past* it is fine: the positions it jumps over hold no sample.
reached_end: usize,
/// Next grid index to evaluate.
cursor: usize,
/// Grid index after the last position the evaluator has decided, whether it holds a value or
@@ -574,7 +581,9 @@ impl RangeEvaluator {
/// The first chunk opens the series with its labels and the one marked `is_last` closes it. The
/// chunks need not tile the input grid: a position no chunk covers holds no sample, which is
/// the same state as a position whose chunk carries a missing value, so the positions a chunk
/// jumps over are simply reported as decided without a sample.
/// jumps over are simply reported as decided without a sample. They may not reach *backwards*,
/// though: this series' samples are stored in arrival order and read back by binary search, so
/// a chunk starting behind where the previous one ended is refused.
///
/// A chunk reports the positions it decided, whether they hold a value or not, so a position
/// an inner evaluation settled without a result crosses this layer too; as on the raw path,
@@ -623,18 +632,34 @@ impl RangeEvaluator {
primary_key: None,
metric: chunk.series.metric.clone(),
last_ts: 0,
reached_end: 0,
cursor: 0,
completed: 0,
emitted_end: 0,
});
}
// The first position this chunk does not carry. Chunks tile the input grid and samples sit
// on its timestamps, so the next sample can only arrive there.
// The first position this chunk does not carry. Samples sit on grid timestamps, so no
// sample can arrive before it -- whether or not a later chunk ever covers it.
let input_end = range.start() + range.len();
// Before anything is stored: a chunk reaching backwards would leave this series' samples
// out of order, and `evaluate` reads them back by binary search.
if let Some(state) = &self.series
&& range.start() < state.reached_end
{
return Err(DataFusionError::Execution(format!(
"evaluated chunks are out of order: {} starts behind {}",
range.start(),
state.reached_end
)));
}
self.ingest(&range.timestamps(), &chunk.series.fields, false)?;
// Advanced only now: `ingest` may fail on a sample it cannot shift or convert.
if let Some(state) = self.series.as_mut() {
state.reached_end = state.reached_end.max(input_end);
}
if !chunk.is_last {
// A point is decided once no sample that can still arrive falls in its window, and the
// next sample can only sit at `grid[input_end]`. `ready` is inclusive and in the
// next sample cannot arrive before `grid[input_end]`. `ready` is inclusive and in the
// shifted sample domain, so it is that boundary shifted by the offset, minus one: a
// point at the boundary itself still reads a sample arriving there. At the end of the
// input grid no sample can follow at all, which is what `None` means.
@@ -749,6 +774,7 @@ impl RangeEvaluator {
primary_key: Some(chunk.primary_key.clone()),
metric,
last_ts: time[time.len() - 1],
reached_end: 0,
cursor: 0,
completed: 0,
emitted_end: 0,
+80
View File
@@ -2551,6 +2551,86 @@ fn a_map_chain_yielding_no_value_keeps_progress_and_the_closing_chunk() {
);
}
/// A chunk reaching backwards is refused before anything is stored: the samples of one series are
/// kept in arrival order and read back by binary search, so an out-of-order one would make that
/// search stop at the wrong sample and report a wrong value with no error to show for it. A chunk
/// starting *past* the end is a different matter and is accepted -- see
/// [`a_series_may_begin_past_the_first_position_of_its_grid`], and the same holds mid-series.
#[test]
fn a_chunk_reaching_backwards_is_refused() {
let grid = grid(0, 10, 5);
let schema = Arc::new(SeriesSchema {
fields: raw_schema(1).fields,
eval_grid: Some(grid.clone()),
});
let mut consumer = RangeEvaluator::try_new(
&schema,
grid.clone(),
last_window(10),
RangeFunction::LastOverTime,
)
.unwrap();
consumer
.push_evaluated(&evaluated_chunk(
&schema,
0,
vec![Some(1.0), Some(2.0), Some(3.0)],
false,
))
.expect("the first chunk opens the series");
// Starts at 2, behind the position 3 the series reached.
let backwards = evaluated_chunk(&schema, 2, vec![Some(9.0)], true);
let error = consumer
.push_evaluated(&backwards)
.expect_err("a chunk behind the reached position is out of order");
assert!(
matches!(error, DataFusionError::Execution(_)),
"not an execution error: {error}"
);
let message = error.to_string();
assert!(
message.contains("2 starts behind 3"),
"the message does not name both positions: {message}"
);
}
/// A gap in the middle of a series is accepted, exactly like one at the head: the positions it
/// jumps over hold no sample, and a window over them takes none.
#[test]
fn a_gap_in_the_middle_of_a_series_is_accepted() {
let grid = grid(0, 10, 5);
let schema = Arc::new(SeriesSchema {
fields: raw_schema(1).fields,
eval_grid: Some(grid.clone()),
});
let mut consumer = RangeEvaluator::try_new(
&schema,
grid.clone(),
last_window(10),
RangeFunction::LastOverTime,
)
.unwrap();
let mut handed_out = consumer
.push_evaluated(&evaluated_chunk(
&schema,
0,
vec![Some(1.0), Some(2.0)],
false,
))
.unwrap();
assert!(handed_out.is_some(), "the first chunk decides a point");
// Jumps over positions 2 and 3.
handed_out = consumer
.push_evaluated(&evaluated_chunk(&schema, 4, vec![Some(9.0)], true))
.expect("a gap holds no sample, so the chunk is not out of order");
assert!(
handed_out.is_some_and(|chunk| chunk.is_last),
"the closing chunk is lost"
);
consumer.finish().expect("the series closed");
}
/// A position no chunk covers reads exactly like one whose chunk carries a missing value: both say
/// there is no sample there, so a window over either takes no sample. The consumer does not ask
/// which of the two it was given, and reports the position as decided either way rather than