From 0b860627a8420895873abea5e55c134977b97db3 Mon Sep 17 00:00:00 2001 From: Wilco Date: Thu, 23 Oct 2025 17:14:27 +0200 Subject: [PATCH 1/2] Include allReduce in the Pattern docs where QPPs are described --- modules/ROOT/pages/functions/predicate.adoc | 2 +- .../patterns/variable-length-patterns.adoc | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/ROOT/pages/functions/predicate.adoc b/modules/ROOT/pages/functions/predicate.adoc index 4a67747cf..fde2c9e65 100644 --- a/modules/ROOT/pages/functions/predicate.adoc +++ b/modules/ROOT/pages/functions/predicate.adoc @@ -110,7 +110,7 @@ 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`, 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. +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] (not within a xref:patterns/shortest-paths.adoc[shortest path pattern]), it allows for the early pruning of paths that do not satisfy the predicate. .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`. diff --git a/modules/ROOT/pages/patterns/variable-length-patterns.adoc b/modules/ROOT/pages/patterns/variable-length-patterns.adoc index 251dbdc2b..d4c019b98 100644 --- a/modules/ROOT/pages/patterns/variable-length-patterns.adoc +++ b/modules/ROOT/pages/patterns/variable-length-patterns.adoc @@ -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 QPP, 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`: @@ -558,6 +562,27 @@ 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 the information that we want to find paths with a total distance less than `6.05`. +From the `ALL SHORTEST` query, we know that there exists a path with a total distance of `6.04`. +Once the path exceeds this limit, 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 From ddb5a3b511c8298a0a961a3bd5f59d7feeb58755 Mon Sep 17 00:00:00 2001 From: Wilco Date: Tue, 28 Oct 2025 08:46:30 +0100 Subject: [PATCH 2/2] Apply PR feedback --- modules/ROOT/pages/functions/predicate.adoc | 4 +++- modules/ROOT/pages/patterns/variable-length-patterns.adoc | 7 +++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/ROOT/pages/functions/predicate.adoc b/modules/ROOT/pages/functions/predicate.adoc index fde2c9e65..55fae0ef2 100644 --- a/modules/ROOT/pages/functions/predicate.adoc +++ b/modules/ROOT/pages/functions/predicate.adoc @@ -110,7 +110,9 @@ 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`, 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] (not within a xref:patterns/shortest-paths.adoc[shortest path pattern]), it allows for the early pruning of paths that do not satisfy the predicate. +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`. diff --git a/modules/ROOT/pages/patterns/variable-length-patterns.adoc b/modules/ROOT/pages/patterns/variable-length-patterns.adoc index d4c019b98..f8864f9d2 100644 --- a/modules/ROOT/pages/patterns/variable-length-patterns.adoc +++ b/modules/ROOT/pages/patterns/variable-length-patterns.adoc @@ -380,7 +380,7 @@ 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 QPP, an aggregated value over the constructed path so far should satisfy a predicate. +* 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. @@ -562,9 +562,8 @@ 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 the information that we want to find paths with a total distance less than `6.05`. -From the `ALL SHORTEST` query, we know that there exists a path with a total distance of `6.04`. -Once the path exceeds this limit, we can prune it and continue searching other paths. +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