Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #1399 EXISTS doesn't handle non-existent labels (#1400) #1403

Merged
Merged
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
172 changes: 172 additions & 0 deletions regress/expected/cypher_match.out
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,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 @@ -2892,6 +3053,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 @@ -1178,13 +1178,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