Python Driver calling APOC with parameters

While calling following query with param placeholder, it just ignores and does nothing (no exception) but harcoding values inside the query works fine.

Query (Parameterized):
"""
CALL apoc.periodic.iterate(
"MATCH (:Book)-[c:CONTAINS]->(:Instrument) RETURN c",
"SET c.eodMarkers = c.eodMarkers + date({eodDate}), c.states=c.states + {state}",
{batchSize: 20000}
)
"""
Called as tx.run(query, eodDate=someDate, state=someState)

But the following works: (values hardcoded inside query)

        """
            CALL apoc.periodic.iterate(
                "MATCH (:Book)-[c:CONTAINS]->(:Instrument)  RETURN c",
                "SET c.eodMarkers = c.eodMarkers + date('2019-06-13'), c.states=c.states + 'ACTIVE'",
                {batchSize: 20000}
            )
        """

Any idea why?

The scope used within the queries in iterate() is separate from the scope of the outermost query, and that includes query parameters.

You'll need to pass the parameters to use via the params config property entry:

CALL apoc.periodic.iterate(
"MATCH (:Book)-[c:CONTAINS]->(:Instrument) RETURN c",
"SET c.eodMarkers = c.eodMarkers + date({eodDate}), c.states=c.states + {state}",
{batchSize: 20000, params:{eodDate:{eodDate}, state:{state}} }
)

We'd also encourage you to use the $state style of parameter usage rather than {state}.

2 Likes

Thanks @andrew_bowman , my code as follows

delete_clause = """
CALL apoc.periodic.iterate(
    "MATCH (c:City{name:$city})-[:BELONGTO]-(d:District)-[:LOCATE_AT]-(s:Shop) RETURN s",
    "DETACH DELETE s",
    {batchSize:10000, parallel:false, params:{city:$city}}
)
"""
tx.run(delete_clause, city=city)

Please note the city parameter.