3.7.2. Expand operators

These operators explore the graph by expanding graph patterns.

Operators:

3.7.2.1. Expand All

Given a start node, expand-all will follow incoming or outgoing relationships, depending on the pattern relationship. Can also handle variable length pattern relationships.

Query. 

MATCH (p:Person { name: 'me' })-[:FRIENDS_WITH]->(fof)
RETURN fof

Query Plan. 

Compiler CYPHER 3.3

Planner COST

Runtime INTERPRETED

+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+--------------------+----------------------------+
| Operator        | Estimated Rows | Rows | DB Hits | Page Cache Hits | Page Cache Misses | Page Cache Hit Ratio | Variables          | Other                      |
+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+--------------------+----------------------------+
| +ProduceResults |              0 |    1 |       0 |               2 |                 0 |               1.0000 | anon[30], fof, p   |                            |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+--------------------+----------------------------+
| +Expand(All)    |              0 |    1 |       2 |               2 |                 0 |               1.0000 | anon[30], fof -- p | (p)-[:FRIENDS_WITH]->(fof) |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+--------------------+----------------------------+
| +NodeIndexSeek  |              1 |    1 |       2 |               2 |                 0 |               1.0000 | p                  | :Person(name)              |
+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+--------------------+----------------------------+

Total database accesses: 4

3.7.2.2. Expand Into

When both the start and end node have already been found, expand-into is used to find all connecting relationships between the two nodes.

Query. 

MATCH (p:Person { name: 'me' })-[:FRIENDS_WITH]->(fof)-->(p)
RETURN fof

Query Plan. 

Compiler CYPHER 3.3

Planner COST

Runtime INTERPRETED

+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+
| Operator        | Estimated Rows | Rows | DB Hits | Page Cache Hits | Page Cache Misses | Page Cache Hit Ratio | Variables                    | Other                      |
+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+
| +ProduceResults |              0 |    0 |       0 |               2 |                 0 |               1.0000 | anon[30], anon[53], fof, p   |                            |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+
| +Filter         |              0 |    0 |       0 |               2 |                 0 |               1.0000 | anon[30], anon[53], fof, p   | NOT(anon[30] = anon[53])   |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+
| +Expand(Into)   |              0 |    0 |       0 |               2 |                 0 |               1.0000 | anon[30] -- anon[53], fof, p | (p)-[:FRIENDS_WITH]->(fof) |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+
| +Expand(All)    |              0 |    0 |       1 |               2 |                 0 |               1.0000 | anon[53], fof -- p           | (p)<--(fof)                |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+
| +NodeIndexSeek  |              1 |    1 |       2 |               2 |                 0 |               1.0000 | p                            | :Person(name)              |
+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+------------------------------+----------------------------+

Total database accesses: 3

3.7.2.3. Optional Expand All

Optional expand traverses relationships from a given node, and makes sure that predicates are evaluated before producing rows.

If no matching relationships are found, a single row with null for the relationship and end node variable is produced.

Query. 

MATCH (p:Person)
OPTIONAL MATCH (p)-[works_in:WORKS_IN]->(l)
WHERE works_in.duration > 180
RETURN p, l

Query Plan. 

Compiler CYPHER 3.3

Planner COST

Runtime INTERPRETED

+----------------------+----------------+------+---------+-----------------+-------------------+----------------------+------------------+----------------------------------------------------------------+
| Operator             | Estimated Rows | Rows | DB Hits | Page Cache Hits | Page Cache Misses | Page Cache Hit Ratio | Variables        | Other                                                          |
+----------------------+----------------+------+---------+-----------------+-------------------+----------------------+------------------+----------------------------------------------------------------+
| +ProduceResults      |             14 |   15 |       0 |               3 |                 0 |               1.0000 | l, p, works_in   |                                                                |
| |                    +----------------+------+---------+-----------------+-------------------+----------------------+------------------+----------------------------------------------------------------+
| +OptionalExpand(All) |             14 |   15 |      44 |               3 |                 0 |               1.0000 | l, works_in -- p | works_in.duration > {  AUTOINT0}; (p)-[works_in:WORKS_IN]->(l) |
| |                    +----------------+------+---------+-----------------+-------------------+----------------------+------------------+----------------------------------------------------------------+
| +NodeByLabelScan     |             14 |   14 |      15 |               4 |                 0 |               1.0000 | p                | :Person                                                        |
+----------------------+----------------+------+---------+-----------------+-------------------+----------------------+------------------+----------------------------------------------------------------+

Total database accesses: 59