Return query result values in apoc.periodic.iterate

Is there a way to return a query result value while using a apoc.periodic.iterate statement?

call apoc.periodic.iterate ('

UNWIND $input as input

WITH input

MATCH (b:Node1) 
WHERE b.prop1 = input.prop1
AND substring(b.prop2,0,10) = "2021-09-08"

WITH input, b
MATCH (c:Node2 {prop3:b.prop3})

RETURN 
b.prop4 as prop4,
b.prop3 as prop3,
c.prop2 as prop2,
input

','

WITH input, prop4, prop3, prop2
MERGE (a:NodeA {prop1:prop4})

SET a.prop2 = "ABC"
SET a.prop3 = prop3
SET a.prop4 = prop2
SET a.prop5 = input.prop1
SET a.prop6 = input.prop2
SET a.date = "2021-09-09"

FOREACH (
_ IN CASE WHEN input.prop10 = "low" THEN [1] END |
SET a:NodeB
)
FOREACH (
_ IN CASE WHEN input.prop10 <> "low" THEN [1] END |
SET a:NodeC
)
FOREACH (
_ IN CASE WHEN input.error IS NOT NULL OR input.errorCode <> 200 OR input IS NULL THEN [1] END |
SET a:NodeD
SET a.errorCode = input.errorCode
SET a.errorDesc = input.error
)
RETURN input.prop1 as prop1,
input.prop2 as prop2
',
{batchSize:100, parallel:true, params:{input:$input}}
)

Would it be possible to get input.prop1 and input.prop2 as a result of the apoc.periodic.iterate statement?

No, it's not currently possible, the procedure was designed for batch processing only, return values aren't handled.

Thank you @andrew_bowman

Hi there,
Since it is a fair functionality of collecting the query result, I would like to know whether " [Return query result values in apoc.periodic.iterate]" becomes possible in 2025? My Neo4j Browser version is 5.21.0, and Server version is 5.21.2. Thanks.

I don’t see anything in the documentation about a return value

You can use cypher’s CALL subquery IN TRANSACTIONS clause to batch. It allows return values.

2 Likes

Thanks @glilienfield.

It seems to me that calling subqueries in transactions could be a workaround for collecting results from apoc.periodic.iterate()

After giving it a second thought, I realize that it might not be a 'good idea' to collect return values from an iterating task. apoc.periodic.iterate() should instead be considered a tool for efficiently modifying the graph structure.

Surprisingly, the same demo case for calling subqueries given on official Neo4j does not work and instead shows a syntax error message.

Passing parameters to a call subquery is a relatively new feature. You must be using a version before the update.

Remove the parenthesis and pass the value of ‘i’ using a ‘with’ clause.

I agree. If you have so many updates that you need to batch with transactions, you probably have too much data to return, especially to collect. You may consume a lot of memory.