Type Conversion/Type coercions¶
Converting an expression of a given type to another type is known as type conversion.
Legacy version compatibility¶
- nGQL 1.0 adopts the
C
-style of type conversion (implicitly or explicitly):(type_name)expression
. For example, the results ofYIELD (int)(TRUE)
is1
. But it is error-prone to users who are not familiar with the C language.
- since nGQL 2.0, the openCypher way of type coercions is adopted.
Type coercions functions¶
Function | Description |
---|---|
toBoolean() | Converts a string value to a boolean value. |
toFloat() | Converts an integer or string value to a floating point number. |
toInteger() | Converts a floating point or string value to an integer value. |
type() | Returns the string representation of the relationship type. |
Examples¶
nebula> UNWIND [true, false, 'true', 'false', NULL] AS b \
RETURN toBoolean(b) AS b;
+----------+
| b |
+----------+
| true |
| false |
| true |
| false |
| __NULL__ |
+----------+
nebula> RETURN toFloat(1), toFloat('1.3'), toFloat('1e3'), toFloat('not a number');
+------------+----------------+----------------+-------------------------+
| toFloat(1) | toFloat("1.3") | toFloat("1e3") | toFloat("not a number") |
+------------+----------------+----------------+-------------------------+
| 1.0 | 1.3 | 1000.0 | __NULL__ |
+------------+----------------+----------------+-------------------------+
nebula> RETURN toInteger(1), toInteger('1'), toInteger('1e3'), toInteger('not a number');
+--------------+----------------+------------------+---------------------------+
| toInteger(1) | toInteger("1") | toInteger("1e3") | toInteger("not a number") |
+--------------+----------------+------------------+---------------------------+
| 1 | 1 | 1000 | __NULL__ |
+--------------+----------------+------------------+---------------------------+
nebula> MATCH (a:player)-[e]-() \
RETURN type(e);
+----------+
| type(e) |
+----------+
| "follow" |
| "follow" |
+----------+
nebula> MATCH (a:player {name: "Tim Duncan"}) \
WHERE toInteger(right(id(a),3)) == 100 \
RETURN a;
+----------------------------------------------------+
| a |
+----------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------+
nebula> MATCH (n:player) \
WITH n LIMIT toInteger(ceil(1.8)) \
RETURN count(*) AS count;
+-------+
| count |
+-------+
| 2 |
+-------+
Last update:
February 8, 2022