mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-24 19:54:04 +00:00
chore: merge feat/narthex-select-compile into feat/narthex-histogram-quantile-node
Signed-off-by: Dennis Zhuang <killme2008@gmail.com> # Conflicts: # src/query/src/promql/native_compile.rs # src/query/src/promql/native_compile/tests.rs
This commit is contained in:
@@ -19,24 +19,66 @@
|
||||
//! accepts is one recursive rule:
|
||||
//!
|
||||
//! ```text
|
||||
//! <selector> e.g. x
|
||||
//! <range function>(<selector>[<range>]) e.g. rate(x[5m])
|
||||
//! <range function>(<constant>, <selector>[<range>]) e.g. quantile_over_time(0.9, x[5m])
|
||||
//! <math function>(<expression>[, <constant>]*) e.g. abs(rate(x[5m])), clamp_min(x, 0)
|
||||
//! <sort>(<expression>[, <string>]*) e.g. sort_desc(x), sort_by_label(x, "job")
|
||||
//! absent(<expression>) e.g. absent(x{job="a"})
|
||||
//! <histogram>(<constant>+, <expression>) e.g. histogram_quantile(0.9, rate(x[5m]))
|
||||
//! <expression> one of, recursively:
|
||||
//! <selector> e.g. x, x offset -5m
|
||||
//! <range function>(<matrix>) e.g. rate(x[5m]), rate(x[5m:1m] @ 1234)
|
||||
//! quantile_over_time(<constant>, <matrix>) e.g. quantile_over_time(0.9, x[5m])
|
||||
//! predict_linear(<matrix>, <constant>) e.g. predict_linear(x[5m], 300)
|
||||
//! double_exponential_smoothing(<matrix>, <constant>, <constant>)
|
||||
//! e.g. double_exponential_smoothing(x[5m], .5, .1)
|
||||
//! <math function>(<expression>[, <constant>]*) e.g. abs(rate(x[5m])), clamp_min(x, 0)
|
||||
//! <sort>(<expression>[, <string>]*) e.g. sort_desc(x), sort_by_label(x, "job")
|
||||
//! absent(<expression>) e.g. absent(x{job="a"})
|
||||
//! <histogram>(<constant>+, <expression>) e.g. histogram_quantile(0.9, rate(x[5m]))
|
||||
//! <expression> <operator> <constant> e.g. x * 100, x > bool 0
|
||||
//! <constant> <operator> <expression> e.g. 1 / x, 0 < x
|
||||
//! -<expression> e.g. -x
|
||||
//! <aggregator> [(by|without)(<labels>)] (<expression>)
|
||||
//! e.g. avg by(job)(rate(x[5m]) * 100)
|
||||
//! <aggregator> (<expression>) [(by|without)(<labels>)]
|
||||
//! e.g. count (x) by(job)
|
||||
//! quantile(<constant>, <expression>) [(by|without)(<labels>)]
|
||||
//! e.g. quantile(0.9, x) by(job)
|
||||
//!
|
||||
//! <matrix> <selector>[<range>]<modifiers> a range selector: raw samples
|
||||
//! | <expression>[<range>:<step>]<modifiers>
|
||||
//! a subquery: <expression> evaluated on a grid;
|
||||
//! the step may be omitted as `[<range>:]`
|
||||
//! <modifiers> nothing, `offset <duration>`, `@ <anchor>`, or
|
||||
//! both, in either order — e.g. `x[5m] @ 1234
|
||||
//! offset 1m`
|
||||
//! ```
|
||||
//!
|
||||
//! which become `Input` (raw samples) → `Instant` (the last sample within the statement's lookback),
|
||||
//! `Input` (raw samples) → `Window` (the range function over the statement's grid) and
|
||||
//! `<expression>` → `Map` (a pointwise math function over what its argument evaluates to), and
|
||||
//! `<expression>` → `Vector` (a sort, `absent` or a classic histogram function, which read the
|
||||
//! whole vector). A
|
||||
//! selector may carry `offset` in either selection shape and, in the window shape, an `@` anchor.
|
||||
//! 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.
|
||||
//! where `<aggregator>` is `sum`, `avg`, `count`, `min`, `max`, `group`, `stddev`, `stdvar` or
|
||||
//! `quantile`, the function names are [`range_signature`]'s and [`math_signature`]'s, and which side
|
||||
//! of the matrix a range function's constants go on is [`RangeSignature`]'s: before it for
|
||||
//! `quantile_over_time`, after it for `predict_linear` and `double_exponential_smoothing`. A math
|
||||
//! function's constants always follow its vector.
|
||||
//!
|
||||
//! Parentheses are transparent wherever they appear — `rate((x[5m]))` is `rate(x[5m])` — because
|
||||
//! they say how a statement was typed rather than what it asks for.
|
||||
//!
|
||||
//! A subquery's `<expression>` is any shape above, so a subquery nests inside a subquery on a grid
|
||||
//! of its own, as in `rate(rate(x[5m:1m])[10m:30s])`; a `<step>` left out means Prometheus's default
|
||||
//! evaluation interval, one minute. Its `offset` and its `@` anchor are its own, not the inner
|
||||
//! expression's, so `rate(x[5m:1m] @ end() offset 1m)` reads one fixed interval. A `<selector>`
|
||||
//! outside a `<matrix>` takes an `offset` and no anchor: the instant shape has no field for one, so
|
||||
//! `x @ 100` is refused and `rate(x[5m] @ 100)` is not.
|
||||
//!
|
||||
//! Those shapes become `Input` (raw samples) → `Instant` (the last sample within the statement's
|
||||
//! 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) and `Aggregate` (one of the nine aggregators that fold a group's values
|
||||
//! into one value per grid point, optionally grouped). 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.
|
||||
//!
|
||||
//! The compiler binds a subquery's child grid and `RangeWindow` independently of runtime execution:
|
||||
//! compilation describes the anchored interval, while runtime support determines how that bound
|
||||
//! plan is consumed.
|
||||
//!
|
||||
//! Two decisions keep the recursion from growing a branch per query. A constant argument — a
|
||||
//! quantile, a rounding step, a clamp bound — is folded out of the statement by [`constant_value`],
|
||||
@@ -47,6 +89,28 @@
|
||||
//! alone, so the number this compiler binds and the number the kernel computes cannot disagree
|
||||
//! about `%`, `^` or `atan2`.
|
||||
//!
|
||||
//! Two more are about what a node means rather than about which node to build. A binary operation
|
||||
//! keeps its operands in the order they were written, because most operators are not commutative:
|
||||
//! `1 / x` is not `x / 1`, and which side the constant is on is the only thing that tells them
|
||||
//! apart. `return_bool` is carried as its own field, so `x > 0` (which filters) and `x > bool 0`
|
||||
//! (which reports zero or one) are one node with one field changed. A negation then needs no node of
|
||||
//! 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.
|
||||
//!
|
||||
//! 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
|
||||
//! input. The caller has to keep that claim. One tree per partition does not: handing every
|
||||
//! partition the same `Complete` tree sums each partition's part and reports it as the group's
|
||||
//! whole, so a partitioned caller has to feed whole groups to one execution or split the aggregation
|
||||
//! into `Partial` and `Final` instead. What a `Complete` node guarantees here is only the arithmetic
|
||||
//! — that the values it folded are all the values it was given.
|
||||
//!
|
||||
//! 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
|
||||
@@ -62,30 +126,52 @@ use datatypes::series::samples::SeriesSchema;
|
||||
use datatypes::series::time::{EvalGrid, GridSpec};
|
||||
use datatypes::series::{MetricName, SeriesOrdering};
|
||||
use promql::histogram_eval::HistogramFunction;
|
||||
use promql::map::{ArithmeticOp, MathOp};
|
||||
use promql::map::{ArithmeticOp, ComparisonOp, MathOp};
|
||||
use promql::native::plan::{
|
||||
BoundInput, BoundInstant, BoundMap, BoundVectorOp, BoundWindow, NativeExpr, NativeNode,
|
||||
NativeTree, NativeType,
|
||||
AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar,
|
||||
BoundVectorOp, BoundWindow, Grouping, NativeExpr, NativeNode, NativeTree, NativeType,
|
||||
};
|
||||
use promql::range_eval::{RangeFunction, RangeWindow};
|
||||
use promql::range_eval::{RangeFunction, RangeWindow, subquery_grid};
|
||||
use promql::select::{LabelMatcher, MatchOp, SortOrder, absent_labels};
|
||||
use promql::vector_match::{BinaryMatching, BinaryOp, Cardinality, FillValues, MatchingLabels};
|
||||
use promql_parser::label::MatchOp as ParserMatchOp;
|
||||
use promql_parser::parser::token::TokenType;
|
||||
use promql_parser::parser::value::ValueType;
|
||||
use promql_parser::parser::{
|
||||
AtModifier, Call, EvalStmt, Expr as PromExpr, MatrixSelector, NumberLiteral, Offset,
|
||||
StringLiteral, VectorSelector, token,
|
||||
AggregateExpr, AtModifier, BinModifier, BinaryExpr, Call, EvalStmt, Expr as PromExpr,
|
||||
LabelModifier, MatrixSelector, NumberLiteral, Offset, StringLiteral, SubqueryExpr,
|
||||
VectorMatchCardinality, VectorMatchFillValues, VectorSelector, token,
|
||||
};
|
||||
|
||||
use crate::promql::label_values::signed_millis_since_epoch;
|
||||
|
||||
/// The shapes this compiler accepts, quoted in every rejection.
|
||||
const SUPPORTED: &str = "a selector, a range function over a range selector — \
|
||||
`<range function>(<selector>[<range>])`, with its constants in front of \
|
||||
the selector as in `quantile_over_time(0.9, x[5m])` — or a math function, \
|
||||
a sort, `absent` or a histogram function over any of them, as in \
|
||||
`abs(rate(x[5m]))`: for example `x`, `rate(x[5m])`, \
|
||||
`quantile_over_time(0.9, x[5m])` or `abs(rate(x[5m]))`";
|
||||
///
|
||||
/// It is one sentence of grammar rather than a list of examples, because it is what a refused query
|
||||
/// reads: the shapes have to be true of every query the compiler binds, and the examples only show
|
||||
/// them. The two matrix shapes and the recursion through them are stated first, because that
|
||||
/// recursion is what a reader is most likely to get wrong.
|
||||
const SUPPORTED: &str = "a selector — `x`, optionally with an `offset` — a range function over \
|
||||
a matrix, where a matrix is `<selector>[<range>]` as in `rate(x[5m])` or \
|
||||
`<expression>[<range>:<step>]` as in `rate(x[5m:1m])`, with the step \
|
||||
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 binary \
|
||||
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 folds 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 \
|
||||
grouped by `by` or `without` as in `avg by(job)(rate(x[5m]) * 100)`; a \
|
||||
sort, `absent` or `histogram_quantile` / `histogram_fraction` over any of \
|
||||
them (`sort_desc(x)`, `absent(x{job=\"a\"})`, \
|
||||
`histogram_quantile(0.9, rate(x[5m]))`); a range function's own \
|
||||
constants sit where that function takes them, before \
|
||||
the matrix for `quantile_over_time(0.9, x[5m])` and after it for \
|
||||
`predict_linear(x[5m], 300)` and \
|
||||
`double_exponential_smoothing(x[5m], 0.5, 0.1)`";
|
||||
|
||||
/// Compiles `stmt` into a tree that reads `input` and evaluates the statement's expression over it.
|
||||
///
|
||||
@@ -124,10 +210,25 @@ pub fn compile(
|
||||
series may not interleave",
|
||||
));
|
||||
}
|
||||
// A tree reads a scan and reports evaluated samples, so an expression whose value is not a
|
||||
// vector has nothing to compile into: `1 + 2` is a scalar, and the scalar node is bound as a
|
||||
// constant next to a vector that gives it something to broadcast over rather than on its own.
|
||||
// The check is on the statement's type rather than on the compiled node, so it does not depend on
|
||||
// which kinds of expression happen to compile to a scalar today.
|
||||
if stmt.expr.value_type() != ValueType::Vector {
|
||||
return Err(not_implemented(&format!(
|
||||
"the expression `{}`, which is a {} where a tree reports evaluated samples",
|
||||
stmt.expr,
|
||||
stmt.expr.value_type()
|
||||
)));
|
||||
}
|
||||
let context = Context {
|
||||
stmt,
|
||||
input,
|
||||
ordering,
|
||||
// The grid is built once for the whole compilation rather than per node: every node of a
|
||||
// tree without a subquery lands on this one, and a subquery replaces it for its own subtree.
|
||||
grid: statement_grid(stmt)?,
|
||||
};
|
||||
// The whole tree is validated once, here: a node compiled as another node's input is checked as
|
||||
// part of the tree rather than on its own, so the same node cannot be legal in one place and
|
||||
@@ -147,6 +248,13 @@ struct Context<'a> {
|
||||
input: Arc<SeriesSchema>,
|
||||
/// The ordering the caller's scan guarantees.
|
||||
ordering: SeriesOrdering,
|
||||
/// The grid the expression being compiled is evaluated on.
|
||||
///
|
||||
/// It is the statement's grid for everything except the inside of a subquery, which opens a grid
|
||||
/// of its own: `rate(x[5m:1m])` evaluates `x` at one-minute steps over the five-minute lookback,
|
||||
/// not at the statement's steps. Carrying it here is what lets [`compile_expr`] recurse into a
|
||||
/// subquery without every node builder having to ask which grid it is on.
|
||||
grid: Arc<EvalGrid>,
|
||||
}
|
||||
|
||||
/// Compiles `expr` into the node it denotes, recursing into whatever it is made of.
|
||||
@@ -155,16 +263,320 @@ struct Context<'a> {
|
||||
/// its argument, so a query nests as deeply as it was written. A kind this compiler has no node for
|
||||
/// is refused with what was found, which is why the rejection is useful without a coverage list.
|
||||
fn compile_expr(context: &Context, expr: &PromExpr) -> Result<NativeNode> {
|
||||
// A constant is a value rather than a computation, so it binds as the number it folds to, by the
|
||||
// same fold a call's parameters use: `x * (1 + 2)` carries `3` rather than an addition the tree
|
||||
// would have to know how to run. The fold is checked first so that no shape below has to say
|
||||
// what it does with a constant operand.
|
||||
if let Some(value) = constant_value(expr) {
|
||||
return Ok(scalar_node(context, value));
|
||||
}
|
||||
match expr {
|
||||
// Parentheses group, so they change how a statement was typed rather than what it asks for:
|
||||
// the expression inside them is the one that compiles.
|
||||
PromExpr::Paren(paren) => compile_expr(context, &paren.expr),
|
||||
PromExpr::Call(call) => compile_call(context, call),
|
||||
PromExpr::VectorSelector(selector) => instant_selection(context, selector),
|
||||
PromExpr::Unary(unary) => negate(context, &unary.expr),
|
||||
PromExpr::Binary(binary) => binary_operation(context, binary),
|
||||
PromExpr::Aggregate(aggregate) => aggregate_expr(context, aggregate),
|
||||
found => Err(not_implemented(&format!("the expression `{found}`"))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Binds `value` as a constant scalar on the grid of the expression being compiled.
|
||||
///
|
||||
/// The grid is the context's rather than the constant's own: a constant has no timestamps, so the
|
||||
/// only positions it can be broadcast over are the ones the expression it sits next to is evaluated
|
||||
/// at — the statement's grid, or a subquery's inside one. [`NativeTree::validate`] compares a binary
|
||||
/// operation's two grids by spec rather than by allocation.
|
||||
///
|
||||
/// Nothing here can fail: the grid is the one [`Context`] already carries, so a caller needs no `?`.
|
||||
fn scalar_node(context: &Context, value: f64) -> NativeNode {
|
||||
NativeNode {
|
||||
expr: NativeExpr::Scalar(BoundScalar { value }),
|
||||
output: NativeType::Scalar(Arc::clone(&context.grid)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compiles a negation as the multiplication it is: `-x` is `-1 * x`.
|
||||
///
|
||||
/// The tree has no negate node and needs none: broadcasting a constant over the expression's value
|
||||
/// is the same arithmetic, and the multiplication kernel is the one that already computes it. A
|
||||
/// dedicated negate kernel would be an optimization of this node's work rather than a different
|
||||
/// meaning, so binding the multiplication does not bind anything the query did not ask for.
|
||||
fn negate(context: &Context, operand: &PromExpr) -> Result<NativeNode> {
|
||||
// The constant goes on the left, where the minus was: `-x` subtracts nothing, but a reader of
|
||||
// the tree should see the same order the query was written in.
|
||||
Ok(broadcast(
|
||||
scalar_node(context, -1.0),
|
||||
compile_expr(context, operand)?,
|
||||
BinaryOp::Mul,
|
||||
false,
|
||||
))
|
||||
}
|
||||
|
||||
/// Compiles a binary operation whose operands are not both vectors.
|
||||
///
|
||||
/// 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.
|
||||
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
|
||||
// between two vectors is refused as the set operation it is rather than as a binary operation
|
||||
// the compiler has no node for.
|
||||
if binary.op.is_set_operator() {
|
||||
return Err(not_implemented(&format!(
|
||||
"the set operator `{}` in `{binary}`, which has no native node: it combines two \
|
||||
vectors' series, and every binary node here is an arithmetic or comparison operation \
|
||||
over values",
|
||||
binary.op
|
||||
)));
|
||||
}
|
||||
let Some(op) = binary_op(&binary.op) else {
|
||||
return Err(not_implemented(&format!(
|
||||
"the binary operator `{}` in `{binary}`",
|
||||
binary.op
|
||||
)));
|
||||
};
|
||||
// Two vectors are refused before the modifier is looked at, so a vector–vector query with
|
||||
// matching says what is actually missing. The refusal names the missing identity rather than the
|
||||
// query, because that is what whoever adds it has to add.
|
||||
if binary.lhs.value_type() == ValueType::Vector && binary.rhs.value_type() == ValueType::Vector
|
||||
{
|
||||
return Err(not_implemented(&format!(
|
||||
"the binary operation `{binary}` between two vectors, which needs each input to name \
|
||||
the selector it reads: an input binding carries its ordering and what it produces, but \
|
||||
not which selector it was bound to"
|
||||
)));
|
||||
}
|
||||
// What is left has a constant on one side, and a modifier is refused rather than dropped. This
|
||||
// is deliberately narrower than Prometheus, which drops an `on` or `ignoring` written on a
|
||||
// scalar rather than refusing it; refusing is the conservative choice here, because the tree's
|
||||
// matching has no field that could carry the modifier, so binding the operation would compute
|
||||
// something the query did not ask for. A fill value is refused for the same reason: the tree's
|
||||
// matching has no field for one, and dropping it would compute the operation without the fill
|
||||
// the query asked for.
|
||||
if refuses_modifier(binary.modifier.as_ref()) {
|
||||
return Err(not_implemented(&format!(
|
||||
"the vector matching in `{binary}`, which a constant operand has nothing to match: \
|
||||
`on`, `ignoring`, `group_left` and `group_right` name labels of a vector, and a fill \
|
||||
value fills in a match group that could miss"
|
||||
)));
|
||||
}
|
||||
// Two constants are a value rather than an operation over a vector. A `bool` comparison of two
|
||||
// of them folded to its number before this point, so what arrives here is a comparison without
|
||||
// `bool`, which Prometheus refuses to parse at all. Binding it would produce a node that only
|
||||
// fails when it is run, so it is refused here with what was found.
|
||||
if binary.lhs.value_type() == ValueType::Scalar && binary.rhs.value_type() == ValueType::Scalar
|
||||
{
|
||||
return Err(not_implemented(&format!(
|
||||
"the binary operation `{binary}` between two constants, which reports no value: a \
|
||||
comparison of two scalars is written with `bool` to say whether it holds, and one \
|
||||
written that way is folded to the number it means"
|
||||
)));
|
||||
}
|
||||
// Both operands are compiled before the node is built, so `2 / (x + 1)` nests. The constant side
|
||||
// is bound by [`compile_expr`]'s fold, which is what makes it a scalar node rather than an
|
||||
// expression the tree would have to run.
|
||||
Ok(broadcast(
|
||||
compile_expr(context, &binary.lhs)?,
|
||||
compile_expr(context, &binary.rhs)?,
|
||||
op,
|
||||
binary.return_bool(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Wraps two compiled operands in the binary node that applies `op` to them.
|
||||
///
|
||||
/// The output is the vector side's own binding, because a scalar carries no fields: a broadcast keeps
|
||||
/// the vector's fields and the grid it is evaluated on, which is the statement's grid on both sides.
|
||||
/// Two scalars are possible as a nested operand — a pair that [`constant_value`] refuses to fold, such
|
||||
/// as a comparison between two constants — and the node reports the left one's grid, which is the
|
||||
/// statement's too. The operands keep the order they were written in, because a non-commutative
|
||||
/// operator means what that order says.
|
||||
fn broadcast(left: NativeNode, right: NativeNode, op: BinaryOp, return_bool: bool) -> NativeNode {
|
||||
// `compile_expr` binds a constant to a scalar node and a selection or a function to a vector, so
|
||||
// this is where the side the operation reports is decided.
|
||||
let output = if matches!(left.output, NativeType::Scalar(_)) {
|
||||
right.output.clone()
|
||||
} else {
|
||||
left.output.clone()
|
||||
};
|
||||
NativeNode {
|
||||
expr: NativeExpr::Binary {
|
||||
left: Box::new(left),
|
||||
right: Box::new(right),
|
||||
matching: scalar_matching(op, return_bool),
|
||||
},
|
||||
output,
|
||||
}
|
||||
}
|
||||
|
||||
/// The matching a vector-and-constant operation records.
|
||||
///
|
||||
/// Nothing is matched: a scalar carries no labels, so [`NativeTree::validate`] broadcasts it and
|
||||
/// reads neither `labels` nor `cardinality`. They carry the default a vector–vector operation of the
|
||||
/// same operator would have, which is also the only matching PromQL allows here — it rejects
|
||||
/// `on`, `ignoring`, `group_left` and `group_right` on a scalar–vector operation. `return_bool` is
|
||||
/// the one modifier that changes what this operation computes: `x > 0` filters the vector and
|
||||
/// `x > bool 0` reports zero or one for each of its values.
|
||||
fn scalar_matching(op: BinaryOp, return_bool: bool) -> BinaryMatching {
|
||||
BinaryMatching {
|
||||
op,
|
||||
return_bool,
|
||||
labels: MatchingLabels::Ignoring(Vec::new()),
|
||||
cardinality: Cardinality::OneToOne,
|
||||
// A fill value fills in a match group that could miss, which a broadcast over a constant never
|
||||
// does: the constant exists at every grid point, so there is no missing side to fill.
|
||||
fill: FillValues::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn binary_op(op: &TokenType) -> Option<BinaryOp> {
|
||||
match op.id() {
|
||||
token::T_ADD => Some(BinaryOp::Add),
|
||||
token::T_SUB => Some(BinaryOp::Sub),
|
||||
token::T_MUL => Some(BinaryOp::Mul),
|
||||
token::T_DIV => Some(BinaryOp::Div),
|
||||
token::T_MOD => Some(BinaryOp::Mod),
|
||||
token::T_POW => Some(BinaryOp::Pow),
|
||||
token::T_ATAN2 => Some(BinaryOp::Atan2),
|
||||
token::T_EQLC => Some(BinaryOp::Eq),
|
||||
token::T_NEQ => Some(BinaryOp::Ne),
|
||||
token::T_GTR => Some(BinaryOp::Gt),
|
||||
token::T_LSS => Some(BinaryOp::Lt),
|
||||
token::T_GTE => Some(BinaryOp::Ge),
|
||||
token::T_LTE => Some(BinaryOp::Le),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Compiles an aggregation into an aggregate node over the expression it groups.
|
||||
///
|
||||
/// Every aggregator that folds a group's values into one value per grid point binds, whether or not
|
||||
/// its kernel exists yet: `sum`, `avg`, `count`, `min`, `max`, `group`, `stddev`, `stdvar` and
|
||||
/// `quantile`. Binding a shape the compiler can describe and letting the runtime refuse a kind it
|
||||
/// cannot execute keeps this the one place that says what an aggregate means, so a kernel landing
|
||||
/// later changes no binding.
|
||||
///
|
||||
/// The rest of the parser's aggregates are a different shape, and are refused with the reason rather
|
||||
/// than folded into a kind that would compute something else: `topk`, `bottomk`, `limitk` and
|
||||
/// `limit_ratio` select some of the input's series, so they do not report one value per group per
|
||||
/// grid point, and `count_values` writes one output label per distinct value rather than folding a
|
||||
/// group's values at all.
|
||||
///
|
||||
/// The grouping is bound in the tree's own terms, because `by` and `without` are not each other's
|
||||
/// inverse: `by(job)` keeps `job` and drops every other label, while `without(instance)` keeps every
|
||||
/// label it does not name and drops `__name__` as well, so a list of the surviving labels could not
|
||||
/// say the same thing. A bare `sum(x)` groups by no label at all, which is Prometheus's reading of
|
||||
/// "no modifier": every label is dropped, `__name__` included, and every series of the input falls
|
||||
/// into one group.
|
||||
///
|
||||
/// The stage is [`AggregateStage::Complete`], which says that every contribution to each group is in
|
||||
/// this execution's input. The module documentation says what keeping that claim costs a caller.
|
||||
fn aggregate_expr(context: &Context, aggregate: &AggregateExpr) -> Result<NativeNode> {
|
||||
let AggregateExpr {
|
||||
op,
|
||||
expr,
|
||||
param,
|
||||
modifier,
|
||||
} = aggregate;
|
||||
let kind = match op.id() {
|
||||
token::T_SUM => AggregateKind::Sum,
|
||||
token::T_AVG => AggregateKind::Avg,
|
||||
token::T_COUNT => AggregateKind::Count,
|
||||
token::T_MIN => AggregateKind::Min,
|
||||
token::T_MAX => AggregateKind::Max,
|
||||
token::T_GROUP => AggregateKind::Group,
|
||||
token::T_STDDEV => AggregateKind::Stddev,
|
||||
token::T_STDVAR => AggregateKind::Stdvar,
|
||||
token::T_QUANTILE => {
|
||||
// `quantile` is the only one of the nine with a parameter, and the parser always gives
|
||||
// it one: a `quantile` with no quantile written is refused before the compiler sees it.
|
||||
// The quantile is the call's first argument, so a parameter that is not a constant is
|
||||
// refused under that name rather than answered from a number bound too early.
|
||||
let param = param.as_ref().ok_or_else(|| {
|
||||
not_implemented(&format!(
|
||||
"the aggregate `{op}` in `{aggregate}`, which takes its quantile as its first \
|
||||
argument"
|
||||
))
|
||||
})?;
|
||||
AggregateKind::Quantile {
|
||||
quantile: constant_argument(&op.to_string(), param, 0)?,
|
||||
}
|
||||
}
|
||||
// A ranking aggregate keeps some of the input's series, so what it reports is not one value
|
||||
// per group per grid point: `topk(3, x)` answers with three series, not with the groups'
|
||||
// folded values.
|
||||
token::T_TOPK | token::T_BOTTOMK | token::T_LIMITK | token::T_LIMIT_RATIO => {
|
||||
return Err(not_implemented(&format!(
|
||||
"the aggregate `{op}` in `{aggregate}`, which selects some of its input's series \
|
||||
rather than folding each group's values into one value per grid point"
|
||||
)));
|
||||
}
|
||||
// `count_values` folds nothing: it writes one output label per distinct value it saw, which
|
||||
// is a shape no folded value can spell.
|
||||
token::T_COUNT_VALUES => {
|
||||
return Err(not_implemented(&format!(
|
||||
"the aggregate `{op}` in `{aggregate}`, which writes one output label per distinct \
|
||||
value rather than folding each group's values into one value per grid point"
|
||||
)));
|
||||
}
|
||||
// The parser's `aggregate_op` rule lists exactly the fourteen names above, so this arm is
|
||||
// an operator this compiler was not taught rather than one it means to fold.
|
||||
_ => {
|
||||
return Err(not_implemented(&format!(
|
||||
"the aggregate `{op}` in `{aggregate}`"
|
||||
)));
|
||||
}
|
||||
};
|
||||
let input = compile_expr(context, expr)?;
|
||||
// A complete aggregate folds each group's values and reports them on its input's grid, one value
|
||||
// per input field, so its layout is its input's.
|
||||
let output = input.output.clone();
|
||||
Ok(NativeNode {
|
||||
expr: NativeExpr::Aggregate {
|
||||
input: Box::new(input),
|
||||
op: BoundAggregate {
|
||||
kind,
|
||||
group: grouping(modifier.as_ref()),
|
||||
stage: AggregateStage::Complete,
|
||||
},
|
||||
},
|
||||
output,
|
||||
})
|
||||
}
|
||||
|
||||
/// How a `by` or `without` modifier identifies a group, in the tree's own terms.
|
||||
///
|
||||
/// The labels keep the order they were written in: a grouping is a set, and the tree records the
|
||||
/// query rather than a key it derived from one.
|
||||
fn grouping(modifier: Option<&LabelModifier>) -> Grouping {
|
||||
/// The written labels, as the tree stores them.
|
||||
fn labels(labels: &[String]) -> Arc<[Arc<str>]> {
|
||||
labels
|
||||
.iter()
|
||||
.map(|label| Arc::<str>::from(label.as_str()))
|
||||
.collect()
|
||||
}
|
||||
match modifier {
|
||||
// No modifier and `by()` ask for the same grouping: no label is kept, so every series of the
|
||||
// input falls into one group.
|
||||
None => Grouping::By(Arc::new([])),
|
||||
Some(LabelModifier::Include(by)) => Grouping::By(labels(&by.labels)),
|
||||
Some(LabelModifier::Exclude(without)) => Grouping::Without(labels(&without.labels)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compiles a call: a range function opens a window over the selector it is given, and a math
|
||||
/// function maps the value of its first argument.
|
||||
///
|
||||
@@ -181,9 +593,8 @@ fn compile_call(context: &Context, call: &Call) -> Result<NativeNode> {
|
||||
return vector_call(context, func.name, op, vector);
|
||||
}
|
||||
// The name is looked up before the call's arguments are read, so a name this compiler has no
|
||||
// kernel for is refused as such whichever arguments it was given: `pi()` and `time()` have no
|
||||
// vector to map, and the rejection names the name rather than describing a vector they never
|
||||
// wrote.
|
||||
// kernel for is refused as such whichever argument it was given: `absent(x)` is refused as a
|
||||
// name with no kernel, not as a selector this compiler could not compile.
|
||||
let Some((op, vector)) = math_op(func.name, &args.args)? else {
|
||||
return Err(not_implemented(&format!(
|
||||
"a call to `{}`, which has no native kernel",
|
||||
@@ -193,10 +604,17 @@ fn compile_call(context: &Context, call: &Call) -> Result<NativeNode> {
|
||||
map_call(context, func.name, op, vector)
|
||||
}
|
||||
|
||||
/// Binds a range function over the range selector it is given, and the window that selector opens.
|
||||
/// Binds a range function over the matrix it is given, and the window that matrix opens.
|
||||
///
|
||||
/// The parameters come first and the range selector is last, for both signatures: a function without
|
||||
/// parameters has no `parameters` to read.
|
||||
/// Where the matrix sits is the signature's, not a convention: `quantile_over_time(0.9, x[5m])` writes
|
||||
/// its constant before the matrix, while `predict_linear(x[5m], 300)` and
|
||||
/// `double_exponential_smoothing(x[5m], 0.5, 0.1)` write theirs after it, and a function with no
|
||||
/// constants at all reads the matrix first. [`RangeSignature::selector_index`] is that position, and
|
||||
/// everything else the call wrote is a constant — which keeps the position it was written at, so a
|
||||
/// rejection names the argument the query wrote rather than the constant's offset among the constants.
|
||||
///
|
||||
/// The matrix is either shape: a range selector, which reads raw samples, or a subquery, which
|
||||
/// evaluates its own expression on a grid of its own.
|
||||
fn window_call(
|
||||
context: &Context,
|
||||
name: &str,
|
||||
@@ -205,7 +623,7 @@ fn window_call(
|
||||
) -> Result<NativeNode> {
|
||||
// The count is the signature's, so the rejection names what this function takes rather than
|
||||
// what one argument happens to be.
|
||||
let expected = signature.parameters() + 1;
|
||||
let expected = signature.arity();
|
||||
if args.len() != expected {
|
||||
return Err(not_implemented(&format!(
|
||||
"a call to `{name}` with {} argument(s), where it takes {expected}: {}",
|
||||
@@ -213,18 +631,24 @@ fn window_call(
|
||||
signature.arguments()
|
||||
)));
|
||||
}
|
||||
let (parameters, selector) = args.split_at(signature.parameters());
|
||||
let function = match signature {
|
||||
RangeSignature::Selector(function) => function,
|
||||
RangeSignature::Scalar(bind) => bind(constant_argument(name, ¶meters[0], 0)?),
|
||||
};
|
||||
let argument = &selector[0];
|
||||
let PromExpr::MatrixSelector(MatrixSelector { vs, range }) = unparen(argument) else {
|
||||
return Err(not_implemented(&format!(
|
||||
"a call to `{name}` whose argument is `{argument}`"
|
||||
)));
|
||||
};
|
||||
window_selection(context, function, vs, range)
|
||||
// The matrix sits where the signature says it does, and everything else is a constant. Reading it
|
||||
// by position rather than by "the last argument" is what lets `predict_linear(x[5m], 300)` and
|
||||
// `quantile_over_time(0.9, x[5m])` share one path.
|
||||
let selector_index = signature.selector_index();
|
||||
// The selector stays in its original argument position for signature binding, while either
|
||||
// matrix form can provide the samples folded by the range function.
|
||||
let argument = &args[selector_index];
|
||||
let function = signature.bind(name, args)?;
|
||||
match unparen(argument) {
|
||||
PromExpr::MatrixSelector(MatrixSelector { vs, range }) => {
|
||||
window_selection(context, function, vs, range)
|
||||
}
|
||||
PromExpr::Subquery(subquery) => subquery_window(context, name, function, subquery),
|
||||
other => Err(not_implemented(&format!(
|
||||
"a call to `{name}` whose matrix is `{other}`, where it has to be a range \
|
||||
selector like `x[5m]` or a subquery like `x[5m:1m]`"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps `op` over the value of `vector`, which is the first argument of a math function.
|
||||
@@ -420,7 +844,7 @@ fn window_selection(
|
||||
let anchor_ms = selector_anchor_ms(&vs.at, context.stmt)?;
|
||||
// The grid is the last thing built: a query refused above, or a window that cannot exist,
|
||||
// never pays for a grid nothing would read.
|
||||
let output_grid = statement_grid(context.stmt)?;
|
||||
let output_grid = Arc::clone(&context.grid);
|
||||
|
||||
// A range function reports one value per input field, so the layout is the input's.
|
||||
let output = NativeType::Samples(Arc::new(SeriesSchema {
|
||||
@@ -458,6 +882,115 @@ fn window_selection(
|
||||
})
|
||||
}
|
||||
|
||||
/// The step a subquery names when it names none, as in `x[5m:]`.
|
||||
///
|
||||
/// Prometheus falls back to the global evaluation interval, whose default is a minute, rather than to
|
||||
/// the statement's own step. The existing planner folds the same default; the constant is restated
|
||||
/// here because that one is private to the planner, and it is a Prometheus protocol constant rather
|
||||
/// than a number either caller may choose.
|
||||
const DEFAULT_SUBQUERY_STEP_MS: i64 = 60_000;
|
||||
|
||||
/// Binds `function` over `subquery`, whose inner expression is evaluated on a grid of its own.
|
||||
///
|
||||
/// A range function folds a matrix, and PromQL writes a matrix two ways. A range selector reads raw
|
||||
/// samples from the scan; a subquery builds one by evaluating an expression. That is why this node
|
||||
/// declares `input_grid`: `rate(x[5m:1m])` samples `x` at one-minute steps over the five-minute
|
||||
/// lookback, and the window folds those points rather than raw samples. The inner expression is
|
||||
/// compiled against the subquery's grid for the same reason — a selector inside it has to land where
|
||||
/// the window will read it.
|
||||
///
|
||||
/// An `@` anchor makes the subquery step-invariant: every output point reads the same interval,
|
||||
/// `(anchor - offset - range, anchor - offset]`. The child grid is built for that interval, while
|
||||
/// the output grid remains the statement's grid; the anchor, range and offset are bound together in
|
||||
/// the window.
|
||||
fn subquery_window(
|
||||
context: &Context,
|
||||
name: &str,
|
||||
function: RangeFunction,
|
||||
subquery: &SubqueryExpr,
|
||||
) -> Result<NativeNode> {
|
||||
let SubqueryExpr {
|
||||
expr,
|
||||
range,
|
||||
step,
|
||||
offset,
|
||||
at,
|
||||
} = subquery;
|
||||
// The anchor is resolved by the same function a range selector's is, so `start()`, `end()` and a
|
||||
// literal — including a negative one — mean the same thing in both shapes.
|
||||
let anchor_ms = selector_anchor_ms(at, context.stmt)?;
|
||||
// A subquery evaluates an expression and hands the window one value per grid point per field, so
|
||||
// the expression inside it has to be a vector: a scalar has no fields for the window to fold.
|
||||
if expr.value_type() != ValueType::Vector {
|
||||
return Err(not_implemented(&format!(
|
||||
"a call to `{name}` over the subquery `{subquery}`, whose expression is a {}, where a \
|
||||
subquery has to evaluate a vector",
|
||||
expr.value_type()
|
||||
)));
|
||||
}
|
||||
let range_ms = range_millis(range.as_millis())?;
|
||||
// A subquery step of zero names no evaluation point at all, so it is reported as its own value
|
||||
// rather than as an empty grid.
|
||||
let step_ms = match step {
|
||||
Some(step) => millis(step.as_millis()).ok_or_else(|| {
|
||||
DataFusionError::Plan(format!(
|
||||
"the subquery's step of {}ms is out of range",
|
||||
step.as_millis()
|
||||
))
|
||||
})?,
|
||||
None => DEFAULT_SUBQUERY_STEP_MS,
|
||||
};
|
||||
if step_ms <= 0 {
|
||||
return Err(DataFusionError::Plan(format!(
|
||||
"the subquery's step is {step_ms}ms, and a step has to be longer than zero"
|
||||
)));
|
||||
}
|
||||
let offset_ms = selector_offset_ms(offset)?;
|
||||
let window = RangeWindow {
|
||||
range_ms,
|
||||
offset_ms,
|
||||
anchor_ms,
|
||||
};
|
||||
let child_spec = subquery_grid(context.grid.spec(), window, step_ms)?.ok_or_else(|| {
|
||||
not_implemented(&format!(
|
||||
"the subquery over {range_ms}ms at {step_ms}ms per step, which names no evaluation point \
|
||||
in its anchored interval or the statement's range"
|
||||
))
|
||||
})?;
|
||||
let child_grid = Arc::new(EvalGrid::try_deferred(child_spec).map_err(grid_error)?);
|
||||
let inner = compile_expr(
|
||||
&Context {
|
||||
stmt: context.stmt,
|
||||
input: Arc::clone(&context.input),
|
||||
ordering: context.ordering,
|
||||
grid: Arc::clone(&child_grid),
|
||||
},
|
||||
expr,
|
||||
)?;
|
||||
|
||||
// A range function reports one value per input field, and the inner expression reports one per
|
||||
// field of the same raw input, so the layout is the input's.
|
||||
let output = NativeType::Samples(Arc::new(SeriesSchema {
|
||||
fields: Arc::clone(&context.input.fields),
|
||||
eval_grid: Some(Arc::clone(&context.grid)),
|
||||
}));
|
||||
|
||||
Ok(NativeNode {
|
||||
expr: NativeExpr::Window {
|
||||
input: Box::new(inner),
|
||||
op: BoundWindow {
|
||||
function,
|
||||
window,
|
||||
// The one field that tells this window from a raw one: its input was evaluated on the
|
||||
// subquery's grid rather than read from the scan.
|
||||
input_grid: Some(child_grid),
|
||||
output_grid: Arc::clone(&context.grid),
|
||||
},
|
||||
},
|
||||
output,
|
||||
})
|
||||
}
|
||||
|
||||
/// Compiles a bare selector into an instant selection over the raw input.
|
||||
///
|
||||
/// The lookback, the offset and the grid are the whole of an instant selection: it reports the last
|
||||
@@ -475,7 +1008,7 @@ fn instant_selection(context: &Context, selector: &VectorSelector) -> Result<Nat
|
||||
}
|
||||
let lookback_ms = lookback_millis(context.stmt.lookback_delta.as_millis())?;
|
||||
let offset_ms = selector_offset_ms(&selector.offset)?;
|
||||
let output_grid = statement_grid(context.stmt)?;
|
||||
let output_grid = Arc::clone(&context.grid);
|
||||
|
||||
// An instant selection reports one value per input field, so the layout is the input's.
|
||||
let output = NativeType::Samples(Arc::new(SeriesSchema {
|
||||
@@ -526,7 +1059,7 @@ fn statement_grid(stmt: &EvalStmt) -> Result<Arc<EvalGrid>> {
|
||||
{error}"
|
||||
))
|
||||
})?;
|
||||
Ok(Arc::new(EvalGrid::try_new(spec).map_err(grid_error)?))
|
||||
Ok(Arc::new(EvalGrid::try_deferred(spec).map_err(grid_error)?))
|
||||
}
|
||||
|
||||
/// The window length of a range selector, which has to be longer than zero to select anything.
|
||||
@@ -602,34 +1135,74 @@ fn grid_error(error: datatypes::error::Error) -> DataFusionError {
|
||||
DataFusionError::Plan(format!("the statement's grid is invalid: {error}"))
|
||||
}
|
||||
|
||||
/// What a call to a range function has to be: the parameters it reads, and how to bind them.
|
||||
/// What a call to a range function has to be: where its range selector sits, how many constants it
|
||||
/// reads, and how they bind.
|
||||
///
|
||||
/// The selector's position is a variant rather than a convention because PromQL does not put it in
|
||||
/// one place: `quantile_over_time(0.9, x[5m])` writes its quantile before the selector, while
|
||||
/// `predict_linear(x[5m], 300)` and `double_exponential_smoothing(x[5m], 0.5, 0.1)` write theirs
|
||||
/// after it. A single "parameters come first" rule would read the selector as a constant in the
|
||||
/// second group and reject an ordinary query, so each shape states its own layout.
|
||||
#[derive(Clone, Copy)]
|
||||
enum RangeSignature {
|
||||
/// A function that reads only its range selector, as in `rate(x[5m])`.
|
||||
Selector(RangeFunction),
|
||||
/// A function that reads one constant before its range selector, and the function that constant
|
||||
/// binds, as in `quantile_over_time(0.9, x[5m])`.
|
||||
///
|
||||
/// The constant is kept out of the variant name because only the call site knows it.
|
||||
Scalar(fn(f64) -> RangeFunction),
|
||||
/// One constant, then the range selector, as in `quantile_over_time(0.9, x[5m])`.
|
||||
ConstantBeforeSelector(fn(f64) -> RangeFunction),
|
||||
/// The range selector, then one constant, as in `predict_linear(x[5m], 300)`.
|
||||
ConstantAfterSelector(fn(f64) -> RangeFunction),
|
||||
/// The range selector, then two constants, as in
|
||||
/// `double_exponential_smoothing(x[5m], 0.5, 0.1)`.
|
||||
TwoConstantsAfterSelector(fn(f64, f64) -> RangeFunction),
|
||||
}
|
||||
|
||||
impl RangeSignature {
|
||||
/// How many scalar parameters the function reads before its range selector.
|
||||
fn parameters(self) -> usize {
|
||||
/// The position of the range selector among the call's arguments.
|
||||
fn selector_index(self) -> usize {
|
||||
match self {
|
||||
Self::Selector(_)
|
||||
| Self::ConstantAfterSelector(_)
|
||||
| Self::TwoConstantsAfterSelector(_) => 0,
|
||||
Self::ConstantBeforeSelector(_) => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// How many constants the function reads.
|
||||
fn constants(self) -> usize {
|
||||
match self {
|
||||
Self::Selector(_) => 0,
|
||||
Self::Scalar(_) => 1,
|
||||
Self::ConstantBeforeSelector(_) | Self::ConstantAfterSelector(_) => 1,
|
||||
Self::TwoConstantsAfterSelector(_) => 2,
|
||||
}
|
||||
}
|
||||
|
||||
/// How many arguments the call writes, its range selector counted in.
|
||||
fn arity(self) -> usize {
|
||||
self.constants() + 1
|
||||
}
|
||||
|
||||
/// What the call's arguments are, for the arity rejection to name them.
|
||||
fn arguments(self) -> &'static str {
|
||||
match self {
|
||||
Self::Selector(_) => "its range selector",
|
||||
Self::Scalar(_) => "a number and its range selector",
|
||||
Self::ConstantBeforeSelector(_) => "a number and its range selector",
|
||||
Self::ConstantAfterSelector(_) => "its range selector and a number",
|
||||
Self::TwoConstantsAfterSelector(_) => "its range selector and two numbers",
|
||||
}
|
||||
}
|
||||
|
||||
/// Binds the function from the call's arguments, which have already passed the arity check.
|
||||
fn bind(self, name: &str, args: &[Box<PromExpr>]) -> Result<RangeFunction> {
|
||||
Ok(match self {
|
||||
Self::Selector(function) => function,
|
||||
Self::ConstantBeforeSelector(bind) => bind(constant_argument(name, &args[0], 0)?),
|
||||
Self::ConstantAfterSelector(bind) => bind(constant_argument(name, &args[1], 1)?),
|
||||
Self::TwoConstantsAfterSelector(bind) => bind(
|
||||
constant_argument(name, &args[1], 1)?,
|
||||
constant_argument(name, &args[2], 2)?,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The signature the PromQL name calls, or `None` when this compiler has no range kernel for it.
|
||||
@@ -640,7 +1213,17 @@ impl RangeSignature {
|
||||
/// its name alone cannot produce the variant the tree binds.
|
||||
fn range_signature(name: &str) -> Option<RangeSignature> {
|
||||
match name {
|
||||
"quantile_over_time" => Some(RangeSignature::Scalar(RangeFunction::QuantileOverTime)),
|
||||
"quantile_over_time" => Some(RangeSignature::ConstantBeforeSelector(
|
||||
RangeFunction::QuantileOverTime,
|
||||
)),
|
||||
// These two write their constants after the selector, which is the opposite order to
|
||||
// `quantile_over_time`.
|
||||
"predict_linear" => Some(RangeSignature::ConstantAfterSelector(
|
||||
RangeFunction::PredictLinear,
|
||||
)),
|
||||
"double_exponential_smoothing" => Some(RangeSignature::TwoConstantsAfterSelector(
|
||||
|smoothing, trend| RangeFunction::DoubleExponentialSmoothing { smoothing, trend },
|
||||
)),
|
||||
_ => range_function(name).map(RangeSignature::Selector),
|
||||
}
|
||||
}
|
||||
@@ -805,14 +1388,18 @@ fn arity_error(name: &str, signature: MathSignature, given: usize) -> DataFusion
|
||||
/// case — has nowhere to go. It is refused rather than answered from the wrong number, and the
|
||||
/// rejection names the expression, because that is what the query has to change.
|
||||
///
|
||||
/// `name` is whoever reads the argument: a call's function name, or an aggregator's, since
|
||||
/// `quantile(0.9, x)` reads a constant too. The rejection therefore names that and the position
|
||||
/// rather than calling everything a call, which would be false for an aggregate.
|
||||
///
|
||||
/// A quantile outside `[0, 1]` is not refused here: Prometheus binds it and the kernel reports
|
||||
/// `-Infinity` below zero and `+Infinity` above one, so refusing it would be this compiler inventing
|
||||
/// a rule the query engine does not have.
|
||||
fn constant_argument(name: &str, argument: &PromExpr, position: usize) -> Result<f64> {
|
||||
constant_value(argument).ok_or_else(|| {
|
||||
not_implemented(&format!(
|
||||
"a call to `{name}` whose {} is `{argument}`, where it has to be a constant expression \
|
||||
this compiler folds to a number literal",
|
||||
"`{name}`, whose {} is `{argument}`, where it has to be a constant expression this \
|
||||
compiler folds to a number literal",
|
||||
argument_position(position)
|
||||
))
|
||||
})
|
||||
@@ -820,14 +1407,30 @@ fn constant_argument(name: &str, argument: &PromExpr, position: usize) -> Result
|
||||
|
||||
/// How a rejection names the argument a parameter was read from.
|
||||
///
|
||||
/// The three positions are the ones a signature can read: the vector, and the two constants behind
|
||||
/// it. Every caller passes the literal position its signature reads, so there is no fourth position
|
||||
/// to name and no default to invent one for.
|
||||
/// The three positions are the ones a supported call can write: a math function's vector and the two
|
||||
/// constants behind it, or a range function's selector and the constants on either side of it. Every
|
||||
/// caller passes the literal position it read, so there is no fourth position to name and no default
|
||||
/// to invent one for.
|
||||
fn argument_position(position: usize) -> &'static str {
|
||||
const POSITIONS: [&str; 3] = ["first argument", "second argument", "third argument"];
|
||||
POSITIONS[position]
|
||||
}
|
||||
|
||||
/// Whether a binary operation carries a modifier this compiler refuses to bind.
|
||||
///
|
||||
/// A scalar carries no labels, so there is nothing to match or name, and the tree's matching has no
|
||||
/// field for a fill value: binding the operation would compute it without the modifier the query
|
||||
/// asked for. The fold and [`binary_operation`] share this test rather than restating it, because a
|
||||
/// fold that computed a constant without reading its modifier would answer a query the dispatch
|
||||
/// below refuses.
|
||||
fn refuses_modifier(modifier: Option<&BinModifier>) -> bool {
|
||||
modifier.is_some_and(|modifier| {
|
||||
modifier.matching.is_some()
|
||||
|| modifier.card != VectorMatchCardinality::OneToOne
|
||||
|| modifier.fill_values != VectorMatchFillValues::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// The number `expr` folds to, or `None` when its value is not the statement's to know.
|
||||
///
|
||||
/// Only what the statement itself decides is folded: a number literal, a group around one, a negated
|
||||
@@ -847,10 +1450,28 @@ fn constant_value(expr: &PromExpr) -> Option<f64> {
|
||||
PromExpr::Paren(paren) => constant_value(&paren.expr),
|
||||
PromExpr::Unary(unary) => constant_value(&unary.expr).map(|value| -value),
|
||||
PromExpr::Binary(binary) => {
|
||||
let op = arithmetic_op(&binary.op)?;
|
||||
// A modifier is read before the operands are folded: `x * (1 + fill(0) 2)` asks for a
|
||||
// fill, and folding the inner addition would answer `x * 3` with the fill gone. The
|
||||
// expression is left unfolded so the dispatch below refuses it with the reason it
|
||||
// writes, which is also why this is a `None` rather than an error here.
|
||||
if refuses_modifier(binary.modifier.as_ref()) {
|
||||
return None;
|
||||
}
|
||||
let left = constant_value(&binary.lhs)?;
|
||||
let right = constant_value(&binary.rhs)?;
|
||||
Some(op.calculate(left, right))
|
||||
if let Some(op) = arithmetic_op(&binary.op) {
|
||||
return Some(op.calculate(left, right));
|
||||
}
|
||||
// A comparison of two constants is the number it reports when it is written with
|
||||
// `bool`: Prometheus reads `1 > bool 2` as `0`, and [`ComparisonOp::apply`] is the same
|
||||
// answer the kernel gives. Without `bool` there is no number to report -- `1 > 2` is a
|
||||
// parse error in Prometheus -- so it is left unfolded and the dispatch refuses it.
|
||||
if binary.return_bool()
|
||||
&& let Some(op) = comparison_op(&binary.op)
|
||||
{
|
||||
return Some(f64::from(op.apply(left, right)));
|
||||
}
|
||||
None
|
||||
}
|
||||
// Calls (`time()`, `scalar(v)`), selectors, range selectors, aggregates and subqueries all
|
||||
// read something this compiler cannot know before the query runs.
|
||||
@@ -862,8 +1483,9 @@ fn constant_value(expr: &PromExpr) -> Option<f64> {
|
||||
///
|
||||
/// The token is matched by its id, which is stable and is what the rest of the planner matches on,
|
||||
/// rather than by the text it displays as: a symbol this compiler does not recognise is not
|
||||
/// arithmetic here rather than a wrong operator. A comparison and a set operator are the cases that
|
||||
/// reach this and fold nothing, which is this batch's boundary rather than an accident of the map.
|
||||
/// arithmetic here rather than a wrong operator. A comparison is `None` here and is folded by
|
||||
/// [`comparison_op`] instead, because only a `bool` comparison of two constants has a number to
|
||||
/// report; a set operator folds nothing at all.
|
||||
fn arithmetic_op(op: &TokenType) -> Option<ArithmeticOp> {
|
||||
match op.id() {
|
||||
token::T_ADD => Some(ArithmeticOp::Add),
|
||||
@@ -877,6 +1499,22 @@ fn arithmetic_op(op: &TokenType) -> Option<ArithmeticOp> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The comparison operator a PromQL token denotes, or `None` when the token is not a comparison.
|
||||
///
|
||||
/// This is the folding half of [`binary_op`], which binds the same tokens to a node: the fold and
|
||||
/// the node have to agree on which token is which comparison, so the two maps sit beside each other.
|
||||
fn comparison_op(op: &TokenType) -> Option<ComparisonOp> {
|
||||
match op.id() {
|
||||
token::T_EQLC => Some(ComparisonOp::Equal),
|
||||
token::T_NEQ => Some(ComparisonOp::NotEqual),
|
||||
token::T_GTR => Some(ComparisonOp::Greater),
|
||||
token::T_LSS => Some(ComparisonOp::Less),
|
||||
token::T_GTE => Some(ComparisonOp::GreaterEqual),
|
||||
token::T_LTE => Some(ComparisonOp::LessEqual),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The range function a PromQL name denotes, or `None` when this compiler has no kernel for it.
|
||||
///
|
||||
/// The map is explicit on purpose: a name that is not listed here is rejected, so supporting one
|
||||
@@ -901,6 +1539,13 @@ fn range_function(name: &str) -> Option<RangeFunction> {
|
||||
"resets" => Some(RangeFunction::Resets),
|
||||
"changes" => Some(RangeFunction::Changes),
|
||||
"deriv" => Some(RangeFunction::Deriv),
|
||||
// These functions are supported unconditionally by this native compiler.
|
||||
"first_over_time" => Some(RangeFunction::FirstOverTime),
|
||||
"mad_over_time" => Some(RangeFunction::MadOverTime),
|
||||
"ts_of_first_over_time" => Some(RangeFunction::TsOfFirstOverTime),
|
||||
"ts_of_last_over_time" => Some(RangeFunction::TsOfLastOverTime),
|
||||
"ts_of_min_over_time" => Some(RangeFunction::TsOfMinOverTime),
|
||||
"ts_of_max_over_time" => Some(RangeFunction::TsOfMaxOverTime),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user