dgraph-io/dgraph · error
Invalid Math expression
Error message
Invalid Math expression
What it means
During DQL math expression evaluation (shunting-yard style), evalMathStack tries to pop an operator from the operator stack and finds it empty. This happens when the expression structure is malformed so an evaluation was triggered with no pending operator. The parser reports it as a generic 'Invalid Math expression'.
Source
Thrown at dql/math.go:105
if !ok {
return false
}
switch f {
case "floor", "/", "%", "ceil", "sqrt", "u-":
return g == 0
case "ln":
return g == 1
}
return false
}
return false
}
func evalMathStack(opStack, valueStack *mathTreeStack) error {
topOp, err := opStack.pop()
if err != nil {
return errors.Errorf("Invalid Math expression")
}
switch {
case isUnary(topOp.Fn):
// Since "not" is a unary operator, just pop one value.
topVal, err := valueStack.pop()
if err != nil {
return errors.Errorf("Invalid math statement. Expected 1 operands")
}
if opStack.size() > 1 {
peek := opStack.peek().Fn
if (peek == "/" || peek == "%") && isZero(topOp.Fn, topVal.Const) {
return errors.Errorf("Division by zero")
}
}
topOp.Child = []*MathTree{topVal}
case isTernary(topOp.Fn):
if valueStack.size() < 3 {View on GitHub (pinned to 759e242be6)
Solutions
- Inspect the math() expression for empty parentheses or missing operators.
- Test the expression with a simple valid form like math(a+1) and build up incrementally.
- Remove stray '(' or ')' characters that don't pair with operators.
- Check nested function calls have the right commas and arguments.
Example fix
// before
root @filter(...) { u as math() }
// after
root @filter(...) { u as math(a + 1) } Defensive patterns
Strategy: validation
Validate before calling
// Validate math expression is non-empty and contains an operator before sending
const expr = 'a + 1';
if (!/[+\-*/%<>=!]|sqrt|exp|ln|cond|pow|logbase|floor|ceil|min|max|since|dot/.test(expr)) {
throw new Error('math() requires at least one operator/function');
} Try / catch
try {
return await dgraph.query(query);
} catch (err) {
if (/Invalid Math expression/.test(String(err))) {
throw new Error(`Malformed math expression in query: ${query}. Check operators/parens.`);
}
throw err;
} Prevention
- Always include at least one operator or function inside math(...)
- Pair-check parentheses and commas before sending the query
- Test new math expressions with a small query first
- Build expressions programmatically with a helper that guarantees valid structure
When it happens
Trigger: A math(...) block whose operator and value stacks get out of sync, e.g. math applied with an empty/degenerate expression inside the parentheses such as math() or an expression that ends without any operators to apply when a comma/paren forces evaluation.
Common situations: Typos in DQL queries like `math()` or stray parentheses: `math(a ())`; copy-pasted queries with removed operators; programmatically generated math expressions missing operators.
Related errors
- Invalid math statement. Expected 1 operands
- Invalid Math expression. Expected 3 operands
- Invalid Math expression. Expected 2 operands
- Expected ( after math
- Unknown math function: %v
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/d7d42d242ba5045d.
Report an issue: GitHub.