From 74bc59198e34992bc295d5391482424d137afb47 Mon Sep 17 00:00:00 2001 From: leo7martins <219400372+leo7martins@users.noreply.github.com> Date: Mon, 30 Nov 2026 08:27:00 +0000 Subject: [PATCH] Fix chained MATCH rebinding a zero-row variable into a path A MATCH on a missing label projects NULL constants and a false WHERE, so it yields no rows. A following MATCH q = (var), including one after WITH, builds _agtype_build_path() around a Var that points at that constant. The function is IMMUTABLE and rejects NULL, and subquery pull-up substitutes the constant into the call. The planner evaluates it while folding constants and raises "argument 1 must not be null" instead of returning count 0. Same-clause paths already turned a NULL entity into a NULL path. Follow a rebound Var through subquery target lists and do the same when it refers to a NULL constant, so the function is not called. Non-empty matches still build the path from the real vertex. Fixes 2616 --- regress/expected/cypher_match.out | 89 ++++++++++++++++++++++++ regress/sql/cypher_match.sql | 51 ++++++++++++++ src/backend/parser/cypher_clause.c | 104 +++++++++++++++++++++++++++-- 3 files changed, 239 insertions(+), 5 deletions(-) diff --git a/regress/expected/cypher_match.out b/regress/expected/cypher_match.out index 53d51b5be..53b38b359 100644 --- a/regress/expected/cypher_match.out +++ b/regress/expected/cypher_match.out @@ -3956,6 +3956,95 @@ NOTICE: graph "issue_2378" has been dropped (1 row) +-- +-- Issue 2616 +-- +-- Rebinding a variable that a previous MATCH bound to zero rows (a missing +-- relationship label compiles to a NULL entity) must not raise +-- "argument 1 must not be null". The query matches nothing, so count is 0. +-- +SELECT create_graph('issue_2616'); +NOTICE: graph "issue_2616" has been created + create_graph +-------------- + +(1 row) + +-- Empty graph: chained MATCH rebinds m2 into a path. +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + b +--- + 0 +(1 row) + +-- Empty graph: WITH between the two MATCHes. +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + WITH m2 + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + b +--- + 0 +(1 row) + +-- Empty graph: a fresh variable, rather than a rebound one, returns 0. +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + MATCH q = (zz) + RETURN count(q) +$$) AS (b agtype); + b +--- + 0 +(1 row) + +-- Non-empty graph: the same rebound queries return the matched path. +SELECT * FROM cypher('issue_2616', $$ + CREATE (:v {id: 1})-[:rt3]->(:v {id: 2}) +$$) AS (a agtype); + a +--- +(0 rows) + +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + b +--- + 1 +(1 row) + +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + WITH m2 + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + b +--- + 1 +(1 row) + +SELECT drop_graph('issue_2616', true); +NOTICE: drop cascades to 4 other objects +DETAIL: drop cascades to table issue_2616._ag_label_vertex +drop cascades to table issue_2616._ag_label_edge +drop cascades to table issue_2616.v +drop cascades to table issue_2616.rt3 +NOTICE: graph "issue_2616" has been dropped + drop_graph +------------ + +(1 row) + -- -- Clean up -- diff --git a/regress/sql/cypher_match.sql b/regress/sql/cypher_match.sql index 8da012ff8..baa36f53e 100644 --- a/regress/sql/cypher_match.sql +++ b/regress/sql/cypher_match.sql @@ -1689,6 +1689,57 @@ $$) AS (name agtype, friend agtype); SELECT drop_graph('issue_2378', true); +-- +-- Issue 2616 +-- +-- Rebinding a variable that a previous MATCH bound to zero rows (a missing +-- relationship label compiles to a NULL entity) must not raise +-- "argument 1 must not be null". The query matches nothing, so count is 0. +-- +SELECT create_graph('issue_2616'); + +-- Empty graph: chained MATCH rebinds m2 into a path. +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + +-- Empty graph: WITH between the two MATCHes. +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + WITH m2 + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + +-- Empty graph: a fresh variable, rather than a rebound one, returns 0. +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + MATCH q = (zz) + RETURN count(q) +$$) AS (b agtype); + +-- Non-empty graph: the same rebound queries return the matched path. +SELECT * FROM cypher('issue_2616', $$ + CREATE (:v {id: 1})-[:rt3]->(:v {id: 2}) +$$) AS (a agtype); + +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + +SELECT * FROM cypher('issue_2616', $$ + MATCH (m1)-[:rt3]->(m2) + WITH m2 + MATCH q = (m2) + RETURN count(q) +$$) AS (b agtype); + +SELECT drop_graph('issue_2616', true); + -- -- Clean up -- diff --git a/src/backend/parser/cypher_clause.c b/src/backend/parser/cypher_clause.c index 147e3e74e..2175f5baf 100644 --- a/src/backend/parser/cypher_clause.c +++ b/src/backend/parser/cypher_clause.c @@ -199,6 +199,7 @@ static Node *make_edge_expr(cypher_parsestate *cpstate, ParseNamespaceItem *pnsi); static Node *make_qual(cypher_parsestate *cpstate, transform_entity *entity, char *name); +static bool expr_is_null_constant(Node *expr, List *rtable); static TargetEntry * transform_match_create_path_variable(cypher_parsestate *cpstate, cypher_path *path, List *entities); @@ -6327,6 +6328,98 @@ static List *make_path_join_quals(cypher_parsestate *cpstate, List *entities) } } +/* + * True when expr is a NULL constant, or a Var that refers, through subquery + * target lists, to a NULL constant. + * + * A missing label is compiled as a NULL constant plus a false WHERE, so the + * MATCH yields no rows. A later clause that rebinds the variable only sees a + * Var. _agtype_build_path() is IMMUTABLE and errors on NULL, and subquery + * pull-up substitutes the constant into that call. The planner then evaluates + * it while folding constants, which raises "argument 1 must not be null" + * even though no row is produced. Callers should emit a NULL path instead. + */ +static bool +expr_is_null_constant(Node *expr, List *rtable) +{ + int depth; + + for (depth = 0; depth < 64; depth++) + { + Var *var; + RangeTblEntry *rte; + TargetEntry *te; + ListCell *lc; + + if (expr == NULL) + { + return false; + } + + if (IsA(expr, RelabelType)) + { + expr = (Node *) ((RelabelType *) expr)->arg; + continue; + } + + if (IsA(expr, Const)) + { + return ((Const *) expr)->constisnull; + } + + if (!IsA(expr, Var)) + { + return false; + } + + var = (Var *) expr; + + /* + * Only a Var of this query's subquery RTE can be followed back to the + * constant produced for a missing label. Outer references and system + * columns are real values at execution time. + */ + if (var->varlevelsup != 0 || var->varattno <= 0 || rtable == NIL) + { + return false; + } + + if (var->varno < 1 || var->varno > (Index) list_length(rtable)) + { + return false; + } + + rte = rt_fetch(var->varno, rtable); + + if (rte->rtekind != RTE_SUBQUERY || rte->subquery == NULL) + { + return false; + } + + te = NULL; + foreach (lc, rte->subquery->targetList) + { + TargetEntry *candidate = (TargetEntry *) lfirst(lc); + + if (!candidate->resjunk && candidate->resno == var->varattno) + { + te = candidate; + break; + } + } + + if (te == NULL) + { + return false; + } + + expr = (Node *) te->expr; + rtable = rte->subquery->rtable; + } + + return false; +} + /* * Create the path variable. Takes the list of entities, extracts the variable * and passes as the argument list for the _agtype_build_path function. @@ -6361,11 +6454,11 @@ transform_match_create_path_variable(cypher_parsestate *cpstate, Node *entity_expr = (Node *)entity->expr; /* - * Is it a NULL constant, meaning there was an invalid label? - * If so, flag it for later + * A NULL constant means an invalid label, in this clause or in + * the clause that bound a rebound variable. Flag it so the path + * is NULL instead of a call to _agtype_build_path(). */ - if (IsA(entity_expr, Const) && - ((Const*)(entity_expr))->constisnull) + if (expr_is_null_constant(entity_expr, pstate->p_rtable)) { null_path_entity = true; } @@ -6386,7 +6479,8 @@ transform_match_create_path_variable(cypher_parsestate *cpstate, /* * If we have a NULL in the path, there is an invalid label, so there aren't * any paths to be selected - the path variable will be NULL. In this case - * we need to return a NULL constant instead. + * we need to return a NULL constant instead of calling _agtype_build_path(), + * which errors on NULL and can be evaluated while the plan is folded. */ if (null_path_entity) {