feat(promql): refuse an interleaved scan at the compiler entry, and in the validator

The `Instant` validation arm replaced an ordering check with a comment saying no
check was needed. The argument was that `unpack_raw_series` rejects `Interleaved`,
which holds only for the legacy `InstantManipulate` path and says nothing about
this tree. Meanwhile the refusal in `compile` sat after the bare-selector early
return, so `compile(eval_stmt("x"), schema, Interleaved)` returned `Ok` against
`compile`'s own rustdoc, and the same hole was open to every nested shape.

The requirement is real for every shape: a native evaluation reads one series to
its last chunk before the next. The refusal now runs before `compile` dispatches
on the shape, so the bare selector, the range function and its parameterised form
all inherit it, and a shape added later inherits it too.

`NativeNode::validate` keeps a check of its own, for a tree built some other way:
`Instant` and `Window` now share `ensure_not_interleaved` instead of one branch
having the check and the other a comment about it.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-20 22:47:18 +08:00
parent c2729e966e
commit 191b7943af
4 changed files with 113 additions and 49 deletions
+23 -20
View File
@@ -122,8 +122,8 @@ pub struct AggregateStateSchema {
/// A scan or exchange boundary bound as a node input.
///
/// The binding records what the input was created for: the output a consumer type-checks against,
/// and the ordering it guarantees. A window reads the ordering, rejecting an interleaved input; no
/// other consumer reads it yet.
/// and the ordering it guarantees. An instant selection and a window both read the ordering,
/// rejecting an interleaved input; no other consumer reads it yet.
#[derive(Clone, Debug)]
pub struct BoundInput {
/// What the input produces: raw samples from a scan, or state from an exchange boundary.
@@ -329,19 +329,13 @@ impl NativeNode {
)));
}
}
ensure_not_interleaved(input)?;
ensure_samples(&self.output, "an instant selection")?;
// A selection is one source row per grid point: `select_instants` picks row
// indices and every field is taken at them, so it carries one value per input
// field.
ensure_same_fields(&input.output, &self.output, "an instant selection")?;
ensure_grid(&self.output, &op.output_grid, "an instant selection")?;
// An instant selection reads one series' complete axis, so its consumer collects
// a series to its `is_last` chunk into one buffer keyed by nothing else:
// `NativeInstantManipulateExec` needs each series' chunks contiguous. This
// validator does not check that. `Instant` has no implementation in the native
// tree yet, and `try_to_native_execution_plan` has no callers, so the requirement
// is unenforced rather than satisfied in advance; it is settled when `Instant`
// lands in the tree.
}
NativeExpr::Window { input, op } => {
input.validate()?;
@@ -359,17 +353,7 @@ impl NativeNode {
"a window reads an empty range: its range must be positive",
));
}
// A range evaluator consumes one series to its last chunk before it starts the
// next, so interleaved chunks of different series cannot be fed to it. Only an
// input node carries an ordering, so only there is there one to check.
if let NativeExpr::Input(binding) = &input.expr
&& binding.ordering == SeriesOrdering::Interleaved
{
return Err(invalid(
"a window reads one series to its last chunk before the next, so \
interleaved chunks cannot be fed to it",
));
}
ensure_not_interleaved(input)?;
// What is checked here and just below is that the declared input grid equals the
// input's grid, and that the output grid matches the declared one. The grid is
// not checked to cover the window's read range
@@ -629,6 +613,25 @@ fn ensure_grid(ty: &NativeType, expected: &Arc<EvalGrid>, what: &str) -> Result<
}
}
/// Checks that `input`, when it is a [`NativeExpr::Input`], does not declare interleaved series.
///
/// A native evaluation reads one series to its last chunk before the next: a range evaluator folds
/// a series completely before it starts another, and an instant selection's consumer collects one
/// series' chunks into one buffer. Neither can be handed chunks of different series that
/// interleave, which is the one rule both consumers state. Only an input node carries an ordering,
/// so only there is there one to check.
fn ensure_not_interleaved(input: &NativeNode) -> Result<()> {
if let NativeExpr::Input(binding) = &input.expr
&& binding.ordering == SeriesOrdering::Interleaved
{
return Err(invalid(
"a native evaluation reads one series to its last chunk before the next, so \
interleaved chunks cannot be fed to it",
));
}
Ok(())
}
fn invalid(reason: &str) -> DataFusionError {
// A rejected tree is a compiler defect, not a query error: the compiler only builds a node
// after deciding that this combination is supported. `Internal` keeps that meaning on the wire
+40 -8
View File
@@ -96,6 +96,18 @@ fn input_with_fields(count: usize) -> NativeNode {
}
}
/// A scan of raw samples whose chunks of different series may interleave.
fn interleaved_input() -> NativeNode {
let output = raw();
NativeNode {
expr: NativeExpr::Input(BoundInput {
output: output.clone(),
ordering: SeriesOrdering::Interleaved,
}),
output,
}
}
/// A constant scalar, which is a scalar type evaluated on the statement's grid.
fn scalar_on(value: f64, grid: Arc<EvalGrid>) -> NativeNode {
NativeNode {
@@ -456,15 +468,35 @@ fn a_window_checks_its_declared_input_grid() {
/// chunks of different series may not interleave.
#[test]
fn a_window_cannot_read_interleaved_input() {
let output = raw();
let interleaved = NativeNode {
expr: NativeExpr::Input(BoundInput {
output: output.clone(),
ordering: SeriesOrdering::Interleaved,
}),
output,
let error = window_over(interleaved_input(), None)
.validate()
.unwrap_err();
assert!(
error
.to_string()
.contains("interleaved chunks cannot be fed to it"),
"{error}"
);
}
/// An instant selection's consumer collects one series' chunks into one buffer, so it reads a
/// series to its last chunk before the next: it refuses an interleaved input for the reason a
/// window does.
#[test]
fn an_instant_selection_cannot_read_interleaved_input() {
let out = grid(0, 60_000, 4);
let node = NativeNode {
expr: NativeExpr::Instant {
input: Box::new(interleaved_input()),
op: BoundInstant {
lookback_ms: 300_000,
offset_ms: 0,
output_grid: Arc::clone(&out),
},
},
output: evaluated(Arc::new([]), out),
};
let error = window_over(interleaved, None).validate().unwrap_err();
let error = node.validate().unwrap_err();
assert!(
error
.to_string()
+22 -17
View File
@@ -34,7 +34,9 @@
//! The statement's grid, the selector's lookback, window, offset and anchor are the only numbers
//! derived here; the legality of the tree is not checked here. [`NativeTree::try_new`] is the
//! single authority on that, so a node added later cannot be legal in one place and illegal in
//! another.
//! another. What is refused before a tree exists is the scan's shape: no shape below can consume
//! chunks of different series that interleave, so an `Interleaved` ordering is refused once, at the
//! entry, and [`NativeTree::validate`] refuses the same ordering for a tree built some other way.
use std::sync::Arc;
use std::time::Duration;
@@ -72,12 +74,12 @@ const SUPPORTED: &str = "a bare selector, a range function over a selector — \
/// one.
///
/// `ordering` is what the caller's scan guarantees, and the tree records it rather than assuming
/// one. Only `Contiguous` and `KeyThenTime` are accepted: a native range window consumes one series
/// to its last chunk before the next, so chunks of different series may not interleave, and an
/// `Interleaved` declaration is refused here — which is where the refusal belongs, since a tree that
/// declares it under a window cannot be built by this compiler at all. [`NativeTree::validate`]'s
/// window branch checks the same thing, so if both land this refusal comes first and the tree check
/// is a backstop for a tree built some other way.
/// one. Only `Contiguous` and `KeyThenTime` are accepted: every shape this compiler accepts reads
/// one series to its last chunk before the next, so an `Interleaved` declaration is refused at the
/// entry, before any shape is built — the bare selector included, which returns before a window
/// exists — and a shape added later inherits the refusal instead of restating it.
/// [`NativeTree::validate`] refuses the same ordering in its `Instant` and `Window` branches, as a
/// backstop for a tree built some other way.
///
/// The tree carries no metric name and no matchers, so `rate(x{job="a"}[5m])` and `rate(y[5m])`
/// compile to identical trees, and an `EXPLAIN` built on this display cannot tell them apart. That
@@ -88,6 +90,17 @@ pub fn compile(
input: Arc<SeriesSchema>,
ordering: SeriesOrdering,
) -> Result<NativeTree> {
// Every shape this compiler accepts reads one series to its last chunk before the next, so raw
// samples whose chunks of different series interleave cannot be fed to any of them. The refusal
// is here rather than in a shape's branch so that a shape added later inherits it, and it comes
// before the early return below for a bare selector.
if ordering == SeriesOrdering::Interleaved {
return Err(not_implemented(
"raw samples whose series interleave, which this compiler cannot consume: a native \
evaluation reads one series to its last chunk before the next, so chunks of different \
series may not interleave",
));
}
// A bare selector is the other shape: an instant selection over the same raw input.
if let PromExpr::VectorSelector(selector) = unparen(&stmt.expr) {
return instant_selection(stmt, selector, input, ordering);
@@ -126,16 +139,8 @@ pub fn compile(
func.name, argument
)));
};
// A native range window reads one series to its last chunk before it reads the next, so raw
// samples whose chunks of different series interleave cannot be folded by it. The declaration
// is refused here rather than recorded onto an input the tree is built over and fails on.
if ordering == SeriesOrdering::Interleaved {
return Err(not_implemented(
"raw samples whose series interleave, which this compiler cannot consume: its range \
window reads one series to its last chunk before the next, so chunks of different \
series may not interleave",
));
}
// The ordering was refused at the entry above, so every shape below is built over a scan whose
// series' chunks do not interleave.
window_selection(stmt, function, vs, range, input, ordering)
}
+28 -4
View File
@@ -631,11 +631,11 @@ fn the_numeric_edges_of_a_statement_are_bound_as_given() {
SeriesOrdering::KeyThenTime,
Expected::Rejected(Rejection::Plan),
),
// A range window reads one series to its last chunk before the next, so a scan whose
// chunks of different series interleave is refused instead of recorded onto an input the
// window would fail on.
// Every shape reads one series to its last chunk before the next, so a scan whose chunks
// of different series interleave is refused instead of recorded onto an input that shape
// would fail on.
(
"an interleaved ordering, which a window cannot consume to its last chunk",
"an interleaved ordering, which no shape can consume to its last chunk",
eval_stmt("rate(x[5m])"),
SeriesOrdering::Interleaved,
Expected::Rejected(Rejection::NotImplemented),
@@ -707,6 +707,30 @@ fn the_numeric_edges_of_a_statement_are_bound_as_given() {
}
}
/// An interleaved scan is refused whatever shape the statement has. The refusal sits at the entry
/// of the compiler rather than in one shape's branch, because a bare selector returns before any
/// window exists and would otherwise be accepted with an ordering it cannot be evaluated on.
#[test]
fn an_interleaved_scan_is_refused_whatever_the_shape() {
for query in ["x", "rate(x[5m])", "quantile_over_time(0.9, x[5m])"] {
let error = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::Interleaved)
.expect_err(&format!("`{query}` compiled over an interleaved scan"));
assert!(
matches!(error, DataFusionError::NotImplemented(_)),
"`{query}` did not report NotImplemented: {error}"
);
let message = error.to_string();
assert!(
message.contains("reads one series to its last chunk before the next"),
"`{query}` did not say why the ordering is refused: {message}"
);
}
// A scan whose chunks of one series are contiguous is recorded and compiles, which is what
// makes the refusal above about the ordering rather than about the shape.
compile(&eval_stmt("x"), raw_schema(), SeriesOrdering::Contiguous)
.expect("a contiguous scan compiles a bare selector");
}
/// A shape outside the subset is refused, and the error says what was found so the query can be
/// rewritten rather than guessed at.
#[test]