Skip to content

Commit 1582ead

Browse files
strange
1 parent d6c403d commit 1582ead

File tree

1 file changed

+47
-61
lines changed

1 file changed

+47
-61
lines changed

modules/ROOT/pages/subqueries/call-subquery.adoc

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ CREATE (teamA:Team {name: 'Team A'}),
3131
(playerF:Player {name: 'Player F', age: 35}),
3232
(playerA)-[:PLAYS_FOR]->(teamA),
3333
(playerB)-[:PLAYS_FOR]->(teamA),
34-
(playerC)-[:PLAYS_FOR]->(teamA),
3534
(playerD)-[:PLAYS_FOR]->(teamB),
3635
(playerE)-[:PLAYS_FOR]->(teamC),
3736
(playerF)-[:PLAYS_FOR]->(teamC),
38-
(playerA)-[:FRIEND_OF]->(playerB),
39-
(playerA)-[:FRIEND_OF]->(playerC),
40-
(playerB)-[:FRIEND_OF]->(playerF),
41-
(playerC)-[:FRIEND_OF]->(playerD),
4237
(teamA)-[:OWES {dollars: 1500}]->(teamB),
4338
(teamA)-[:OWES {dollars: 3000}]->(teamB),
4439
(teamB)-[:OWES {dollars: 1700}]->(teamC),
@@ -93,7 +88,6 @@ CALL () {
9388
SET p.age = p.age + 1
9489
RETURN p.age AS newAge
9590
}
96-
WITH x, newAge
9791
MATCH (p:Player {name: 'Player A'})
9892
RETURN x AS iteration, newAge, p.age AS totalAge
9993
----
@@ -140,7 +134,7 @@ RETURN t AS team, players
140134
| players
141135
142136
| (:Team {name: "Team A"})
143-
| [(:Player {name: "Player C", age: 19}), (:Player {name: "Player B", age: 23}), (:Player {name: "Player A", age: 24})]
137+
| (:Player {name: "Player B", age: 23}), (:Player {name: "Player A", age: 24})]
144138
145139
| (:Team {name: "Team B"})
146140
| [(:Player {name: "Player D", age: 30})]
@@ -161,7 +155,6 @@ Using a `CALL` subquery can therefore reduce the amount of heap memory required
161155
Variables from the outer scope must be explicitly imported into the inner scope of the `CALL` subquery, either by using a xref:subqueries/call-subquery.adoc#variable-scope-clause[variable scope clause] or an xref:subqueries/call-subquery.adoc#importing-with[importing `WITH` clause] (deprecated).
162156
As the subquery is evaluated for each incoming input row, the imported variables are assigned the corresponding values from that row.
163157

164-
165158
[[variable-scope-clause]]
166159
=== The variable scope clause
167160

@@ -279,7 +272,7 @@ RETURN count(t) AS totalTeams, totalPlayers
279272

280273
[NOTE]
281274
=====
282-
As of Neo4j 5.23, it is deprecated to use `CALL` subqueries without a variable scope clause.
275+
`CALL` subqueries without a variable scope clause are deprecated.
283276
284277
.Deprecated
285278
[source, cypher]
@@ -423,7 +416,6 @@ RETURN largeLists
423416
424417
====
425418

426-
427419
[[optional-call]]
428420
== Optional subquery calls
429421

@@ -433,60 +425,60 @@ Similar to xref:clauses/optional-match.adoc[`OPTIONAL MATCH`] any empty rows pro
433425
.Difference between using `CALL` and `OPTIONAL CALL`
434426
====
435427
436-
This example, which finds the friends of each `Player` and xref:functions/aggregating.adoc#functions-count[counts] the number of friends per player, highlights the difference between using `CALL` and `OPTIONAL CALL`.
428+
This example, which finds the team that each `Player` plays for, highlights the difference between using `CALL` and `OPTIONAL CALL`.
437429
438430
.Regular subquery `CALL`
439431
[source, cypher]
440432
----
441433
MATCH (p:Player)
442434
CALL (p) {
443-
MATCH (p)-[:FRIEND_OF]->(friend:Player)
444-
RETURN friend
435+
MATCH (p)-[:PLAYS_FOR]->(team:Team)
436+
RETURN team
445437
}
446-
RETURN p.name AS playerName, count(friend) AS numberOfFriends
447-
ORDER BY numberOfFriends
438+
RETURN p.name AS playerName, team.name AS team
448439
----
449440
450-
.Optional subquery `CALL`
441+
.Result
451442
[role="queryresult",options="header,footer",cols="2*m"]
452443
|===
453-
| playerName | numberOfFriends
444+
| playerName | team
454445
455-
| "Player B" | 1
456-
| "Player C" | 1
457-
| "Player A" | 2
446+
| "Player A" | "Team A"
447+
| "Player B" | "Team A"
448+
| "Player D" | "Team B"
449+
| "Player E" | "Team C"
450+
| "Player F" | "Team C"
458451
459-
2+d|Rows: 3
452+
2+d|Rows: 5
460453
|===
461454
462-
Note that no results are returned for `Player D`, `Player E`, and `Player F`, since they have no outgoing `FRIEND_OF` relationships connected to them.
455+
Note that no results are returned for `Player C`, since they are not connected to any `Team` with a `PLAYS_FOR` relationship.
463456
464457
.Query using regular `OPTIONAL CALL`
465458
[source, cypher]
466459
----
467460
MATCH (p:Player)
468461
OPTIONAL CALL (p) {
469-
MATCH (p)-[:FRIEND_OF]->(friend:Player)
470-
RETURN friend
462+
MATCH (p)-[:PLAYS_FOR]->(team:Team)
463+
RETURN team
471464
}
472-
RETURN p.name AS playerName, count(friend) AS numberOfFriends
473-
ORDER BY numberOfFriends
465+
RETURN p.name AS playerName, team.name AS team
474466
----
475467
476-
Now, all `Player` nodes, regardless of whether they have any friends or not, are returned.
477-
(Those without any outgoing `FRIEND_OF` relationships are returned with the result `0` because `count()` ignores `null` values.)
468+
Now all `Player` nodes, regardless of whether they have any `PLAYS_FOR` relationships connected to a `Team`, are returned.
478469
470+
.Result
479471
.Result
480472
[role="queryresult",options="header,footer",cols="2*m"]
481473
|===
482-
| playerName | numberOfFriends
474+
| playerName | team
483475
484-
| "Player D" | 0
485-
| "Player E" | 0
486-
| "Player F" | 0
487-
| "Player B" | 1
488-
| "Player C" | 1
489-
| "Player A" | 2
476+
| "Player A" | "Team A"
477+
| "Player B" | "Team A"
478+
| "Player C" | NULL
479+
| "Player D" | "Team B"
480+
| "Player E" | "Team C"
481+
| "Player F" | "Team C"
490482
491483
2+d|Rows: 6
492484
|===
@@ -496,8 +488,8 @@ Now, all `Player` nodes, regardless of whether they have any friends or not, are
496488
[[call-execution-order]]
497489
== Execution order of CALL subqueries
498490

