dgraph-io/dgraph · error

Wrong type %v encountered for func u-

Error message

Wrong type %v encountered for func u-

What it means

The unary negation function `u-` (used for math expressions like -x) supports INT, FLOAT, BIGFLOAT, and related numeric types. applyNeg returns this error when the operand's type ID is not one of the supported numeric types, reporting the actual Tid in the message. It is a defensive fallthrough in the type switch after all numeric cases are handled.

Source

Thrown at query/aggregator.go:494

	case FLOAT:
		res.Value = -a.Value.(float64)
		res.Tid = types.FloatID
	case VFLOAT:
		aVal := a.Value.([]float32)
		resVal := make([]float32, len(aVal))
		for i, v := range aVal {
			resVal[i] = -v
		}
		res.Value = resVal
		res.Tid = types.VFloatID

	case BIGFLOAT:
		value := a.Value.(big.Float)
		neg := big.NewFloat(0).SetPrec(types.BigFloatPrecision).Neg(&value)
		res.Value = *neg

	case DEFAULT:
		return errors.Errorf("Wrong type %v encountered for func u-", a.Tid)
	}
	return nil
}

func applySqrt(a, res *types.Val) error {
	vBase := getValType(a)
	switch vBase {
	case INT:
		if a.Value.(int64) < 0 {
			return ErrorNegativeRoot
		}
		res.Value = math.Sqrt(float64(a.Value.(int64)))
		res.Tid = types.FloatID

	case FLOAT:
		if a.Value.(float64) < 0 {
			return ErrorNegativeRoot
		}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Apply the unary minus only to numeric predicates or numeric variables
  2. Confirm predicate types in the schema before using arithmetic/negation
  3. If the value is stored as a string number, migrate the data/schema to a float type
  4. Rewrite the expression so negation happens after a numeric conversion step

Example fix

// before: negating a string
"math -(label)"
// after: negate a numeric field
"math -(balance)"
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Number.isFinite(value)) throw new Error('u- requires a number');

Type guard

function canNegate(v) { return typeof v === 'number' || typeof v === 'bigint'; }

Try / catch

try {
  res = await txn.query(q);
} catch (e) {
  if (String(e).includes('func u-')) { /* operand not numeric; handle */ }
  throw e;
}

Prevention

When it happens

Trigger: Evaluating a unary minus in a math expression where the operand is a string, bool, dateTime, or DEFAULT-typed value, e.g. math -(someStringPredicate).

Common situations: Negating a boolean facet or string field by mistake; a variable expected to hold a number actually holds a datetime from since() mis-use; schema drift changed a predicate's type.

Related errors


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