Skip to content

Commit f90532c

Browse files
authored
PLAN-2443 - Include allReduce in the Pattern docs where QPPs are described (#1407)
1 parent f67ba8e commit f90532c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

modules/ROOT/pages/functions/predicate.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ RETURN all(i in emptyList WHERE true) as allTrue, all(i in emptyList WHERE false
110110
|===
111111
| *Syntax* 3+| `allReduce(accumulator = initial, stepVariable IN list \| reductionFunction, predicate)`
112112
| *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.
113-
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.
113+
If 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], its predicate is inlined where applicable.
114+
This inlining allows for early pruning of the search space by discarding paths as soon as the predicate is not satisfied.
115+
Note that `allReduce()` predicates are not inlined when used in a xref:patterns/shortest-paths.adoc[shortest path pattern], and therefore do not benefit from this pruning.
114116
.7+| *Arguments* | *Name* | *Type* | *Description*
115117
| `accumulator` | `ANY` | A variable that holds the result of the `reductionFunction` as the `list` is iterated.
116118
It is initialized with the value of `initial`.

modules/ROOT/pages/patterns/variable-length-patterns.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ For example, all relationships in the path must be of type `EMPLOYED_BY`.
380380
* Nodes or relationships must have properties satisfying some condition.
381381
For example, all relationships must have the property `distance > 10`.
382382

383+
* For every iteration of the quantified path pattern, an aggregated value over the constructed path so far must satisfy a predicate.
384+
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.
385+
See xref::functions/predicate.adoc#functions-allreduce[allReduce] for more information about this predicate.
386+
383387
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.
384388
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`:
385389

@@ -558,6 +562,26 @@ This query avoids having to find all possible paths and then imposing a `LIMIT 1
558562
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).
559563
Using inline predicates or making quantified path patterns more specific where possible can thus greatly improve query performance.
560564

565+
An alternative is to use an upper bound for the total distance, for example `6.05`.
566+
Once a path exceeds this upper bound, you can prune it and continue searching other paths.
567+
The xref::functions/predicate.adoc#functions-allreduce[allReduce] predicate function expresses this as follows:
568+
569+
.Query
570+
[source,cypher]
571+
----
572+
MATCH (bfr:Station {name: "London Blackfriars"}),
573+
(ndl:Station {name: "North Dulwich"})
574+
MATCH p = (bfr) ((a)-[l:LINK]-(b:Station))+ (ndl)
575+
WHERE allReduce(
576+
pathLength = 0,
577+
link IN l | pathLength + link.distance,
578+
pathLength < 6.05
579+
)
580+
RETURN reduce(acc = 0, r in relationships(p) | round(acc + r.distance, 2))
581+
AS distance
582+
ORDER BY distance LIMIT 1
583+
----
584+
561585
[[further-reading]]
562586
== Further reading
563587

0 commit comments

Comments
 (0)