dgraph-io/dgraph · error
Invalid Math expression. Expected 2 operands
Error message
Invalid Math expression. Expected 2 operands
What it means
A binary math operator (+, -, *, /, %, comparisons, pow, logbase, etc.) was evaluated but fewer than 2 operands were on the value stack. The operator is missing one or both of its operands, indicating an incomplete math expression.
Source
Thrown at dql/math.go:133
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 {
return errors.Errorf("Invalid Math expression. Expected 3 operands")
}
topVal1 := valueStack.popAssert()
topVal2 := valueStack.popAssert()
topVal3 := valueStack.popAssert()
topOp.Child = []*MathTree{topVal3, topVal2, topVal1}
default:
if valueStack.size() < 2 {
return errors.Errorf("Invalid Math expression. Expected 2 operands")
}
if isZero(topOp.Fn, valueStack.peek().Const) {
return errors.Errorf("Division by zero.")
}
topVal1 := valueStack.popAssert()
topVal2 := valueStack.popAssert()
topOp.Child = []*MathTree{topVal2, topVal1}
}
// Push the new value (tree) into the valueStack.
valueStack.push(topOp)
return nil
}
func isMathFunc(f string) bool {
// While adding an op, also add it to the corresponding function type.
return f == "*" || f == "%" || f == "+" || f == "-" || f == "/" ||
f == "exp" || f == "ln" || f == "cond" ||View on GitHub (pinned to 759e242be6)
Solutions
- Ensure every binary operator has two operands: math(a + b).
- Supply both arguments for binary functions: math(pow(base, exp)), math(logbase(v, b)).
- Remove trailing/dangling operators at the end of the expression.
- Add parentheses around sub-expressions so operator grouping is unambiguous.
Example fix
// before u as math(pow(v)) // after u as math(pow(v, 2))
Defensive patterns
Strategy: validation
Validate before calling
// Every binary operator needs an operand on both sides
const expr = 'pow(a, 2)';
if (/(^|[\s(])[+\-*/%<>=!]+\s*([),]|$)/.test(expr)) {
throw new Error('Binary operator missing an operand');
}
if (/\bpow\s*\([^,)]+\)/.test(expr)) throw new Error('pow needs 2 arguments'); Try / catch
try {
return await dgraph.query(query);
} catch (err) {
if (/Expected 2 operands/.test(String(err))) {
throw new Error('A binary math operator/function lacks operands in: ' + query);
}
throw err;
} Prevention
- Give two-argument functions (pow, logbase) both arguments
- Remove dangling operators from edited expressions
- Wrap operand sub-expressions in parentheses
- Validate generated expressions with a regex/sentence check before sending
When it happens
Trigger: math(a +) with trailing operator, math(+ a), or binary function missing an argument like math(pow(a)) — pow needs two arguments (math(pow(a, 2))).
Common situations: Deleting an operand during query editing; writing two-arg functions (pow, logbase, min/max usage patterns) with one argument; an extra closing paren swallowing operands: math((a + b)) typo variants like math((a + )).
Related errors
- Invalid Math expression
- Invalid math statement. Expected 1 operands
- Invalid Math expression. Expected 3 operands
- Expected ( after math
- Unknown math function: %v
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/261b6659ae29ffd2.
Report an issue: GitHub.