dgraph-io/dgraph · error

Division by zero

Error message

Division by zero

What it means

ErrorDivisionByZero is a sentinel error in Dgraph's math package returned when a division or modulus operation has a zero divisor, or when logbase/log is called with an invalid base of 1 (or log of 1 yielding division by zero). The query fails for the affected value.

Source

Thrown at query/math.go:28

	"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].Val
	cl := mNode.Child[0].Const
	cr := mNode.Child[1].Const

View on GitHub (pinned to 759e242be6)

Solutions

  1. Filter out zero denominators first, e.g. add a filter func: gt(denominator, 0) on the affected nodes
  2. Use a conditional-free reformulation, e.g. add a tiny epsilon: math(a / (b + 1e-9))
  3. Handle the sentinel error at the client/transport layer and skip or default the affected results

Example fix

// before
value @filter(type(Metric)) { ratio as math(x / y) }
// after
value @filter(type(Metric) AND gt(y, 0)) { ratio as math(x / y) }
Defensive patterns

Strategy: validation

Validate before calling

// Add a filter so the divisor can never be zero:
// { q(func: type(Metric)) @filter(gt(y, 0)) { ratio as math(x / y) } }
const query = `{ q(func: type(Metric)) @filter(gt(y, 0)) { x\n y\n ratio as math(x / y) } }`;

Try / catch

try {
  const res = await dgraph.newTxn().query(query);
} catch (e) {
  if (String(e).includes('Division by zero')) {
    // add gt(divisor, 0) filter or epsilon and retry
  } else throw e;
}

Prevention

When it happens

Trigger: math(a / b) or math(a % b) where b evaluates to 0; applyLog when base is 1; returned by applyDiv, applyMod, applyLog.

Common situations: Computing ratios where a count predicate is missing/zero on some nodes; percentage math over sparse data; user-driven math templates without zero guards.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/abd733717e62fd68. Report an issue: GitHub.