dgraph-io/dgraph · error
Integer overflow
Error message
Integer overflow
What it means
ErrorIntOverflow is a sentinel error in Dgraph's math package returned when an integer arithmetic operation (+, -, *, unary negation) exceeds the int64 range. Math on ints is done with explicit overflow checks, and any overflow aborts query processing.
Source
Thrown at query/math.go:26
import (
"sync"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/dgraph-io/dgraph/v25/types"
)
type mathTree struct {
Fn string
Var string
Const types.Val // If its a const value node.
Val *types.ShardedMap
Child []*mathTree
}
var (
ErrorIntOverflow = errors.New("Integer overflow")
ErrorFloat32Overflow = errors.New("Float32 overflow")
ErrorDivisionByZero = errors.New("Division by zero")
ErrorFractionalPower = errors.New("Fractional power of negative number")
ErrorNegativeLog = errors.New("Log of negative number")
ErrorNegativeRoot = errors.New("Root of negative number")
ErrorVectorsNotMatch = errors.New("The length of vectors must match")
ErrorArgsDisagree = errors.New("Left and right arguments must match")
ErrorShouldBeVector = errors.New("Type should be []float, but is not. Cannot determine type.")
ErrorBadVectorMult = errors.New("Cannot multiply vector by vector")
)
// processBinary handles the binary operands like
// +, -, *, /, %, max, min, logbase, dot
func processBinary(mNode *mathTree) error {
aggName := mNode.Fn
mpl := mNode.Child[0].Val
mpr := mNode.Child[1].ValView on GitHub (pinned to 759e242be6)
Solutions
- Reduce operand magnitudes or divide earlier in the expression to keep intermediates in int64 range
- Store/compute the value as a float (math converts to float when operands are float) if full int precision is not required
- Clamp or validate the source data so expressions cannot overflow
Example fix
// before math(total * 1000000000000) // after math(total * 1e12) // float arithmetic, no int64 overflow
Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-check magnitude in JS before issuing the query:
function safeIntMul(a, b) {
const r = a * b;
if (r > 9223372036854775807n || r < -9223372036854775808n) {
throw new Error('operands would overflow int64 in Dgraph math');
}
return r;
} Type guard
function isInt64Safe(n) {
return Number.isSafeInteger(n) && Math.abs(n) <= Number.MAX_SAFE_INTEGER;
} Try / catch
try {
const res = await dgraph.newTxn().query(`{ q(func: uid(${id})) { s as math(a * b) } }`);
} catch (e) {
if (String(e).includes('Integer overflow')) {
// retry with float math or reduced operands
} else throw e;
} Prevention
- Avoid multiplying large ints (timestamps, counts) directly in math blocks
- Use float literals (1e12) to force float arithmetic when precision permits
- Clamp source data ranges at write time
When it happens
Trigger: math expressions like math(a + b), math(a * b), math(-a) where int64 operands produce values outside [-2^63, 2^63-1]; returned by applyAdd, applySub, applyMul, applyNeg.
Common situations: Multiplying large counts or timestamps in math blocks; summing huge int predicates across many nodes; accidental string-to-int coercion producing huge intermediate values.
Related errors
- Float32 overflow
- Division by zero
- Fractional power of negative number
- Log of negative number
- Root of negative number
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/de678923e1f1c825.
Report an issue: GitHub.