Skip to content

openCypher: a label disjunction anchor with an incident relationship never reaches the optimizer, so #6397's index seek never fires for it #6482

Closed

Description

@robfrank

Follow-up to #6397 (PR #6480), found in that PR's review.

#6397 taught AnchorSelector/IndexSelectionRule to turn a label-disjunction anchor's equality predicate into a union of per-root NodeIndexSeeks (NodeByLabelDisjunctionIndexSeek) instead of always scanning. That path is real and tested, but it only ever fires for a relationship-free disjunction pattern - MATCH (n:A|B {id: $x}) RETURN n-shaped queries.

Why

CypherExecutionPlanner.shouldUseOptimizer() bails out of the cost-based optimizer for the whole statement whenever any node in a path has a label disjunction and that path has an incident relationship:

// engine/src/main/java/com/arcadedb/query/opencypher/planner/CypherExecutionPlanner.java (~line 259)
if (node.getLabels().size() > 1) {
  if (!node.isLabelDisjunction())
    return false;
  if (path.getRelationshipCount() > 0)
    return false;
}

So MATCH (n:Person|Company {id: $x})-[:KNOWS]->(m) never reaches AnchorSelector at all - it falls straight to the legacy execution engine, which only knows how to scan every alternative's type. This gate predates #6397 and is not a regression from that PR; #6397 just didn't extend past it, since ExpandAll/ExpandInto (the optimizer's hop operators) each carry a single targetLabel and cannot represent a disjunction's OR semantics on the far side of a hop - the existing comment right above the gate says as much.

MATCH (n:A|B {id: $x})-[:REL]->(m) is arguably the more common real-world shape for a disjunction anchor than a bare RETURN n with no relationships at all, so this is worth closing rather than leaving as a silent gap.

Suggested shape

Two independent pieces, either of which unblocks the common case on its own:

  1. Relax the shouldUseOptimizer gate for the anchor node only. The gate exists because a disjunction on the target side of a hop can't be represented by ExpandAll/ExpandInto's single targetLabel - but the anchor itself doesn't go through either of those operators; it seeds the chain via IndexSelectionRule.createAnchorOperator, which openCypher: a label disjunction never uses an index, so (n:A|B {id: $x}) full-scans every alternative where per-type seeks would resolve it #6397 already taught to build a NodeByLabelDisjunctionIndexSeek (or the existing NodeByLabelDisjunctionScan) for it. If the disjunction is only ever on the pattern's anchor - not on a node reached by a relationship - the optimizer should be able to admit the statement.
  2. Or, separately, teach ExpandAll/ExpandInto to accept a disjunction target (a list of accepted types instead of one targetLabel), which would additionally cover a disjunction on a non-anchor node. This is the larger of the two and was explicitly out of scope for openCypher: a label disjunction never uses an index, so (n:A|B {id: $x}) full-scans every alternative where per-type seeks would resolve it #6397.

Either way, a regression test should pin EXPLAIN MATCH (n:A|B {id: $x})-[:REL]->(m) RETURN m actually using the cost-based optimizer (and, once anchored, actually using an index seek rather than a scan when every alternative is indexed) - CypherDisjunctionIndexSeekIssue6397Test intentionally has no such case since it predates this fix.

Scope note

Purely a missed-optimization / performance gap, not a correctness bug: a disjunction+relationship query still returns the right rows today via the legacy scan-only path, just without the index seek speedup #6397 unlocked for the relationship-free case.

Activity

  1. added this to the 26.9.1 milestone on Aug 20, 2026
  2. self-assigned this
    on Aug 24, 2026
  3. added
    in progress
    on Aug 24, 2026
  4. added a commit that references this issue on Aug 24, 2026
  5. removed
    in progress
    on Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions