Advanced Topics

This section covers subqueries, conditional expressions, temporal and spatial values, procedure calls, null handling, and maps.

Subqueries

Existential Subqueries

EXISTS { … } returns true when its read-only subquery produces at least one row and false when it produces none. It checks row presence, so EXISTS { RETURN false } is true. Use it in a predicate or return the Boolean result as a column.

The subquery can refer to variables already in scope, including aliases from an earlier WITH and the current item in a list comprehension or list predicate. Variables it introduces stay local. Give new bindings names distinct from the outer variables.

MATCH (p:Person)
WITH p AS person
RETURN person.name AS name,
       EXISTS { MATCH (person)-[:KNOWS]->(:Person) } AS knowsSomeone

This returns one row per person and indicates whether they know another person. A subquery can also combine branches using UNION or UNION ALL. The following expression returns true because the second branch produces a row.

RETURN EXISTS {
  UNWIND [] AS value
  RETURN value
  UNION ALL
  RETURN 2 AS value
} AS found

Use exists(property) or exists(pattern) for the shorter property and pattern tests.

Returning Subqueries

CALL { … } runs a returning subquery for each incoming row. Each returned row is combined with the incoming row. When the subquery returns zero rows, that incoming row contributes no output. Polypheny supports a single read-only query branch in the enclosing graph and transaction. End the subquery with RETURN. This syntax extends openCypher M23.

CALL {
  MATCH (p:Person { name: 'John Doe' })
  RETURN p.age AS age
}
MATCH (p:Person)
WHERE p.age > age
RETURN p

For each person named John Doe, the subquery returns their age. The enclosing query returns the Person nodes older than that age.

Import outer variables with an initial WITH that lists their names. Subquery variables stay local until they are returned. Give each returned value a name distinct from the variables already bound in the outer query.

UNWIND [1, 2] AS x
CALL {
  WITH x
  RETURN x + 1 AS y
}
RETURN x, y
ORDER BY x

Conditional Execution

The CASE expression selects a value for each row. In the example below, the condition chooses a status string.

MATCH (p:Person)
RETURN p.name,
CASE
  WHEN p.age >= 18 THEN 'Adult'
  ELSE 'Minor'
END AS status

This query returns the status of each person as either ‘Adult’ or ‘Minor’, depending on their age.

Cypher evaluates an EXISTS subquery inside CASE only when it reaches that condition or selects that result branch. This query returns false. The condition guards the division in the subquery.

WITH 0 AS n
RETURN CASE
  WHEN n > 0 THEN EXISTS { WITH n WHERE 1 / n >= 1 RETURN n }
  ELSE false
END AS guarded

Temporal and Spatial Types

Cypher includes support for temporal types (like dates, times, and durations) and spatial types (like points and geometries).

Use temporal functions to construct and manipulate dates, local times, times with offsets, date-times, and durations.

Spatial types can be used to model geographic data. The point() function can be used to create point values, and there are several functions for calculating distances and performing other spatial operations.

Using Cypher with Procedures and Functions

Polypheny supports the db.labels() procedure. It returns one row per distinct node label in the selected graph namespace. In a larger query, use YIELD label to bind the procedure’s output. YIELD label AS name chooses an alias, and a WHERE after YIELD filters the output rows. Use built-in functions, such as labels(n), in expressions.

CALL db.labels()
YIELD label
RETURN count(*) AS numberOfLabels

This query counts the distinct node labels in the selected graph.

A standalone procedure call can return all output fields by omitting YIELD or using YIELD *. When composing a procedure that returns fields with other query clauses, list the fields you want to bind explicitly.

CALL db.labels() YIELD *

The server also resolves tabular procedures supplied by a Java CypherTableProcedureProvider installed on its classpath. Polypheny looks up these procedures by qualified name and graph namespace. The openCypher TCK runner installs its own fixture provider on the test server.

Working with Nulls

A missing property or an unmatched optional binding has the value null. Cypher uses three-valued logic. For example, a comparison with null evaluates to null, false AND null evaluates to false, and true OR null evaluates to true. String predicates such as STARTS WITH, ENDS WITH, and CONTAINS also preserve null when an operand is null. Test explicitly with IS NULL or IS NOT NULL.

For list and map equality, a definite mismatch yields false even when other entries are null. If equality depends on comparing null entries, the result is null. For example, [1, null] = [2, null] is false and [1, null] = [1, null] is null.

Use IN to test list membership. A matching element yields true. If no element matches and a comparison is unknown, the result is null. Otherwise it is false.

RETURN 1 IN [null, 1] AS present, 2 IN [1, null] AS unknown,
       null IN [] AS empty, 2 IN null AS missing

The result is true, null, false, and null.

Chained Comparisons

A comparison chain checks adjacent operands and combines the results with AND. For example, 1 < 2 <= 2 checks 1 < 2 and 2 <= 2. A definite mismatch makes the chain false, even if a different comparison returns null.

RETURN 1 < 2 <= 2 <> 3 AS allTrue,
       1 < null < 3 AS unknownComparison,
       1 < null < 3 < 2 AS falseWithNull

The values are true, null, and false.

Map Values

Map entries can be expressions, including nested lists and maps. Read a fixed key with m.key or use a string expression in brackets for a dynamic key. A missing key returns null.

WITH 7 AS x, 'key' AS k
WITH {key: x, nested: {key: x + 1}} AS m, k
RETURN m.key AS fixed, m.nested[k] AS nested, m.absent AS missing

The result is 7, 8, and null. Maps are query values and can supply named parameters or property assignments.

© Polypheny GmbH. All Rights Reserved.