Skip to content

Allow a MATCH clause to follow OPTIONAL MATCH - #2626

Open
leo7martins wants to merge 1 commit into
apache:masterfrom
leo7martins:fix/2622-match-after-optional-match
Open

leo7martins wants to merge 1 commit into
apache:masterfrom
leo7martins:fix/2622-match-after-optional-match

Conversation

@leo7martins

Copy link
Copy Markdown

Fixes #2622.

A MATCH clause placed directly after OPTIONAL MATCH was rejected with
"MATCH cannot follow OPTIONAL MATCH". openCypher allows those reading
clauses in one scope. AGE already ran the sibling orders (MATCH after
MATCH, OPTIONAL MATCH after OPTIONAL MATCH, and OPTIONAL MATCH, WITH,
MATCH) and returned the right counts on empty and non-empty graphs.

The rejection was a syntax check in transform_cypher_match_pattern(),
added in #740 because the clause order was treated as invalid Cypher.
It is not required for a correct plan. The following MATCH is already
transformed as an inner join against the previous clause. When that
clause is an OPTIONAL MATCH with a predecessor, the previous result is
the LATERAL LEFT JOIN built for OPTIONAL MATCH. Null-extended rows from
that left join are kept unless the MATCH itself filters them. A search
of the git history and of public issue and mailing-list discussion did
not turn up a correctness requirement for the blanket error.

The check is removed. OPTIONAL MATCH followed by another OPTIONAL MATCH
still uses the left-join path.

Regression coverage is in regress/sql/cypher_match.sql (issue 2622):
empty graph and a 2-vertex, 1-edge graph, the reported clause orders,
a missed optional pattern joined with a later MATCH (with and without
WITH), and a property constraint on a variable bound by OPTIONAL MATCH.

Tested on PostgreSQL 18.7:

  • make PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config COPT=-Werror -j4
    (exit 0)
  • sudo make PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config install
    (exit 0)
  • make PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config installcheck
    REGRESS=cypher_match
    (1 of 1 passed)
  • make PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config installcheck
    (44 of 44 passed)

This was written with help from an AI coding assistant.

A MATCH directly after OPTIONAL MATCH was rejected as a syntax error.
openCypher chains those reading clauses in one scope. The following
MATCH is already transformed as an inner join against the previous
clause, which is the LATERAL LEFT JOIN of an OPTIONAL MATCH that has
a predecessor. Null-extended rows stay unless that MATCH filters them,
so the blanket rejection was not required.

Fixes apache#2622
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MATCH directly after OPTIONAL MATCH is rejected with "MATCH cannot follow OPTIONAL MATCH"

1 participant