Skip to content

Commit b760a28

Browse files
Improve WITH shadowing examples to clarify behavior (#591)
I have slightly changed the wording as a result of #542, and also expanded the first two relevant examples so that the error you would get is shown (as was already the case in the section `COLLECT subquery with WITH`).
1 parent 8de64d0 commit b760a28

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

modules/ROOT/pages/syntax/expressions.adoc

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ RETURN person.name AS name, EXISTS {
518518
[[existential-subquery-with-union]]
519519
==== `EXISTS` subquery with a `UNION`
520520

521-
_This feature was introduced in Neo4j 5.3._
521+
_This feature was introduced in Neo4j 5.3._
522522

523523
`Exists` can be used with a `UNION` clause, and the `RETURN` clauses are not required.
524524
It is worth noting that if one branch has a `RETURN` clause, then all branches require one.
@@ -551,12 +551,34 @@ RETURN
551551
[[existential-subquery-with-with]]
552552
==== `EXISTS` subquery with `WITH`
553553

554-
_This feature was introduced in Neo4j 5.3._
554+
_This feature was introduced in Neo4j 5.3._
555555

556556
Variables from the outside scope are visible for the entire subquery, even when using a `WITH` clause.
557-
This means that shadowing of these variables is not allowed.
557+
To avoid confusion, shadowing of these variables is not allowed.
558558
An outside scope variable is shadowed when a newly introduced variable within the inner scope is defined with the same variable.
559-
In the below example, a `WITH` clause introduces a new variable.
559+
In the example below, the outer variable `name` is shadowed and will therefore throw an error.
560+
561+
.Query
562+
[source, cypher, role=test-fail]
563+
----
564+
WITH 'Peter' as name
565+
MATCH (person:Person {name: name})
566+
WHERE EXISTS {
567+
WITH "Ozzy" AS name
568+
MATCH (person)-[:HAS_DOG]->(d:Dog)
569+
WHERE d.name = name
570+
}
571+
RETURN person.name AS name
572+
----
573+
574+
.Error message
575+
[source, output, role="noheader"]
576+
----
577+
The variable `name` is shadowing a variable with the same name from the outer scope and needs to be renamed (line 4, column 20 (offset: 90))
578+
----
579+
580+
New variables can be introduced into the subquery, as long as they use a different identifier.
581+
In the example below, a `WITH` clause introduces a new variable.
560582
Note that the outer scope variable `person` referenced in the main query is still available after the `WITH` clause.
561583

562584
.Query
@@ -583,7 +605,7 @@ RETURN person.name AS name
583605
[[existential-subquery-with-return]]
584606
==== `EXISTS` subquery with `RETURN`
585607

586-
_This feature was introduced in Neo4j 5.3._
608+
_This feature was introduced in Neo4j 5.3._
587609

588610
`EXISTS` subqueries do not require a `RETURN` clause at the end of the subquery. If one is present, it does not
589611
need to be aliased, which is different compared to xref::clauses/call-subquery.adoc[`CALL` subqueries].
@@ -713,9 +735,31 @@ RETURN
713735
_This feature was introduced in Neo4j 5.3._
714736

715737
Variables from the outside scope are visible for the entire subquery, even when using a `WITH` clause.
716-
This means that shadowing of these variables is not allowed.
738+
To avoid confusion, shadowing of these variables is not allowed.
717739
An outside scope variable is shadowed when a newly introduced variable within the inner scope is defined with the same variable.
718-
In the below example, a `WITH` clause introduces a new variable.
740+
In the example below, the outer variable `name` is shadowed and will therefore throw an error.
741+
742+
.Query
743+
[source, cypher, role=test-fail]
744+
----
745+
WITH 'Peter' as name
746+
MATCH (person:Person {name: name})
747+
WHERE COUNT {
748+
WITH "Ozzy" AS name
749+
MATCH (person)-[:HAS_DOG]->(d:Dog)
750+
WHERE d.name = name
751+
} = 1
752+
RETURN person.name AS name
753+
----
754+
755+
.Error message
756+
[source, output, role="noheader"]
757+
----
758+
The variable `name` is shadowing a variable with the same name from the outer scope and needs to be renamed (line 4, column 20 (offset: 90))
759+
----
760+
761+
New variables can be introduced into the subquery, as long as they use a different identifier.
762+
In the example below, a `WITH` clause introduces a new variable.
719763
Note that the outer scope variable `person` referenced in the main query is still available after the `WITH` clause.
720764

721765
.Query
@@ -974,9 +1018,9 @@ RETURN
9741018
==== `COLLECT` subquery with `WITH`
9751019

9761020
Variables from the outside scope are visible for the entire subquery, even when using a `WITH` clause.
977-
This means that shadowing of these variables is not allowed.
1021+
To avoid confusion, shadowing of these variables is not allowed.
9781022
An outside scope variable is shadowed when a newly introduced variable within the inner scope is defined with the same variable.
979-
In the below example, the outer variable `name` is shadowed and will therefore throw an error.
1023+
In the example below, the outer variable `name` is shadowed and will therefore throw an error.
9801024

9811025
.Query
9821026
[source, cypher, role=test-fail]
@@ -997,7 +1041,7 @@ The variable `name` is shadowing a variable with the same name from the outer sc
9971041
----
9981042

9991043
New variables can be introduced into the subquery, as long as they use a different identifier.
1000-
In the below example, a `WITH` clause introduces a new variable.
1044+
In the example below, a `WITH` clause introduces a new variable.
10011045
Note that the outer scope variable `person` referenced in the main query is still available after the `WITH` clause.
10021046

10031047
.Query

0 commit comments

Comments
 (0)