Neo4j: How do null values even work?
Every now and then I find myself wanting to import a CSV file into Neo4j and I always get confused with how to handle the various null values that can lurk within.
Letâs start with an example that doesnât have a CSV file in sight. Consider the following list and my attempt to only return null values:
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value WITH value WHERE value = null RETURN value  (no changes, no records)
WITH [null, “null”, “”, “Mark”] AS values UNWIND values AS value WITH value WHERE value = null RETURN value (no changes, no records)
Hmm thatâs weird. Iâd have expected that at least keep the first value in the collection. What about if we do the inverse?
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value WITH value WHERE value <> null RETURN value  (no changes, no records)
WITH [null, “null”, “”, “Mark”] AS values UNWIND values AS value WITH value WHERE value <> null RETURN value (no changes, no records)
Still nothing! Letâs try returning the output of our comparisons rather than filtering rows:
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value RETURN value = null AS outcome  âââââââââĪââââââââââ â"value"â"outcome"â âââââââââŠâââââââââ⥠ânull ânull â âââââââââžââââââââââĪ â"null" ânull â âââââââââžââââââââââĪ â"" ânull â âââââââââžââââââââââĪ â"Mark" ânull â âââââââââīââââââââââ
Ok so that isnât what we expected. Everything has an âoutcomeâ of ânullâ! What about if we want to check whether the value is the string âMarkâ?
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value RETURN value = "Mark" AS outcome  âââââââââĪââââââââââ â"value"â"outcome"â âââââââââŠâââââââââ⥠ânull ânull â âââââââââžââââââââââĪ â"null" âfalse â âââââââââžââââââââââĪ â"" âfalse â âââââââââžââââââââââĪ â"Mark" âtrue â âââââââââīââââââââââ
From executing this query we learn that if one side of a comparison is null then the return value is always going to be null.
So how do we exclude a row if itâs null?
It turns out we have to use the âisâ keyword rather than using the equality operator. Letâs see what that looks like:
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value WITH value WHERE value is null RETURN value  âââââââââ â"value"â ââââââââ⥠ânull â âââââââââ
And the positive case:
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value WITH value WHERE value is not null RETURN value  âââââââââ â"value"â ââââââââ⥠â"null" â âââââââââĪ â"" â âââââââââĪ â"Mark" â âââââââââ
What if we want to get rid of empty strings?
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value WITH value WHERE value <> "" RETURN value  âââââââââ â"value"â ââââââââ⥠â"null" â âââââââââĪ â"Mark" â âââââââââ
Interestingly that also gets rid of the null value which I hadnât expected. But if we look for values matching the empty string:
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value WITH value WHERE value = "" RETURN value  âââââââââ â"value"â ââââââââ⥠â"" â âââââââââ
Itâs not there either! Hmm whatâs going on here:
WITH [null, "null", "", "Mark"] AS values UNWIND values AS value RETURN value, value = "" AS isEmpty, value <> "" AS isNotEmpty  âââââââââĪââââââââââĪâââââââââââââ â"value"â"isEmpty"â"isNotEmpty"â âââââââââŠââââââââââŠââââââââââââ⥠ânull ânull ânull â âââââââââžââââââââââžâââââââââââââĪ â"null" âfalse âtrue â âââââââââžââââââââââžâââââââââââââĪ â"" âtrue âfalse â âââââââââžââââââââââžâââââââââââââĪ â"Mark" âfalse âtrue â âââââââââīââââââââââīâââââââââââââ
null values seem to get filtered out for every type of equality match unless we explicitly check that a value âis nullâ.
So how do we use this knowledge when weâre parsing CSV files using Neo4jâs LOAD CSV tool?
Letâs say we have a CSV file that looks like this:
$ cat nulls.csv name,company "Mark", "Michael","" "Will",null "Ryan","Neo4j"
$ cat nulls.csv name,company “Mark”, “Michael”,”” “Will”,null “Ryan”,”Neo4j”
So none of the first three rows have a value for âcompanyâ. I donât have any value at all, Michael has an empty string, and Will has a null value. Letâs see how LOAD CSV interprets this:
load csv with headers from "file:///nulls.csv" AS row
RETURN row
Â
âââââââââââââââââââââââââââââââââââ
â"row" â
âââââââââââââââââââââââââââââââââââĄ
â{"name":"Mark","company":null} â
âââââââââââââââââââââââââââââââââââĪ
â{"name":"Michael","company":""} â
âââââââââââââââââââââââââââââââââââĪ
â{"name":"Will","company":"null"} â
âââââââââââââââââââââââââââââââââââĪ
â{"name":"Ryan","company":"Neo4j"}â
âââââââââââââââââââââââââââââââââââWeâve got the full sweep of all the combinations from above. Weâd like to create a Person node for each row but only create a Company node and associated âWORKS_FORâ relationshp if an actual company is defined â we donât want to create a null company.
So we only want to create a company node and âWORKS_FORâ relationship for the Ryan row.
The following query does the trick:
load csv with headers from "file:///nulls.csv" AS row
MERGE (p:Person {name: row.name})
WITH p, row
WHERE row.company <> "" AND row.company <> "null"
MERGE (c:Company {name: row.company})
MERGE (p)-[:WORKS_FOR]->(c)
Â
Added 5 labels, created 5 nodes, set 5 properties, created 1 relationship, statement completed in 117 ms.load csv with headers from “file:///nulls.csv” AS row MERGE (p:Person {name: row.name}) WITH p, row WHERE row.company <> “” AND row.company <> “null” MERGE (c:Company {name: row.company}) MERGE (p)-[:WORKS_FOR]->(c) Added 5 labels, created 5 nodes, set 5 properties, created 1 relationship, statement completed in 117 ms.
And if we visualise whatâs been created:
Perfect. Perhaps this behaviour is obvious but it always trips me up so hopefully itâll be useful to someone else as well!
| Reference: | Neo4j: How do null values even work? from our JCG partner Mark Needham at the Mark Needham Blog blog. |





