Explorar o código

doc(functions): add doc and meta for new conversion functions

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang %!s(int64=2) %!d(string=hai) anos
pai
achega
d8a65dd700

+ 1 - 1
docs/directory.json

@@ -474,7 +474,7 @@
 							"path": "extension/native/overview"
 						},
 						{
-							"title": "原插件开发",
+							"title": "原插件开发",
 							"path": "extension/native/develop/overview"
 						},
 						{

+ 19 - 6
docs/en_US/sqls/built-in_functions.md

@@ -140,9 +140,10 @@ Examples:
 
 | Function         | Example                          | Description                                                                                                                                                                                                                                                                                       |
 |------------------|----------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| cast             | cast(col,  "bigint")             | Converts a value from one data type to another. The supported types includes: bigint, float, string, boolean and datetime.                                                                                                                                                                        |
+| cast             | cast(col,  "bigint")             | Converts a value from one data type to another. The supported types includes: bigint, float, string, boolean, bytea and datetime.                                                                                                                                                                 |
 | chr              | chr(col1)                        | Returns the ASCII character that corresponds to the given Int argument                                                                                                                                                                                                                            |
 | encode           | encode(col1, "base64")           | Use the encode function to encode the payload, which potentially might be non-JSON data, into its string representation based on the encoding scheme. Currently, only "base64" encoding type is supported.                                                                                        |
+| decode           | decode(col1, "base64")           | Decode the input string with specified decoding method. Currently, only "base64" encoding type is supported.                                                                                                                                                                                      |
 | trunc            | trunc(dec, int)                  | Truncates the first argument to the number of Decimal places specified by the second argument. If the second argument is less than zero, it is set to zero. If the second argument is greater than 34, it is set to 34. Trailing zeroes are stripped from the result.                             |
 | object_construct | object_construct(key1, col, ...) | Return a struct type object/map constructed by the arguments. The arguments are series of key value pairs, thus the arguments count must be an odd number. The key must a string and the value can be of any type. If the value is null, the key/value pair will not present in the final object. |
 
@@ -163,15 +164,27 @@ When casting to datetime type, the supported column type and casting rule are:
 | sha256   | sha256(col1) | Hashed value of the argument |
 | sha384   | sha384(col1) | Hashed value of the argument |
 | sha512   | sha512(col1) | Hashed value of the argument |
+
 ## JSON Functions
-| Function              | Example                               | Description                                                                                |
-|-----------------------|---------------------------------------|--------------------------------------------------------------------------------------------|
-| json_path_exists      | json_path_exists(col1, "$.name")      | Checks whether JSON path returns any item for the specified JSON value. Return bool value. |
-| json_path_query       | json_path_query(col1, "$.name")       | Gets all items returned by JSON path for the specified JSON value.                         |
-| json_path_query_first | json_path_query_first(col1, "$.name") | Gets the first item returned by JSON path for the specified JSON value.                    |
+| Function              | Example                               | Description                                                                                                                  |
+|-----------------------|---------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
+| to_json               | to_json(col1)                         | Converts a value to a string containing the JSON representation of the value. If the input is NULL, the result is also NULL. |
+| parse_json            | parse_json(col1)                      | Converts a JSON string to a value. If the input is NULL, the result is also NULL.                                            |
+| json_path_exists      | json_path_exists(col1, "$.name")      | Checks whether JSON path returns any item for the specified JSON value. Return bool value.                                   |
+| json_path_query       | json_path_query(col1, "$.name")       | Gets all items returned by JSON path for the specified JSON value.                                                           |
+| json_path_query_first | json_path_query_first(col1, "$.name") | Gets the first item returned by JSON path for the specified JSON value.                                                      |
 
 **Please refer to [json path functions](./json_expr.md#json-path-functions) for how to compose a json path.**  
 
+## Compression/Decompression Functions
+
+| Function   | Example                   | Description                                                           |
+|------------|---------------------------|-----------------------------------------------------------------------|
+| compress   | compress(input, "zlib")   | Compress the input string or binary value with a compression method   |
+| decompress | decompress(input, "zlib") | Decompress the input string or binary value with a compression method |
+
+Currently, only 'zlib' method is supported.
+
 ## Analytic Functions
 
 Analytic functions always use state to do analytic jobs. In streaming processing, analytic functions are evaluated first so that they are not affected by predicates in WHERE clause.

+ 18 - 6
docs/zh_CN/sqls/built-in_functions.md

@@ -134,9 +134,10 @@ eKuiper 具有许多内置函数,可以对数据执行计算。
 
 | 函数               | 示例                               | 说明                                                                                      |
 |------------------|----------------------------------|-----------------------------------------------------------------------------------------|
-| cast             | cast(col,  "bigint")             | 将值从一种数据类型转换为另一种数据类型。 支持的类型包括:bigint,float,string,boolean 和 datetime。                    |
+| cast             | cast(col,  "bigint")             | 将值从一种数据类型转换为另一种数据类型。 支持的类型包括:bigint,float,string,boolean,bytea 和 datetime。              |
 | chr              | chr(col1)                        | 返回与给定 Int 参数对应的 ASCII 字符                                                                |
 | encode           | encode(col1, "base64")           | 使用 encode 函数根据编码方案将负载(可能是非 JSON 数据)编码为其字符串表示形式。目前,只支持"base64" 编码类型。                     |
+| decode           | decode(col1, "base64")           | 使解码输入字符串。目前,只支持"base64" 类型。                                                             |
 | trunc            | trunc(dec, int)                  | 将第一个参数截断为第二个参数指定的小数位数。 如果第二个参数小于零,则将其设置为零。 如果第二个参数大于34,则将其设置为34。从结果中去除尾随零。              |
 | object_construct | object_construct(key1, col, ...) | 返回由参数构建的 oject/map 。参数为一系列的键值对,因此必须为偶数个。键必须为 string 类型,值可以为任意类型。如果值为空,则该键值对不会出现在最终的对象中。 |
 
@@ -159,14 +160,25 @@ eKuiper 具有许多内置函数,可以对数据执行计算。
 | sha512 | sha512(col1) | 参数的哈希值 |
 
 ## JSON 函数
-| 函数                    | 示例                                    | 说明                                  |
-|-----------------------|---------------------------------------|-------------------------------------|
-| json_path_exists      | json_path_exists(col1, "$.name")      | 检查 JSON 路径是否返回指定JSON 值的任何项目。 返回布尔值。 |
-| json_path_query       | json_path_query(col1, "$.name")       | 获取 JSON 路径返回的指定 JSON值的所有项目。         |
-| json_path_query_first | json_path_query_first(col1, "$.name") | 获取 JSON 路径返回的指定 JSON值的第一项。          |
+| 函数                    | 示例                                    | 说明                                             |
+|-----------------------|---------------------------------------|------------------------------------------------|
+| to_json               | to_json(col1)                         | 将输入值转换为包含该值 JSON 表示的字符串。如果输入为 NULL,则结果也为 NULL。 |
+| parse_json            | parse_json(col1)                      | 将输入的 JSON 字符串转换为值。如果输入为 NULL,则结果也为 NULL。       |
+| json_path_exists      | json_path_exists(col1, "$.name")      | 检查 JSON 路径是否返回指定JSON 值的任何项目。 返回布尔值。            |
+| json_path_query       | json_path_query(col1, "$.name")       | 获取 JSON 路径返回的指定 JSON值的所有项目。                    |
+| json_path_query_first | json_path_query_first(col1, "$.name") | 获取 JSON 路径返回的指定 JSON值的第一项。                     |
 
 **请参阅 [json 路径函数](./json_expr.md#Json-路径函数) 了解如何编写json路径。**
 
+## 压缩/解压函数
+
+| 函数         | 示例                        | 说明              |
+|------------|---------------------------|-----------------|
+| compress   | compress(input, "zlib")   | 压缩输入的字符串或二进制值。  |
+| decompress | decompress(input, "zlib") | 解压缩输入的字符串或二进制值。 |
+
+目前只支持 'zlib' 压缩算法。
+
 ## 分析函数
 
 分析函数会保持状态来做分析工作。在流式处理规则中,分析函数会首先被执行,这样它们就不会受到 WHERE 子句的影响而必不更新状态。

+ 236 - 1
etc/functions/internal.json

@@ -2632,6 +2632,59 @@
 			}
 		}
 	}, {
+		"name": "decode",
+		"example": "decode(col1, \"base64\")",
+		"hint": {
+			"en_US": "Decode the input string with specified decoding method. Currently, only \"base64\" encoding type is supported.",
+			"zh_CN": "解码输入字符串。目前,只支持\"base64\" 类型。"
+		},
+		"args": [
+			{
+				"name": "field",
+				"optional": false,
+				"control": "field",
+				"type": "string",
+				"hint": {
+					"en_US": "The field to encode.",
+					"zh_CN": "需要解码的字段名"
+				},
+				"label": {
+					"en_US": "Field",
+					"zh_CN": "字段"
+				}
+			},
+			{
+				"name": "encodingType",
+				"optional": false,
+				"control": "select",
+				"type": "string",
+				"values": ["base64"],
+				"hint": {
+					"en_US": "The type of encoding",
+					"zh_CN": "编码方案"
+				},
+				"label": {
+					"en_US": "Encoding Type",
+					"zh_CN": "编码方案"
+				}
+			}
+		],
+		"return": {
+			"type": "bytea",
+			"hint": {
+				"en_US": "Decoded Value",
+				"zh_CN": "解码值"
+			}
+		},
+		"node": {
+			"category": "function",
+			"icon": "iconPath",
+			"label": {
+				"en_US": "Decode",
+				"zh_CN": "Decode"
+			}
+		}
+	}, {
 		"name": "trunc",
 		"example": "trunc(dec, int)",
 		"hint": {
@@ -2870,7 +2923,189 @@
 			"icon": "iconPath",
 			"label": {
 				"en_US": "SHA 512",
-				"zh_CN": "参数的 SHA512 哈希值"
+				"zh_CN": "SHA 512"
+			}
+		}
+	}, {
+		"name": "compress",
+		"example": "compress(input, \"zlib\")",
+		"hint": {
+			"en_US": "Compress the input string or binary value with a compression method",
+			"zh_CN": "压缩输入的字符串或二进制值。"
+		},
+		"args": [
+			{
+				"name": "field",
+				"optional": false,
+				"control": "field",
+				"type": "string",
+				"hint": {
+					"en_US": "The field to be compressed.",
+					"zh_CN": "需要压缩的字段名"
+				},
+				"label": {
+					"en_US": "Field",
+					"zh_CN": "字段"
+				}
+			},
+			{
+				"name": "method",
+				"optional": false,
+				"control": "select",
+				"type": "string",
+				"values": ["zlib"],
+				"hint": {
+					"en_US": "The type of compression",
+					"zh_CN": "压缩方法"
+				},
+				"label": {
+					"en_US": "Compression Method",
+					"zh_CN": "压缩方法"
+				}
+			}
+		],
+		"return": {
+			"type": "bytea",
+			"hint": {
+				"en_US": "Compressed Value",
+				"zh_CN": "压缩值"
+			}
+		},
+		"node": {
+			"category": "function",
+			"icon": "iconPath",
+			"label": {
+				"en_US": "Compress",
+				"zh_CN": "压缩"
+			}
+		}
+	}, {
+		"name": "decompress",
+		"example": "decompress(input, \"zlib\")",
+		"hint": {
+			"en_US": "Decompress the input string or binary value with a compression method",
+			"zh_CN": "解压缩输入的字符串或二进制值。"
+		},
+		"args": [
+			{
+				"name": "field",
+				"optional": false,
+				"control": "field",
+				"type": "string",
+				"hint": {
+					"en_US": "The field to be decompressed.",
+					"zh_CN": "需要解压缩的字段名"
+				},
+				"label": {
+					"en_US": "Field",
+					"zh_CN": "字段"
+				}
+			},
+			{
+				"name": "method",
+				"optional": false,
+				"control": "select",
+				"type": "string",
+				"values": ["zlib"],
+				"hint": {
+					"en_US": "The type of decompression",
+					"zh_CN": "解压缩方法"
+				},
+				"label": {
+					"en_US": "Decompression Method",
+					"zh_CN": "解压缩方法"
+				}
+			}
+		],
+		"return": {
+			"type": "bytea",
+			"hint": {
+				"en_US": "Decompressed Value",
+				"zh_CN": "解压缩值"
+			}
+		},
+		"node": {
+			"category": "function",
+			"icon": "iconPath",
+			"label": {
+				"en_US": "Decompress",
+				"zh_CN": "解压缩"
+			}
+		}
+	}, {
+		"name": "to_json",
+		"example": "to_json(input)",
+		"hint": {
+			"en_US": "Converts a value to a string containing the JSON representation of the value. If the input is NULL, the result is also NULL.",
+			"zh_CN": "将输入值转换为包含该值 JSON 表示的字符串。如果输入为 NULL,则结果也为 NULL。"
+		},
+		"args": [
+			{
+				"name": "field",
+				"optional": false,
+				"control": "field",
+				"type": "any",
+				"hint": {
+					"en_US": "The field to be converted to JSON string.",
+					"zh_CN": "需要转换成 JSON 字符串的输入字段"
+				},
+				"label": {
+					"en_US": "Field",
+					"zh_CN": "字段"
+				}
+			}
+		],
+		"return": {
+			"type": "string",
+			"hint": {
+				"en_US": "the converted JSON string",
+				"zh_CN": "转换后的 JSON 字符串"
+			}
+		},
+		"node": {
+			"category": "function",
+			"icon": "iconPath",
+			"label": {
+				"en_US": "To JSON",
+				"zh_CN": "转换为 JSON"
+			}
+		}
+	}, {
+		"name": "parse_json",
+		"example": "parse_json(input)",
+		"hint": {
+			"en_US": "Converts a JSON string to a value. If the input is NULL, the result is also NULL.",
+			"zh_CN": "将输入的 JSON 字符串转换为值。如果输入为 NULL,则结果也为 NULL。"
+		},
+		"args": [
+			{
+				"name": "field",
+				"optional": false,
+				"control": "field",
+				"type": "string",
+				"hint": {
+					"en_US": "The input JSON string.",
+					"zh_CN": "输入的 JSON 字符串"
+				},
+				"label": {
+					"en_US": "Field",
+					"zh_CN": "字段"
+				}
+			}
+		],
+		"return": {
+			"type": "any",
+			"hint": {
+				"en_US": "the unmarshalled value",
+				"zh_CN": "反序列 JSON 字符串的值"
+			}
+		},
+		"node": {
+			"category": "function",
+			"icon": "iconPath",
+			"label": {
+				"en_US": "Parse JSON",
+				"zh_CN": "JSON 解析"
 			}
 		}
 	}, {