Cypher reference¶
lance-csr implements a read-only subset of openCypher rich enough to run the
GDC SNB Interactive complex queries (IC1βIC14), including
OPTIONAL MATCH, EXISTS, variable-length trails, named path values, shortestPath,
comprehensions and reduce.
This reference applies equally across Python, Rust and
TypeScript. For engine setup, parameters, streaming, deadlines and explain(), see
Running queries.
A query may end with one optional semicolon; each execution call accepts a single statement. The runnable examples below use this graph:
import pyarrow as pa
from lance_csr import CypherEngine, GraphConfig
config = (
GraphConfig.builder()
.node_table("Person", "id")
.node_table("City", "id")
.rel_table("KNOWS", "Person", "Person", "src", "dst")
.rel_table("LIVES_IN", "Person", "City", "src", "dst")
.build()
)
datasets = {
"Person": pa.table({
"id": [1, 2, 3, 4],
"name": ["Alice", "Bob", "Carol", "David"],
"age": [28, 34, 29, 42],
}),
"City": pa.table({"id": [10, 20], "name": ["London", "Sydney"]}),
"KNOWS": pa.table({"src": [1, 1, 2, 3], "dst": [2, 3, 4, 4]}),
"LIVES_IN": pa.table({"src": [1, 2, 3, 4], "dst": [10, 10, 20, 20]}),
}
engine = CypherEngine(config, datasets, use_csr=True)
Clauses¶
| Clause | Notes |
|---|---|
MATCH |
Comma-separated patterns are supported. |
OPTIONAL MATCH |
Must share a variable with an earlier clause; cannot be the first clause. |
WHERE |
Follows MATCH, OPTIONAL MATCH (filters inside the optional part) or WITH. |
WITH [DISTINCT] |
Supports ORDER BY, LIMIT and WHERE; chain as many as needed. WITH SKIP is unsupported. |
UNWIND list AS x |
Expands a list into one row per element. |
UNION, UNION ALL |
Combines independent queries with matching column names/order and compatible Arrow types. |
RETURN [DISTINCT] |
Supports AS aliases. Without a MATCH, expressions run on one implicit row (a configured engine is still required). |
ORDER BY, SKIP, LIMIT |
Nulls sort last ascending and first descending, as in openCypher. SKIP and LIMIT take non-negative integer literals or $parameters. |
Not supported
Write clauses (CREATE, MERGE, SET, DELETE, DETACH DELETE, REMOVE, FOREACH, LOAD CSV),
CALL { ... } subqueries and procedure calls (CALL name(...) / YIELD). They raise
NotImplementedError naming the clause and its line and column. See Per-row work without
CALL for the openCypher forms of common subqueries.
Labels are required or inferred¶
Because each node label maps to a table in GraphConfig, every node in a
pattern must either state a label ((n:Person)) or inherit one from a connected relationship ((p:Person)-[:LIVES_IN]->(c)
infers City, and untyped MATCH (a)-[]-(b) ranges over every configured relationship's endpoints). An
unlabeled standalone node such as MATCH (n) RETURN n raises ValueError:
Query planning error: A node pattern has no label and no relationship to infer one from: add a label, e.g. `(n:Person)`
UNION and UNION ALL¶
UNION removes duplicate rows; UNION ALL keeps them. Each branch has its own variable scope, ORDER BY,
SKIP and LIMIT, and combined output has no guaranteed order. As in openCypher, one query cannot mix UNION
and UNION ALL. Compatible numeric columns promote to a common type; mismatched column counts, names or types
raise ValueError.
List literals¶
Integer list literals preserve signed 64-bit values (so UNWIND [1131, 19791209300775] AS id RETURN id keeps
exact IDs). Lists with a decimal point or exponent ([0.1, 1e-3]) form float32 vectors; whole-number lists
also work in vector functions and convert to float32 inside the
vector operation.
Per-row work without CALL¶
CALL { ... } subqueries, which run a query once for each incoming row, are a Neo4j extension rather than part of
openCypher 9, so lance-csr doesn't run them. The common per-row uses have openCypher forms:
| For each incoming row | openCypher form |
|---|---|
| Count matches, keeping rows with none | OPTIONAL MATCH (p)-[:KNOWS]->(f) RETURN p.name, count(f) |
| Join another pattern | A further MATCH that shares a variable with the earlier ones |
| Keep the top match | Sort in a WITH, then take collect(...)[0] |
For example, each person's oldest friend:
print(engine.execute("""
MATCH (p:Person)-[:KNOWS]->(f:Person)
WITH p, f ORDER BY f.age DESC
WITH p, collect(f.name)[0] AS friend
RETURN p.name AS person, friend ORDER BY person
""").to_pylist())
# [{'person': 'Alice', 'friend': 'Bob'}, {'person': 'Bob', 'friend': 'David'},
# {'person': 'Carol', 'friend': 'David'}]
Patterns¶
// Direction: outgoing, incoming or either
MATCH (a:Person)-[:KNOWS]->(b) // outgoing
MATCH (a:Person)<-[:KNOWS]-(b) // incoming
MATCH (a:Person)-[:KNOWS]-(b) // either direction
// Variable length: *, *n, *n..m, *..m, *n..
MATCH (a:Person {id: 1})-[:KNOWS*1..3]->(b:Person)
// Alternation (single-hop or variable-length)
MATCH (p:Person)-[:LIKES_POST|LIKES_COMMENT]->(m)
// Relationship variables and properties
MATCH (a:Person)-[k:KNOWS]->(b:Person) WHERE k.since > 2020
// Shortest paths (one relationship type, both ends bound)
MATCH p = shortestPath((a:Person {id: 1})-[:KNOWS*]-(b:Person {id: 4}))
RETURN length(p), nodes(p)
Variable-length paths¶
- Bounds: ranges default to a minimum of
1and a maximum of20hops; anything above20raisesNotImplementedError.*0..includes a zero-hop identity match when start and end share the same label and key;*0..0matches only that identity. - Trail semantics (edge uniqueness): each stored relationship can be used at most once per variable-length segment, in either direction. A path may revisit a node through a different edge, and parallel edges between the same two nodes produce distinct trails.
- Execution: eligible CSR paths run as one iterative expansion across the full range (see Variable-length traversal); the join fallback uses one branch per length.
- Filtering edges along the path: an inline property map such as
[:KNOWS*1..3 {since: 2020}]must hold for every edge on the path (this form uses the join fallback). On a named path with the CSR,WHERE all(r IN relationships(p) WHERE ...)andnone(r IN relationships(p) WHERE ...)prune failing edges during the walk whenever the predicate only readsr's properties, parameters and literals (comparisons,IN,OR,IS NULL); other predicates (any(),single(), orall()underOR/NOT) run once each path is complete.
Alternation and untyped relationships¶
- Typed alternation (
[:A|B]and[:hasType|isSubclassOf*1..3]) follows each relationship type's configured endpoint labels. With CSR and node indexes on every participating type and label, variable-length alternation stays in one iterative operator. Standalone bounded undirected alternation ([:KNOWS|WORKS_WITH*1..3]-) can change direction at each hop (including inOPTIONAL MATCH) when endpoints have known labels and integer keys. - Untyped relationships (
(a)-[]-(b)orpath = (a)-[]-(b)) range over all configured relationship types without mixing identities across node tables. Undirected matches include both orientations, self-loops appear once, and parallel edges stay distinct. Whole-node and path returns include the union of participating fields (with nulls where a label lacks a field); conflicting Arrow types for the same property name fail at planning time.
Relationship uniqueness and reuse¶
- Within one
MATCH: two single-hop relationships of the same type never bind the same edge, so(a)-[:KNOWS]->(b)<-[:KNOWS]-(c)never matchesc = a. SeparateMATCHclauses may reuse the same edge. For this single-hop check, edges compare by endpoints (parallel edges count as one). - Across segments of a path: named paths track edge occurrences across all of their segments. Unnamed paths
such as
(p)-[:KNOWS*1..2]-(f)-[:KNOWS]-(g)do the same when the relationships are anonymous and typed, their boundary labels resolve, and a shared CSR walk or bounded join chain (at most 64 branches) applies; separate comma-separated paths in the sameMATCHdo not share history. - Reusing a relationship variable: writing a bound variable again (such as
(a)-[r:KNOWS]->(b), (c)-[r]->(d), in a laterMATCH, or afterWITH r) matches that same edge (c = a,d = b). The type may be omitted; a conflicting type matches nothing. Undirected reuse matches both orientations; if the first binding was undirected, a later directed reuse follows the traversed direction. Reusing a relationship variable inside a variable-length segment is unsupported. count(r): counts rows wherermatched (an unmatchedOPTIONAL MATCHcounts0).count(DISTINCT r)compares all ofr's columns, so identical parallel edges count once.
Shortest paths¶
shortestPath and allShortestPaths require one relationship type, no relationship variable, and named
endpoints. Lengths are unbounded unless you specify a maximum. When no path exists, MATCH drops the row and
OPTIONAL MATCH returns null. shortestPath returns one minimum-length path; allShortestPaths returns all
of them, including distinct parallel edges. With CSR indexes, single-target lookups search bidirectionally and
multi-source batches share forward searches (see ShortestPath).
Path values¶
A named path (MATCH path = (a)-[:KNOWS*1..2]->(b)) retains its ordered node and relationship records across
WITH, collect() and UNWIND, keeping parallel edges distinct even when their properties are identical:
paths = engine.execute("""
MATCH path = (a:Person {id: 1})-[:KNOWS]->(b:Person)
WITH collect(path) AS paths
UNWIND paths AS path
RETURN [n IN nodes(path) | n.name] AS names,
[r IN relationships(path) | endNode(r).id] AS targets
ORDER BY names
""")
print(paths.to_pylist())
# [{'names': ['Alice', 'Bob'], 'targets': [2]},
# {'names': ['Alice', 'Carol'], 'targets': [3]}]
nodes(path)returns integer keys when returned directly; comprehensions overnodes(path)read the stored node records, keeping equal keys from different labels distinct.relationships(path)returns relationship records (binding a relationship variable inside a named variable-length segment is unsupported).startNode(r)andendNode(r)return the relationship's stored source and target even when the path walked it backwards.- A zero-hop path has one node and an empty relationship list. All path functions return
nullfor anullpath. - Cost: constructing path values reads participating node tables in full. With
use_csr=True, named trail paths walk the CSR and read relationship properties only for returned paths; shortest paths and non-CSR relationship types build a query-local edge inventory whose memory grows with the graph. A path whose name appears nowhere else in its query (such asMATCH p = ... RETURN count(*)) is planned as the same pattern without the name, so it builds no path values and reads no node table in full.
Arrow struct layout and exact path semantics
RETURN pathyields an Arrow struct with fieldslength,nodes,node_recordsandrelationships.- Entity records store user properties in
__lance_csr_propertiesand{label, id}identity metadata in__lance_csr_identity. Relationship records also carry__lance_csr_startand__lance_csr_endnode records. These metadata fields never collide with user properties of the same name. - Prefer Cypher property and endpoint functions (
startNode,endNode,keys,labels) rather than relying on internal struct fields. Relationship identities distinguish occurrences within a single query only; they are not persistent IDs across queries or snapshots. - Rich paths require the same integer key types as the CSR (signed integers and
uint8/uint16/uint32) and unique non-null node IDs per participating label (duplicates raise an error). Mixed labels may share compatible properties; conflicting Arrow types fail explicitly. Expanding a graph pattern from a mixed-label value is unsupported. - Queries reading only shortest-path length, node keys, path null checks or homogeneous node-property
comprehensions (including through
WITHaliases) stay on the fast CSR shortest-path route within a single label. Cross-label shortest paths (including insideUNIONor when equal-label endpoints pass through another label) use typed identities so equal numeric keys from different labels never form a false zero-hop match. - Rich path queries can reuse cached plan templates, bind fresh parameters per execution, and honor snapshot isolation for open batch readers.
Expressions and predicates¶
| Kind | Supported |
|---|---|
| Arithmetic | + - * / % |
| Comparison | = <> != < <= > >=, and chains such as 18 <= p.age < 65 |
| Boolean | AND OR NOT; bare boolean properties/expressions work directly (WHERE n.active) |
| Strings | STARTS WITH, ENDS WITH, CONTAINS, and LIKE / ILIKE with SQL wildcards |
| Lists | IN, literals [1, 2], indexing list[0], list comprehensions [x IN list WHERE ... | ...] |
| Maps | Literals {k: v} and key access m.k |
| Patterns | EXISTS { MATCH ... WHERE ... }, bare pattern predicates NOT (a)-[:R]->(b), pattern comprehensions [(a)-->(b) | b.name] |
| Conditionals | CASE WHEN ... THEN ... ELSE ... END and CASE x WHEN ... |
| Nulls | IS NULL, IS NOT NULL |
| Temporal | d.year, d.month, d.day, d.hour and other component access |
No regular expressions
Regular expressions (=~) are not supported. STARTS WITH, ENDS WITH and CONTAINS match literal text;
use LIKE or ILIKE for wildcards.
Aggregate results can be compared in RETURN or WITH, combined with AND/OR/NOT, or used in CASE:
On empty input, a global count returns 0 (so the query above returns one row with present = false), and
count(DISTINCT value) skips nulls and duplicates, including when a filter fixes the counted property to a
constant.
Functions¶
| Group | Functions |
|---|---|
| Aggregates | count (count(*), count(DISTINCT x)), sum, avg, min, max, collect (supports DISTINCT; skips nulls) |
| Strings | toLower / lower, toUpper / upper, toString, left, right, substring, replace, split, trim, lTrim, rTrim, reverse, size |
| Conversion | toInteger, toFloat, coalesce |
| Properties | keys(map), keys(node), keys(relationship), labels(node) |
| Lists | size, head, last, tail, reverse, range(start, end [, step]) (inclusive), reduce |
| List predicates | all, any, none, single |
| Math | abs, round, floor, ceil, sqrt |
| Temporal | date, datetime, date_part('month', d), to_epoch_ms, duration.inSeconds(a, b).seconds |
| Paths | length(p), nodes(p), relationships(p); startNode(r), endNode(r) on relationship values |
| Vectors | vector_distance(a, b, metric), vector_similarity(a, b, metric); see Vector search |
Any unlisted function raises ValueError with Cypher function 'name' is not implemented.
result = engine.execute("""
RETURN substring('aη«πz', 1, 2) AS text,
reverse(range(1, 5, 2)) AS numbers,
round(-1.5) AS rounded
""")
print(result.to_pylist()) # [{'text': 'η«π', 'numbers': [5, 3, 1], 'rounded': -1.0}]
keys() returns field names of a map (including null-valued keys) or the non-null exposed property names of a
node or relationship. labels(node) returns the canonical configured label of a single-label node binding
(preserving aliases through WITH, and returning null for an unmatched OPTIONAL MATCH):
result = engine.execute("""
MATCH (p:Person {name: 'Alice'}) WITH p AS person
RETURN labels(person) AS labels, size(keys(person)) AS properties
""")
print(result.to_pylist()) # [{'labels': ['Person'], 'properties': 3}]
Labels on mixed-label bindings
labels(p) requires a binding with one configured label. A multi-label pattern such as (p:Person:City)
raises a mixed-label error (including after WITH p AS x), because the engine does not retain per-row labels
across the union and never guesses a label from an ID.
Exact function semantics and edge cases
String and list functions follow the openCypher 9 reference; math functions follow the Neo4j 4.4 numeric reference.
- Strings:
size,left,rightandsubstring(text, start [, length])count Unicode characters (substringstarts at0). Starts and lengths must be non-negative integers; a null string returns null, while a null start or length on a non-null string raises an error.trim/lTrim/rTrimstrip whitespace;replacereplaces all literal matches;splitpreserves empty fields (split('', ',')returns[''], and an empty delimiter splits into characters). - Lists:
sizecounts outer elements;last([])returns null;tail([])andreverse([])return[];reversealso accepts strings.range(start, end [, step])is inclusive, defaults to step1, accepts negative steps, returns[]when the step points away fromend, returns null on null arguments, and raises an error on step0, non-integers, or unsigned Arrow integers outsideInt64range. - Math:
abspreserves integers and raises on overflow or unsigned inputs outsideInt64range.round,floor,ceilandsqrtconvert numeric inputs (includingUInt64) toFloat64.roundtakes one argument and breaks ties toward $+\infty$ (round(-1.5) == -1.0).sqrtof a negative number returnsNaN. All math functions return null for null input and reject non-numeric values. - Properties:
keys(entity)returns normalized column spellings in unspecified order (including ID or endpoint columns);keys({})returns[]. - Direct count of unmapped list comprehensions:
size([x IN values WHERE x > 0])counts retained elements directly instead of materializing the filtered list (duplicates and retained nulls still count as1, an empty or null list produces0, and the Arrow output column is non-nullable). Mapped comprehensions (| expr) and pattern comprehensions use the regular list-building path.
Ordered reduce and list predicates¶
reduce(total = initial, item IN list | expression) folds a list in order, passing each step the previous
accumulator. The list predicates all, any, none and single test function(item IN list WHERE predicate):
result = engine.execute("""
RETURN reduce(total = 0, item IN [1, 2, 3] | total * 10 + item) AS ordered,
all(item IN [1, 2, 3] WHERE item > 0) AS positive,
single(item IN [1, 2, 3] WHERE item = 2) AS exactly_one
""")
print(result.to_pylist()) # [{'ordered': 123, 'positive': True, 'exactly_one': True}]
- Both forms can capture outer variables and nest scalar folds or predicates.
- List predicates also support correlated graph predicates such as
EXISTS { MATCH ... }. reduceaccepts list comprehensions, pattern comprehensions orcollect(...)as its input list; its body supports scalar expressions only (compute nested graph patterns, comprehensions or aggregates beforehand).
Empty-list, null and type semantics for reduce and list predicates
Semantics follow the pinned Cypher 4.4 list and predicate references:
reduce: an empty list returnsinitial; a null list returnsnull. Null elements are passed to the body (usecoalesce()to replace them). The accumulator and iterator names are local to the body whileinitialandlistread outer variables. The accumulator must keep a compatible Arrow type (integers can promote to float).- List predicates on empty/null lists: on
[],allandnonereturntrue, whileanyandsinglereturnfalse. A null list returnsnull. - Three-valued logic: a null predicate result makes the outcome
nullunless a decisive element already fixes it (falsedecidesall,truedecidesanyandnone, twotrues decidesingle). Inspect null elements explicitly withitem IS NULL.
Putting it together¶
result = engine.execute("""
MATCH (p:Person)
OPTIONAL MATCH (p)-[:KNOWS]->(f:Person)
WITH p, count(f) AS friends
WHERE EXISTS { MATCH (p)-[:LIVES_IN]->(:City {name: 'London'}) }
RETURN p.name AS name,
friends,
CASE WHEN p.age >= 30 THEN 'senior' ELSE 'junior' END AS band
ORDER BY name
""")
print(result.to_pylist())
# [{'name': 'Alice', 'friends': 2, 'band': 'junior'},
# {'name': 'Bob', 'friends': 1, 'band': 'senior'}]
Case sensitivity and quoted names¶
Keywords, function names, variables, labels, relationship types and property names are all case-insensitive:
p.firstName, p.firstname and p.FIRSTNAME refer to the same Arrow or Lance column (including in inline
filters and relationship properties). Source field names stay intact, unaliased RETURN items keep the spelling
you wrote, and explicit aliases follow the lowercase convention.
An output alias may reuse an input variable's name: expressions in the same RETURN still read the input
variable, while a following ORDER BY reads the output alias (for example,
RETURN p.name AS p, p.id AS id ORDER BY p sorts by name, including with aggregation and OPTIONAL MATCH).
Use backticks to quote labels, relationship types, variables, properties or aliases containing spaces, special characters or keywords (double a backtick inside a quoted name). Quoting does not change case-insensitivity:
device_config = GraphConfig.builder().node_table("Device Type", "asset id").build()
devices = {
"Device Type": pa.table({"asset id": ["a-1", "b-2"], "status": ["active", "retired"]})
}
device_engine = CypherEngine(device_config, devices)
print(
device_engine.execute(
"MATCH (x:`Device Type`) WHERE x.`asset id` IN $keys RETURN x.`asset id` AS `asset id`",
{"keys": ["a-1"]},
).to_pylist()
) # [{'asset id': 'a-1'}]