Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions regress/expected/cypher_match.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
51 changes: 51 additions & 0 deletions regress/sql/cypher_match.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
104 changes: 99 additions & 5 deletions src/backend/parser/cypher_clause.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down