Cypher on NetworkXGraph

cvcdocdb.nx_cypher is the engine behind NetworkXGraph.query(cypher) for read-only queries: it parses the query and evaluates it against the in-memory graph with Neo4j semantics, so the same Cypher gives the same results on NetworkXGraph and Neo4jGraph. Syntax outside the supported subset raises ValueError instead of returning wrong results. Write queries (CREATE, MERGE, SET, DELETE) are handled separately by NetworkXGraph.

Read-only Cypher engine for NetworkXGraph.

NetworkXGraph.query(cypher) routes every read-only query here (write queries keep using the older engine in networkx_graph). Instead of matching the query text with regular expressions, it is tokenized, parsed into a small AST and evaluated against the graph, following Neo4j’s semantics for the supported subset:

  • Clauses: MATCH, OPTIONAL MATCH, WHERE, WITH, RETURN (DISTINCT, *), ORDER BY (ASC/DESC), SKIP, LIMIT.

  • Patterns: (v:Label1:Label2 {key: expr}), anonymous nodes, chains of relationships -[r:T1|T2 {key: expr}]->, <-[...]-, -[...]-, several comma-separated patterns. Main and alternative labels both match. A relationship is not matched twice within one MATCH (as in Neo4j).

  • Expressions: literals (numbers, strings, booleans, null, lists, maps), $params (bound as values, never substituted into the text), property access, AND/OR/XOR/NOT with three-valued null logic, =, <>, <, <=, >, >=, IS [NOT] NULL, IN, STARTS WITH, ENDS WITH, CONTAINS, =~, label predicates (n:Label), arithmetic, and the functions in _SCALAR_FUNCTIONS.

  • Aggregations: count (incl. count(*) and DISTINCT), sum, avg, min, max, collect, with implicit grouping by the non-aggregated items.

Anything outside this subset (variable-length paths, path variables, UNWIND, UNION, CASE, subqueries, unknown functions…) raises NxCypherError (a ValueError) instead of returning wrong results.

Returned values have the same shape as before: nodes as {"labels": [...], "properties": {...}} and relationships as {"type", "start_node", "end_node", "properties"}.

exception cvcdocdb.nx_cypher.NxCypherError[source]

Invalid or unsupported Cypher for the NetworkX engine.

cvcdocdb.nx_cypher.execute_read(graph, cypher, params=None)[source]

Run a read-only Cypher query on a NetworkXGraph.

Raises:

NxCypherError – The query is invalid, uses unsupported syntax, or references a missing parameter.

Parameters:
Return type:

List[Dict[str, Any]]

cvcdocdb.nx_cypher.is_write_query(cypher)[source]

Whether cypher contains a clause that modifies the graph.

Parameters:

cypher (str)

Return type:

bool