dgraph-io/dgraph · error

Wrong type %v encountered for func exp

Error message

Wrong type %v encountered for func exp

What it means

The `exp` unary function computes e^x only for INT, FLOAT, and BIGFLOAT inputs. If the argument's type ID is anything else (DEFAULT, string, bool, dateTime, etc.), applyExp returns this error naming the unsupported type. Like the other math aggregators it relies on matchType to coerce types first, so this fires only when no numeric coercion was possible.

Source

Thrown at query/aggregator.go:460

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

func applyExp(a, res *types.Val) error {
	vBase := getValType(a)
	switch vBase {
	case INT:
		res.Value = math.Exp(float64(a.Value.(int64)))
		res.Tid = types.FloatID

	case FLOAT:
		res.Value = math.Exp(a.Value.(float64))

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

func applyNeg(a, res *types.Val) error {
	vBase := getValType(a)
	switch vBase {
	case INT:
		// -ve of math.MinInt64 is evaluated as itself (due to overflow)
		if a.Value.(int64) == math.MinInt64 {
			return ErrorIntOverflow
		}
		res.Value = -a.Value.(int64)
		res.Tid = types.IntID

	case FLOAT:
		res.Value = -a.Value.(float64)
		res.Tid = types.FloatID

View on GitHub (pinned to 759e242be6)

Solutions

  1. Verify the input predicate/variable is int or float typed in the schema
  2. Cast or transform the value to a number before applying exp (e.g. via since() which returns float, or arithmetic)
  3. Remove or replace non-numeric arguments in the math expression
  4. Test the query with a literal (math exp(1)) to isolate which variable carries the bad type

Example fix

// before
"math exp(name)"
// after
"math exp(age)"
Defensive patterns

Strategy: type-guard

Validate before calling

const t = schemaOf('score');
if (!['int','float'].includes(t)) throw new Error('exp requires numeric predicate');

Type guard

function isNumber(v) { return typeof v === 'number'; }

Try / catch

try {
  return await txn.query(q);
} catch (e) {
  if (String(e).includes('func exp')) throw new Error('exp operand must be numeric: ' + e);
  throw e;
}

Prevention

When it happens

Trigger: A query like math exp(v) where v binds to a non-numeric value: string predicate, boolean, dateTime, or an untyped/default value that matchType could not convert.

Common situations: Feeding exp() a uid variable or count alias mismatch; predicate schema changed from float to another type; using exp on a facet stored as string.

Related errors


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