Cypher Overview

LatticeDB uses the Cypher query language for graph operations. Cypher is a declarative language where you describe patterns to match and operations to perform, rather than specifying how to execute them.

Syntax at a Glance

Nodes are written in parentheses, edges in square brackets:

// Match a pattern
MATCH (person:Person)-[:KNOWS]->(friend:Person)
WHERE person.name = "Alice"
RETURN friend.name

Supported Clauses

ClausePurpose
MATCHFind patterns in the graph
WHEREFilter results with conditions
RETURNSpecify output columns
CREATECreate nodes and edges
SETUpdate properties and labels
DELETERemove nodes and edges
REMOVERemove properties and labels
MERGEMatch or create a pattern
WITHChain query parts, pipe results
UNWINDExpand lists into rows
OPTIONAL MATCHMatch a pattern, or keep the row with nulls when it is absent
UNION / UNION ALLCombine the rows of queries with the same columns
FOREACHRun CREATE, SET, MERGE, DELETE, REMOVE or a nested FOREACH for each element of a list
CALL { }Run a subquery once for each row
CALL db.labels()List the catalog: db.labels(), db.relationshipTypes(), db.propertyKeys(), db.indexes()
ORDER BYSort results, with NULLS FIRST or NULLS LAST
LIMITLimit number of results
SKIPSkip first N results

A query may begin with MATCH, OPTIONAL MATCH, CREATE, MERGE, UNWIND, WITH, RETURN, CALL or FOREACH; RETURN 1 + 2 on its own returns one row. Only a RETURN, or a catalog CALL that is the whole query, gives a query columns: a query without one, such as a bare CREATE, MERGE, SET, DELETE or FOREACH, returns no columns and no rows. Add a RETURN to get values back.

MATCH (p:Person)
OPTIONAL MATCH (p)-[:MANAGES]->(r:Person)
RETURN p.name AS name, collect(r.name) AS reports
UNION
MATCH (t:Team) RETURN t.name AS name, [] AS reports
MATCH (a:Person {name: "Alice"})
FOREACH (tag IN ["admin", "owner"] | CREATE (a)-[:HAS]->(:Tag {name: tag}))

A CALL of a catalog procedure that is the whole query returns its columns; inside a larger query it names them with YIELD:

CALL db.labels() YIELD label
RETURN label ORDER BY label

Expressions beyond the basics

ConstructExample
CASECASE WHEN n.age >= 18 THEN 'adult' ELSE 'minor' END, CASE n.kind WHEN 'a' THEN 1 END
Indexing and slicinglist[0], list[-1], list[1..3], map['key'], n[$prop]
List comprehension[x IN list WHERE x > 0 | x * 2]
List predicatesall(x IN list WHERE x > 0), any(...), none(...), single(...)
reducereduce(total = 0, x IN list | total + x)
Pattern predicateWHERE (n)-[:KNOWS]->(), WHERE NOT (n)-->()
EXISTS { }, COUNT { }, COLLECT { }EXISTS { (n)-[:KNOWS]->(m) WHERE m.age > 30 }, COUNT { (n)--() }, COLLECT { MATCH (n)-->(m) RETURN m.name ORDER BY m.name }; see Subqueries in expressions
Pattern comprehension[(n)-[:KNOWS]->(m) WHERE m.age > 30 | m.name]
Named pathMATCH p = (a)-[:KNOWS*]->(b) RETURN length(p), nodes(p), relationships(p)
Shortest pathMATCH p = shortestPath((a)-[:KNOWS*]-(b)); see Shortest Paths
Label testn:Person, n:Person:Admin, n:Person|Robot, n:!Admin; r:KNOWS tests a relationship's type; see Label Expressions
exists() on a propertyexists(n.email), the same as n.email IS NOT NULL
Map projectionn {.name, .age, friends: size(fs)}, n {.*}; see RETURN
RETURN *, WITH *every variable in scope; see RETURN
Literals0x1F, 0o17, .5, `a name with spaces`

Not Supported

These parts of Cypher are not implemented. Each fails before the query runs, with an error that names it:

  • quantified and parenthesised path patterns, such as ((a)-->(b)){1,3} or (x) ((a)-->(b))+, and quantified relationships such as -[:R]->{1,3} or -[:R]->+; use a variable-length relationship -[:R*1..3]->
  • &, ! and % in a relationship's types, as in -[:!KNOWS]->; use WHERE NOT r:KNOWS
  • WHERE inside a pattern, as in (n WHERE n.age > 30)
  • writing inside EXISTS { }, COUNT { } or COLLECT { }
  • CALL { } IN TRANSACTIONS, OPTIONAL CALL, and procedures other than the four catalog procedures above
  • LOAD CSV; import files with lattice import or through the API
  • type predicates (x IS :: INTEGER) and || for concatenation (use +)
  • schema and administration statements: CREATE INDEX, CREATE CONSTRAINT, DROP, SHOW, USE, EXPLAIN, PROFILE, GRANT and the like. Indexes are created through the API.
  • the GQL clauses FINISH, FILTER, LET and INSERT, and the legacy START
  • temporal and spatial values and the functions on them (date(), point())
  • an integer with a leading zero, 012: openCypher 9 read it as octal and other dialects as decimal, so it is refused with a message giving both; write 12, or 0o12 for octal. Floats may start with zero (01.5).

