diff --git a/regress/expected/cypher_match.out b/regress/expected/cypher_match.out index 53d51b5be..ca52a257a 100644 --- a/regress/expected/cypher_match.out +++ b/regress/expected/cypher_match.out @@ -1698,25 +1698,205 @@ SELECT * FROM cypher('cypher_match', $$ "someone" | | | "somebody" | | (12 rows) --- Tests to catch match following optional match logic --- this syntax is invalid in cypher -SELECT * FROM cypher('cypher_match', $$ - OPTIONAL MATCH (n) +-- +-- Issue 2622: MATCH directly after OPTIONAL MATCH +-- +-- openCypher allows the reading-clause orders MATCH -> MATCH, +-- OPTIONAL MATCH -> OPTIONAL MATCH, OPTIONAL MATCH -> WITH -> MATCH, +-- and OPTIONAL MATCH -> MATCH. The last one used to be rejected with +-- "MATCH cannot follow OPTIONAL MATCH". A following MATCH is an inner +-- join against the OPTIONAL MATCH result, including the null-extended +-- rows of a preceding left join. +-- +SELECT create_graph('issue_2622'); +NOTICE: graph "issue_2622" has been created + create_graph +-------------- + +(1 row) + +-- Empty graph: every order returns count 0. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) MATCH (m) RETURN count(m) +$$) AS (c agtype); + c +--- + 0 +(1 row) + +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) OPTIONAL MATCH (m) RETURN count(m) +$$) AS (c agtype); + c +--- + 0 +(1 row) + +SELECT * FROM cypher('issue_2622', $$ + MATCH (n) MATCH (m) RETURN count(m) +$$) AS (c agtype); + c +--- + 0 +(1 row) + +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) WITH n MATCH (m) RETURN count(m) +$$) AS (c agtype); + c +--- + 0 +(1 row) + +-- Non-empty: 2 vertices, 1 edge. The rt3 path matches once. +SELECT * FROM cypher('issue_2622', $$ + CREATE (:src {name: 'n1'})-[:rt3]->(:dst {name: 'n2'}) +$$) AS (a agtype); + a +--- +(0 rows) + +-- Reported form: OPTIONAL MATCH directly followed by MATCH. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n1)-[:rt3]->(n2) + MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + c +--- + 1 +(1 row) + +-- Minimal reproduction. Two vertices, so the cross product counts 4. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) MATCH (m) RETURN count(m) +$$) AS (c agtype); + c +--- + 4 +(1 row) + +-- Sibling: OPTIONAL MATCH -> OPTIONAL MATCH +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n1)-[:rt3]->(n2) + OPTIONAL MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + c +--- + 1 +(1 row) + +-- Sibling: MATCH -> MATCH +SELECT * FROM cypher('issue_2622', $$ + MATCH (n1)-[:rt3]->(n2) + MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + c +--- + 1 +(1 row) + +-- Sibling: OPTIONAL MATCH -> WITH -> MATCH +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n1)-[:rt3]->(n2) + WITH n1, n2 + MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + c +--- + 1 +(1 row) + +-- The optional pattern misses. Each outer vertex is kept, null-extended, +-- then cross-joined with the following independent MATCH: 2 x 2 = 4. +-- The WITH form must agree. +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:missing]->(b) + MATCH (c) + RETURN count(c) +$$) AS (c agtype); + c +--- + 4 +(1 row) + +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:missing]->(b) + WITH a, b + MATCH (c) + RETURN count(c) +$$) AS (c agtype); + c +--- + 4 +(1 row) + +-- A following MATCH that needs the optional binding drops the +-- null-extended rows. There is no :missing edge, so the count is 0. +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:missing]->(b) + MATCH (b)-[:rt3]->(c) + RETURN count(c) +$$) AS (c agtype); + c +--- + 0 +(1 row) + +-- The optional rt3 edge matches; the following MATCH keeps that row. +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:rt3]->(b) + MATCH (b) + RETURN count(b) +$$) AS (c agtype); + c +--- + 1 +(1 row) + +-- Property constraint against the variable bound by OPTIONAL MATCH. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) + MATCH (n {name: 'n1'}) + RETURN n.name +$$) AS (name agtype); + name +------ + "n1" +(1 row) + +-- Property constraint on the optional pattern, then an independent MATCH. +-- One vertex satisfies the constraint, times two vertices: count 2. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n {name: 'n1'}) MATCH (m) - RETURN n,m - $$) AS (n agtype, m agtype); -ERROR: MATCH cannot follow OPTIONAL MATCH -LINE 1: SELECT * FROM cypher('cypher_match', $$ - ^ -SELECT * FROM cypher('cypher_match', $$ - MATCH (n) - OPTIONAL MATCH (m) - MATCH (o) - RETURN n,m - $$) AS (n agtype, m agtype); -ERROR: MATCH cannot follow OPTIONAL MATCH -LINE 1: SELECT * FROM cypher('cypher_match', $$ - ^ + RETURN count(m) +$$) AS (c agtype); + c +--- + 2 +(1 row) + +SELECT drop_graph('issue_2622', true); +NOTICE: drop cascades to 5 other objects +DETAIL: drop cascades to table issue_2622._ag_label_vertex +drop cascades to table issue_2622._ag_label_edge +drop cascades to table issue_2622.src +drop cascades to table issue_2622.rt3 +drop cascades to table issue_2622.dst +NOTICE: graph "issue_2622" has been dropped + drop_graph +------------ + +(1 row) + -- -- Tests retrieving Var from some parent's cpstate during transformation -- diff --git a/regress/sql/cypher_match.sql b/regress/sql/cypher_match.sql index 8da012ff8..e78bf5122 100644 --- a/regress/sql/cypher_match.sql +++ b/regress/sql/cypher_match.sql @@ -805,20 +805,125 @@ SELECT * FROM cypher('cypher_match', $$ ORDER BY n, p, m, q $$) AS (n agtype, r agtype, p agtype, m agtype, s agtype, q agtype); --- Tests to catch match following optional match logic --- this syntax is invalid in cypher -SELECT * FROM cypher('cypher_match', $$ - OPTIONAL MATCH (n) +-- +-- Issue 2622: MATCH directly after OPTIONAL MATCH +-- +-- openCypher allows the reading-clause orders MATCH -> MATCH, +-- OPTIONAL MATCH -> OPTIONAL MATCH, OPTIONAL MATCH -> WITH -> MATCH, +-- and OPTIONAL MATCH -> MATCH. The last one used to be rejected with +-- "MATCH cannot follow OPTIONAL MATCH". A following MATCH is an inner +-- join against the OPTIONAL MATCH result, including the null-extended +-- rows of a preceding left join. +-- +SELECT create_graph('issue_2622'); + +-- Empty graph: every order returns count 0. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) MATCH (m) RETURN count(m) +$$) AS (c agtype); + +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) OPTIONAL MATCH (m) RETURN count(m) +$$) AS (c agtype); + +SELECT * FROM cypher('issue_2622', $$ + MATCH (n) MATCH (m) RETURN count(m) +$$) AS (c agtype); + +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) WITH n MATCH (m) RETURN count(m) +$$) AS (c agtype); + +-- Non-empty: 2 vertices, 1 edge. The rt3 path matches once. +SELECT * FROM cypher('issue_2622', $$ + CREATE (:src {name: 'n1'})-[:rt3]->(:dst {name: 'n2'}) +$$) AS (a agtype); + +-- Reported form: OPTIONAL MATCH directly followed by MATCH. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n1)-[:rt3]->(n2) + MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + +-- Minimal reproduction. Two vertices, so the cross product counts 4. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) MATCH (m) RETURN count(m) +$$) AS (c agtype); + +-- Sibling: OPTIONAL MATCH -> OPTIONAL MATCH +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n1)-[:rt3]->(n2) + OPTIONAL MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + +-- Sibling: MATCH -> MATCH +SELECT * FROM cypher('issue_2622', $$ + MATCH (n1)-[:rt3]->(n2) + MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + +-- Sibling: OPTIONAL MATCH -> WITH -> MATCH +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n1)-[:rt3]->(n2) + WITH n1, n2 + MATCH (n1)-[:rt3]->(n2) + RETURN count(n2) +$$) AS (c agtype); + +-- The optional pattern misses. Each outer vertex is kept, null-extended, +-- then cross-joined with the following independent MATCH: 2 x 2 = 4. +-- The WITH form must agree. +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:missing]->(b) + MATCH (c) + RETURN count(c) +$$) AS (c agtype); + +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:missing]->(b) + WITH a, b + MATCH (c) + RETURN count(c) +$$) AS (c agtype); + +-- A following MATCH that needs the optional binding drops the +-- null-extended rows. There is no :missing edge, so the count is 0. +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:missing]->(b) + MATCH (b)-[:rt3]->(c) + RETURN count(c) +$$) AS (c agtype); + +-- The optional rt3 edge matches; the following MATCH keeps that row. +SELECT * FROM cypher('issue_2622', $$ + MATCH (a) + OPTIONAL MATCH (a)-[:rt3]->(b) + MATCH (b) + RETURN count(b) +$$) AS (c agtype); + +-- Property constraint against the variable bound by OPTIONAL MATCH. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n) + MATCH (n {name: 'n1'}) + RETURN n.name +$$) AS (name agtype); + +-- Property constraint on the optional pattern, then an independent MATCH. +-- One vertex satisfies the constraint, times two vertices: count 2. +SELECT * FROM cypher('issue_2622', $$ + OPTIONAL MATCH (n {name: 'n1'}) MATCH (m) - RETURN n,m - $$) AS (n agtype, m agtype); + RETURN count(m) +$$) AS (c agtype); -SELECT * FROM cypher('cypher_match', $$ - MATCH (n) - OPTIONAL MATCH (m) - MATCH (o) - RETURN n,m - $$) AS (n agtype, m agtype); +SELECT drop_graph('issue_2622', true); -- -- Tests retrieving Var from some parent's cpstate during transformation diff --git a/src/backend/parser/cypher_clause.c b/src/backend/parser/cypher_clause.c index 147e3e74e..a1da573f0 100644 --- a/src/backend/parser/cypher_clause.c +++ b/src/backend/parser/cypher_clause.c @@ -4137,23 +4137,17 @@ static Query *transform_cypher_match_pattern(cypher_parsestate *cpstate, query = makeNode(Query); query->commandType = CMD_SELECT; - if(self->optional == true && clause->next) - { - cypher_clause *next = clause->next; - - if (is_ag_node(next->self, cypher_match)) - { - cypher_match *next_self = (cypher_match *)next->self; - if (!next_self->optional) - { - ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("MATCH cannot follow OPTIONAL MATCH"), - parser_errposition(pstate, - exprLocation((Node *) next_self)))); - } - } - } + /* + * MATCH may follow OPTIONAL MATCH. Both are reading clauses, and + * openCypher chains them in one scope. The following MATCH is + * transformed on its own and inner-joined against this clause's + * result: the LATERAL LEFT JOIN built below when a previous clause + * exists, or this match when OPTIONAL MATCH is the first clause. + * Null-extended rows from that left join are kept unless the MATCH + * itself filters them, so a blanket rejection is not required + * (issue #2622). Another OPTIONAL MATCH still takes the left-join + * path below. + */ /* If there is no previous clause, transform to a general MATCH clause. */ if (self->optional == true && clause->prev != NULL)