Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion modules/ROOT/pages/functions/predicate.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it a bit longer and as a follow-up sentence, not between prentheses.

.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
25 changes: 25 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 QPP, an aggregated value over the constructed path so far should satisfy a predicate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.

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,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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid mentioning SHORTEST here, it muddies the water. We don't want to suggest the association of SHORTEST with allReduce().
Like in the previous example we say "This works fine where the solution is known to lie within some range of hops.", here I would say that I pick a known upper bound of 6.05 or maybe even 7 or 10 for the total distance. I wouldn't specify how I know it to be a reasonable upper bound.

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

Expand Down