A predicate specifies a condition that can be evaluated to give a boolean value (true or false).
Category | Predicates |
|---|---|
| Comparison Predicates | =, <> (or !=), >, <, >=, <=, =~ |
| Exists Predicate | EXISTS |
| None Predicate | NONE |
| Null Predicates | IS NULL, IS NOT NULL |
| Normalized Predicates | IS NORMALIZED, IS NOT NORMALIZED |
| Labeled Predicates | IS LABELED, IS NOT LABELED, : |
| Property Exists Predicate | PROPERTY_EXISTS |
| Value Type Predicates | IS TYPED, IS NOT TYPED |
| Boolean Value Predicates | IS TRUE, IS FALSE |
| All Different Predicate | ALL_DIFFERENT |
| Same Predicate | SAME |
| Source/Destination Predicates | IS SOURCE OF, IS NOT SOURCE OF, IS DESTINATION OF, IS NOT DESTINATION OF |
| Directed Predicates | IS DIRECTED, IS NOT DIRECTED |
Compares two values or expressions and returns true or false. GQL supports the following comparison operators:
=<> (or !=)><>=<==~The >, <, >=, and <= can be used only with numeric, textual, temporal, boolean, and null values.
In GQL, two values are considered comparable if they can be meaningfully evaluated using comparison operators. There are two kinds of comparable values:
12 with the string "13ab" attempts a conversion before evaluation.GQLRETURN 30.1 > 30 // true
The first differing character (from left to right) determines the result of the comparison. The characters are compared based on their Unicode values.
GQLRETURN "campus" < "camera" // false
This query returns false because the first differing character, p, has a higher Unicode value than e (Unicode of p is 112, while e is 101).
GQLLET email = "[email protected]" RETURN email =~ "[a-zA-Z0-9_.-]+@[a-zA-Z0-9]+\.(com|cn)" // true
This query returns true because the email matches the specified email address pattern.
Temporal values are treated like numeric values, as time is measured in units such as seconds, hours, days, and years. In GQL, the comparison between two temporal values is based on their duration—the difference between the two points in time. If the duration is 0, the values are equal; if negative, the left value is smaller; otherwise, the left value is greater.
GQL// p1.birthday is 1987-10-01, p2.birthday is 1987-10-02 MATCH (p1 {name: "Alex"}), (p2 {name: "Joy"}) RETURN p1.birthday < p2.birthday // true
Two lists are considered equal if they contain the same elements in the exact same order.
GQLRETURN [1,2,3] = [1,2,3] // true
GQLRETURN [] = [] // true
GQLRETURN [1,2,3] = [1,3,2] // false
Two records are considered equal if they have the same fields with identical values.
GQLRETURN {a:1, b:2} = {a:1, b:2} // true
GQLRETURN {a:1, b:2} = {a:2, b:2} // false
GQLRETURN {a:1} = {b:1} // false
Paths are similar to lists, as they consist of sequences of nodes and edges.
GQLMATCH p1 = (:User {name: "mochaeach"})-[:Joins]->(:Club {_id: "C02"}) MATCH p2 = (:User {name: "mochaeach"})-[:Joins]->(:Club {_id: "C02"}) RETURN p1 = p2 // true
GQLMATCH p1 = (:User {name: "mochaeach"})-[:Joins]->(:Club {_id: "C02"}) MATCH p2 = (:Club {_id: "C02"})<-[:Joins]-(:User {name: "mochaeach"}) RETURN p1 = p2 // false
Nodes and edges can be treated like records, where property names act as keys and their corresponding property values serve as values.
GQLMATCH (n1:User {name: "mochaeach"}), (n2:Club {_id: "C02"}) RETURN n1 = n2 // false
GQL attempts to convert an entire string to a numeric value when it is compared with a numeric operand. The conversion succeeds only if the string is in a valid numeric format. For example:
" 123 " → 123"-2" → -2"+2.3" → 2.3If the string is not in a valid numeric format, it is interpreted as 0.
GQLRETURN "-2.9" > -3 // true; converts "-2.9" to 2.9
GQLRETURN "11a" > 10 // false; converts "11a" to 0
In GQL, strings can be compared with temporal values by implicitly converting the string to a temporal type. This behavior is similar to string-numeric comparisons. For a string to be successfully compared to a temporal value, it must:
GQL// p.birthday is 1987-10-01 MATCH (p1 {name: "Alex"}), (p2 {name: "Joy"}) RETURN p1.birthday < "1987-10-02" // true
Boolean values can be compared with other data types, such as numeric or textual values, through implicit type conversion.
GQLRETURN true = 1 // true
GQLRETURN false = 0 // true
GQLRETURN true = "true" // false
The EXISTS predicate evaluates whether a specified graph pattern or query returns any results. If it finds matching data, the predicate evaluates to true; otherwise, it evaluates to false.
Syntax<exists predicate> ::= "EXISTS" { "{" <graph pattern> "}" | "(" <graph pattern> ")" | "{" { <match statement>... } "}" | "(" { <match statement>... } ")" | "{" <query specification> "}" }
This query checks whether there is any path originating from node A exists in the graph:
GQLRETURN EXISTS { MATCH ({_id: "A"})->() }
The MATCH keyword can be omitted when EXISTS contains only a graph pattern. Additionally, a WHERE clause can be used with the pattern to apply conditions:
GQLRETURN EXISTS { (n)->() WHERE n._id = "A" }
EXISTS can also be used in the WHERE clause of a MATCH statement:
GQLMATCH (n:movie) WHERE n.rating > 7.5 AND EXISTS { MATCH (n)<-[:direct]-(m) WHERE m.name = "Ang Lee" } RETURN n.name
This query checks whether any element in a list is greater than 3:
GQLRETURN EXISTS { FOR item in [1,2,3] FILTER item > 3 RETURN item }
This query checks whether the retrieved node has the property name; note that it returns true even if the name value is null:
GQLMATCH (n:Paper {_id: "book92"}) LIMIT 1 RETURN EXISTS(n.name)
The NONE predicate evaluates whether a specified graph pattern or query returns no results. If it doesn't find any matching data, the predicate evaluates to true; otherwise, it evaluates to false.
Syntax<none predicate> ::= "NONE" { "{" <graph pattern> "}" | "(" <graph pattern> ")" | "{" { <match statement>... } "}" | "(" { <match statement>... } ")" | "{" <query specification> "}" }
This query checks whether there is no path originating from node A in the graph:
GQLRETURN NONE { MATCH ({_id: "A"})->() }
The MATCH keyword can be omitted when NONE contains only a graph pattern. Additionally, a WHERE clause can be used with the pattern to apply conditions:
GQLRETURN NONE { (n)->() WHERE n._id = "A" }
NONE can also be used in the WHERE clause of a MATCH statement:
GQLMATCH (n:movie) WHERE n.rating > 7.5 AND NONE { MATCH (n)<-[:direct]-(m) WHERE m.name = "Ang Lee" } RETURN n.name
This query checks whether there is no element in a list is greater than 3:
GQLRETURN NONE { FOR item in [1,2,3] FILTER item > 3 RETURN item }
Specifies a test for a null value. GQL supports the following null predicates:
IS NULLIS NOT NULLThis query retrieves the title of each Paper node if the value is not null; otherwise, it returns the message TITLE NOT FOUND:
GQLMATCH (n:Paper) RETURN CASE WHEN n.title IS NOT NULL THEN n.title ELSE "TITLE NOT FOUND" END
Determines whether a character string value is normalized. GQL supports the following normalized predicates:
IS [ <normal form> ] NORMALIZEDIS NOT [ <normal form> ] NORMALIZEDDetails
<normal form> defaults to NFC. Other available normalization forms are NFD, NFKC and NFKD.GQLRETURN "Å" IS NORMALIZED AS normRes
Result:
| normRes |
|---|
| 1 |
GQLRETURN "Å" IS NFD NORMALIZED AS normRes
Result:
| normRes |
|---|
| 0 |
Determines whether a graph element satisfies a label expression. GQL supports the following labeled predicates:
IS LABELEDIS NOT LABELED:GQLMATCH (n) WHERE n IS NOT LABELED Paper RETURN n
GQLMATCH (n) WHERE n:Paper RETURN n
The PROPERTY_EXISTS predicate evaluates whether a referenced graph element contains a property, regardless of its value. This means it returns true even if the property's value is null.
GQLMATCH (n:Paper) LIMIT 1 RETURN PROPERTY_EXISTS(n, "name")
Determines whether a value conforms to a specific type. GQL supports the following value type predicates:
IS TYPED <value type>IS NOT TYPED <value type>Details
<value type> supports the following data type keywords: STRING, BOOL.GQLRETURN "a" IS TYPED BOOL AS typeCheck
Result:
| typeCheck |
|---|
| false |
Evaluates the truthiness of a boolean expression or variable, determining whether it is true or false. GQL supports the following boolean value predicates:
IS TRUEIS FALSEGQLRETURN 1 > 2 IS TRUE
Result:
| 1 > 2 IS TRUE |
|---|
| 0 |
The ALL_DIFFERENT predicate evaluates whether all graph elements bound to a list of element variables are pairwise different from one another.
GQLMATCH (n1 {_id:"P1"}) MATCH ({_id:"P1"})-(n2) MATCH ({_id:"P3"})-(n3) RETURN table(n1._id, n2._id, n3._id, ALL_DIFFERENT(n1, n2, n3))
Result:
| n1._id | n2._id | n3._id | ALL_DIFFERENT(n1, n2, n3) |
|---|---|---|---|
| P1 | P2 | P2 | 0 |
The SAME predicate evaluates whether all element variables bind to the same graph element.
GQLMATCH ({_id:"P1"})-(n1) MATCH ({_id:"P3"})-(n2) RETURN table(n1._id, n2._id, SAME(n1, n2))
Result:
| n1._id | n2._id | SAME(n1, n2) |
|---|---|---|
| P2 | P2 | 1 |
Determines whether a node is the source or destination of an edge. GQL supports the following source/destination predicates:
<node reference> IS SOURCE OF <edge reference><node reference> IS NOT SOURCE OF <edge reference><node reference> IS DESTINATION OF <edge reference><node reference> IS NOT DESTINATION OF <edge reference>GQLMATCH (n {_id: "P1"}), ()-[e:Cites]->() WHERE n IS SOURCE OF e RETURN e
Directed predicates determine whether an edge variable is bound to a directed edge. GQL supports the following directed predicates:
IS DIRECTEDIS NOT DIRECTEDDetails
GQLMATCH ()-[e]-() RETURN e IS DIRECTED
Result:
| e IS DIRECTED |
|---|
| 1 |