Syntax
In this page, we list the syntactic features of Cypher as implemented in Kùzu. As described in the overview page, Cypher is a declarative graph query language, and Kùzu’s implementation is based on openCypher.
Parsing
Encoding
The Cypher query parser looks for an input STRING
that consists of ASCII or unicode characters from non-English
languages. An example is shown below for creating and querying from a node table of German books.
Escaping
To use special characters in identifiers, you can escape them by encapsulating the identifier in backticks `. An example is shown below for creating a node table of house names that contain special characters.
Multiline statements and termination
Breaking a query into multiple lines is allowed (and recommended for readability reasons). The query parser ignores leading and trailing whitespaces.
Termination is always indicated by a semicolon ;
, and the parser looks for this symbol to
know when a statement is complete.
Clauses
A Cypher query may contain one or more clauses and their associated subclauses, and can span multiple lines.
The end of a statement is marked with a semicolon ;
, and the query parser looks for this symbol
to know when a statement is complete.
Examples of clauses include:
MATCH
: Find patterns in the graphRETURN
: Specify what subset of the matched data to return
Examples of subclauses (that must reside under a clause) include:
WHERE
: Filter the results of aMATCH
clauseLIMIT
: Limit the number of results returned by a query
Comments
Comments are for humans to read and document their code, and are ignored by the query parser.
- Single line comments begin with a double slash (
//
) and continue up until the end of the line. They can be placed at the beginning, in the middle, or at the end of a query. - Multi-line comments begins with a slash and asterisk (
/*
) and continues until it ends with an asterisk and a slash (*/
). They can be useful for comments that are too long for one line.
Some examples are below.
Naming rules and recommendations
As a general rule of thumb, ensure the following:
- Names should begin with an valid alphabetic character of type unicode string —
Person
,CarOwner
- Names should not begin with a number —
1Person
is invalid, butPerson1
is valid - Names should not contain whitespaces or special characters other than underscores —
CarOwner
is valid, butCar Owner
is invalid - Names are generally case-insensitive —
Person
is the same asperson
, during table creation and querying
The following naming conventions are recommended for node and relationship tables:
Type | Naming convention | Do | Don’t |
---|---|---|---|
Node tables | CamelCase (begin with upper case letter) | CarOwner | car_owner |
Relationship tables | CamelCase or UPPERCASE separated by underscores | IsPartOf /IS_PART_OF | isPartOf or is_part_of |
Parameters
Parameters in Cypher queries are placeholders for values that are provided at runtime.
Parameters are prefixed with a dollar sign $
and can be used in any part of a query. They are useful for
preventing Cypher injection attacks, and for reusing query templates with different values.
See the prepared statements guide for more information on how to use parameters in Kùzu.
Reserved keywords
Reserved keywords are words that have a special meaning in Cypher. They cannot be used as identifiers in the following contexts:
- Variables
- Function names
- Parameters
To use a reserved keyword as an identifier in the above contexts, you can escape it by
encapsulating the keyword in backticks `, such as `DEFAULT
`, and this makes it a valid identifier.
The following list shows the reserved keywords in Cypher, organized by category:
Clauses
COLUMN
CREATE
DBTYPE
DEFAULT
GROUP
HEADERS
INSTALL
MACRO
OPTIONAL
PROFILE
RDFGRAPH
UNION
UNWIND
WITH
Subclauses
LIMIT
ONLY
ORDER
WHERE
Expressions
ALL
CASE
CAST
ELSE
END
ENDS
EXISTS
GLOB
SHORTEST
THEN
WHEN
Literals
NULL
FALSE
TRUE
Modifiers
ASC
ASCENDING
DESC
DESCENDING
ON
Operators
AND
DISTINCT
IN
IS
NOT
OR
STARTS
XOR
Schema
FROM
PRIMARY
TABLE
TO