{"record":{"id":"de678923e1f1c825","repo":"dgraph-io/dgraph","slug":"integer-overflow","errorCode":null,"errorMessage":"Integer overflow","messagePattern":"Integer overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/math.go","lineNumber":26,"sourceCode":"import (\n\t\"sync\"\n\n\t\"github.com/golang/glog\"\n\t\"github.com/pkg/errors\"\n\n\t\"github.com/dgraph-io/dgraph/v25/types\"\n)\n\ntype mathTree struct {\n\tFn    string\n\tVar   string\n\tConst types.Val // If its a const value node.\n\tVal   *types.ShardedMap\n\tChild []*mathTree\n}\n\nvar (\n\tErrorIntOverflow     = errors.New(\"Integer overflow\")\n\tErrorFloat32Overflow = errors.New(\"Float32 overflow\")\n\tErrorDivisionByZero  = errors.New(\"Division by zero\")\n\tErrorFractionalPower = errors.New(\"Fractional power of negative number\")\n\tErrorNegativeLog     = errors.New(\"Log of negative number\")\n\tErrorNegativeRoot    = errors.New(\"Root of negative number\")\n\tErrorVectorsNotMatch = errors.New(\"The length of vectors must match\")\n\tErrorArgsDisagree    = errors.New(\"Left and right arguments must match\")\n\tErrorShouldBeVector  = errors.New(\"Type should be []float, but is not. Cannot determine type.\")\n\tErrorBadVectorMult   = errors.New(\"Cannot multiply vector by vector\")\n)\n\n// processBinary handles the binary operands like\n// +, -, *, /, %, max, min, logbase, dot\nfunc processBinary(mNode *mathTree) error {\n\taggName := mNode.Fn\n\n\tmpl := mNode.Child[0].Val\n\tmpr := mNode.Child[1].Val","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/math.go#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"Multiplying large counts or timestamps in math blocks; summing huge int predicates across many nodes; accidental string-to-int coercion producing huge intermediate values.","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"],"exampleFix":"// before\nmath(total * 1000000000000)\n// after\nmath(total * 1e12) // float arithmetic, no int64 overflow","handlingStrategy":"try-catch","validationCode":"// Pre-check magnitude in JS before issuing the query:\nfunction safeIntMul(a, b) {\n  const r = a * b;\n  if (r > 9223372036854775807n || r < -9223372036854775808n) {\n    throw new Error('operands would overflow int64 in Dgraph math');\n  }\n  return r;\n}","typeGuard":"function isInt64Safe(n) {\n  return Number.isSafeInteger(n) && Math.abs(n) <= Number.MAX_SAFE_INTEGER;\n}","tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(`{ q(func: uid(${id})) { s as math(a * b) } }`);\n} catch (e) {\n  if (String(e).includes('Integer overflow')) {\n    // retry with float math or reduced operands\n  } else throw e;\n}","preventionTips":["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"],"tags":["dgraph","math","integer-overflow","query"],"backgroundTag":"integer-overflow","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}