Help with Neo4j Cypher vs OpenCypher

53 views
Skip to first unread message

Michael Burbidge

unread,
Jan 26, 2023, 9:09:27 PM1/26/23
to openCypher
I'm not sure if there is a resource to ask OpenCypher questions, from a user's perspective. I'm trying to rewrite some Cypher queries that work with Neo4j in OpenCypher. Consider the following multipart query, which uses CALL { ... } statement liberally. This form of the CALL statement does not appear to be supported in OpenCypher. Is there a way to reform this query.

Ignore the apoc procedure used. I know that isn't supported.

UNWIND $batch as row
MERGE (e:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})
WITH e, row
CALL {
WITH row
WITH row WHERE row.has_parent=True AND row.parent <> ''
MATCH (a:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})
SET a.fract_index = row.fract_index
}
WITH e, row
CALL {
WITH row
WITH row WHERE row.has_parent=True AND row.parent = ''
MATCH (a:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})
REMOVE a.fract_index
}
WITH e, row
CALL apoc.create.addLabels(e, row.labels) YIELD node
WITH node as e, row
MERGE (e)<-[:ATTACHED_TO]-(c:Component {asset_id: row.asset_id, version: row.version, id: row.component_id})
ON MATCH
SET
c.value = row.value
ON CREATE
SET
c.value = row.value
WITH e as child, row
CALL {
WITH child, row
// conditional execution
WITH child, row WHERE row.has_parent=True AND row.parent <> ''
MERGE (e:Entity {asset_id: row.asset_id, version: row.version, id: row.parent})
MERGE (e)<-[:CHILD_OF]-(child)
}
WITH child, row
CALL {
WITH child, row
WITH child, row WHERE row.has_parent = True AND row.parent = ''
MATCH (child:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})-[r:CHILD_OF]->(:Entity)
DELETE r
}
WITH child, row
CALL {
WITH child, row
// conditional execution
WITH child, row WHERE row.referenced <> ''
MERGE (e:Entity {asset_id: row.asset_id, version: row.version, id: row.referenced})
MERGE (e)<-[:REFERS_TO]-(child)
}

Nathalie Charbel

unread,
Feb 3, 2023, 9:29:32 AM2/3/23
to Michael Burbidge, openCypher

Hi Michael,


You are right, the CALL statement isn’t yet supported in openCypher. The openCypher specification activities have recently slowed down in order to prioritize the work on the GQL standard but we expect these activities to resume shortly so that mature GQL features (such as CALL statement) can be incorporated into the openCypher specification (via CIPs).


However, looking at your query, it is possible to achieve the same purpose while avoiding the use of the CALL statement. For instance, you can simply use a linear composition of MERGE, MATCH, SET, … as follows:


UNWIND $batch as row

MERGE (e:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})

WITH row where row.has_parent=True and row.parent<>''

MATCH (e:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})

SET e.fract_index=row.fract_index

WITH row where row.has_parent=True and row.parent= ''

MATCH (e:Entity {asset_id: row.asset_id, version: row.version, id: row.entity_id})

REMOVE e.fract_index


I hope this was helpful.


Cheers,


Nathalie


--
You received this message because you are subscribed to the Google Groups "openCypher" group.
To unsubscribe from this group and stop receiving emails from it, send an email to opencypher+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/opencypher/cef5f0b3-53a7-423f-aeb4-51ecf703907en%40googlegroups.com.

Michael Hunger

unread,
Feb 3, 2023, 9:47:58 AM2/3/23
to Nathalie Charbel, Michael Burbidge, openCypher
Actually I don't think so.

Because false predicate expressions will yield 0 row cardinality so that stops the execution at that point for subsequent lines.

in cypher you can use 

// empty list for false, one element list for true
FOREACH ( _ in case when predicate then [true] else [] | UPDATE-OPERATIONS )

if you want to use subqueries, you actually have to return a pure aggregation to avoid shortcutting the execution

CALL { with x
// conditional
WITH * WHERE pred(x)
 -  UPDATE-AND-OTHER-CLAUSES
// pure aggregation to avoid 0 rows
RETURN count(*) as numX
...

Cheers, Michael

Neo4j Germany GmbH; Viktualienmarkt 8, 80331 München, DE; AG M HRB 252331; GF Mike Asher, Emil Eifrém


Michael Burbidge

unread,
Feb 3, 2023, 1:24:59 PM2/3/23
to michael...@neo4j.com, Nathalie Charbel, openCypher

Thanks for your responses!

Duane Nickull

unread,
Feb 3, 2023, 5:15:01 PM2/3/23
to Nathalie Charbel, Michael Burbidge, openCypher
Nathalie et al:

Do you have an approximate timeframe for the inclusion of the CALL statement in OC?  I am working on a Neo4J project for knowledge management and it is actually something that is of interest to me.

Duane



--
******************************
CTO Hired Gun - speaking only for myself
s. Bootstrap 5, jQuery, HTML5, CSS3+, PHP, Node.js, Neo4J & more
@duane...@hachyderm.io

NOTICE: This e-mail and any attachments may contain confidential information. If you are the intended recipient, please consider this a privileged communication, not to be forwarded without explicit approval from the sender.  If you are not the intended recipient, please notify the sender immediately by return e-mail, delete this e-mail and destroy any copies. Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal. The originator reserves the right to monitor all e-mail communications through its networks for quality control purposes.

Nathalie Charbel

unread,
Feb 6, 2023, 11:58:39 AM2/6/23
to Duane Nickull, openCypher
Hi Duane,

We have no timeline yet for that. You'll be notified as soon as we have additional information.

Thank you for your patience.

Nathalie

Duane Nickull

unread,
Feb 6, 2023, 6:20:50 PM2/6/23
to Nathalie Charbel, openCypher
Thank you.  Please let me know if I can be of any help. I worked on several specs/sandards for Web Services and SOA and am happy to roll up sleeves if there is a work plan.

Duane

Reply all
Reply to author
Forward
0 new messages