dgraph-io/dgraph · error
Division by zero.
Error message
Division by zero.
What it means
Static zero-divisor check: when evaluating a binary operator, if the top-of-stack (right) operand is a constant equal to 0 and the operator is '/', '%' (or zero is invalid for ceil/sqrt/u- contexts), the parser rejects the query with 'Division by zero.'. Note the trailing period distinguishes it from the unary-branch variant.
Source
Thrown at dql/math.go:136
}
}
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" ||
f == "<" || f == ">" || f == ">=" || f == "<=" ||
f == "==" || f == "!=" ||
f == "min" || f == "max" || f == "sqrt" ||View on GitHub (pinned to 759e242be6)
Solutions
- Use cond to guard: math(cond(d == 0, 0, a / d)) — but since this fires on constants, replace the constant 0 with a nonzero value.
- Replace the zero constant divisor with a real variable or 1.
- Precompute the divisor as a variable and filter zero values before the math block.
- Review templating that injects numeric divisors to ensure 0 can never be emitted.
Example fix
// before total as math(sum / 0) // after avg as math(cond(cnt == 0, 0, sum / cnt))
Defensive patterns
Strategy: validation
Validate before calling
function assertNoZeroDivisor(expr) {
if (/\/\s*0+(\.0+)?\b/.test(expr) || /%\s*0+(\.0+)?\b/.test(expr)) {
throw new Error('math expression divides by constant zero');
}
}
assertNoZeroDivisor('sum / 0'); Try / catch
try {
return await dgraph.query(query);
} catch (err) {
if (/Division by zero\.$/.test(String(err))) {
throw new Error('Constant zero divisor found; use a nonzero constant or a guarded variable');
}
throw err;
} Prevention
- Replace placeholder divisors (0) with real values before shipping templates
- Use cond to guard any potentially-zero divisor
- Interpolate divisor variables with a Math.max(d, 1) clamp
- Add a lint rule scanning DQL strings for '/ 0' and '% 0'
When it happens
Trigger: math(a / 0), math(a % 0), or the right-hand operand is a zero constant produced by a parsed numeric literal, e.g. math(a / 0.0) or math(a / (1 - 1)).
Common situations: Template-generated queries where a variable interpolated to 0; hardcoded placeholder divisor never replaced; computing a constant sub-expression that evaluates to zero like math(count / (len - len)) style constructs.
Related errors
- Division by zero
- Division by zero
- Invalid Math expression
- Invalid math statement. Expected 1 operands
- Invalid Math expression. Expected 3 operands
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/722c28b7b2caf184.
Report an issue: GitHub.