The limits of shortestPath are listed under Shortest Paths.

Errors

A query that fails says where and why: the stage that refused it (parse, semantic, plan or execution), a diagnostic code, a message, and the line and column of the part of the query at fault. A plan error has no line and column; its message names the part instead, as in "ORDER BY item 2 aggregates, but the RETURN does not return that aggregate". The CLI prints the place with a marker under that part:

$ lattice exec db.lattice --query="RETURN 1/0"
Error: Execution error (DivisionByZero) at 1:9: Division by zero: 1 / 0
  1 | RETURN 1/0
    |         ^
$ lattice exec db.lattice --query="RETURN 1 LIMIT -1"
Error: Execution error (TypeError) at 1:16: SKIP and LIMIT take a non-negative integer, but this is -1
  1 | RETURN 1 LIMIT -1
    |                ^
$ lattice exec db.lattice --query="RETURN \$nope"
Error: Execution error (ParameterNotFound) at 1:8: Parameter $nope was not given a value
  1 | RETURN $nope
    |        ^^^^^

The bindings carry the same fields on their query error (in Python, LatticeQueryError with stage, diagnostic_code and location), and the C API returns them through lattice_query_last_error_*:

from latticedb import LatticeQueryError

try:
    db.query("RETURN 1/0")
except LatticeQueryError as e:
    print(e, e.code, e.stage, e.diagnostic_code, e.location)
# Division by zero: 1 / 0 -1 execution DivisionByZero {'line': 1, 'column': 9, 'length': 1}

The error code tells what kind of failure it was:

FailureCode
Parse or semantic error (syntax, unknown function, wrong argument count)LATTICE_ERROR_INVALID_ARG (-6)
Plan or execution error in the query (division by zero, type error, overflow)LATTICE_ERROR (-1)
DELETE of a node that still has relationshipsLATTICE_ERROR_HAS_RELATIONSHIPS (-17), as from lattice_node_delete
A write to a node or relationship that does not exist, or was deleted earlier in the queryLATTICE_ERROR_NOT_FOUND (-4), as from lattice_edge_create
The storage underneath failed while the query read or wroteLATTICE_ERROR_IO (-2)
Memory ran outLATTICE_ERROR_OUT_OF_MEMORY (-13)

A failure to read stored data fails the query. It is never read as null, so a WHERE cannot quietly drop a row because a read failed. Null stands only for what is really absent: a missing property, a deleted node or relationship, a property name the database has never seen, or a parameter passed as null.

LatticeDB Extensions

LatticeDB extends Cypher with two operators for search:

Vector Distance (<=>)

Find nodes with similar embeddings:

MATCH (chunk:Chunk)
WHERE chunk.embedding <=> $query_vector < 0.5
RETURN chunk.text
ORDER BY chunk.embedding <=> $query_vector

See Vector Search for details.

Full-Text Search (@@)

Search indexed text content:

MATCH (doc:Document)
WHERE doc.content @@ "neural networks"
RETURN doc.title

See Full-Text Search for details.

Expressions

Operators

CategoryOperators
Comparison=, <>, <, <=, >, >=
LogicalAND, OR, NOT, XOR
Arithmetic+, -, *, /, %, ^
StringCONTAINS, STARTS WITH, ENDS WITH, =~ (regular expression)
ListIN
NullIS NULL, IS NOT NULL
Search<=> (vector distance), @@ (full-text)

Functions

FunctionDescription
id(node)Get node ID
coalesce(a, b, ...)Return first non-null value
abs(x)Absolute value
size(x)Length of a list, or characters in a string
toInteger(x)Convert to integer
toFloat(x)Convert to float

Function names are not case-sensitive. See Functions for the full list.

Aggregations

FunctionDescription
count(x)Count non-null values
count(*)Count rows
sum(x)Sum values
avg(x)Average values
min(x)Minimum value
max(x)Maximum value
collect(x)Collect into list

See Aggregations for details.

Parameters

Use $name syntax to pass values safely:

MATCH (n:Person) WHERE n.name = $name RETURN n

See Parameters for language-specific binding.