{"record":{"id":"9443fa3ed31b0c79","repo":"dgraph-io/dgraph","slug":"unexpected-length-while-adding-groupby-idx-v","errorCode":null,"errorMessage":"Unexpected length while adding Groupby. Idx: [%v], len: [%v]","messagePattern":"Unexpected length while adding Groupby\\. Idx: \\[(.+?)\\], len: \\[(.+?)\\]","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/outputnode.go","lineNumber":1390,"sourceCode":"\t\t\tcontinue\n\t\t}\n\n\t\tif len(pc.uidMatrix) == 0 {\n\t\t\t// Can happen in recurse query.\n\t\t\tcontinue\n\t\t}\n\t\tif len(pc.facetsMatrix) > 0 && len(pc.facetsMatrix) != len(pc.uidMatrix) {\n\t\t\treturn errors.Errorf(\"Length of facetsMatrix and uidMatrix mismatch: %d vs %d\",\n\t\t\t\tlen(pc.facetsMatrix), len(pc.uidMatrix))\n\t\t}\n\n\t\tidx := algo.IndexOf(pc.SrcUIDs, uid)\n\t\tif idx < 0 {\n\t\t\tcontinue\n\t\t}\n\t\tif pc.Params.IsGroupBy {\n\t\t\tif len(pc.GroupbyRes) <= idx {\n\t\t\t\treturn errors.Errorf(\"Unexpected length while adding Groupby. Idx: [%v], len: [%v]\",\n\t\t\t\t\tidx, len(pc.GroupbyRes))\n\t\t\t}\n\t\t\tif err := pc.addGroupby(enc, dst, pc.GroupbyRes[idx], pc.fieldName()); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\n\t\tfieldName := pc.fieldName()\n\t\tswitch {\n\t\tcase len(pc.counts) > 0:\n\t\t\tif err := pc.addCount(enc, uint64(pc.counts[idx]), dst); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\tcase pc.SrcFunc != nil && pc.SrcFunc.Name == \"checkpwd\":\n\t\t\tif err := pc.addCheckPwd(enc, pc.valueMatrix[idx].Values, dst); err != nil {\n\t\t\t\treturn err","sourceCodeStart":1372,"sourceCodeEnd":1408,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/outputnode.go#L1372-L1408","documentation":"In preTraverse, when the path is a @groupby query, Dgraph indexes groupby results by the position (idx) of the source UID in SrcUIDs. If GroupbyRes has fewer entries than that index, the two structures are out of sync, so it errors instead of reading out of bounds.","triggerScenarios":"toRDF path over a query whose Params.IsGroupBy is true and pc.GroupbyRes was not populated for every SrcUIDs entry (e.g. groupby block matched fewer groups than source UIDs, or an internal mismatch in recurse/multi-block queries).","commonSituations":"RDF-format responses requested for @groupby queries; groupby combined with recurse or keys where result rows are sparser than source UIDs; Dgraph version bugs in groupby result alignment.","solutions":["Request JSON output instead of RDF for @groupby queries (drop Accept: application/rdf)","Add filters so groupby groups align with source UIDs, or split the groupby into its own query","Upgrade Dgraph to a release fixing groupby/RDF traversal alignment","Avoid combining @recurse with @groupby in the same query"],"exampleFix":"// before\ncurl -H 'Accept: application/rdf' 'localhost:8080/query?...groupby-query...'\n// after\ncurl -H 'Accept: application/json' 'localhost:8080/query?...groupby-query...'","handlingStrategy":"fallback","validationCode":"if (/@groupby/.test(q) && wantsRdfOutput) throw new Error('use JSON output for groupby queries');","typeGuard":null,"tryCatchPattern":"try {\n  return await fetchRdf(q);\n} catch (e) {\n  if (String(e).includes('Unexpected length while adding Groupby')) {\n    return await fetchJson(q); // fall back to JSON encoding\n  }\n  throw e;\n}","preventionTips":["Never request RDF output for @groupby queries","Avoid combining @recurse with @groupby","Upgrade Dgraph to a release with groupby/RDF alignment fixes","Split groupby aggregations into dedicated queries"],"tags":["dgraph","groupby","rdf","internal-state"],"backgroundTag":"groupby-index-out-of-sync","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}