dgraph-io/dgraph · error
Invalid math statement. Expected 1 operands
Error message
Invalid math statement. Expected 1 operands
What it means
A unary math operator (exp, ln, u-, sqrt, floor, ceil, since) was being evaluated but the value stack had no operand to pop. The unary operator is starved of its single operand, indicating a malformed math expression.
Source
Thrown at dql/math.go:112
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 {
return errors.Errorf("Invalid Math expression. Expected 3 operands")
}
topVal1 := valueStack.popAssert()
topVal2 := valueStack.popAssert()
topVal3 := valueStack.popAssert()
topOp.Child = []*MathTree{topVal3, topVal2, topVal1}
View on GitHub (pinned to 759e242be6)
Solutions
- Ensure every unary operator/function has exactly one operand: math(sqrt(a)), math(-a).
- Fix dangling operators at the end of the expression like math(a -).
- Check unary minus is written directly before a value: math(-a + b).
- Validate the query in a REPL/ small query first to isolate the failing expression.
Example fix
// before u as math(a * ) // after u as math(a * sqrt(b))
Defensive patterns
Strategy: validation
Validate before calling
// Ensure unary ops/functions have an operand
const expr = 'sqrt(a)';
const unary = ['sqrt','exp','ln','floor','ceil','since'];
for (const f of unary) {
const re = new RegExp(f + '\\s*\\(\\s*\\)');
if (re.test(expr)) throw new Error(`${f} is missing its operand`);
}
if (/[+\-*/%]\s*([),]|$)/.test(expr)) throw new Error('Dangling operator without operand'); Try / catch
try {
return await dgraph.query(query);
} catch (err) {
if (/Expected 1 operands/.test(String(err))) {
throw new Error('A unary math operator is missing its operand in: ' + query);
}
throw err;
} Prevention
- Write unary minus directly attached to a value (-a, not a -)
- Never leave a trailing operator at the end of a math expression
- Always parenthesize function arguments: sqrt(a), not sqrt a
- Lint generated DQL for dangling operators before submission
When it happens
Trigger: Writing math(a -) or math(sqrt) with no operand following/preceding the unary operator, or an expression where a unary function ends up with an empty sub-expression, e.g. math(a * - ).
Common situations: Hand-edited DQL where an operand was deleted; negation with missing value: math(u- ) or trailing '-'; generated queries with empty variable values interpolated.
Related errors
- Invalid Math expression
- 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/46872d334e67fa5a.
Report an issue: GitHub.