Working with null
In Cypher®, null is used to represent missing or undefined values.
All data types in Cypher are nullable.
This means that type predicate expressions always return true for null values.
Conceptually, null means a missing or unknown value, and it is treated somewhat differently from other values.
For example, returning a property from a node that does not have said property produces null.
Most expressions that take null as input will produce null.
In the case of a predicate used in a WHERE clause, anything that is not true is interpreted as being false.
null is not equal to null.
Not knowing two values does not imply that they are the same value.
This means that the expression null = null yields null, and not true.
Logical operations with null
The boolean operators (AND, OR, XOR, NOT) treat null as the unknown value of three-valued logic.
| a | b | a AND b |
a OR b |
a XOR b |
NOT a |
|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The IN operator and null
The IN operator follows similar logic.
If Cypher can ascertain that something exists in a list, the result will be true.
Any list that contains a null and does not have a matching element will return null.
Otherwise, the result will be false.
| Expression | Result |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using all, any, none, and single follows a similar rule.
If the result can be calculated definitively, true or false is returned.
Otherwise null is produced.
The [] operator and null
Accessing a list or a map with null will result in null:
| Expression | Result |
|---|---|
|
|
|
|
|
|
|
|
Using parameters to pass in the bounds, such as a[$lower..$upper], may result in a null for the lower or upper bound (or both).
The following workaround will prevent this from happening by setting the absolute minimum and maximum bound values:
a[coalesce($lower,0)..coalesce($upper,size(a))]
Expressions that return null
-
Getting a missing element from a list:
[][0],head([]). -
Trying to access a property that does not exist on a node or relationship:
n.missingProperty. -
Comparisons when either side is
null:1 < null. -
Arithmetic expressions containing
null:1 + null. -
Some function calls where any argument is
null: e.g.,sin(null).
Using IS NULL and IS NOT NULL
Testing any value against null, either with the = operator or with the <> operator, always evaluates to null.
Therefore, use the special equality operators IS NULL or IS NOT NULL.