{"record":{"id":"4c3b6f55b98ff98a","repo":"dgraph-io/dgraph","slug":"error-in-applying-aggregation-s","errorCode":null,"errorMessage":"Error in applying aggregation %s","messagePattern":"Error in applying aggregation (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/query.go","lineNumber":1112,"sourceCode":"\t\t\t\trelSG = ch\n\t\t\t}\n\t\t}\n\t}\n\tif relSG == nil {\n\t\treturn nil, errors.Errorf(\"Invalid variable aggregation. Check the levels.\")\n\t}\n\n\tvals := doneVars[needsVar].Vals\n\tmp = types.NewShardedMap()\n\t// Go over the sibling node and aggregate.\n\tfor i, list := range relSG.uidMatrix {\n\t\tag := aggregator{\n\t\t\tname: sg.SrcFunc.Name,\n\t\t}\n\t\tfor _, uid := range list.Uids {\n\t\t\tif val, ok := vals.Get(uid); ok {\n\t\t\t\tif err := ag.Apply(val); err != nil {\n\t\t\t\t\treturn nil, errors.Errorf(\"Error in applying aggregation %s\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tv, err := ag.Value()\n\t\tif err != nil && err != ErrEmptyVal {\n\t\t\treturn nil, err\n\t\t}\n\t\tif v.Value != nil {\n\t\t\tmp.Set(relSG.SrcUIDs.Uids[i], v)\n\t\t}\n\t}\n\treturn mp, nil\n}\n\nfunc (mt *mathTree) extractVarNodes() []*mathTree {\n\tvar nodeList []*mathTree\n\tfor _, ch := range mt.Child {\n\t\tnodeList = append(nodeList, ch.extractVarNodes()...)","sourceCodeStart":1094,"sourceCodeEnd":1130,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/query.go#L1094-L1130","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before: aggregating a string variable\nscore as math(val(name))  // name is string -> Apply fails\n// after: aggregate a numeric predicate\nscore as sum(val(age))","handlingStrategy":"validation","validationCode":"// Check schema types of aggregated predicates are numeric\nresp, _ := http.Get(dgraphURL + \"/state\")\n// verify the predicates feeding sum/min/max/avg are int or float in schema.json\nif schemaType(pred) != \"int\" && schemaType(pred) != \"float\" {\n    return fmt.Errorf(\"predicate %s must be numeric for aggregation\", pred)\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"Error in applying aggregation\") {\n    return fmt.Errorf(\"non-numeric value fed into aggregator; check schema and variable types: %w\", err)\n}","preventionTips":["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"],"tags":["dgraph","query","aggregation","type-mismatch"],"backgroundTag":"aggregation-type-mismatch","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}