Neo4j CALL subquery with UNWIND returns 0 records

28 Views Asked by At

I have came across a strange behaviour when using UNWIND in CALL sub queries. The following query returns no records:

WITH [] as a, [1] as b, [1,2] as c
CALL {
    WITH a
    UNWIND a as row
    RETURN row as A
}
CALL {
    WITH b
    UNWIND b as row
    RETURN row as B
}
CALL {
    WITH c
    UNWIND c as row
    RETURN row as C
}
RETURN A, B, C

Result:

(no changes, no records)

I assume it's because of UNWIND-ing an empty list, reduces the number of rows to zero.

However, when I change the RETURN clause on the first sub query to collect(row), the query suddenly returns two records (because of list c with 2 entries):

WITH [] as a, [1] as b, [1,2] as c
CALL {
    WITH a
    UNWIND a as row
    RETURN collect(row) as A
}
CALL {
    WITH b
    UNWIND b as row
    RETURN row as B
}
CALL {
    WITH c
    UNWIND c as row
    RETURN row as C
}
RETURN A, B, C

Result:

╒═══╤═══╤═══╕
│A  │B  │C  │
╞═══╪═══╪═══╡
│[] │1  │1  │
├───┼───┼───┤
│[] │1  │2  │
└───┴───┴───┘

Why does collect() in the subquery alter the result of the query?

1

There are 1 best solutions below

1
Finbar Good On BEST ANSWER

Your diagnosis of the first result is correct. The second is a result of the behaviour of aggregating functions: collect() returns one row even when there are no incoming rows.

See this article for more information.