mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-29 22:24:02 +00:00
feat(promql): expose selector scan requirements and input bindings
Signed-off-by: discord9 <discord9@163.com>
(cherry picked from commit ee992adb28)
This commit is contained in:
@@ -126,6 +126,7 @@
|
||||
//! chunks of different series that interleave, so an `Interleaved` ordering is refused once, at the
|
||||
//! entry, and [`NativeNode::validate`] refuses the same ordering for a tree built some other way.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -137,6 +138,7 @@ use promql::histogram_eval::HistogramFunction;
|
||||
use promql::map::{ArithmeticOp, ComparisonOp, MathOp};
|
||||
use promql::map_label::LabelRewrite;
|
||||
use promql::map_time::DateFunction;
|
||||
use promql::native::metrics::NodePath;
|
||||
use promql::native::plan::{
|
||||
AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundScalar,
|
||||
BoundVectorOp, BoundWindow, Grouping, MapOp, NativeExpr, NativeNode, NativeTree, NativeType,
|
||||
@@ -145,6 +147,7 @@ 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::label::Matchers;
|
||||
use promql_parser::parser::token::TokenType;
|
||||
use promql_parser::parser::value::ValueType;
|
||||
use promql_parser::parser::{
|
||||
@@ -155,6 +158,94 @@ use promql_parser::parser::{
|
||||
|
||||
use crate::promql::label_values::signed_millis_since_epoch;
|
||||
|
||||
/// The time horizon one selector reads.
|
||||
///
|
||||
/// A selector reads either one instant per grid point, within the statement's lookback, or a window
|
||||
/// before it. The two differ in which side of the lookback or range their scan starts on, so the
|
||||
/// distinction travels with the requirement rather than being re-derived by each caller.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum SelectorTiming {
|
||||
/// Instant selection excludes the lookback's lower boundary, except at lookback zero.
|
||||
Instant { lookback_ms: i64 },
|
||||
/// A range selector excludes its window's lower boundary.
|
||||
Window { range_ms: i64 },
|
||||
}
|
||||
|
||||
/// What one selector asks a scan for, apart from the layout it is bound to.
|
||||
///
|
||||
/// The grid is the grid this selector is evaluated on, whose final point may precede the statement's
|
||||
/// `end`. To derive the raw scan interval, shift grid points by `offset_ms`; an instant selection
|
||||
/// extends its lower bound by the lookback, a window by its range. For an anchored window, the anchor
|
||||
/// stands in for the grid endpoints. Instant bounds are closed only at lookback zero; window bounds
|
||||
/// are open on the left. These bounds form a scan hull: with a sparse grid they may include
|
||||
/// timestamps that contribute to no evaluation point.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SelectorRequirement {
|
||||
/// The metric the selector names, when it names one.
|
||||
pub metric: Option<String>,
|
||||
/// The selector's label matchers, as written.
|
||||
pub matchers: Matchers,
|
||||
/// Selector offset; positive reads earlier samples.
|
||||
pub offset_ms: i64,
|
||||
/// The resolved `@` anchor, if the selector has one.
|
||||
pub at_ms: Option<i64>,
|
||||
/// The first point of the grid this selector is evaluated on.
|
||||
pub grid_start_ms: i64,
|
||||
/// The last point of that grid, which may precede the statement's end.
|
||||
pub grid_end_ms: i64,
|
||||
/// What this selector reads around each point.
|
||||
pub timing: SelectorTiming,
|
||||
}
|
||||
|
||||
impl SelectorRequirement {
|
||||
/// The raw scan endpoints this requirement covers and whether the lower one is included.
|
||||
pub fn raw_scan_bounds(&self) -> Result<(i64, i64, bool)> {
|
||||
let end = self
|
||||
.at_ms
|
||||
.unwrap_or(self.grid_end_ms)
|
||||
.checked_sub(self.offset_ms)
|
||||
.ok_or_else(|| DataFusionError::Plan("selector scan end overflows".into()))?;
|
||||
// An anchored selector reads the same point at every step, so the anchor stands in for the
|
||||
// grid's first point on both edges; unanchored, the earliest point the selector reads is the
|
||||
// grid's own start.
|
||||
let lower_base = self
|
||||
.at_ms
|
||||
.unwrap_or(self.grid_start_ms)
|
||||
.checked_sub(self.offset_ms)
|
||||
.ok_or_else(|| DataFusionError::Plan("selector scan start overflows".into()))?;
|
||||
let (duration, inclusive) = match self.timing {
|
||||
SelectorTiming::Instant { lookback_ms } => (lookback_ms, lookback_ms == 0),
|
||||
SelectorTiming::Window { range_ms } => (range_ms, false),
|
||||
};
|
||||
let start = lower_base
|
||||
.checked_sub(duration)
|
||||
.ok_or_else(|| DataFusionError::Plan("selector scan start overflows".into()))?;
|
||||
Ok((start, end, inclusive))
|
||||
}
|
||||
}
|
||||
|
||||
/// One input a compiled tree reads, and the selector it was bound to.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CompiledInput {
|
||||
/// Where the executor reads this input in the compiled tree.
|
||||
pub path: NodePath,
|
||||
/// What the selector asked the scan for.
|
||||
pub requirement: SelectorRequirement,
|
||||
/// The layout the caller bound this selector to.
|
||||
pub schema: Arc<SeriesSchema>,
|
||||
/// The ordering the caller's scan guarantees for this input.
|
||||
pub ordering: SeriesOrdering,
|
||||
}
|
||||
|
||||
/// A compiled tree and the inputs it reads.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CompiledTree {
|
||||
/// The bound tree to run.
|
||||
pub tree: NativeTree,
|
||||
/// One entry per input node, in the order the executor visits them.
|
||||
pub inputs: Arc<[CompiledInput]>,
|
||||
}
|
||||
|
||||
/// The shapes this compiler accepts, quoted in every rejection.
|
||||
///
|
||||
/// It is one sentence of grammar rather than a list of examples, because it is what a refused query
|
||||
@@ -172,8 +263,8 @@ const SUPPORTED: &str = "a selector — `x`, optionally with an `offset` — a r
|
||||
(`label_replace(x, \"a\", \"$1\", \"b\", \"(.*)\")`), 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 \
|
||||
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)`; \
|
||||
`topk`, `bottomk`, `limitk`, `limit_ratio` or `count_values` over any of \
|
||||
@@ -206,13 +297,40 @@ const SUPPORTED: &str = "a selector — `x`, optionally with an `offset` — a r
|
||||
///
|
||||
/// 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
|
||||
/// is fine while a tree has one selector; a tree with two inputs will need each `Input` to name its
|
||||
/// selector.
|
||||
/// is fine while a tree has one selector; a tree that reads two scans is refused here, and
|
||||
/// [`compile_selectors`] is what a caller that knows which scan feeds which selector calls.
|
||||
pub fn compile(
|
||||
stmt: &EvalStmt,
|
||||
input: Arc<SeriesSchema>,
|
||||
ordering: SeriesOrdering,
|
||||
) -> Result<NativeTree> {
|
||||
let compiled = compile_selectors(stmt, &|_requirement| Ok(Arc::clone(&input)), ordering)?;
|
||||
// The compatibility entry answers with the tree alone, so a statement that needs more than one
|
||||
// input would lose the identity of all but one of them here. Callers that know which scan feeds
|
||||
// which selector call [`compile_selectors`] instead.
|
||||
if compiled.inputs.len() > 1 {
|
||||
return Err(not_implemented(&format!(
|
||||
"`compile` with {} selectors; use `compile_selectors` to bind each input",
|
||||
compiled.inputs.len()
|
||||
)));
|
||||
}
|
||||
Ok(compiled.tree)
|
||||
}
|
||||
|
||||
/// Compiles `stmt` into a tree and the inputs it reads, binding each selector through `bindings`.
|
||||
///
|
||||
/// `bindings` answers what a scan of one selector produces, given everything that selector asks for:
|
||||
/// its metric and matchers, its offset and anchor, the grid it is evaluated on and whether it reads
|
||||
/// an instant or a window. Two selectors of one statement may be fed differently shaped samples, so
|
||||
/// the answer is asked per selector rather than once per statement.
|
||||
///
|
||||
/// Each selector's path is recorded as its recursive node is compiled, so its requirement and
|
||||
/// schema stay structurally paired with the input the executor names at that position.
|
||||
pub fn compile_selectors(
|
||||
stmt: &EvalStmt,
|
||||
bindings: &dyn Fn(&SelectorRequirement) -> Result<Arc<SeriesSchema>>,
|
||||
ordering: SeriesOrdering,
|
||||
) -> Result<CompiledTree> {
|
||||
// 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.
|
||||
@@ -235,18 +353,26 @@ pub fn compile(
|
||||
stmt.expr.value_type()
|
||||
)));
|
||||
}
|
||||
let inputs = RefCell::new(Vec::new());
|
||||
let context = Context {
|
||||
stmt,
|
||||
input,
|
||||
bindings,
|
||||
ordering,
|
||||
inputs: &inputs,
|
||||
// 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)?,
|
||||
path: NodePath::root(),
|
||||
};
|
||||
// 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
|
||||
// illegal in another.
|
||||
NativeTree::try_new(compile_expr(&context, &stmt.expr)?)
|
||||
let tree = NativeTree::try_new(compile_expr(&context, &stmt.expr)?)?;
|
||||
let inputs = inputs.into_inner();
|
||||
Ok(CompiledTree {
|
||||
tree,
|
||||
inputs: inputs.into(),
|
||||
})
|
||||
}
|
||||
|
||||
/// What every node of one compilation derives from the statement rather than from the expression.
|
||||
@@ -257,8 +383,13 @@ pub fn compile(
|
||||
struct Context<'a> {
|
||||
/// The statement being compiled.
|
||||
stmt: &'a EvalStmt,
|
||||
/// The raw series the caller bound; shared by every input node rather than copied per node.
|
||||
input: Arc<SeriesSchema>,
|
||||
/// The layout to feed each selector in, asked once per selector.
|
||||
bindings: &'a dyn Fn(&SelectorRequirement) -> Result<Arc<SeriesSchema>>,
|
||||
/// The inputs bound so far, in the order they were bound.
|
||||
///
|
||||
/// Shared rather than owned because a subquery compiles its body through a context of its own,
|
||||
/// and the inputs that body binds are still this compilation's inputs.
|
||||
inputs: &'a RefCell<Vec<CompiledInput>>,
|
||||
/// The ordering the caller's scan guarantees.
|
||||
ordering: SeriesOrdering,
|
||||
/// The grid the expression being compiled is evaluated on.
|
||||
@@ -268,6 +399,21 @@ struct Context<'a> {
|
||||
/// 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>,
|
||||
/// Structural location of the node being compiled.
|
||||
path: NodePath,
|
||||
}
|
||||
|
||||
impl Context<'_> {
|
||||
fn with_path(&self, path: NodePath) -> Context<'_> {
|
||||
Context {
|
||||
stmt: self.stmt,
|
||||
bindings: self.bindings,
|
||||
inputs: self.inputs,
|
||||
ordering: self.ordering,
|
||||
grid: Arc::clone(&self.grid),
|
||||
path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compiles `expr` into the node it denotes, recursing into whatever it is made of.
|
||||
@@ -322,7 +468,7 @@ fn negate(context: &Context, operand: &PromExpr) -> Result<NativeNode> {
|
||||
// the tree should see the same order the query was written in.
|
||||
Ok(broadcast(
|
||||
scalar_node(context, -1.0),
|
||||
compile_expr(context, operand)?,
|
||||
compile_expr(&context.with_path(context.path.right()), operand)?,
|
||||
BinaryOp::Mul,
|
||||
false,
|
||||
))
|
||||
@@ -395,8 +541,8 @@ fn binary_operation(context: &Context, binary: &BinaryExpr) -> Result<NativeNode
|
||||
// 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)?,
|
||||
compile_expr(&context.with_path(context.path.left()), &binary.lhs)?,
|
||||
compile_expr(&context.with_path(context.path.right()), &binary.rhs)?,
|
||||
op,
|
||||
binary.return_bool(),
|
||||
))
|
||||
@@ -545,7 +691,7 @@ fn aggregate_expr(context: &Context, aggregate: &AggregateExpr) -> Result<Native
|
||||
)));
|
||||
}
|
||||
};
|
||||
let input = compile_expr(context, expr)?;
|
||||
let input = compile_expr(&context.with_path(context.path.input()), 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();
|
||||
@@ -732,7 +878,7 @@ fn map_call(context: &Context, name: &str, op: MapOp, vector: &PromExpr) -> Resu
|
||||
vector.value_type()
|
||||
)));
|
||||
}
|
||||
let input = compile_expr(context, vector)?;
|
||||
let input = compile_expr(&context.with_path(context.path.input()), vector)?;
|
||||
let output = input.output.clone();
|
||||
Ok(NativeNode {
|
||||
expr: NativeExpr::Map {
|
||||
@@ -759,7 +905,7 @@ fn vector_call(
|
||||
vector.value_type()
|
||||
)));
|
||||
}
|
||||
let input = compile_expr(context, vector)?;
|
||||
let input = compile_expr(&context.with_path(context.path.input()), vector)?;
|
||||
let output = input.output.clone();
|
||||
Ok(NativeNode {
|
||||
expr: NativeExpr::Vector {
|
||||
@@ -908,15 +1054,28 @@ fn window_selection(
|
||||
let output_grid = Arc::clone(&context.grid);
|
||||
|
||||
// A range function reports one value per input field, so the layout is the input's.
|
||||
let (start_ms, end_ms) = grid_endpoints(&output_grid)?;
|
||||
let raw_schema = bind_selector(
|
||||
context,
|
||||
SelectorRequirement {
|
||||
metric: vs.name.clone(),
|
||||
matchers: vs.matchers.clone(),
|
||||
offset_ms,
|
||||
at_ms: anchor_ms,
|
||||
grid_start_ms: start_ms,
|
||||
grid_end_ms: end_ms,
|
||||
timing: SelectorTiming::Window { range_ms },
|
||||
},
|
||||
)?;
|
||||
let output = NativeType::Samples(Arc::new(SeriesSchema {
|
||||
fields: Arc::clone(&context.input.fields),
|
||||
fields: Arc::clone(&raw_schema.fields),
|
||||
eval_grid: Some(Arc::clone(&output_grid)),
|
||||
}));
|
||||
|
||||
// One allocation shared by the binding and the node's output: `NativeTree::try_new` compares
|
||||
// them by identity, because a compiler that meant one schema has no reason to allocate it
|
||||
// twice.
|
||||
let raw = NativeType::Samples(Arc::clone(&context.input));
|
||||
let raw = NativeType::Samples(Arc::clone(&raw_schema));
|
||||
|
||||
Ok(NativeNode {
|
||||
expr: NativeExpr::Window {
|
||||
@@ -1022,17 +1181,25 @@ fn subquery_window(
|
||||
let inner = compile_expr(
|
||||
&Context {
|
||||
stmt: context.stmt,
|
||||
input: Arc::clone(&context.input),
|
||||
bindings: context.bindings,
|
||||
inputs: context.inputs,
|
||||
ordering: context.ordering,
|
||||
grid: Arc::clone(&child_grid),
|
||||
path: context.path.input(),
|
||||
},
|
||||
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.
|
||||
// A range function reports one value per field of what it folds, so the layout is the inner
|
||||
// expression's: `rate(x[5m:1m])` folds one evaluated field per field `x` was bound to, and a
|
||||
// nested subquery keeps passing that same field set outward.
|
||||
let inner_schema = inner.output.samples().ok_or_else(|| {
|
||||
DataFusionError::Internal(
|
||||
"a subquery's body is a vector, so it reports samples rather than a scalar".into(),
|
||||
)
|
||||
})?;
|
||||
let output = NativeType::Samples(Arc::new(SeriesSchema {
|
||||
fields: Arc::clone(&context.input.fields),
|
||||
fields: Arc::clone(&inner_schema.fields),
|
||||
eval_grid: Some(Arc::clone(&context.grid)),
|
||||
}));
|
||||
|
||||
@@ -1072,13 +1239,27 @@ fn instant_selection(context: &Context, selector: &VectorSelector) -> Result<Nat
|
||||
let output_grid = Arc::clone(&context.grid);
|
||||
|
||||
// An instant selection reports one value per input field, so the layout is the input's.
|
||||
let (start_ms, end_ms) = grid_endpoints(&output_grid)?;
|
||||
let raw_schema = bind_selector(
|
||||
context,
|
||||
SelectorRequirement {
|
||||
metric: selector.name.clone(),
|
||||
matchers: selector.matchers.clone(),
|
||||
offset_ms,
|
||||
// An `@` anchor was refused above, so an instant selection never has one.
|
||||
at_ms: None,
|
||||
grid_start_ms: start_ms,
|
||||
grid_end_ms: end_ms,
|
||||
timing: SelectorTiming::Instant { lookback_ms },
|
||||
},
|
||||
)?;
|
||||
let output = NativeType::Samples(Arc::new(SeriesSchema {
|
||||
fields: Arc::clone(&context.input.fields),
|
||||
fields: Arc::clone(&raw_schema.fields),
|
||||
eval_grid: Some(Arc::clone(&output_grid)),
|
||||
}));
|
||||
|
||||
// One allocation shared by the binding and the node's output, as in the window shape.
|
||||
let raw = NativeType::Samples(Arc::clone(&context.input));
|
||||
let raw = NativeType::Samples(Arc::clone(&raw_schema));
|
||||
|
||||
Ok(NativeNode {
|
||||
expr: NativeExpr::Instant {
|
||||
@@ -1099,6 +1280,39 @@ fn instant_selection(context: &Context, selector: &VectorSelector) -> Result<Nat
|
||||
})
|
||||
}
|
||||
|
||||
/// Binds the layout `selector` will be fed in, and records what it asked the scan for.
|
||||
///
|
||||
/// The callback decides one thing — what a scan of this selector produces — while the requirement
|
||||
/// records what the selector named, how it shifts time and which points it is read at. The input
|
||||
/// path is carried through recursive compilation, so the binding is paired with its tree node here.
|
||||
fn bind_selector(context: &Context, requirement: SelectorRequirement) -> Result<Arc<SeriesSchema>> {
|
||||
let schema = (context.bindings)(&requirement)?;
|
||||
context.inputs.borrow_mut().push(CompiledInput {
|
||||
path: context.path.input(),
|
||||
requirement,
|
||||
schema: Arc::clone(&schema),
|
||||
ordering: context.ordering,
|
||||
});
|
||||
Ok(schema)
|
||||
}
|
||||
|
||||
/// The first and last point of `grid`, as milliseconds since the Unix epoch.
|
||||
///
|
||||
/// Read off the specification rather than the materialised timestamps: a subquery's grid can be
|
||||
/// deferred, and asking it for its points would allocate a vector the size of the range before
|
||||
/// anything has been evaluated.
|
||||
fn grid_endpoints(grid: &EvalGrid) -> Result<(i64, i64)> {
|
||||
let spec = grid.spec();
|
||||
let last = ((spec.len - 1) as i128)
|
||||
.checked_mul(i128::from(spec.step_ms))
|
||||
.and_then(|offset| i128::from(spec.start_ms).checked_add(offset))
|
||||
.and_then(|last| i64::try_from(last).ok())
|
||||
.ok_or_else(|| {
|
||||
DataFusionError::Internal("an evaluation grid's last point overflows".into())
|
||||
})?;
|
||||
Ok((spec.start_ms, last))
|
||||
}
|
||||
|
||||
/// The grid the statement is evaluated on: `(end - start) / step + 1` points, the last of which
|
||||
/// does not pass `end`.
|
||||
///
|
||||
|
||||
@@ -27,18 +27,20 @@ use datatypes::series::{Label, MetricName};
|
||||
use promql::evaluated::EvaluatedChunk;
|
||||
use promql::map::MathOp;
|
||||
use promql::native::execute::{NativeExecutionState, NativeExecutor};
|
||||
use promql::native::metrics::NodePath;
|
||||
use promql::native::plan::{
|
||||
AggregateKind, AggregateStage, BoundAggregate, BoundInstant, BoundMap, BoundWindow, Grouping,
|
||||
MapOp, NativeExpr, NativeNode, NativeTree, NativeType,
|
||||
AggregateKind, AggregateStage, BoundAggregate, BoundInput, BoundInstant, BoundMap, BoundWindow,
|
||||
Grouping, MapOp, NativeExpr, NativeNode, NativeTree, NativeType,
|
||||
};
|
||||
use promql::range_eval::RangeFunction;
|
||||
use promql::vector_match::{BinaryMatching, BinaryOp};
|
||||
use promql_parser::label::MatchOp;
|
||||
use promql_parser::parser::{
|
||||
AtModifier, BinModifier, EvalStmt, Expr as PromExpr, NumberLiteral, Offset,
|
||||
VectorMatchFillValues,
|
||||
};
|
||||
|
||||
use super::compile;
|
||||
use super::{SelectorRequirement, SelectorTiming, compile, compile_selectors};
|
||||
|
||||
/// The raw series schema a caller binds from the table: one field and no evaluation grid.
|
||||
fn raw_schema() -> Arc<SeriesSchema> {
|
||||
@@ -4307,4 +4309,243 @@ fn an_invalid_label_replace_expression_fails_the_statement() {
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(error, DataFusionError::Plan(_)), "{error}");
|
||||
/// Every selector is bound through the callback, so each input reports the layout its own selector
|
||||
/// asked for and the requirement beside it names the series and the matchers that selector wrote.
|
||||
#[test]
|
||||
fn selector_binding_returns_requirement_and_input_path() {
|
||||
let schema = Arc::new(SeriesSchema {
|
||||
fields: Arc::new([
|
||||
FieldSpec {
|
||||
name: "bound_a".into(),
|
||||
},
|
||||
FieldSpec {
|
||||
name: "bound_b".into(),
|
||||
},
|
||||
]),
|
||||
eval_grid: None,
|
||||
});
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt("abs(rate(x{job=\"api\", env!=\"test\"}[10s] @ end() offset 2s))"),
|
||||
&|requirement| {
|
||||
assert_eq!(requirement.metric.as_deref(), Some("x"));
|
||||
assert_eq!(requirement.matchers.matchers.len(), 2);
|
||||
assert!(
|
||||
requirement
|
||||
.matchers
|
||||
.matchers
|
||||
.iter()
|
||||
.any(|m| m.name == "job" && m.op == MatchOp::Equal && m.value == "api")
|
||||
);
|
||||
assert!(
|
||||
requirement
|
||||
.matchers
|
||||
.matchers
|
||||
.iter()
|
||||
.any(|m| m.name == "env" && m.op == MatchOp::NotEqual && m.value == "test")
|
||||
);
|
||||
assert_eq!(requirement.offset_ms, 2_000);
|
||||
assert_eq!(requirement.at_ms, Some(100_000));
|
||||
assert_eq!(
|
||||
(requirement.grid_start_ms, requirement.grid_end_ms),
|
||||
(0, 100_000)
|
||||
);
|
||||
assert_eq!(
|
||||
requirement.timing,
|
||||
SelectorTiming::Window { range_ms: 10_000 }
|
||||
);
|
||||
Ok(Arc::clone(&schema))
|
||||
},
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(compiled.inputs.len(), 1);
|
||||
let input = &compiled.inputs[0];
|
||||
assert_eq!(input.path, NodePath::root().input().input());
|
||||
assert!(Arc::ptr_eq(&input.schema, &schema));
|
||||
assert_eq!(input.ordering, SeriesOrdering::KeyThenTime);
|
||||
let NativeExpr::Map {
|
||||
input: map_input, ..
|
||||
} = &compiled.tree.root().expr
|
||||
else {
|
||||
panic!("outer expression is not a map");
|
||||
};
|
||||
let NativeExpr::Window {
|
||||
input: window_input,
|
||||
..
|
||||
} = &map_input.expr
|
||||
else {
|
||||
panic!("inner expression is not a window");
|
||||
};
|
||||
let NativeExpr::Input(BoundInput {
|
||||
output: NativeType::Samples(bound_schema),
|
||||
ordering,
|
||||
}) = &window_input.expr
|
||||
else {
|
||||
panic!(
|
||||
"window child is not the expected raw input: {:?}",
|
||||
window_input.expr
|
||||
);
|
||||
};
|
||||
assert!(Arc::ptr_eq(bound_schema, &schema));
|
||||
assert_eq!(*ordering, SeriesOrdering::KeyThenTime);
|
||||
}
|
||||
|
||||
/// Scan bounds follow the grid the selector is actually evaluated on, open a window's lower edge, and
|
||||
/// close an instant one's only when the lookback is zero.
|
||||
#[test]
|
||||
fn selector_requirement_bounds_account_for_grid_end_open_edges_and_zero_lookback() {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt_over("rate(x[10s])", Duration::from_secs(97)),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
compiled.inputs[0].requirement.raw_scan_bounds().unwrap(),
|
||||
(-10_000, 95_000, false)
|
||||
);
|
||||
|
||||
let mut instant = eval_stmt_over("x offset 2s", Duration::from_secs(97));
|
||||
instant.lookback_delta = Duration::from_secs(5);
|
||||
let compiled =
|
||||
compile_selectors(&instant, &|_| Ok(raw_schema()), SeriesOrdering::KeyThenTime).unwrap();
|
||||
assert_eq!(
|
||||
compiled.inputs[0].requirement.raw_scan_bounds().unwrap(),
|
||||
(-7_000, 93_000, false)
|
||||
);
|
||||
|
||||
let mut zero = eval_stmt("x");
|
||||
zero.lookback_delta = Duration::ZERO;
|
||||
let compiled =
|
||||
compile_selectors(&zero, &|_| Ok(raw_schema()), SeriesOrdering::KeyThenTime).unwrap();
|
||||
assert_eq!(
|
||||
compiled.inputs[0].requirement.raw_scan_bounds().unwrap(),
|
||||
(0, 100_000, true)
|
||||
);
|
||||
}
|
||||
|
||||
/// An anchored instant selection reads one point rather than every grid point, so both edges of its
|
||||
/// scan come from the anchor. Taking the lower edge from the grid instead would scan from the
|
||||
/// statement's start, which for an anchor before the statement is the wrong side of the data and
|
||||
/// for an anchor after it skips the sample the selection reads.
|
||||
#[test]
|
||||
fn an_anchored_instant_selector_scans_from_its_anchor() {
|
||||
// `x @ 50 offset 2s` with a five-second lookback: the sample is read at 48s, so the window it is
|
||||
// selected from is 43s..=48s even though the statement starts at the epoch.
|
||||
let requirement = SelectorRequirement {
|
||||
metric: None,
|
||||
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 anchor before the statement scans before it too: the same rule, the other direction. The
|
||||
// grid's own start would have put the lower edge at a time the anchor never reads.
|
||||
let requirement = SelectorRequirement {
|
||||
at_ms: Some(-30_000),
|
||||
..requirement
|
||||
};
|
||||
assert_eq!(
|
||||
requirement.raw_scan_bounds().unwrap(),
|
||||
(-37_000, -32_000, false)
|
||||
);
|
||||
|
||||
// A zero lookback closes both edges on the one sample, anchored or not.
|
||||
let requirement = SelectorRequirement {
|
||||
at_ms: Some(50_000),
|
||||
offset_ms: 0,
|
||||
timing: SelectorTiming::Instant { lookback_ms: 0 },
|
||||
..requirement
|
||||
};
|
||||
assert_eq!(
|
||||
requirement.raw_scan_bounds().unwrap(),
|
||||
(50_000, 50_000, true)
|
||||
);
|
||||
}
|
||||
|
||||
/// An anchor replaces the grid's endpoints on both edges, and a bound that does not fit is reported
|
||||
/// rather than wrapped.
|
||||
#[test]
|
||||
fn selector_requirement_bounds_apply_offset_and_anchor_and_check_overflow() {
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt("rate(x[10s] offset 2s @ 50)"),
|
||||
&|_| Ok(raw_schema()),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
compiled.inputs[0].requirement.raw_scan_bounds().unwrap(),
|
||||
(38_000, 48_000, false)
|
||||
);
|
||||
|
||||
let requirement = SelectorRequirement {
|
||||
metric: None,
|
||||
matchers: promql_parser::label::Matchers::empty(),
|
||||
offset_ms: 0,
|
||||
at_ms: None,
|
||||
grid_start_ms: i64::MIN,
|
||||
grid_end_ms: 0,
|
||||
timing: SelectorTiming::Window { range_ms: 1 },
|
||||
};
|
||||
assert!(requirement.raw_scan_bounds().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selector_binding_propagates_callback_errors() {
|
||||
let error = compile_selectors(
|
||||
&eval_stmt("rate(x[1m])"),
|
||||
&|_| Err(DataFusionError::Plan("binding failed".into())),
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.to_string().contains("binding failed"));
|
||||
}
|
||||
|
||||
/// Selector bindings keep the path of their input through every recursive wrapper shape.
|
||||
#[test]
|
||||
fn selector_bindings_follow_structural_input_paths() {
|
||||
let cases = [
|
||||
("x", "root.input"),
|
||||
("rate(x[5m])", "root.input"),
|
||||
("abs(x)", "root.input.input"),
|
||||
("sum(x)", "root.input.input"),
|
||||
("1 / x", "root.right.input"),
|
||||
("x / 2", "root.left.input"),
|
||||
("-x", "root.right.input"),
|
||||
("rate(x[5m:1m])", "root.input.input"),
|
||||
("rate(abs(x)[5m:1m])", "root.input.input.input"),
|
||||
];
|
||||
for (query, expected_path) in cases {
|
||||
let schema = raw_schema();
|
||||
let compiled = compile_selectors(
|
||||
&eval_stmt(query),
|
||||
&|requirement| {
|
||||
assert_eq!(requirement.metric.as_deref(), Some("x"), "query: {query}");
|
||||
Ok(Arc::clone(&schema))
|
||||
},
|
||||
SeriesOrdering::KeyThenTime,
|
||||
)
|
||||
.unwrap_or_else(|error| panic!("query {query}: {error}"));
|
||||
assert_eq!(compiled.inputs.len(), 1, "query: {query}");
|
||||
let input = &compiled.inputs[0];
|
||||
assert_eq!(input.path.as_str(), expected_path, "query: {query}");
|
||||
assert_eq!(
|
||||
input.requirement.metric.as_deref(),
|
||||
Some("x"),
|
||||
"query: {query}"
|
||||
);
|
||||
assert!(Arc::ptr_eq(&input.schema, &schema), "query: {query}");
|
||||
assert_eq!(
|
||||
input.ordering,
|
||||
SeriesOrdering::KeyThenTime,
|
||||
"query: {query}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user