mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 13:44:03 +00:00
docs(promql): name the real run-time parameter, and reuse unparen
The doc said `1 + 1` is computed at run time. It is not: `try_build_literal_expr` folds `Paren`, `Unary` and literal-only `Binary` in the existing planner, so `quantile_over_time(1 - 0.1, x[5m])` and `quantile_over_time(-(0.9), x[5m])` plan today and reach this compiler unfolded. Refusing them is the agreed behaviour for this stage, not a run-time-only case, and the doc now says so; `time()` is the real run-time example. A test pins both folded forms as refused, so implementing folding later is a deliberate change rather than a silent one. `scalar_parameter` open-coded the parenthesis loop `unparen` already implements, so the rule for transparent wrappers now lives in one place. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
@@ -391,20 +391,22 @@ fn range_signature(name: &str) -> Option<RangeSignature> {
|
||||
|
||||
/// The number a parameterised range function was given, as the literal it was written as.
|
||||
///
|
||||
/// A literal is the only parameter this compiler binds: a parameter computed at run time, such as
|
||||
/// `time()` or `1 + 1`, would make the kernel's constant a query-time value, which the tree has no
|
||||
/// field for. 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.
|
||||
/// A number literal is the only parameter this compiler binds. A parameter whose value is only
|
||||
/// known at run time, `time()` being the real case, would make the kernel's constant a query-time
|
||||
/// value, which the tree has no field for.
|
||||
///
|
||||
/// Parentheses are looked through, as the planner looks through them when it reads a literal: a
|
||||
/// query written `(0.9)` is the same query, and the plan it already runs says so.
|
||||
/// A parameter written as an expression the existing planner folds to a literal is refused here
|
||||
/// and is a gap this stage leaves open on purpose: `quantile_over_time(1 - 0.1, x[5m])` and
|
||||
/// `quantile_over_time(-(0.9), x[5m])` plan today through `try_build_literal_expr`, which folds
|
||||
/// `Paren`, `Unary` and literal-only `Binary`, and reach this function still unfolded. They are
|
||||
/// refused rather than answered from the wrong number, and folding them here would be the work
|
||||
/// to close the gap.
|
||||
///
|
||||
/// 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 scalar_parameter(name: &str, parameter: &PromExpr) -> Result<f64> {
|
||||
let mut parameter = parameter;
|
||||
while let PromExpr::Paren(paren) = parameter {
|
||||
parameter = &paren.expr;
|
||||
}
|
||||
match parameter {
|
||||
match unparen(parameter) {
|
||||
PromExpr::NumberLiteral(NumberLiteral { val }) => Ok(*val),
|
||||
found => Err(not_implemented(&format!(
|
||||
"a call to `{name}` whose first argument is `{found}`, where it has to be a number \
|
||||
|
||||
@@ -277,6 +277,30 @@ fn a_parenthesised_selector_binds_as_written() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A parameter the planner folds to a literal is still refused, and that gap is deliberate: the
|
||||
/// kernel's constant is a field of the tree, so binding `1 - 0.1` means folding it here rather than
|
||||
/// carrying an expression. A refusal is the agreed behaviour for this stage; an answer computed
|
||||
/// from the wrong number would not be.
|
||||
#[test]
|
||||
fn a_parameter_folded_from_an_expression_is_refused() {
|
||||
for query in [
|
||||
"quantile_over_time(1 - 0.1, x[5m])",
|
||||
"quantile_over_time(-(0.9), x[5m])",
|
||||
] {
|
||||
let error = compile(&eval_stmt(query), raw_schema(), SeriesOrdering::KeyThenTime)
|
||||
.expect_err(&format!("`{query}` compiled, and it should not have"));
|
||||
assert!(
|
||||
matches!(error, DataFusionError::NotImplemented(_)),
|
||||
"`{query}` did not report NotImplemented: {error}"
|
||||
);
|
||||
let message = error.to_string();
|
||||
assert!(
|
||||
message.contains("number literal"),
|
||||
"`{query}` did not say what the argument has to be: {message}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A parameter computed at query time is refused, and the error names it: the tree binds a kernel's
|
||||
/// constant, so a quantile that would only exist at run time has no field to go in.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user