dgraph-io/dgraph · error

Expected GroupbyRes to have length > 0.

Error message

Expected GroupbyRes to have length > 0.

What it means

For @groupby queries, processNodeUids expects GroupbyRes to contain at least one result entry before calling addGroupby with GroupbyRes[0]. An empty GroupbyRes means the groupby aggregation produced nothing, which is treated as an internal consistency error rather than an empty response.

Source

Thrown at query/outputnode.go:1115

	if sg.Params.IsEmpty {
		return sg.addAggregations(enc, fj)
	}

	enc.curSize += uint64(len(sg.Params.Alias))

	attrID := enc.idForAttr(sg.Params.Alias)
	if sg.uidMatrix == nil {
		enc.AddListChild(fj, enc.newNode(attrID))
		return nil
	}

	hasChild, err := sg.handleCountUIDNodes(enc, fj, len(sg.DestUIDs.Uids))
	if err != nil {
		return err
	}
	if sg.Params.IsGroupBy {
		if len(sg.GroupbyRes) == 0 {
			return errors.Errorf("Expected GroupbyRes to have length > 0.")
		}
		return sg.addGroupby(enc, fj, sg.GroupbyRes[0], sg.Params.Alias)
	}

	lenList := len(sg.uidMatrix[0].Uids)
	for i := range lenList {
		uid := sg.uidMatrix[0].Uids[i]
		if algo.IndexOf(sg.DestUIDs, uid) < 0 {
			// This UID was filtered. So Ignore it.
			continue
		}

		n1 := enc.newNode(attrID)
		enc.setAttr(n1, enc.idForAttr(sg.Params.Alias))
		if err := sg.preTraverse(enc, uid, n1); err != nil {
			if err.Error() == "_INV_" {
				continue
			}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Verify the groupby block actually matches data: run the inner func/filter without @groupby first
  2. Add a has() filter or correct the predicate name so the block matches nodes
  3. Guard client-side: catch this error and treat it as an empty result set if the data legitimately may not exist
  4. Upgrade Dgraph if this occurs despite non-empty matches — it can indicate an encoding-path bug

Example fix

// before
{
  q(func: type(Person)) @groupby(neverIndexedPred) {
    count(uid)
  }
}
// after
{
  q(func: type(Person)) @filter(has(someField)) @groupby(city) {
    count(uid)
  }
}
Defensive patterns

Strategy: try-catch

Validate before calling

const [{ count }] = await dgraph.query('{ c(func: type(Person)) { count(uid) } }');
if (count === 0) throw new Error('skip groupby: no source data');

Try / catch

try {
  return await dgraph.query(groupbyQuery);
} catch (e) {
  if (String(e).includes('Expected GroupbyRes to have length > 0')) {
    return []; // treat as empty grouping
  }
  throw e;
}

Prevention

When it happens

Trigger: A query with @groupby whose source UIDs match nothing (sg.DestUIDs empty or uidMatrix empty) yet Params.IsGroupBy is set, so sg.GroupbyRes is length 0 when encoding toFastJSON runs.

Common situations: @groupby on a predicate combined with a filter matching no nodes; groupby over an empty reverse edge; querying a groupby block before data exists (fresh instance / wrong namespace).

Related errors


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