Skip to content

Commit 86c25c0

Browse files
CALL subqueries for cheat sheet (includes ON ERROR RETRY) (neo4j#1226)
1 parent 002170d commit 86c25c0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The variables returned in a subquery are available to the outer scope of the enc
5050
In this example, the `CALL` subquery executes three times, one for each row that the xref:clauses/unwind.adoc[`UNWIND`] clause outputs.
5151
5252
.Query
53+
// tag::subqueries_call_subquery_basic_example[]
5354
[source, cypher]
5455
----
5556
UNWIND [0, 1, 2] AS x
@@ -58,6 +59,8 @@ CALL () {
5859
}
5960
RETURN innerReturn
6061
----
62+
// end::subqueries_call_subquery_basic_example[]
63+
6164
6265
.Result
6366
[role="queryresult",options="header,footer",cols="m"]
@@ -117,6 +120,7 @@ As a result, `CALL` subqueries can help maintain optimal performance and scalabi
117120
In this example, a `CALL` subquery is used to xref:functions/aggregating.adoc#functions-collect[`collect`] a `LIST` containing all players who play for a particular team.
118121
119122
.Collect a list of all players playing for a particular team
123+
// tag::subqueries_call_subquery_variable_scope[]
120124
[source, cypher]
121125
----
122126
MATCH (t:Team)
@@ -126,6 +130,8 @@ CALL (t) {
126130
}
127131
RETURN t AS team, players
128132
----
133+
// end::subqueries_call_subquery_variable_scope[]
134+
129135
130136
.Result
131137
[source, role="queryresult",options="header,footer",cols="m,2m"]
@@ -455,6 +461,7 @@ RETURN p.name AS playerName, team.name AS team
455461
Note that no results are returned for `Player C`, since they are not connected to any `Team` with a `PLAYS_FOR` relationship.
456462
457463
.Query using regular `OPTIONAL CALL`
464+
// tag::subqueries_call_subquery_optional_call[]
458465
[source, cypher]
459466
----
460467
MATCH (p:Player)
@@ -464,6 +471,7 @@ OPTIONAL CALL (p) {
464471
}
465472
RETURN p.name AS playerName, team.name AS team
466473
----
474+
// end::subqueries_call_subquery_optional_call[]
467475
468476
Now all `Player` nodes, regardless of whether they have any `PLAYS_FOR` relationships connected to a `Team`, are returned.
469477
@@ -617,6 +625,7 @@ Call subqueries can be used to further process the results of a xref:queries/com
617625
This example query finds the youngest and the oldest `Player` in the graph.
618626
619627
.Find the oldest and youngest players
628+
// tag::subqueries_call_subquery_optional_union[]
620629
[source, cypher]
621630
----
622631
CALL () {
@@ -632,6 +641,7 @@ UNION
632641
}
633642
RETURN p.name AS playerName, p.age AS age
634643
----
644+
// end::subqueries_call_subquery_optional_union[]
635645
636646
.Result
637647
[role="queryresult",options="header,footer",cols="2*<m"]

modules/ROOT/pages/subqueries/subqueries-in-transactions.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ It creates nodes in separate transactions using `CALL { ... } IN TRANSACTIONS`:
4646
----
4747

4848
.Query
49+
// tag::subqueries_in_transactions_basic_example[]
4950
[source, cypher]
5051
----
5152
LOAD CSV FROM 'file:///friends.csv' AS line
5253
CALL (line) {
5354
CREATE (:Person {name: line[1], age: toInteger(line[2])})
5455
} IN TRANSACTIONS
5556
----
57+
// end::subqueries_in_transactions_basic_example[]
58+
5659

5760
.Result
5861
[role="queryresult",options="footer",cols="1*<m"]
@@ -154,13 +157,15 @@ This example loads a CSV file with one transaction for every `2` input rows:
154157
----
155158

156159
.Query
160+
// tag::subqueries_in_transactions_batching[]
157161
[source, cypher]
158162
----
159163
LOAD CSV FROM 'file:///friends.csv' AS line
160164
CALL (line) {
161165
CREATE (:Person {name: line[1], age: toInteger(line[2])})
162166
} IN TRANSACTIONS OF 2 ROWS
163167
----
168+
// end::subqueries_in_transactions_batching[]
164169

165170
.Result
166171
[role="queryresult",options="footer",cols="1*<m"]
@@ -370,6 +375,7 @@ RETURN e.num
370375
In the following example, `ON ERROR CONTINUE` is used after a failed inner transaction to execute the remaining inner transactions and not fail the outer transaction:
371376
372377
.Transactions batched in 1 row with `ON ERROR CONTINUE`
378+
// tag::subqueries_in_transactions_error_behavior[]
373379
[source, cypher]
374380
----
375381
UNWIND [1, 0, 2, 4] AS i
@@ -381,6 +387,8 @@ CALL (i) {
381387
ON ERROR CONTINUE
382388
RETURN n.num
383389
----
390+
// end::subqueries_in_transactions_error_behavior[]
391+
384392
385393
.Result
386394
[role="queryresult",options="header,footer",cols="1*<m"]
@@ -788,6 +796,8 @@ If a negative number is specified (which can only be done through a parameter),
788796
`CALL { ... } IN CONCURRENT TRANSACTIONS` is particularly suitable for importing data without dependencies.
789797
This example creates `Person` nodes from a unique `tmdbId` value assigned to each person row in the CSV file (444 in total) in 3 concurrent transactions.
790798
799+
.`CALL` subquery run in `CONCURRENT TRANSACTIONS`
800+
// tag::subqueries_in_transactions_concurrent_transactions[]
791801
[source, cypher]
792802
----
793803
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/persons.csv' AS row
@@ -797,6 +807,7 @@ CALL (row) {
797807
} IN 3 CONCURRENT TRANSACTIONS OF 10 ROWS
798808
RETURN count(*) AS personNodes
799809
----
810+
// end::subqueries_in_transactions_concurrent_transactions[]
800811
801812
.Result
802813
[role="queryresult",options="header,footer",cols="m"]
@@ -936,6 +947,7 @@ To retry the any failed inner transactions, use the error option `ON ERROR RETRY
936947
The following query uses `ON ERROR RETRY ... THEN CONTINUE` to retry the above query for a maximum of `3` seconds and then continue the execution of subsequent inner transactions by ignoring any recoverable errors.
937948
938949
.Query using `ON ERROR RETRY ... THEN CONTINUE` to retry deadlocked inner transactions and complete outer transaction
950+
// tag::subqueries_in_transactions_deadlock_example[]
939951
[source, cypher]
940952
----
941953
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/movies.csv' AS row
@@ -946,6 +958,7 @@ CALL (row) {
946958
} IN 2 CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY FOR 3 SECONDS THEN CONTINUE REPORT STATUS AS status
947959
RETURN status.transactionID as transaction, status.committed AS successfulTransaction
948960
----
961+
// end::subqueries_in_transactions_deadlock_example[]
949962
950963
The result shows that all transactions are now successful:
951964

0 commit comments

Comments
 (0)