dgraph-io/dgraph · error

Expected 2 child for not but got %d.

Error message

Expected 2 child for not but got %d.

What it means

The `and` branch of the facet filter arity check requires exactly two children. The message text mistakenly says "for not" (a known copy-paste quirk in the message string), but the failure is an AND node with child count other than 2.

Source

Thrown at worker/task.go:2487

	}

	for _, c := range tree.Children {
		ftreec, err := preprocessFilter(c)
		if err != nil {
			return nil, err
		}
		ftree.children = append(ftree.children, ftreec)
	}

	numChild := len(tree.Children)
	switch ftree.op {
	case "not":
		if numChild != 1 {
			return nil, errors.Errorf("Expected 1 child for not but got %d.", numChild)
		}
	case "and":
		if numChild != 2 {
			return nil, errors.Errorf("Expected 2 child for not but got %d.", numChild)
		}
	case "or":
		if numChild != 2 {
			return nil, errors.Errorf("Expected 2 child for not but got %d.", numChild)
		}
	default:
		return nil, errors.Errorf("Unsupported operation in facet filtering: %s.", tree.Op)
	}
	return ftree, nil
}

type countParams struct {
	readTs  uint64
	counts  []int64
	attr    string
	gid     uint32
	reverse bool   // If query is asking for ~pred
	fn      string // function name

View on GitHub (pinned to 759e242be6)

Solutions

  1. Limit each AND to two operands and nest the rest: (a AND b) AND c.
  2. Recheck the query's boolean tree shape; the confusing message still points to an arity problem on an `and` node.
  3. Ensure generated queries produce binary AND nodes.

Example fix

// before
@facets(eq(a,"1") AND eq(b,"2") AND eq(c,"3"))
// after
@facets((eq(a,"1") AND eq(b,"2")) AND eq(c,"3"))
Defensive patterns

Strategy: validation

Validate before calling

function validateAndArity(tree) {
  if (tree.op === "and" && tree.children.length !== 2)
    throw new Error("and must have exactly 2 children in facet filter");
  (tree.children || []).forEach(validateAndArity);
}

Try / catch

try {
  return await txn.query(query);
} catch (e) {
  if (/Expected 2 child/.test(String(e))) {
    // binarize AND/OR nodes and retry
  }
  throw e;
}

Prevention

When it happens

Trigger: An AND facet filter with one or three-plus operands, e.g. @facets(eq(a,"1") AND eq(b,"2") AND eq(c,"3")) built as a flat 3-child AND.

Common situations: Writing multi-term AND facet filters expecting n-ary support; query builders emitting n-ary AND nodes.

Related errors


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