Skip to content

Commit 2c202b7

Browse files
authored
docs(RETURN): document pattern expressions in projections + cost note (#364)
Pattern expressions (path patterns such as (a)-[:KNOWS]->(:Person)) can be used directly in a RETURN/WITH projection, not only as WHERE predicates. Adds a worked example to the RETURN reference with verified output, plus a performance note: a pattern expression is evaluated as an EXISTS subquery once per result row, so its cost scales with the number of rows projected. Documents the projection capability added in apache/age#2360.
1 parent 7d9a381 commit 2c202b7

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

docs/clauses/return.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,75 @@ Result
333333

334334

335335

336+
## Pattern expressions in projections
337+
338+
A *pattern expression* is a path pattern, such as `(a)-[:KNOWS]->(:Person)`, used in a position where a value is expected. It evaluates to a boolean that is `true` when at least one matching path exists. In addition to being used as a predicate in a `WHERE` clause, a pattern expression can be returned directly from a `RETURN` (or `WITH`) projection.
339+
340+
Query
341+
342+
343+
```postgresql
344+
SELECT *
345+
FROM cypher('graph_name', $$
346+
MATCH (a:Person)
347+
RETURN a.name, (a)-[:KNOWS]->(:Person) AS knows_someone
348+
ORDER BY a.name
349+
$$) as (name agtype, knows_someone agtype);
350+
```
351+
352+
353+
For each person, this returns a boolean indicating whether they have an outgoing `KNOWS` relationship to another `Person`.
354+
355+
Result
356+
357+
358+
<table>
359+
<tr>
360+
<td><strong>name</strong>
361+
</td>
362+
<td><strong>knows_someone</strong>
363+
</td>
364+
</tr>
365+
<tr>
366+
<td>"A"
367+
</td>
368+
<td>true
369+
</td>
370+
</tr>
371+
<tr>
372+
<td>"B"
373+
</td>
374+
<td>true
375+
</td>
376+
</tr>
377+
<tr>
378+
<td>"C"
379+
</td>
380+
<td>true
381+
</td>
382+
</tr>
383+
<tr>
384+
<td>"D"
385+
</td>
386+
<td>false
387+
</td>
388+
</tr>
389+
<tr>
390+
<td>"D"
391+
</td>
392+
<td>false
393+
</td>
394+
</tr>
395+
<tr>
396+
<td colspan="2" >(5 rows)
397+
</td>
398+
</tr>
399+
</table>
400+
401+
402+
**Performance:** A pattern expression is evaluated as an `EXISTS` subquery. In a projection it is evaluated once for every row produced by the match, so its cost grows with the number of rows returned. Returning several pattern expressions, or returning them over a large result set, can be expensive on large graphs; where possible, narrow the result set with a `WHERE` clause before projecting pattern expressions.
403+
404+
336405
## Unique results
337406

338407
`DISTINCT` retrieves only unique records depending on the fields that have been selected to output.

0 commit comments

Comments
 (0)