Cypher Types, Lists, and Maps
This section describes AnzoGraph DB compatibility with the Cypher Language specification for Cypher types, lists, and maps.
- Types (Partially Supported)
- Type Coercions (Partially Supported)
- Lists (Supported)
- Maps (Supported)
- Working with Null (Supported)
Types (Partially Supported)
The Cypher standard specifies data type support in three different categories:
- Property types
- Structural types
- Composite types
Property Types
Property types include the following:
- NUMBER – Abstract type, which has INTEGER or FLOAT as subtypes.
- STRING – Unicode string type.
- BOOLEAN – true and false values. (Cypher uses ternary logic in the WHERE clause; in addition to true and false values, a third state is a null ternary value indicating an indeterminate state.)
Each property type can be returned from Cypher queries, used as parameters, stored as properties, or constructed with Cypher literals.
Structural Types
Structural types include the following:
- NODE – comprised of ID, label(s), or a map (of properties).
- RELATIONSHIP – comprised of an ID, type, map (of properties), or the ID of the start and end nodes.
- PATH – An alternating sequence of nodes and relationships.
The PATH type is not supported in the current AnzoGraph DB release.
Each structural type can be returned from Cypher queries. Structural types cannot be used as parameters, stored as properties, or constructed with Cypher literals.
Nodes, relationships, and paths are returned as a result of pattern matching. Labels are not values but are a form of pattern syntax.
Composite Types
Composite types include:
- LIST OF T — is a heterogeneous, ordered collections of values, each of which has any property, structural or composite type T.
- MAP is a heterogeneous, unordered collections of key-value pairs, where the key is a string and the value has any property, structural, or composite type.
Composite types can be returned from Cypher queries, used as parameters. or constructed with Cypher literals.
Composite values can also contain null. Composite types cannot be stored as properties.
Type Coercions (Partially Supported)
There are two type coercions described in the Cypher language specification:
- LIST OF NUMBER to LIST OF FLOAT (Not Supported)
- INTEGER to FLOAT
Only the INTEGER to FLOAT coercion is supported in the current AnzoGraph DB release.
Lists (Supported)
The Cypher specification describes support for creating a literal list. You can create a list by using brackets and separating all elements in the list with commas. For example:
RETURN [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] AS list
This returns the following result:
[0,1,2,3,4,5,6,7,8,9]
To access elements in the list, you can use the square brackets again. For example, with a list of numbers you could use the range function, which will extract all numbers between and including a starting and ending number:
RETURN range(0, 10)[3]
With the range function, you can also specify a negative number in square brackets, to start from the end of a list, rather than from the beginning. For example:
RETURN range(0, 10)[-3]
Finally, you can use ranges within the square brackets to return a range of values from the list:
RETURN range(0, 10)[0..3]
List and Pattern Comprehension (Partially Supported)
The Cypher language specification also describes two syntactic constructs for lists, List Comprehension and Pattern Comprehension (not yet supported).
- List comprehension is a syntactic construct available in Cypher for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) instead of the use of map and filter functions.
- Pattern comprehension (not yet supported) is a syntactic construct available in Cypher for creating a list based on matching a pattern. A pattern comprehension will match the specified pattern just like a normal MATCH clause, with predicates (just like in a normal WHERE clause), but it will yield a custom projection.
List Comprehension (Supported)
List comprehension provides a query construct to create another list based on other existing lists, for example:
RETURN [x IN range(0,10) WHERE x % 2 = 0 | x^3] AS result
This returns the following result:
[0.0,8.0,64.0,216.0,512.0,1000.0]
In the previous query, either the WHERE part or the expression can be omitted, if you only want to filter or map results. For example:
RETURN [x IN range(0,10) WHERE x % 2 = 0] AS result
This query, omitting the expression, returns the following result:
[0,2,4,6,8,10]
The following query omits the WHERE clause:
RETURN [x IN range(0,10)| x^3] AS result
This query returns the following result:
[0.0,1.0,8.0,27.0,64.0,125.0,216.0,343.0,512.0,729.0,1000.0]
Maps (Supported)
The Cypher specification describes how to construct maps using Cypher and construct map projections from nodes, relationships, and other map values.
Literal Maps
The following query example shows how you can create a map based on the Neo4j Movies graph data:
RETURN {key: 'Value', listKey: [{inner: 'Map1'}, {inner: 'Map2'}]}
The result from this query is the following:
{key: 'Value', listKey: [{inner: 'Map1'}, {inner: 'Map2'}]}
{listKey -> [{inner -> "Map1"},{inner -> "Map2"}], key -> "Value"}
Map Projections (Partially Supported)
Cypher supports a concept called map projection that you can easily construct from nodes, relationships and other map values. A map projection begins with the variable bound to the graph entity to be projected from, and contains a body of comma-separated map elements, enclosed by { and }.
map_variable {map_element, [, …n]}
A map element projects one or more key-value pairs to the map projection. There are four different types of map projection elements:
- Property selector — Projects the property name as the key, and the value from the map_variable as the value for the projection.
- Literal entry — This is a key-value pair, with the value being arbitrary expression key: <expression>.
- Variable selector — Not yet supported in current AnzoGraph DB release. Projects a variable, with the variable name as the key, and the value the variable is pointing to as the value of the projection.
- All-properties selector — Not yet supported in current AnzoGraph DB release. Projects all key-value pairs from the map_variable value.
If the map_variable points to a null value, the whole map projection will evaluate to null.
The following example creates a map projection with a literal entry, which in turn also uses map projection inside the aggregating collect() .
Query MATCH (actor:Person {name: 'Charlie Sheen'})-[:ACTED_IN]->(movie:Movie) RETURN actor { .name, .realName, movies: collect(movie { .title, .year })}
This query locates 'Charlie Sheen' and return data about him and the movies he has acted in:
actor
{name -> "Charlie Sheen", movies -> [{title -> "Apocalypse Now", year -> 1979},
{title -> "Red Dawn", year -> 1984},{title -> "Wall Street", year -> 1987}],
realName -> "Carlos Irwin Est évez
Two variations of map projections are not yet supported:
- Variable selector
- All properties selector
For example:
MATCH (actor:Person {name: 'Charlie Sheen'})[:ACTED_IN]>(movie:Movie) RETURN actor { .name, .realName, movies: collect(movie { .title, .year })}
MATCH (actor:Person)[:ACTED_IN]>(movie:Movie)WITH actor, count(movie) AS nrOfMoviesRETURN actor { .name, nrOfMovies }
Working with Null (Supported)
In Cypher, null is used to represent missing or undefined values. Conceptually, null represents a missing or unknown value and it is treated somewhat differently from other values. For example, obtaining a property value from a node that does not have that property value defined produces a null. Most expressions that take null as input will also produce a null result. This includes boolean expressions that are used as predicates in the WHERE clause.
Logical Operations with Null
The logical operators (AND, OR, XOR, NOT) treat null as the unknown value of three-valued logic (true, false, and unknown). In this case, null values are interpreted as being false.
The IN Operator and Null
If Cypher determines that a value or element exists in a list, the result returned will be true. Any list that contains a null and doesn’t have an element that matches will return null. Otherwise, the result returned will be false.
Expressions That Return Null
The following expressions will return null values:
- Accessing a property that does not exist on a node or relationship, that is,
n.missingProperty
- Comparisons where either side of the expression is null, for example:
1 < null
- Arithmetic expressions containing null, for example:
1 + null
- Function calls where any arguments are null, for example:
sin(null)