跳转至

类型转换函数

本文介绍 NebulaGraph 支持的类型转换函数。

toBoolean()

toBoolean() 将字符串转换为布尔。

语法:toBoolean(<value>)

  • 返回类型:bool。

示例:

nebula> UNWIND [true, false, 'true', 'false', NULL] AS b \
        RETURN toBoolean(b) AS b;
+----------+
| b        |
+----------+
| true     |
| false    |
| true     |
| false    |
| __NULL__ |
+----------+

toFloat()

toFloat() 将整数或字符串转换为浮点数。

语法:toFloat(<value>)

  • 返回类型:float。

示例:

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

toString()

toString() 将任意非复合数据类型数据转换为字符串类型。

语法:toString(<value>)

  • 返回类型:string。

示例:

nebula> RETURN toString(9669) AS int2str, toString(null) AS null2str;
+---------+----------+
| int2str | null2str |
+---------+----------+
| "9669"  | __NULL__ |
+---------+----------+

toInteger()

toInteger() 将浮点或字符串转换为整数。

语法:toInteger(<value>)

  • 返回类型:int。

示例:

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

toSet()

toSet() 将列表或集合转换为集合。

语法:toSet(<value>)

  • 返回类型:set。

示例:

nebula> RETURN toSet(list[1,2,3,1,2]) AS list2set;
+-----------+
| list2set  |
+-----------+
| {3, 1, 2} |
+-----------+

hash()

hash() 返回参数的哈希值。其参数可以是数字、字符串、列表、布尔值、NULL 等类型的值,或者计算结果为这些类型的表达式。

hash()函数采用 MurmurHash2 算法,种子(seed)为0xc70f6907UL。用户可以在 MurmurHash2.h 中查看其源代码。

在 Java 中的调用方式如下:

MurmurHash2.hash64("to_be_hashed".getBytes(),"to_be_hashed".getBytes().length, 0xc70f6907)

语法:hash(<string>)

  • 返回类型:int。

示例:

nebula> RETURN hash("abcde");
+--------------------+
| hash("abcde")      |
+--------------------+
| 811036730794841393 |
+--------------------+

nebula> YIELD hash([1,2,3]);
+----------------+
| hash([1,2,3])  |
+----------------+
| 11093822460243 |
+----------------+

nebula> YIELD hash(NULL);
+------------+
| hash(NULL) |
+------------+
| -1         |
+------------+

nebula> YIELD hash(toLower("HELLO NEBULA"));
+-------------------------------+
| hash(toLower("HELLO NEBULA")) |
+-------------------------------+
| -8481157362655072082          |
+-------------------------------+

最后更新: July 6, 2023