zihaozeng0021 opened a new issue, #2618:
URL: https://github.com/apache/age/issues/2618

   # Leading OPTIONAL MATCH drops the input row when the pattern has no match
   
   ### Configuration
   
   Docker Image: `apache/age:release_PG18_1.8.0`
   Image Digest: 
`sha256:47a0b054c3663a3e64a25fc0c5a982f1b58348019274b326a691cdf77f78eb58`
   PostgreSQL Version: `18.6`
   Apache AGE Extension Version: `1.8.0`
   Operating System: WSL2, Ubuntu 24.04.1 LTS
   Installation Method: Docker
   Query Interface: `psql` (`SELECT * FROM cypher('xgdb', $$ ... $$) AS (...)`)
   
   ---
   
   ### Description
   
   When `OPTIONAL MATCH` is the first (leading/standalone) clause of a query and
   the pattern has no match, AGE returns 0 rows instead of the 1 row with null 
bindings required by
   openCypher's left-join semantics. The same `OPTIONAL MATCH` following a 
successful `MATCH` keeps
   the input row correctly, which shows the defect is specific to the leading 
position. This matches
   upstream apache/age#2473: only when the first clause is `OPTIONAL MATCH` 
does the engine run it
   as a plain `MATCH` path.
   
   ---
   
   ### Steps to reproduce
   
   1. Start the AGE container and connect with `psql`.
   2. On a fresh empty graph run the three forms below:
   
   ```sql
   SELECT ag_catalog.create_graph('xgdb');
   
   -- R1 minimal: standalone OPTIONAL MATCH, empty graph -> expect 1 row 
(n=null)
   SELECT * FROM cypher('xgdb', $$ OPTIONAL MATCH (n) RETURN n $$) AS (n 
agtype);
   
   -- R2 combo query shape (multiline RETURN list + LIMIT), empty graph
   SELECT * FROM cypher('xgdb', $$
     OPTIONAL MATCH (n1)-[:rt0]->(n0)
     WITH n1,n0
     RETURN n0,
         n1
     LIMIT 3
   $$) AS (n0 agtype, n1 agtype);
   
   -- R3 control: OPTIONAL MATCH after a successful MATCH
   SELECT * FROM cypher('xgdb', $$ CREATE (:v1 {id: 1}) $$) AS (r agtype);
   SELECT * FROM cypher('xgdb', $$
     MATCH (v:v1)
     OPTIONAL MATCH (x)-[:r]->(y)
     RETURN v, x, y
   $$) AS (v agtype, x agtype, y agtype);
   ```
   
   ---
   
   ### Expected behaviour
   
   openCypher `OPTIONAL MATCH` is a left join: with no match it must produce 
one row with the
   pattern variables bound to null. R1 and R2 should each return exactly 1 row 
with null values;
   R3 should return 1 row with `x` and `y` null. Neo4j 2026.10.2 was verified 
to behave this way on
   the same shapes.
   
   ---
   
   ### Actual behaviour
   
   Both leading forms return 0 rows on both fresh-container runs:
   
   ```text
    n
   ---
   (0 rows)
   
    n0 | n1
   ----+----
   (0 rows)
   ```
   
   The R3 control is correct and shows the row is only lost in the leading 
position:
   
   ```text
                                       v                                    | x 
| y
   
-------------------------------------------------------------------------+---+---
    {"id": 844424930131969, "label": "v1", "properties": {"id": 1}}::vertex |   
|
   (1 row)
   ```
   
   Multi-line RETURN lists parse fine, the anomaly is only the missing null row.
   Backend alive (`RestartCount=0`), no FATAL/PANIC in the server log.
   
   ---
   
   ### Minimal reproduction
   
   ```sql
   SELECT * FROM cypher('xgdb', $$ OPTIONAL MATCH (n) RETURN n $$) AS (n 
agtype);
   ```
   
   Empty graph; expect 1 row `(null)`, get 0 rows.
   
   ---
   
   ### Why this appears to be a bug
   
   Dropping the implicit input row silently changes query results: aggregate 
pipelines that expect a
   null row (`count`, `collect`, outer-join style reporting) see empty input 
instead. The behavior
   contradicts openCypher's left-join semantics, differs from AGE's own 
behavior for the same clause
   in non-leading position, and reproduces the still-open upstream report 
[apache/age#2473](https://github.com/apache/age/issues/2473).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to