dgraph-io/dgraph · error
Wrong type %v encountered for func sqrt
Error message
Wrong type %v encountered for func sqrt
What it means
The `sqrt` unary function computes the square root only for INT, FLOAT, and BIGFLOAT values (BIGFLOAT handled with big.Float at configured precision). If the operand type is anything else, the DEFAULT branch of the type switch returns this error with the offending type ID.
Source
Thrown at query/aggregator.go:521
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
}
res.Value = math.Sqrt(a.Value.(float64))
case BIGFLOAT:
value := a.Value.(big.Float)
rt := big.NewFloat(0).SetPrec(types.BigFloatPrecision).Sqrt(&value)
res.Value = *rt
case DEFAULT:
return errors.Errorf("Wrong type %v encountered for func sqrt", a.Tid)
}
return nil
}
func applyFloor(a, res *types.Val) error {
vBase := getValType(a)
switch vBase {
case INT:
res.Value = a.Value.(int64)
case FLOAT:
res.Value = math.Floor(a.Value.(float64))
case BIGFLOAT:
value := a.Value.(big.Float)
f, _ := value.Float64()
res.Value = *big.NewFloat(math.Floor(f)).SetPrec(types.BigFloatPrecision)
View on GitHub (pinned to 759e242be6)
Solutions
- Ensure sqrt's argument is an int/float predicate or numeric variable
- Check the schema to confirm the predicate's type
- Filter or coerce the value to numeric before sqrt
- Validate the expression with a literal argument to confirm the rest of the query works
Example fix
// before "math sqrt(distance_str)" // after "math sqrt(distance)"
Defensive patterns
Strategy: type-guard
Validate before calling
const t = schemaOf('distance');
if (t !== 'float' && t !== 'int') throw new Error('sqrt needs numeric type'); Type guard
function isSqrtSafe(v) { return typeof v === 'number' && v >= 0; } Try / catch
try {
out = await txn.query(q);
} catch (e) {
if (String(e).includes('func sqrt')) throw new TypeError('sqrt operand must be numeric');
throw e;
} Prevention
- Use sqrt only on int/float predicates
- Verify operand types in generated queries
- Keep schema and query in sync after migrations
- Guard against negative inputs at query design time
When it happens
Trigger: A query math sqrt(x) where x is a non-numeric value: string/bool/dateTime predicate or a default-typed variable.
Common situations: sqrt over a string predicate; variable shadows a uid; predicate resharded or schema changed so values no longer arrive as float; facet values that are strings.
Related errors
- Wrong type %v encountered for func ln
- Wrong type %v encountered for func exp
- Wrong type %v encountered for func u-
- Wrong type %v encountered for func floor
- Wrong type %v encountered for fun ceil
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/a6e2b21fabff98d5.
Report an issue: GitHub.