499-
The order in which subqueries are executed is not defined.
500-
If a query result depends on the order of execution of subqueries, an `ORDER BY` clause should precede the `CALL` clause.
491+
The order in which rows from the outer scope are passed into subqueries is not defined.
492+
If the results of the subquery depend on the order of these rows, use an `ORDER BY` clause before the `CALL` clause to guarantee a specific processing order for the rows.
501493

502494
.Ordering results before `CALL` subquery
503495
====
@@ -591,13 +583,13 @@ UNION
591583
ORDER BY p.age DESC
592584
LIMIT 1
593585
}
594-
RETURN p.name AS name, p.age AS age
586+
RETURN p.name AS playerName, p.age AS age
595587
----
596588
597589
.Result
598590
[role="queryresult",options="header,footer",cols="2*<m"]
599591
|===
600-
| name | age
592+
| playerName | age
601593
| "Player C" | 19
602594
| "Player F" | 35
603595
2+d|Rows: 2
@@ -642,39 +634,33 @@ The result of the `CALL` subquery is the combined result of evaluating the subqu
642634

643635
.`CALL` subquery changing returned rows of outer query
644636
====
645-
The following example finds the name of each `Player` and the names of their friends.
646-
No rows are returned for the `Player` nodes without any `FRIEND_OF` relationships, the number of results of the subquery thus changed the number of results of the enclosing query.
637+
The following example finds the name of each `Player` and the team they play for.
638+
No rows are returned for `Player C`, since they are not connected to a `Team` with a `PLAYS_FOR` relationship.
639+
The number of results of the subquery thus changed the number of results of the enclosing query.
647640
648641
.Find the friends of players
649642
[source, cypher]
650643
----
651644
MATCH (p:Player)
652645
CALL (p) {
653-
MATCH (p)-[:FRIEND_OF]->(p2:Player)
654-
RETURN p2.name AS friend
646+
MATCH (p)-[:PLAYS_FOR]->(team:Team)
647+
RETURN team.name AS team
655648
}
656-
RETURN p.name AS player, friend
649+
RETURN p.name AS playerName, team
657650
----
658651
659652
.Result
660-
[role="queryresult",options="header,footer",cols="2*<m"]
653+
[role="queryresult",options="header,footer",cols="2*m"]
661654
|===
662-
| player
663-
| friend
664-
665-
| "Player A"
666-
| "Player B"
667-
668-
| "Player A"
669-
| "Player C"
670-
671-
| "Player B"
672-
| "Player F"
655+
| playerName | team
673656
674-
| "Player C"
675-
| "Player D"
657+
| "Player A" | "Team A"
658+
| "Player B" | "Team A"
659+
| "Player D" | "Team B"
660+
| "Player E" | "Team C"
661+
| "Player F" | "Team C"
676662
677-
2+d|Rows: 4
663+
2+d|Rows: 5
678664
|===
679665
680666
====
@@ -772,7 +758,7 @@ Labels added: 18
772758

773759
* `CALL` subqueries optimize data handling and query efficiency, and can perform changes to the database.
774760

775-
* `CALL` subqueries enable progressive data transformation and can accumulate results across multiple row executions.
761+
* `CALL` subqueries allow for row-by-row data transformation and enable the accumulation of results across multiple rows, facilitating complex operations that depend on intermediate or aggregated data.
776762

777763
* `CALL` subqueries can only refer to variables from the enclosing query if they are explicitly imported by either a variable scope clause or an importing `WITH` clause (deprecated).
778764

@@ -782,4 +768,4 @@ Labels added: 18
782768

783769
* An `ORDER BY` clause can be used before `CALL` subqueries to ensure a specific order.
784770

785-
* `CALL` subqueries can be used in combination with `UNION` to process and aggregate different parts of a query result.
771+
* `CALL` subqueries can be used in combination with `UNION` to process and aggregate different parts of a query result.

0 commit comments

Comments
 (0)