pub(crate) fn constant_value(expr: &Expr) -> Option<f64>Expand description
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
one, pi(), and arithmetic whose two operands both fold. Any other call, a selector, a range
selector, an aggregate and a subquery are None, because their value comes from the data or from
the evaluation time, and a kernel’s constant is a field of the tree rather than something bound
while it runs. Returning None rather than an error is what lets the caller report where the
expression was found, which is the useful half of the refusal.
pi() belongs on the folding side because the statement itself decides it: it is one number, not
a value read from the data. Folding it is what makes pi() * x the multiplication by
std::f64::consts::PI it means, clamp_max(x, pi()) a kernel constant like clamp_max(x, 3),
and quantile(pi() / 4, x) a parameter this compiler can bind. The number is the one the promql
crate’s map_pi answers the previous route with — both are std::f64::consts::PI — so the two
routes cannot disagree about what pi() is.
Arithmetic uses the kernel operation named by [BinaryOp::kind], including
[ArithmeticOp::calculate]’s platform math behavior; defining %, ^ or atan2 again here
would be a second answer for the two to disagree over. Set operators do not fold.
Visible to the planner, which asks whether a statement’s value is a constant before binding it.