Skip to content

Commit cc7133a

Browse files
CALL subqueries for cheat sheet (includes ON ERROR RETRY) (#1226)
1 parent 9ce9818 commit cc7133a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
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"]
@@ -457,6 +463,7 @@ RETURN p.name AS playerName, team.name AS team
457463
Note that no results are returned for `Player C`, since they are not connected to any `Team` with a `PLAYS_FOR` relationship.
458464
459465
.Query using regular `OPTIONAL CALL`
466+
// tag::subqueries_call_subquery_optional_call[]
460467
[source, cypher]
461468
----
462469
MATCH (p:Player)
@@ -466,10 +473,10 @@ OPTIONAL CALL (p) {
466473
}
467474
RETURN p.name AS playerName, team.name AS team
468475
----
476+
// end::subqueries_call_subquery_optional_call[]
469477
470478
Now all `Player` nodes, regardless of whether they have any `PLAYS_FOR` relationships connected to a `Team`, are returned.
471479
472-
.Result
473480
.Result
474481
[role="queryresult",options="header,footer",cols="2*m"]
475482
|===
@@ -572,6 +579,7 @@ Call subqueries can be used to further process the results of a xref:clauses/uni
572579
This example query finds the youngest and the oldest `Player` in the graph.
573580
574581
.Find the oldest and youngest players
582+
// tag::subqueries_call_subquery_optional_union[]
575583
[source, cypher]
576584
----
577585
CALL () {
@@ -587,6 +595,7 @@ UNION
587595
}
588596
RETURN p.name AS playerName, p.age AS age
589597
----
598+
// end::subqueries_call_subquery_optional_union[]
590599
591600
.Result
592601
[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
@@ -47,13 +47,16 @@ It creates nodes in separate transactions using `CALL { ... } IN TRANSACTIONS`:
4747
----
4848

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

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

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

166171
.Result
167172
[role="queryresult",options="footer",cols="1*<m"]
@@ -372,6 +377,7 @@ RETURN e.num
372377
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:
373378
374379
.Transactions batched in 1 row with `ON ERROR CONTINUE`
380+
// tag::subqueries_in_transactions_error_behavior[]
375381
[source, cypher]
376382
----
377383
UNWIND [1, 0, 2, 4] AS i
@@ -383,6 +389,8 @@ CALL (i) {
383389
ON ERROR CONTINUE
384390
RETURN n.num
385391
----
392+
// end::subqueries_in_transactions_error_behavior[]
393+
386394
387395
.Result
388396
[role="queryresult",options="header,footer",cols="1*<m"]
@@ -791,6 +799,8 @@ If a negative number is specified (which can only be done through a parameter),
791799
`CALL { ... } IN CONCURRENT TRANSACTIONS` is particularly suitable for importing data without dependencies.
792800
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.
793801
802+
.`CALL` subquery run in `CONCURRENT TRANSACTIONS`
803+
// tag::subqueries_in_transactions_concurrent_transactions[]
794804
[source, cypher]
795805
----
796806
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/persons.csv' AS row
@@ -800,6 +810,7 @@ CALL (row) {
800810
} IN 3 CONCURRENT TRANSACTIONS OF 10 ROWS
801811
RETURN count(*) AS personNodes
802812
----
813+
// end::subqueries_in_transactions_concurrent_transactions[]
803814
804815
.Result
805816
[role="queryresult",options="header,footer",cols="m"]
@@ -939,6 +950,7 @@ To retry the any failed inner transactions, use the error option `ON ERROR RETRY
939950
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.
940951
941952
.Query using `ON ERROR RETRY ... THEN CONTINUE` to retry deadlocked inner transactions and complete outer transaction
953+
// tag::subqueries_in_transactions_deadlock_example[]
942954
[source, cypher]
943955
----
944956
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/movies.csv' AS row
@@ -949,6 +961,7 @@ CALL (row) {
949961
} IN 2 CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY FOR 3 SECONDS THEN CONTINUE REPORT STATUS AS status
950962
RETURN status.transactionID as transaction, status.committed AS successfulTransaction
951963
----
964+
// end::subqueries_in_transactions_deadlock_example[]
952965
953966
The result shows that all transactions are now successful:
954967

0 commit comments

Comments
 (0)