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

  1. Read the wrapped inner error to see the actual type mismatch
  2. Check the predicate's schema type with /state and ensure it is int/float for numeric aggregation
  3. Fix the query to aggregate only numeric value variables
  4. 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

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


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