mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 13:44:03 +00:00
fix(promql): settle a point as soon as no later sample can reach it
Chunks tile the input grid and samples sit on its timestamps, so the next sample one can carry is at the following grid position, not the last one already seen. The readiness bound used the last position, which held every point between the two until the chunk after it arrived: with an inner grid of 0/10 and an outer one of 5/20, the outer point at 5 was settled by the inner chunk covering position 0 and still waited for the next chunk, and a non-final chunk reaching the end of the input grid held the rest until the empty closing chunk. The bound is now the following position, shifted by the offset and inclusive, and no bound at all once the input grid is spent. A test that only passed because of the old lag is re-fixtured: its middle sample sat where a later sample was still possible, so nothing there was decided. The dead map branch is gone with it: `map_math` keeps the input's time axis, so a mapped series always covers exactly its chunk and the range-trimming path could not run. It mapped histograms to missing where the float path treats them as unreachable, so a future trimming kernel admitting histograms would have dropped them silently. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
+10
-46
@@ -142,9 +142,9 @@ pub fn map_math(schema: &SeriesSchema, series: &Series, op: MathOp) -> Result<Op
|
||||
/// positions the chunk decided still travel with it and its consumer still advances. Trimming the
|
||||
/// result to the rows holding a value, as the output does, is a separate stage.
|
||||
///
|
||||
/// At least one kernel rewrites the labels as [`map_math`] does -- dropping the metadata labels
|
||||
/// and every label whose value is empty -- even from a chunk without any value to map. A chain
|
||||
/// without kernels maps nothing, so it is the identity on the whole chunk and returns its labels
|
||||
/// A kernel rewrites the labels as [`map_math`] does -- dropping the metadata labels and every
|
||||
/// label whose value is empty -- even from a chunk without any value to map. A chain without
|
||||
/// kernels maps nothing, so it is the identity on the whole chunk and returns its labels
|
||||
/// unchanged: a consumer of the series reads the chunks of one series as one series only while
|
||||
/// they agree on them. Raw time axes are rejected.
|
||||
pub fn map_chunk(
|
||||
@@ -181,53 +181,17 @@ pub fn map_chunk(
|
||||
};
|
||||
mapped = map_math(schema, &series, *op)?;
|
||||
}
|
||||
if let Some(mapped) = &mapped
|
||||
&& let TimeAxis::Grid(mapped_range) = &mapped.time
|
||||
&& mapped_range == range
|
||||
{
|
||||
// The chain did not move the ends of the series, so it is already the chunk.
|
||||
return EvaluatedChunk::try_new(mapped.clone(), chunk.is_last);
|
||||
if let Some(mapped) = mapped {
|
||||
// `map_math` keeps the input's time axis, so the mapped series covers exactly the positions
|
||||
// this chunk covers: it is already the chunk, labels rewritten.
|
||||
return EvaluatedChunk::try_new(mapped, chunk.is_last);
|
||||
}
|
||||
|
||||
// The chain trimmed rows off one or both ends, or dropped every value. Place its rows back at
|
||||
// the offset they kept and hold the span open to the input chunk's ends.
|
||||
// The chain dropped every value, which is a property of the values and not of how far
|
||||
// evaluation has progressed: the positions this chunk decided still travel with it, so the
|
||||
// span is held open to its ends and every position in it is missing.
|
||||
let mut builder = GridSeriesBuilder::try_new(Arc::new(schema.clone()))?;
|
||||
builder.cover_from(range.start())?;
|
||||
if let Some(mapped) = &mapped {
|
||||
let TimeAxis::Grid(mapped_range) = &mapped.time else {
|
||||
return Err(DataFusionError::Execution(
|
||||
"Map math requires an evaluated grid time axis".into(),
|
||||
));
|
||||
};
|
||||
let offset = mapped_range
|
||||
.start()
|
||||
.checked_sub(range.start())
|
||||
.ok_or_else(|| {
|
||||
DataFusionError::Execution(format!(
|
||||
"mapped series begins at {} before its input chunk at {}",
|
||||
mapped_range.start(),
|
||||
range.start()
|
||||
))
|
||||
})?;
|
||||
// One cursor per field, for the reason `map_math` advances iterators rather than calling
|
||||
// `sample(row)`.
|
||||
let mut cursors = mapped
|
||||
.fields
|
||||
.iter()
|
||||
.map(|samples| samples.iter())
|
||||
.collect::<Vec<_>>();
|
||||
let mut values = vec![None; mapped.fields.len()];
|
||||
for row in 0..mapped_range.len() {
|
||||
for (value, cursor) in values.iter_mut().zip(cursors.iter_mut()) {
|
||||
*value = match cursor.next() {
|
||||
// A mapped field holds no histogram, so anything else stays missing.
|
||||
Some(SampleRef::Float(value)) => Some(value),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
builder.push(range.start() + offset + row, &values)?;
|
||||
}
|
||||
}
|
||||
builder.cover_through(range.start() + range.len())?;
|
||||
// `cover_from` opens the span for every non-empty range, and the empty one returned above, so
|
||||
// the builder always hands back the series it spans. Refuse to report a shorter range than the
|
||||
|
||||
@@ -579,7 +579,11 @@ impl RangeEvaluator {
|
||||
/// The first chunk opens the series with its labels, each later one starts exactly where the
|
||||
/// previous one ended, and the one marked `is_last` closes it. The first chunk may begin at any
|
||||
/// position of the input grid: a producer that trims its output starts at its first row
|
||||
/// holding a value, and the positions before it carry no sample rather than a missing one.
|
||||
/// holding a value, and the positions before it carry no sample rather than a missing one. A
|
||||
/// consumer therefore cannot tell a dropped leading chunk from a trimmed prefix -- both leave
|
||||
/// the same positions without a sample and are reported as decided. A gap after the first
|
||||
/// chunk is rejected instead, because there it would claim positions between two chunks of one
|
||||
/// series are settled while a sample may still arrive in them.
|
||||
///
|
||||
/// 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,
|
||||
@@ -622,6 +626,14 @@ impl RangeEvaluator {
|
||||
// row, and the positions before it carry no sample rather than a missing one. Reporting
|
||||
// them as decided is still correct, because no input ever arrives there, so a window over
|
||||
// them can never gain a sample.
|
||||
//
|
||||
// The concession is deliberately leading-only, and it costs the consumer no information:
|
||||
// `emit`, the only producer here, begins a series at position 0 and tiles every chunk after
|
||||
// it, so a leading gap can only mean an outside producer trimmed the series head -- a
|
||||
// dropped leading chunk and a trimmed prefix are the same thing to this layer, since both
|
||||
// leave the positions before the start without a sample. A gap in the middle is not the
|
||||
// same: the positions in it lie between two chunks of one series, so they would read as
|
||||
// decided-without-a-sample while a sample may still arrive there, and it is rejected.
|
||||
if let Some(state) = &self.series
|
||||
&& range.start() != state.input_end
|
||||
{
|
||||
@@ -649,18 +661,27 @@ impl RangeEvaluator {
|
||||
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.
|
||||
let input_end = range.start() + range.len();
|
||||
self.ingest(&range.timestamps(), &chunk.series.fields, false)?;
|
||||
// Advanced only now: `ingest` may fail on a sample it cannot shift or convert.
|
||||
self.series
|
||||
.as_mut()
|
||||
.expect("opened at the entry of this push")
|
||||
.input_end = range.start() + range.len();
|
||||
if let Some(state) = self.series.as_mut() {
|
||||
// Open, because the first chunk above opens the series and `ingest` never closes it.
|
||||
state.input_end = input_end;
|
||||
}
|
||||
if !chunk.is_last {
|
||||
// `ready` is in the shifted sample domain, so it is the last grid timestamp this
|
||||
// chunk carries plus the offset its samples were shifted by: every point up to there
|
||||
// can no longer gain a sample.
|
||||
let last = range.grid().timestamps()[range.start() + range.len() - 1];
|
||||
self.evaluate(Some(i128::from(last) + i128::from(self.window.offset_ms)))?;
|
||||
// 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
|
||||
// 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.
|
||||
let ready = range
|
||||
.grid()
|
||||
.timestamps()
|
||||
.get(input_end)
|
||||
.map(|&next| i128::from(next) + i128::from(self.window.offset_ms) - 1);
|
||||
self.evaluate(ready)?;
|
||||
return self.emit(false);
|
||||
}
|
||||
self.close_series()
|
||||
@@ -735,7 +756,7 @@ impl RangeEvaluator {
|
||||
pub fn finish(self) -> Result<()> {
|
||||
if self.series.is_some() {
|
||||
return Err(DataFusionError::Execution(
|
||||
"raw series input ended before the last chunk of a series".into(),
|
||||
"series input ended before the last chunk of a series".into(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
@@ -807,7 +828,7 @@ impl RangeEvaluator {
|
||||
fields: &SeriesFields,
|
||||
drop_stale: bool,
|
||||
) -> bool {
|
||||
// Lengths are checked by `check_chunk` for raw input and by `push_series` for evaluated
|
||||
// Lengths are checked by `check_chunk` for raw input and by `push_evaluated` for evaluated
|
||||
// input, so only the shape of the columns is decided here.
|
||||
if self.window.offset_ms != 0 {
|
||||
return false;
|
||||
|
||||
@@ -2462,6 +2462,121 @@ fn a_chunk_indexed_on_the_inner_grid_is_absolute_on_the_outer_one() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A point is decided by the chunk after which no sample can reach its window, so it leaves with
|
||||
/// that chunk: the outer point at 5 ms is settled by the inner chunk covering position 0, and it
|
||||
/// must not wait for the chunk that follows it.
|
||||
///
|
||||
/// The grids differ on purpose -- inner 0/10, outer 5/20 -- so the first outer point reads the
|
||||
/// first inner sample and no other, and the next sample that can arrive sits at inner 10 ms, five
|
||||
/// steps past the end of its window.
|
||||
#[test]
|
||||
fn a_point_no_later_sample_can_reach_leaves_with_the_chunk_that_settles_it() {
|
||||
let raw = consumer_chunks();
|
||||
let mut inner = inner_layer();
|
||||
let schema = inner.output_schema().clone();
|
||||
let mut consumer = outer_layer(&schema);
|
||||
|
||||
// The sample at 0 ms is the whole of inner position 0, so this push hands the consumer the
|
||||
// inner chunk covering that position alone.
|
||||
let first = inner
|
||||
.push(&raw[0])
|
||||
.unwrap()
|
||||
.expect("the sample at 0 ms settles the inner point at 0 ms");
|
||||
assert_eq!(
|
||||
(first.range().start(), first.range().len()),
|
||||
(0, 1),
|
||||
"the inner fixture hands out a chunk other than the one covering position 0 alone"
|
||||
);
|
||||
assert!(!first.is_last);
|
||||
|
||||
let settled = consumer
|
||||
.push_evaluated(&first)
|
||||
.unwrap()
|
||||
.expect("nothing after inner position 0 can reach the window of the outer point at 5 ms");
|
||||
assert_eq!(
|
||||
(settled.range().start(), settled.range().len()),
|
||||
(0, 1),
|
||||
"the outer point the first inner chunk settles waited for the next one"
|
||||
);
|
||||
assert_eq!(
|
||||
expand(&settled.series),
|
||||
vec![vec![Some(4.0)], vec![None], vec![None]],
|
||||
"the outer point at 5 ms does not report the value of the inner sample at 0 ms alone"
|
||||
);
|
||||
|
||||
// The rest of the series still streams through both layers, and the whole series is the same
|
||||
// as the one handed out one chunk at a time.
|
||||
let mut handed_out = vec![settled];
|
||||
for piece in &raw[1..] {
|
||||
let inner_chunk = inner.push(piece).unwrap();
|
||||
if let Some(inner_chunk) = inner_chunk {
|
||||
handed_out.extend(consumer.push_evaluated(&inner_chunk).unwrap());
|
||||
}
|
||||
}
|
||||
inner.finish().expect("the inner series closed");
|
||||
let outer_schema = consumer.output_schema().clone();
|
||||
consumer.finish().expect("the outer series closed");
|
||||
|
||||
let series = fold_chunks(&handed_out, &outer_schema);
|
||||
assert_series_eq(
|
||||
series.as_ref(),
|
||||
&vec![vec![Some(4.0)], vec![Some(6.0)], vec![Some(8.0)]],
|
||||
&metric(0),
|
||||
);
|
||||
}
|
||||
|
||||
/// A non-final chunk that reaches the end of the input grid decides everything that is left: no
|
||||
/// sample can follow it, so the outer points behind it are handed out at once instead of waiting
|
||||
/// for the chunk that closes the series.
|
||||
#[test]
|
||||
fn a_non_final_chunk_ending_the_input_grid_decides_the_rest() {
|
||||
let inner_grid = grid(0, 10, 5);
|
||||
let schema = Arc::new(SeriesSchema {
|
||||
fields: raw_schema(1).fields,
|
||||
eval_grid: Some(inner_grid),
|
||||
});
|
||||
// One chunk carrying the whole input grid, without closing the series.
|
||||
let whole = evaluated_chunk(
|
||||
&schema,
|
||||
0,
|
||||
vec![Some(4.0), Some(5.0), Some(6.0), Some(7.0), Some(8.0)],
|
||||
false,
|
||||
);
|
||||
let mut consumer = outer_layer(&schema);
|
||||
let decided = consumer
|
||||
.push_evaluated(&whole)
|
||||
.unwrap()
|
||||
.expect("the last input position leaves no outer point waiting for a sample");
|
||||
assert!(
|
||||
!decided.is_last,
|
||||
"the chunk closes a series its input does not close"
|
||||
);
|
||||
assert_eq!(
|
||||
(decided.range().start(), decided.range().len()),
|
||||
(0, 3),
|
||||
"the outer points behind the last inner position waited for a closing chunk"
|
||||
);
|
||||
assert_eq!(
|
||||
expand(&decided.series),
|
||||
vec![vec![Some(4.0)], vec![Some(6.0)], vec![Some(8.0)]],
|
||||
"the outer points do not read the inner samples their windows hold"
|
||||
);
|
||||
|
||||
// Only the empty chunk that closes the series is left, and it decides nothing new.
|
||||
let closing = evaluated_chunk(&schema, 5, vec![], true);
|
||||
let closing = consumer
|
||||
.push_evaluated(&closing)
|
||||
.unwrap()
|
||||
.expect("a series closes");
|
||||
assert!(closing.is_last);
|
||||
assert!(
|
||||
closing.range().is_empty(),
|
||||
"the closing chunk decided {} position(s) the chunk before it already decided",
|
||||
closing.range().len()
|
||||
);
|
||||
consumer.finish().expect("the outer series closed");
|
||||
}
|
||||
|
||||
/// A map chain that yields no value still carries progress: the chunk keeps the range the inner
|
||||
/// layer decided, and its consumer advances to its end rather than waiting for a value.
|
||||
#[test]
|
||||
@@ -2763,6 +2878,11 @@ mod composition {
|
||||
/// vacuous. Before `map_chunk` treated a chain without kernels as the identity it dropped the
|
||||
/// metadata labels from the zero-length chunk alone, so the chunk that closed the series was
|
||||
/// labelled differently from the chunk before it.
|
||||
///
|
||||
/// No plan reaches this state: a `Map` is only built around at least one kernel, and
|
||||
/// `map_chunk` is the only caller-side contract here. The empty slice is therefore pinned as
|
||||
/// what `map_chunk` documents it to be -- the identity -- rather than as a case a planner
|
||||
/// produces. Rejecting the empty slice instead would drop this test and nothing else.
|
||||
#[test]
|
||||
fn an_empty_map_chain_keeps_the_chunk_identity() {
|
||||
let window = RangeWindow {
|
||||
@@ -2965,13 +3085,28 @@ mod composition {
|
||||
/// crosses both layers is progress, not values.
|
||||
#[test]
|
||||
fn an_all_null_middle_chunk_still_advances_the_outer_window() {
|
||||
let raw = composed_raw();
|
||||
// This fixture is its own: the sample of the middle push lands at 25 ms, past both inner
|
||||
// windows it crosses, so the inner layer settles the positions at 10 and 20 ms without a
|
||||
// value while its own sample belongs to no window at all. The next sample the input can
|
||||
// still deliver then sits at 30 ms -- past the window of the outer point at 25 ms, and so
|
||||
// past the point this push makes final. `composed_raw` cannot show this: its push lands at
|
||||
// 12 ms, which still leaves a sample at 20 ms possible, and the outer window at 25 ms
|
||||
// (15..25) could still gain it, so nothing there is decided yet.
|
||||
let raw = [
|
||||
chunk(&[(0, vec![Some(1.0)])], 1, 0, false),
|
||||
chunk(&[(3, vec![Some(2.0)]), (25, vec![Some(3.0)])], 1, 0, false),
|
||||
chunk(&[(40, vec![Some(8.0)])], 1, 0, true),
|
||||
];
|
||||
let mut pipeline = Pipeline::new();
|
||||
pipeline.push(&raw[0]);
|
||||
|
||||
// The samples of this push fall into the 5 ms gaps the inner windows leave between the
|
||||
// 0/10 grid points, so the inner layer decides the point at 10 ms without a value.
|
||||
// The outer layer already reported the point at 5 ms: nothing after inner position 0 can
|
||||
// reach its window.
|
||||
let before = pipeline.outer_extent();
|
||||
assert_eq!(
|
||||
before, 1,
|
||||
"the fixture does not advance the outer layer on its first push"
|
||||
);
|
||||
pipeline.push(&raw[1]);
|
||||
let decided = pipeline
|
||||
.inner_chunks
|
||||
|
||||
Reference in New Issue
Block a user