age icon indicating copy to clipboard operation
age copied to clipboard

Single-node labeled pattern (a:Label) does not filter by label (vertex-only EXISTS skips label quals)

Open gregfelice opened this issue 5 months ago • 2 comments

Summary

Single-node labeled pattern expressions (a:Label) are accepted as boolean expressions but do not filter by label — they evaluate as trivially true. This is a pre-existing limitation in the vertex-only pattern transform, surfaced by #2360 (which lets a bare pattern appear anywhere an expression is valid).

Reproduction

SELECT * FROM cypher('g', $$ CREATE (:Person {name:'Alice'}), (:Animal {name:'Rex'}) $$) AS (r agtype);

-- expected: only "Alice"; actual: "Alice" AND "Rex"
SELECT * FROM cypher('g', $$ MATCH (a) WHERE (a:Person) RETURN a.name $$) AS (n agtype);

-- expected: a boolean reflecting whether a has label Person; actual: always true
SELECT * FROM cypher('g', $$ MATCH (a:Person) RETURN (a:Person) $$) AS (b agtype);

Root cause

make_path_join_quals() in src/backend/parser/cypher_clause.c:5454 early-returns for vertex-only patterns:

/* for vertex only queries, there is no work to do */
if (list_length(entities) < 3)
{
    return NIL;
}

The label-filter quals (_extract_label_id(id) = label_id) are only emitted inside make_join_condition_for_edge(), which is never reached without a relationship. So a single-vertex pattern desugars to an EXISTS subquery with no label constraint → trivially true.

Scope

  • Relationship patterns ((a)-[:R]->(b)) correlate and filter correctly — the path goes through the ≥3-entity branch. This is the feature #2360 targets and it is unaffected.
  • The buggy early-return is byte-identical on master; #2360 touches no transform code. It only makes the limitation reachable via the new (a:Label) expression surface.

Suggested fix

Emit the label-filter qual for single bound vertices before the early return in make_path_join_quals (or hoist the label-filter logic out of make_join_condition_for_edge so it applies to vertex-only patterns), plus a regression test asserting WHERE (a:Person) filters and RETURN (a:Label) reflects the label.

Found during review of #2360.

gregfelice avatar Jun 22 '26 20:06 gregfelice

Correction — the root cause above is incomplete. Verified by instrumenting the transform and inspecting the generated plans; the real cause is a missing correlation, not the make_path_join_quals early-return on its own.

What actually happens: a single-node labeled pattern desugars to an EXISTS sub-pattern. The inner vertex is not correlated to the outer variable — it is handled like any vertex, and with no edge to carry a correlation qual, the sub-pattern references nothing from the enclosing query. The planner therefore produces an uncorrelated one-time InitPlan that is true whenever any vertex of that label exists, independent of the outer row:

InitPlan 1
  ->  Result
One-Time Filter: ((InitPlan 1).col1)::agtype

Relationship patterns work precisely because the edge-driven join (start_id = a.id) correlates them to the outer variable — confirmed by identical node-level state in both cases but a correlated SubPlan only for the relationship form.

So make_path_join_quals is the right place to fix, but the fix is to emit a correlated label filter, not merely "add the label qual": for a vertex-only pattern whose vertex has a non-default label and whose variable is bound in an ancestor parse state, emit _extract_label_id(<id>) = <label_id> via make_qual — whose name-based id reference resolves to the outer variable, which both correlates the sub-pattern and enforces the label. After the fix the plan is a correlated SubPlan:

SubPlan 1
  ->  Result
        One-Time Filter: ((_extract_label_id(a_1.id))::integer = 3)

Fix + regression coverage in #2444. Note: RETURN (a:Label) in a projection on an unlabeled bound variable is a separate, pre-existing "multiple labels" guard, orthogonal to this issue and left unchanged.

gregfelice avatar Jun 22 '26 21:06 gregfelice

Fix is up and ready for review: #2444. It makes single-node labeled patterns a correlated label predicate (WHERE / EXISTS), verified against a clean PG18 build with the full regression suite green (41/41). Merging #2444 will close this.

gregfelice avatar Jun 22 '26 21:06 gregfelice