Repository navigation
Description
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:
- Relax the
shouldUseOptimizergate for the anchor node only. The gate exists because a disjunction on the target side of a hop can't be represented byExpandAll/ExpandInto's singletargetLabel- but the anchor itself doesn't go through either of those operators; it seeds the chain viaIndexSelectionRule.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 aNodeByLabelDisjunctionIndexSeek(or the existingNodeByLabelDisjunctionScan) 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. - Or, separately, teach
ExpandAll/ExpandIntoto accept a disjunction target (a list of accepted types instead of onetargetLabel), 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
fix(#6482): admit a label-disjunction anchor with a relationship into…