Skip to content

Commit

Permalink
Fix issue #1399 EXISTS doesn't handle non-existent labels (#1405)
Browse files Browse the repository at this point in the history
Fixed issue #1399 where EXISTS doesn't handle non-existent labels.
However, the issue actually is due to the extra processing (read:
extra distance between the AST nodes) that prevented the transform
logic from being able to resolve variables created in previous clauses
further than one parent up.

Added logic to allow the `find_variable` function to search up the
parent parse nodes.

Added regression tests to cover these cases.
  • Loading branch information
jrgemignani authored Nov 17, 2023
1 parent 97dd87b commit a4911fd
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 23 deletions.
172 changes: 172 additions & 0 deletions regress/expected/cypher_match.out
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,167 @@ SELECT * FROM cypher('cypher_match', $$ MATCH p1=(n {name:'Dave'})-[]->() MATCH
true
(1 row)

--
-- Issue 1399 EXISTS leads to an error if a relation label does not exists as database table
--
SELECT create_graph('issue_1399');
NOTICE: graph "issue_1399" has been created
create_graph
--------------

(1 row)

-- this is an empty graph so these should return 0
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
c
---
(0 rows)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
c
---
(0 rows)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
c
---
(0 rows)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
c
---
(0 rows)

-- this is an empty graph so these should return false
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[]->())
RETURN count(foo) > 0
$$) as (c agtype);
c
-------
false
(1 row)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[]->())
RETURN count(foo) > 0
$$) as (c agtype);
c
-------
false
(1 row)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR]->())
RETURN count(foo) > 0
$$) as (c agtype);
c
-------
false
(1 row)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR]->())
RETURN count(foo) > 0
$$) as (c agtype);
c
-------
false
(1 row)

-- create 1 path
SELECT * FROM cypher('issue_1399', $$
CREATE (foo)-[:BAR]->() RETURN foo
$$) as (c agtype);
c
----------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
(1 row)

-- these should each return 1 row as it is a directed edge and
-- only one vertex can match.
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
c
----------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
(1 row)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
c
----------------------------------------------------------------
{"id": 281474976710658, "label": "", "properties": {}}::vertex
(1 row)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
c
----------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
(1 row)

SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
c
----------------------------------------------------------------
{"id": 281474976710658, "label": "", "properties": {}}::vertex
(1 row)

-- this should return 0 rows as it can't exist - that path isn't in BAR2
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR2]->())
RETURN foo
$$) as (c agtype);
c
---
(0 rows)

-- this should return 2 rows as they all exist
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR2]->())
RETURN foo
$$) as (c agtype);
c
----------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
{"id": 281474976710658, "label": "", "properties": {}}::vertex
(2 rows)

--
-- Clean up
--
Expand Down Expand Up @@ -2889,6 +3050,17 @@ NOTICE: graph "issue_945" has been dropped

(1 row)

SELECT drop_graph('issue_1399', true);
NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to table issue_1399._ag_label_vertex
drop cascades to table issue_1399._ag_label_edge
drop cascades to table issue_1399."BAR"
NOTICE: graph "issue_1399" has been dropped
drop_graph
------------

(1 row)

--
-- End
--
86 changes: 86 additions & 0 deletions regress/sql/cypher_match.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1182,13 +1182,99 @@ SELECT * FROM cypher('cypher_match', $$ MATCH p=()-[*]->() WHERE size(nodes(p))
SELECT * FROM cypher('cypher_match', $$ MATCH (n {name:'Dave'}) MATCH p=()-[*]->() WHERE nodes(p)[0] = n RETURN length(p) $$) as (length agtype);
SELECT * FROM cypher('cypher_match', $$ MATCH p1=(n {name:'Dave'})-[]->() MATCH p2=()-[*]->() WHERE p2=p1 RETURN p2=p1 $$) as (path agtype);

--
-- Issue 1399 EXISTS leads to an error if a relation label does not exists as database table
--
SELECT create_graph('issue_1399');
-- this is an empty graph so these should return 0
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
-- this is an empty graph so these should return false
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[]->())
RETURN count(foo) > 0
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[]->())
RETURN count(foo) > 0
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR]->())
RETURN count(foo) > 0
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR]->())
RETURN count(foo) > 0
$$) as (c agtype);
-- create 1 path
SELECT * FROM cypher('issue_1399', $$
CREATE (foo)-[:BAR]->() RETURN foo
$$) as (c agtype);
-- these should each return 1 row as it is a directed edge and
-- only one vertex can match.
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[]->())
RETURN foo
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR]->())
RETURN foo
$$) as (c agtype);
-- this should return 0 rows as it can't exist - that path isn't in BAR2
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE exists((foo)-[:BAR2]->())
RETURN foo
$$) as (c agtype);
-- this should return 2 rows as they all exist
SELECT * FROM cypher('issue_1399', $$
MATCH (foo)
WHERE NOT exists((foo)-[:BAR2]->())
RETURN foo
$$) as (c agtype);

--
-- Clean up
--
SELECT drop_graph('cypher_match', true);
SELECT drop_graph('test_retrieve_var', true);
SELECT drop_graph('test_enable_containment', true);
SELECT drop_graph('issue_945', true);
SELECT drop_graph('issue_1399', true);

--
-- End
Expand Down
53 changes: 30 additions & 23 deletions src/backend/parser/cypher_transform_entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,40 @@ transform_entity *find_variable(cypher_parsestate *cpstate, char *name)
{
ListCell *lc;

foreach (lc, cpstate->entities)
/* while we have cypher_parsestates to check */
while (cpstate)
{
transform_entity *entity = lfirst(lc);
char *entity_name;

if (entity->type == ENT_VERTEX)
{
entity_name = entity->entity.node->name;
}
else if (entity->type == ENT_EDGE || entity->type == ENT_VLE_EDGE)
{
entity_name = entity->entity.rel->name;
}
else if (entity->type == ENT_PATH)
foreach (lc, cpstate->entities)
{
entity_name = entity->entity.path->var_name;
}
else
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("unknown entity type")));
}
transform_entity *entity = lfirst(lc);
char *entity_name = NULL;

if (entity_name != NULL && !strcmp(name, entity_name))
{
return entity;
if (entity->type == ENT_VERTEX)
{
entity_name = entity->entity.node->name;
}
else if (entity->type == ENT_EDGE || entity->type == ENT_VLE_EDGE)
{
entity_name = entity->entity.rel->name;
}
else if (entity->type == ENT_PATH)
{
entity_name = entity->entity.path->var_name;
}
else
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("unknown entity type")));
}

if (entity_name != NULL && !strcmp(name, entity_name))
{
return entity;
}
}

/* go up to the next parent parse state */
cpstate = (cypher_parsestate*)cpstate->pstate.parentParseState;
}

return NULL;
Expand Down

0 comments on commit a4911fd

Please sign in to comment.