dgraph-io/dgraph · error
Length of facetsMatrix and uidMatrix mismatch: %d vs %d
Error message
Length of facetsMatrix and uidMatrix mismatch: %d vs %d
What it means
While converting query output to RDF (toRDF/preTraverse), each UID in the path must have matching entries in both uidMatrix and facetsMatrix. A non-empty facetsMatrix whose length differs from uidMatrix indicates internal state corruption or a code path that populated facets without corresponding UID rows.
Source
Thrown at query/outputnode.go:1380
if pc.IsInternal() {
if pc.Params.Expand != "" {
continue
}
if pc.Params.Normalize && pc.Params.Alias == "" {
continue
}
if err := pc.addInternalNode(enc, uid, dst); err != nil {
return err
}
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
}
View on GitHub (pinned to 759e242be6)
Solutions
- Upgrade Dgraph — several facet/recurse matrix mismatches were fixed in later releases
- Remove or isolate the facet portion of the query to confirm it triggers the mismatch, then report with repro
- Restructure: fetch facets in a separate simpler query without recurse
- If programmatically building the SubGraph, ensure facetsMatrix rows are appended 1:1 with uidMatrix rows
Example fix
// before
{
q(func: uid(0x1)) @recurse {
friend @facets
}
}
// after (workaround)
{
q(func: uid(0x1)) {
friend @facets
}
} Defensive patterns
Strategy: try-catch
Validate before calling
// avoid the trigger: don't request facets on recurse paths
if (/@recurse/.test(q) && /@facets/.test(q)) throw new Error('facets with recurse unsupported; split query'); Try / catch
try {
return await dgraph.query(q);
} catch (e) {
if (String(e).includes('facetsMatrix and uidMatrix mismatch')) {
return await dgraph.query(stripFacets(q)); // retry without @facets
}
throw e;
} Prevention
- Don't combine @recurse with @facets on known-affected versions
- Upgrade Dgraph; several facet matrix bugs were fixed in later releases
- Fetch facets in a separate non-recursive query keyed by uid
- Report repros: mismatch indicates internal state inconsistency
When it happens
Trigger: A facets query path where facetsMatrix was appended but uidMatrix skipped/shortened (the source notes it can happen in recurse queries when len(uidMatrix)==0 is skipped but partial mismatch remains), passed to rdf conversion at outputnode.go:1380.
Common situations: Recurse queries with facets; bugs in specific Dgraph versions' facet handling; mixing facets on reverse edges where the matrix population diverges.
Related errors
- unexpected end of facets
- expected '(' but found %v at facet
- expected key but found %v
- empty facetKeys not allowed
- Unexpected length while adding Groupby. Idx: [%v], len: [%v]
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/4e422fbaff4ba40e.
Report an issue: GitHub.