跳转至

类型转换Graph

类型转换是指将表达式的类型转换为另一个类型。

遗留兼容问题Graph

nGQL 1.0使用C语言风格的类型转换(显示或隐式):(type_name)expression。例如YIELD (int)(TRUE),结果为1。但是对于不熟悉C语言的用户来说,很容易出错。

nGQL 2.0使用openCypher的方式进行类型强制转换。

类型强制转换函数Graph

函数 说明
toBoolean() 将字符串转换为布尔。
toFloat() 将整数或字符串转换为浮点数。
toInteger() 将浮点或字符串转换为整数。
type() 返回字符串格式的关系类型。

示例Graph

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     |
+-------+

最后更新: October 27, 2021
Back to top