dgraph-io/dgraph · error

Unexpected length while adding Groupby. Idx: [%v], len: [%v]

Error message

Unexpected length while adding Groupby. Idx: [%v], len: [%v]

What it means

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.

Source

Thrown at query/outputnode.go:1390

			continue
		}

		if len(pc.uidMatrix) == 0 {
			// Can happen in recurse query.
			continue
		}
		if len(pc.facetsMatrix) > 0 && len(pc.facetsMatrix) != len(pc.uidMatrix) {
			return errors.Errorf("Length of facetsMatrix and uidMatrix mismatch: %d vs %d",
				len(pc.facetsMatrix), len(pc.uidMatrix))
		}

		idx := algo.IndexOf(pc.SrcUIDs, uid)
		if idx < 0 {
			continue
		}
		if pc.Params.IsGroupBy {
			if len(pc.GroupbyRes) <= idx {
				return errors.Errorf("Unexpected length while adding Groupby. Idx: [%v], len: [%v]",
					idx, len(pc.GroupbyRes))
			}
			if err := pc.addGroupby(enc, dst, pc.GroupbyRes[idx], pc.fieldName()); err != nil {
				return err
			}
			continue
		}

		fieldName := pc.fieldName()
		switch {
		case len(pc.counts) > 0:
			if err := pc.addCount(enc, uint64(pc.counts[idx]), dst); err != nil {
				return err
			}

		case pc.SrcFunc != nil && pc.SrcFunc.Name == "checkpwd":
			if err := pc.addCheckPwd(enc, pc.valueMatrix[idx].Values, dst); err != nil {
				return err

View on GitHub (pinned to 759e242be6)

Solutions

  1. Request JSON output instead of RDF for @groupby queries (drop Accept: application/rdf)
  2. Add filters so groupby groups align with source UIDs, or split the groupby into its own query
  3. Upgrade Dgraph to a release fixing groupby/RDF traversal alignment
  4. Avoid combining @recurse with @groupby in the same query

Example fix

// before
curl -H 'Accept: application/rdf' 'localhost:8080/query?...groupby-query...'
// after
curl -H 'Accept: application/json' 'localhost:8080/query?...groupby-query...'
Defensive patterns

Strategy: fallback

Validate before calling

if (/@groupby/.test(q) && wantsRdfOutput) throw new Error('use JSON output for groupby queries');

Try / catch

try {
  return await fetchRdf(q);
} catch (e) {
  if (String(e).includes('Unexpected length while adding Groupby')) {
    return await fetchJson(q); // fall back to JSON encoding
  }
  throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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