fix(promql): decline unsupported group-right fills

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
discord9
2026-10-23 20:08:15 +08:00
parent f1da64fadc
commit fdcb00e166
3 changed files with 75 additions and 78 deletions
+39 -36
View File
@@ -75,11 +75,11 @@
//! lookback), `Input` (raw samples) → `Window` (the range function over the statement's grid),
//! `<expression>` → `Window` whose input is evaluated (the subquery, whose input grid is its own),
//! `<expression>` → `Map` (a pointwise math function over what its argument evaluates to) and, over
//! any of those, `Binary` (an arithmetic or comparison operation with a constant on one side),
//! `Scalar` (that constant), `Aggregate` (one of the nine aggregators that fold a group's values
//! into one value per grid point, optionally grouped) and `Vector` (an operation over the whole
//! vector at each point: a sort, `absent`, a classic histogram function, a selecting aggregator or
//! `count_values`). Everything else is a
//! any of those, `Binary` (an arithmetic or comparison operation between vectors or with a
//! constant on one side), `Scalar` (that constant), `Aggregate` (one of the nine aggregators that
//! fold a group's values into one value per grid point, optionally grouped) and `Vector` (an
//! operation over the whole vector at each point: a sort, `absent`, a classic histogram function, a
//! selecting aggregator or `count_values`). Everything else is a
//! [`DataFusionError::NotImplemented`] that names what was found and quotes the shapes above,
//! because rejecting a query the compiler does not understand is the only honest alternative to
//! compiling it into something else.
@@ -105,11 +105,10 @@
//! its own: `-x` binds as the multiplication by `-1` that it is, which the multiplication kernel
//! already computes.
//!
//! What a tree cannot yet express is deliberate too. The one input binding carries the ordering it
//! guarantees and what it produces, but not the selector it reads, so a tree with two selectors
//! cannot tell its inputs apart and an operation between two vectors is refused. A statement whose
//! value is a scalar has nothing to read, so it is refused as well: a constant is bound next to a
//! vector that gives it something to broadcast over.
//! Vector matching is bound with each selector's own input, including the labels and cardinality
//! written on a vector–vector operation. A scalar has no input of its own: it is bound next to the
//! vector that gives it something to broadcast over, while a statement whose value is only a scalar
//! has nothing to read and is refused.
//!
//! An aggregate is bound as [`AggregateStage::Complete`], which is a claim about the whole execution
//! rather than about the node: it says that every contribution to every group is in this execution's
@@ -260,13 +259,13 @@ const SUPPORTED: &str = "a selector — `x`, optionally with an `offset` — a r
optionally omitted as `[<range>:]`, where the subquery's `<expression>` \
is any shape here, so a subquery nests on a grid of its own as in \
`rate(rate(x[5m:1m])[10m:30s])`, and where either matrix shape carries the \
`offset` and `@` anchor written on it as in `rate(x[5m] @ 1234)`; a math \
function over any of them (`abs(rate(x[5m]))`, `clamp_min(x, 0)`), a label \
function over any of them \
`offset` and `@` anchor written on it as in `rate(x[5m] @ 1234)`; vector \
matching between two such vectors (`a + b`, `a / on(job) group_left b`, \
`a > bool ignoring(zone) b`); a math function over any of them \
(`abs(rate(x[5m]))`, `clamp_min(x, 0)`), a label function over any of them \
(`label_replace(x, \"a\", \"$1\", \"b\", \"(.*)\")`), a binary \
operation between two of them (`a + b`, `a / on(job) group_left b`, \
`a > bool ignoring(zone) b`) or between one of them and a constant \
(`2 / (x + 1)`, `x > bool 0`), a negation of one of them (`-x`), or one of \
operation between one of them and a constant (`2 / (x + 1)`, \
`x > bool 0`), a negation of one of them (`-x`), or one of \
the aggregators that fold a group's values into one value per grid point — \
`sum`, `avg`, `count`, `min`, `max`, `group`, `stddev`, `stdvar` and \
`quantile(<constant>, <expression>)` — over any of them, optionally \
@@ -479,13 +478,11 @@ fn negate(context: &Context, operand: &PromExpr) -> Result<NativeNode> {
))
}
/// Compiles a binary operation whose operands are not both vectors.
/// Compiles a binary operation, preserving written operand order and vector matching modifiers.
///
/// The tree holds one input binding, and that binding carries the ordering it guarantees and what it
/// produces but not the selector it reads, so two vectors cannot be told apart: `a + b` and `a + a`
/// would compile to the same tree. That is why a vector–vector operation is refused until an input
/// names its selector, and why an operation over one vector and one scalar is the shape the tree can
/// bind: a scalar carries no labels, so broadcasting it is unambiguous.
/// A scalar carries no labels, so broadcasting it over a vector is unambiguous. Vector operands
/// retain their own input bindings and matching; a `group_right` operation with fill is refused
/// until the kernel supports that combination.
fn binary_operation(context: &Context, binary: &BinaryExpr) -> Result<NativeNode> {
// A set operator combines two vectors' series rather than their values, and the tree has no node
// for it at all. Its name is reported before the operands are looked at, so a set operation
@@ -618,17 +615,12 @@ fn scalar_matching(op: BinaryOp, return_bool: bool) -> BinaryMatching {
}
}
/// The binary operator a PromQL token denotes, or `None` when there is no node for it.
///
/// A comparison is bound like an arithmetic operator, because the node carries `return_bool`
/// separately: `x > 0` and `x > bool 0` are one operator with one field changed. [`arithmetic_op`]
/// is not reused because it decides what a *fold* may do at compile time and a comparison folds
/// nothing, while this map decides what a *node* computes once the query runs.
/// The matching a vector–vector operation records.
///
/// Each part is read from the modifier the query wrote, and a modifier it did not write is the
/// default PromQL applies: every label but `__name__` matches (`ignoring` nothing), one series of
/// each side may share a match group, and no fill value stands in for a missing sample.
/// default PromQL applies: every label but `__name__` matches (`ignoring` nothing), and the
/// cardinality is one-to-one. Fill fields retain the parser's left/right values; one-to-many
/// matching with fill is refused until the native kernel supports that combination.
///
/// `ManyToMany` is the cardinality of a set operator, which [`binary_operation`] refuses before
/// asking for a matching, so reaching it means the two disagree; it is reported as a shape with no
@@ -660,12 +652,18 @@ fn vector_matching(binary: &BinaryExpr, op: BinaryOp) -> Result<BinaryMatching>
)));
}
};
// The two fields keep the parser's names, so `fill_values.lhs` is what `fill_left` wrote and
// `fill_values.rhs` is what `fill_right` wrote. Which sample a fill stands in for depends on the
// cardinality, because the kernel picks the field by which side of the *match* is missing:
// `fill.rhs` answers a many-side series that found no match, and `fill.lhs` a one-side series
// that found none. The default and `group_left` make the many side the left operand;
// `group_right` swaps them.
if matches!(&cardinality, Cardinality::OneToMany(_))
&& modifier.is_some_and(|modifier| {
modifier.fill_values.lhs.is_some() || modifier.fill_values.rhs.is_some()
})
{
return Err(not_implemented(&format!(
"the one-to-many vector matching with fill in `{binary}`, which the native kernel does not support"
)));
}
// The fields keep the parser's operand names: `fill_values.lhs` is `fill_left`, and
// `fill_values.rhs` is `fill_right`. One-to-many matching with either field set was refused
// above; the remaining supported cardinalities preserve these parser-side values for the kernel.
let fill = modifier.map_or_else(FillValues::default, |modifier| FillValues {
lhs: modifier.fill_values.lhs,
rhs: modifier.fill_values.rhs,
@@ -684,6 +682,11 @@ fn label_names(labels: &[String]) -> Vec<Arc<str>> {
labels.iter().map(|name| Arc::from(name.as_str())).collect()
}
/// The binary operator a PromQL token denotes, or `None` when there is no node for it.
///
/// A comparison carries `return_bool` separately, so `x > 0` and `x > bool 0` use the same
/// operator. Unlike [`arithmetic_op`], which decides what a constant fold may compute, this map
/// selects the operation the runtime executes.
fn binary_op(op: &TokenType) -> Option<BinaryOp> {
match op.id() {
token::T_ADD => Some(BinaryOp::Add),
@@ -654,6 +654,24 @@ fn an_offset_on_a_bare_selector_keeps_its_sign() {
}
}
/// An instant selector's raw scan bounds use its `@` anchor rather than the statement grid.
#[test]
fn an_anchored_instant_selector_scans_from_its_anchor() {
let requirement = SelectorRequirement {
metric: Some("x".into()),
matchers: promql_parser::label::Matchers::empty(),
offset_ms: 2_000,
at_ms: Some(50_000),
grid_start_ms: 0,
grid_end_ms: 100_000,
timing: SelectorTiming::Instant { lookback_ms: 5_000 },
};
assert_eq!(
requirement.raw_scan_bounds().unwrap(),
(43_000, 48_000, false)
);
}
/// An `@`-anchored bare selector is refused because the instant node has no anchor field, not
/// because anchoring is unsupported: the window shape binds an anchor.
#[test]
+18 -42
View File
@@ -530,15 +530,10 @@ fn a_two_input_tree_refuses_a_chunk_that_names_no_input() {
}
/// `group_right(env)` makes the right operand the many side and copies `env` from the left one, so
/// one left series answers two right series. The fill then shows the same swap from the other end:
/// the kernel fills its many side from `fill.rhs`, and in `group_right` the many side is the right
/// operand, so a right series with no left match is answered by `fill_right` — the field named
/// after the side the fill *is* rather than the side it stands in for. That mismatch between the
/// field's name and the operand the fill becomes is the point of this case: the arithmetic itself is
/// covered by the kernel's own golden fixtures, so what is pinned here is only that the compiler put
/// `fill_right`'s value in the field the kernel reads for that side.
/// one left series answers two right series. Group-right matching without fill executes; fill on
/// this one-to-many shape is refused until the native kernel supports its semantics.
#[test]
fn a_group_right_swaps_the_many_side_and_fills_the_missing_one_side_sample() {
fn group_right_executes_without_fill_and_declines_fill() {
let pushes = |compiled: &CompiledTree, with_unmatched: bool| {
let mut pushes = vec![
(
@@ -602,40 +597,21 @@ fn a_group_right_swaps_the_many_side_and_fills_the_missing_one_side_sample() {
"the second right series is not `2 / 20`: {output:?}"
);
// `fill_right(100)` supplies the stand-in for the left operand: `group_right` makes the right
// operand the many side, the kernel fills that side from `fill.rhs`, and so the query writes
// `fill_right` for a sample that stands in for a missing left-hand one. The `job="y"` right
// series has no left match, so it is answered as `100 / 5` while the two matched series keep
// their own values.
let filled = compile("a{job=\"x\"} / on(job) group_right(env) fill_right(100) b");
let output = results(&run(&filled, pushes(&filled, true)));
assert_eq!(
output.len(),
3,
"`fill_right(100)` did not answer the right series with no left match: {output:?}"
);
assert_eq!(
output.get("{env=\"prod\", instance=\"i1\", job=\"x\"}"),
Some(&every_point(0.2)),
"the fill changed the first matched series: {output:?}"
);
assert_eq!(
output.get("{env=\"prod\", instance=\"i2\", job=\"x\"}"),
Some(&every_point(0.1)),
"the fill changed the second matched series: {output:?}"
);
// A fill row pairs no two series, so there is no one side to copy `env` from: `group_right`'s
// included label is removed rather than invented, which is why the filled series carries no
// `env` while its value is `100 / 5`.
assert_eq!(
output.get("{instance=\"i3\", job=\"y\"}"),
Some(&every_point(20.0)),
"the filled series is not `100 / 5`: {output:?}"
);
assert!(
!output.contains_key("{env=\"prod\", instance=\"i3\", job=\"y\"}"),
"the filled series invented the included label from the side it stands in for: {output:?}"
);
// The kernel currently applies the wrong fill side for one-to-many matching; refuse this shape
// until its matching/fill semantics are corrected.
for fill in ["fill_right(83)", "fill(41)"] {
let query = format!("a{{job=\"x\"}} / on(job) group_right(env) {fill} b");
let error = compile_selectors(
&eval_stmt(&query),
&|_| Ok(raw_schema()),
SeriesOrdering::KeyThenTime,
)
.expect_err("one-to-many matching with fill must fall back");
assert!(
matches!(error, datafusion::error::DataFusionError::NotImplemented(_)),
"`{query}` was refused for the wrong reason: {error}"
);
}
}
#[test]