Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/ROOT/pages/functions/predicate.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ RETURN all(i in emptyList WHERE true) as allTrue, all(i in emptyList WHERE false
| *Syntax* 3+| `allReduce(accumulator = initial, stepVariable IN list \| reductionFunction, predicate)`
| *Description* 3+| Returns true if, during the stepwise evaluation of a value across the elements in a given `LIST<ANY>`, the accumulated result satisfies a specified predicate at every step.
Where that list is a xref:patterns/variable-length-patterns.adoc#group-variables[group variable] defined in a xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path pattern], it allows for the early pruning of paths that do not satisfy the predicate.

*Note*: AllReduce should not be used for a group variable within a xref:patterns/shortest-paths.adoc[shortest path pattern].
.7+| *Arguments* | *Name* | *Type* | *Description*
| `accumulator` | `ANY` | A variable that holds the result of the `reductionFunction` as the `list` is iterated.
It is initialized with the value of `initial`.
Expand Down
24 changes: 24 additions & 0 deletions modules/ROOT/pages/patterns/variable-length-patterns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ For example, all relationships in the path must be of type `EMPLOYED_BY`.
* Nodes or relationships must have properties satisfying some condition.
For example, all relationships must have the property `distance > 10`.

* For every iteration of the quantified path pattern, an aggregated value over the constructed path so far should satisfy a predicate.
For example, the sum of the property `distance` of the relationships in the path must be less than 50 for every step in the construction of the path.
See xref::functions/predicate.adoc#functions-allreduce[allReduce] for more information about this predicate.

To demonstrate the utility of predicates in quantified path patterns, this section considers an example of finding the shortest path by physical distance and compares that to the results yielded by using the xref:patterns/shortest-paths.adoc[`SHORTEST`] keyword.
The graph in this example continues with `Station` nodes, but adds both a geospatial `location` property to the `Stations`, as well as `LINK` relationships with a `distance` property representing the distance between pairs of `Stations`:

Expand Down Expand Up @@ -558,6 +562,26 @@ This query avoids having to find all possible paths and then imposing a `LIMIT 1
It also shows that there is only one path to solving the query (a number that remains constant even if the data from the rest of the UK railway network was included).
Using inline predicates or making quantified path patterns more specific where possible can thus greatly improve query performance.

An alternative is to use an upper bound for the total distance, for example `6.05`.
Once a path exceeds this upper bound, we can prune it and continue searching other paths.
The xref::functions/predicate.adoc#functions-allreduce[allReduce] predicate function expresses this as follows:

.Query
[source,cypher]
----
MATCH (bfr:Station {name: "London Blackfriars"}),
(ndl:Station {name: "North Dulwich"})
MATCH p = (bfr) ((a)-[l:LINK]-(b:Station))+ (ndl)
WHERE allReduce(
pathLength = 0,
link IN l | pathLength + link.distance,
pathLength < 6.05
)
RETURN reduce(acc = 0, r in relationships(p) | round(acc + r.distance, 2))
AS distance
ORDER BY distance LIMIT 1
----

[[further-reading]]
== Further reading

Expand Down