cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Syntax: Conditional Call Procedure

Jiropole
Graph Voyager

I feel like I'm being stupid, but I can't figure out how to conditionally call a (custom) procedure, and ensure a valid number is returned. Here's what I'm trying:

MATCH (a { uuid: "bad-uuid" })
RETURN CASE WHEN NOT a IS NULL 
    THEN CALL specialDelete(a) YIELD out 
    ELSE 0 END 
AS out

That's a syntax error immediately after the CALL, but I get how this is bending the intent of CASE. This also does not work:

MATCH (a { uuid: "bad-uuid" })
CALL specialDelete(a) YIELD out
RETURN CASE WHEN out IS NULL 
    THEN 0
    ELSE out END 
AS out

Here, the return set is empty. This use case is for a Neo4jRepository @Query for a deleteBy function, and it really doesn't like an empty result. (What I'd really like to do is throw a 404, which I could probably do at the service layer, if I can avoid the 500 caused by the above.)

I'm sure I'm missing something obvious – thanks!

1 ACCEPTED SOLUTION

For one, you want to use an OPTIONAL MATCH, as otherwise if the match fails there is nothing to operate on, so nothing will happen in the rest of the query.

You can use coalesce() to supply a default value for a potentially null expression:

OPTIONAL MATCH (a { uuid: "bad-uuid" })
CALL specialDelete(a) YIELD out
RETURN coalsece(out, 0) AS out

Keep in mind the match without a label will do an all node scan, this won't be very performant as your graph grows. You should add a label, and have an index via uuid.

View solution in original post

2 REPLIES 2

For one, you want to use an OPTIONAL MATCH, as otherwise if the match fails there is nothing to operate on, so nothing will happen in the rest of the query.

You can use coalesce() to supply a default value for a potentially null expression:

OPTIONAL MATCH (a { uuid: "bad-uuid" })
CALL specialDelete(a) YIELD out
RETURN coalsece(out, 0) AS out

Keep in mind the match without a label will do an all node scan, this won't be very performant as your graph grows. You should add a label, and have an index via uuid.

Oh man, thanks for the super-rapid response – good call on OPTIONAL, and I had totally forgotten about coalesce – That fixed'r!

Agreed on labels – that cypher was redacted for simplicity.