diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs index 31fc6d69dd4..88e5023aa42 100644 --- a/src/query/src/promql/native_compile.rs +++ b/src/query/src/promql/native_compile.rs @@ -391,20 +391,22 @@ fn range_signature(name: &str) -> Option { /// 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 { - 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 \ diff --git a/src/query/src/promql/native_compile/tests.rs b/src/query/src/promql/native_compile/tests.rs index 3b4d7e74fd7..5e375858111 100644 --- a/src/query/src/promql/native_compile/tests.rs +++ b/src/query/src/promql/native_compile/tests.rs @@ -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]