dgraph-io/dgraph · error

Invalid Math expression. Expected 2 operands

Error message

Invalid Math expression. Expected 2 operands

What it means

A binary math operator (+, -, *, /, %, comparisons, pow, logbase, etc.) was evaluated but fewer than 2 operands were on the value stack. The operator is missing one or both of its operands, indicating an incomplete math expression.

Source

Thrown at dql/math.go:133

			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}

	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" ||

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure every binary operator has two operands: math(a + b).
  2. Supply both arguments for binary functions: math(pow(base, exp)), math(logbase(v, b)).
  3. Remove trailing/dangling operators at the end of the expression.
  4. Add parentheses around sub-expressions so operator grouping is unambiguous.

Example fix

// before
u as math(pow(v))
// after
u as math(pow(v, 2))
Defensive patterns

Strategy: validation

Validate before calling

// Every binary operator needs an operand on both sides
const expr = 'pow(a, 2)';
if (/(^|[\s(])[+\-*/%<>=!]+\s*([),]|$)/.test(expr)) {
  throw new Error('Binary operator missing an operand');
}
if (/\bpow\s*\([^,)]+\)/.test(expr)) throw new Error('pow needs 2 arguments');

Try / catch

try {
  return await dgraph.query(query);
} catch (err) {
  if (/Expected 2 operands/.test(String(err))) {
    throw new Error('A binary math operator/function lacks operands in: ' + query);
  }
  throw err;
}

Prevention

When it happens

Trigger: math(a +) with trailing operator, math(+ a), or binary function missing an argument like math(pow(a)) — pow needs two arguments (math(pow(a, 2))).

Common situations: Deleting an operand during query editing; writing two-arg functions (pow, logbase, min/max usage patterns) with one argument; an extra closing paren swallowing operands: math((a + b)) typo variants like math((a + )).

Related errors


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