dgraph-io/dgraph · error
Error in applying aggregation %s
Error message
Error in applying aggregation %s
What it means
While aggregating values for a variable, Dgraph applies each collected value to an aggregator (sum, min, max, avg). If ag.Apply rejects a value (e.g. wrong type such as a string or bool where a number is required, or incompatible types mixed), the error is wrapped as 'Error in applying aggregation %s' with the underlying cause.
Source
Thrown at query/query.go:1112
relSG = ch
}
}
}
if relSG == nil {
return nil, errors.Errorf("Invalid variable aggregation. Check the levels.")
}
vals := doneVars[needsVar].Vals
mp = types.NewShardedMap()
// Go over the sibling node and aggregate.
for i, list := range relSG.uidMatrix {
ag := aggregator{
name: sg.SrcFunc.Name,
}
for _, uid := range list.Uids {
if val, ok := vals.Get(uid); ok {
if err := ag.Apply(val); err != nil {
return nil, errors.Errorf("Error in applying aggregation %s", err)
}
}
}
v, err := ag.Value()
if err != nil && err != ErrEmptyVal {
return nil, err
}
if v.Value != nil {
mp.Set(relSG.SrcUIDs.Uids[i], v)
}
}
return mp, nil
}
func (mt *mathTree) extractVarNodes() []*mathTree {
var nodeList []*mathTree
for _, ch := range mt.Child {
nodeList = append(nodeList, ch.extractVarNodes()...)View on GitHub (pinned to 759e242be6)
Solutions
- Read the wrapped inner error to see the actual type mismatch
- Check the predicate's schema type with /state and ensure it is int/float for numeric aggregation
- Fix the query to aggregate only numeric value variables
- Migrate/convert data if the underlying predicate holds non-numeric values
Example fix
// before: aggregating a string variable score as math(val(name)) // name is string -> Apply fails // after: aggregate a numeric predicate score as sum(val(age))
Defensive patterns
Strategy: validation
Validate before calling
// Check schema types of aggregated predicates are numeric
resp, _ := http.Get(dgraphURL + "/state")
// verify the predicates feeding sum/min/max/avg are int or float in schema.json
if schemaType(pred) != "int" && schemaType(pred) != "float" {
return fmt.Errorf("predicate %s must be numeric for aggregation", pred)
} Try / catch
if err != nil && strings.Contains(err.Error(), "Error in applying aggregation") {
return fmt.Errorf("non-numeric value fed into aggregator; check schema and variable types: %w", err)
} Prevention
- Keep numeric predicates typed int/float in the Dgraph schema
- Do not change a predicate's type after data exists without migrating data
- Aggregate only variables produced from numeric predicates or numeric facets
- Validate queries against schema in CI
When it happens
Trigger: Aggregating (sum/min/max/avg) over a value variable whose stored values are not numeric (e.g. strings, bools, datetimes); mixing facet or uid values with typed values in the same variable.
Common situations: Predicate schema changed from numeric to string after data was written; using sum(val(x)) where x was set from a non-numeric facet or value; schema mismatch between predicates.
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 sqrt
- Wrong type %v encountered for func floor
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/4c3b6f55b98ff98a.
Report an issue: GitHub.