dgraph-io/dgraph · error

invalid types %v, %v for func %s

Error message

invalid types %v, %v for func %s

What it means

invalidTypeError is the shared error constructor used by binary math aggregators (mul, div, dot) and matchType when the left and right operand types cannot be reconciled for the given function. matchType attempts to coerce operands to a common type (int/float/bigfloat); when both operands are incompatible for the function, this error reports both type IDs and the function name.

Source

Thrown at query/aggregator.go:647

func getValType(v *types.Val) valType {
	var vBase valType
	switch v.Tid {
	case types.IntID:
		vBase = INT
	case types.FloatID:
		vBase = FLOAT
	case types.VFloatID:
		vBase = VFLOAT
	case types.BigFloatID:
		vBase = BIGFLOAT
	default:
		vBase = DEFAULT
	}
	return vBase
}

func invalidTypeError(left, right valType, funcName string) error {
	return errors.Errorf("invalid types %v, %v for func %s", left, right, funcName)
}

// matchType(left, right) will make sure that the left and right type
// arguments agree, and otherwise convert left/right to an appropriate
// type in some cases where it can.
// matchType is invoked right before evaluating either a unary or binary
// function indicated by ag.name. If evaluating a unary function, then
// right is actually the single argument type, and left is actually the
// resulting type. For invoking a binary function then left and right
// of course play the role of being the left/right types (as expected).
func (ag *aggregator) matchType(left, right *types.Val) error {
	leftType := getValType(left)
	rightType := getValType(right)

	if rightType == VFLOAT {
		if _, ok := opsAllowingVectorsOnRight[ag.name]; !ok {
			return invalidTypeError(leftType, rightType, ag.name)
		}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure both operands of the binary math function are numeric (int/float)
  2. Check the schema types of both predicates involved
  3. Cast datetime values via since() to floats before arithmetic
  4. Rewrite the expression so string/bool values are not part of numeric math

Example fix

// before: dividing a string by a number
"math div(total_str, 2)"
// after: numeric predicate
"math div(total, 2)"
Defensive patterns

Strategy: type-guard

Validate before calling

function bothNumeric(t1, t2) {
  const nums = ['int','float'];
  return nums.includes(t1) && nums.includes(t2);
}

Type guard

function isNumericValue(v) { return typeof v === 'number' && Number.isFinite(v); }

Try / catch

try {
  r = await txn.query(q);
} catch (e) {
  if (String(e).includes('invalid types') ) {
    console.error('Binary math operand type mismatch:', e.message);
  }
  throw e;
}

Prevention

When it happens

Trigger: Binary math between incompatible operands, e.g. adding/dividing/multiplying a string by a number, a bool by a float, or a dateTime by an int in math expressions; also division involving non-numeric typed values where no valid vBase can be computed.

Common situations: Mixing a string predicate with a numeric literal; comparing datetime arithmetic wrongly; schema drift making one side of the expression a different type; using uid variables inside arithmetic.

Related errors


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