Переглянути джерело

doc(function): update datetime related function doc

ngjaying 4 роки тому
батько
коміт
691ebb7e0a

+ 37 - 2
docs/en_US/sqls/built-in_functions.md

@@ -80,7 +80,7 @@ Aggregate functions perform a calculation on a set of values and return a single
 | -------- | ----------- | ---------------------------------------------- |
 | -------- | ----------- | ---------------------------------------------- |
 | concat   | concat(col1...)  | Concatenates arrays or strings. This function accepts any number of arguments and returns a String or an Array        |
 | concat   | concat(col1...)  | Concatenates arrays or strings. This function accepts any number of arguments and returns a String or an Array        |
 | endswith | endswith(col1, col2) | Returns a Boolean indicating whether the first String argument ends with the second String argument.              |
 | endswith | endswith(col1, col2) | Returns a Boolean indicating whether the first String argument ends with the second String argument.              |
-| format_time| format_time(col1, format) | Format a datetime to string.    |
+| format_time| format_time(col1, format) | Format a datetime to string. The 'col1' will be [casted to datetime type](#cast-to-datetime) if it is bigint, float or string type before formatting. Please check [format patterns](#format_time-patterns) for how to compose the format.  |
 | indexof  | indexof(col1, col2)  | Returns the first index (0-based) of the second argument as a substring in the first argument.                    |
 | indexof  | indexof(col1, col2)  | Returns the first index (0-based) of the second argument as a substring in the first argument.                    |
 | length   | length(col1)| Returns the number of characters in the provided string.                                                                  |
 | length   | length(col1)| Returns the number of characters in the provided string.                                                                  |
 | lower    | lower(col1) | Returns the lowercase version of the given String.                                                                         |
 | lower    | lower(col1) | Returns the lowercase version of the given String.                                                                         |
@@ -98,15 +98,50 @@ Aggregate functions perform a calculation on a set of values and return a single
 | trim      | trim(col1) | Removes all leading and trailing whitespace (tabs and spaces) from the provided String.                                    |
 | trim      | trim(col1) | Removes all leading and trailing whitespace (tabs and spaces) from the provided String.                                    |
 | upper     | upper(col1)| Returns the uppercase version of the given String.|
 | upper     | upper(col1)| Returns the uppercase version of the given String.|
 
 
+### Format_time patterns
+
+A pattern is used to create a format string. Patterns are based on a simple sequence of letters and symbols which is common in many languages like Java etc. The supported symbols in Kuiepr are
+
+| Symbol | Meaning     | Example                                    |
+| -------- | ----------- | ---------------------------------------------- |
+/ G        /  era        / G(AD)    /
+/ Y        /  year/ YYYY(2004), YY(04) /
+/ M   / month / M(1), MM(01), MMM(Jan), MMMM(January) /
+/ d  / day of month / d(2), dd(02) /
+/ E / day of week / EEE(Mon), EEEE(Monday) /
+/ H / hour in 24 hours format / HH(15) /
+/ h / hour in 12 hours format / h(2), hh(03) /
+/ a / AM or PM / a(PM) /
+/ m / minute / m(4), mm(04) /
+/ s / second / s(5), ss(05) /
+/ S / fraction of second / S(.0), SS(.00), SSS(.000) /
+/ z / time zone name / z(MST) /
+/ Z / 4 digits time zone offset / Z(-0700) /
+/ X / time zone offset / X(-07), XX(-0700), XXX(-07:00) /
+
+Examples:
+
+- YYYY-MM-dd T HH:mm:ss -> 2006-01-02 T 15:04:05
+- YYYY/MM/dd HH:mm:ssSSS XXX -> 2006/01/02 15:04:05.000 -07:00
+ 
 ## Conversion Functions
 ## Conversion Functions
 
 
 | Function | Example     | Description                                    |
 | 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(not supported now). |
+| cast     | cast(col,  "bigint") | Converts a value from one data type to another. The supported types includes: bigint, float, string, boolean and datetime. |
 | chr      | chr(col1)   | Returns the ASCII character that corresponds to the given Int argument                                                   |
 | 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" econding type is supported.                             |
 | 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" econding 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.       |
 | 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.       |
 
 
+### Cast to datetime
+
+When casting to datetime type, the supported column type and casting rule are:
+
+1. If column is datatime type, just return the value.
+2. If column is bigint or float type, the number will be treated as the milliseconds elapsed since January 1, 1970 00:00:00 UTC and converted.
+3. If column is string, it will be parsed to datetime with the default format: ``"2006-01-02T15:04:05.000Z07:00"``.
+4. Other types are not supported.
+
 ## Hashing Functions
 ## Hashing Functions
 | Function | Example     | Description                                    |
 | Function | Example     | Description                                    |
 | -------- | ----------- | ---------------------------------------------- |
 | -------- | ----------- | ---------------------------------------------- |

+ 2 - 6
docs/en_US/sqls/data_types.md

@@ -13,7 +13,7 @@ Below is the list of data types supported.
 | 1    | bigint    | The int type.                                                |
 | 1    | bigint    | The int type.                                                |
 | 2    | float     | The float type.                                              |
 | 2    | float     | The float type.                                              |
 | 3    | string    | Text values, comprised of Unicode characters.                |
 | 3    | string    | Text values, comprised of Unicode characters.                |
-| 4    | datetime  | datatime type - *Currently it's NOT supported yet*.          |
+| 4    | datetime  | datatime type.          |
 | 5    | boolean   | The boolean type, the value could be ``true`` or ``false``.  |
 | 5    | boolean   | The boolean type, the value could be ``true`` or ``false``.  |
 | 6    | array     | The array type, can be any types from simple data or struct type (#1 - #5, and #7). |
 | 6    | array     | The array type, can be any types from simple data or struct type (#1 - #5, and #7). |
 | 7    | struct    | The complex type. Set of name/value pairs. Values must be of supported data type. |
 | 7    | struct    | The complex type. Set of name/value pairs. Values must be of supported data type. |
@@ -41,8 +41,4 @@ Array and struct are not supported in any binary operations. The compatibility o
 
 
 ## Type conversions
 ## Type conversions
 
 
-These are the rules governing *data type conversions*:
-
-- ...
-- 
-
+There is a built-in function `cast(col, targetType)` to explicitly convert from one date type to another in runtime. Please refer to [cast](./built-in_functions.md#conversion-functions) for detail.

+ 38 - 2
docs/zh_CN/sqls/built-in_functions.md

@@ -79,7 +79,7 @@ Kuiper 具有许多内置函数,可以对数据执行计算。
 | -------- | ----------- | ---------------------------------------------- |
 | -------- | ----------- | ---------------------------------------------- |
 | concat   | concat(col1...)  | 连接数组或字符串。 此函数接受任意数量的参数并返回 String 或 Array |
 | concat   | concat(col1...)  | 连接数组或字符串。 此函数接受任意数量的参数并返回 String 或 Array |
 | endswith | endswith(col1, col2) | 返回一个布尔值,该布尔值指示第一个 String参数是否以第二个 String 参数结尾。 |
 | endswith | endswith(col1, col2) | 返回一个布尔值,该布尔值指示第一个 String参数是否以第二个 String 参数结尾。 |
-| format_time| format_time(col1, format) | 将日期时间格式化为字符串。 |
+| format_time| format_time(col1, format) | 将日期时间格式化为字符串。其中,若参数 col1 为兼容类型,则在格式化之前 [转换为 datetime 类型](#转换为-datetime-类型)。关于格式字符串,请参考 [时间格式](#时间格式)。|
 | indexof  | indexof(col1, col2)  | 返回第二个参数的第一个索引(从0开始),作为第一个参数中的子字符串。 |
 | indexof  | indexof(col1, col2)  | 返回第二个参数的第一个索引(从0开始),作为第一个参数中的子字符串。 |
 | length   | length(col1)| 返回提供的字符串中的字符数。                                                    |
 | length   | length(col1)| 返回提供的字符串中的字符数。                                                    |
 | lower    | lower(col1) | 返回给定 String 的小写版本。                                                       |
 | lower    | lower(col1) | 返回给定 String 的小写版本。                                                       |
@@ -96,15 +96,51 @@ Kuiper 具有许多内置函数,可以对数据执行计算。
 | trim      | trim(col1) | 从提供的字符串中删除所有前导和尾随空格(制表符和空格)。        |
 | trim      | trim(col1) | 从提供的字符串中删除所有前导和尾随空格(制表符和空格)。        |
 | upper     | upper(col1)| 返回给定 String 的大写版本。 |
 | upper     | upper(col1)| 返回给定 String 的大写版本。 |
 
 
+### 时间格式
+
+时间格式为一些特定符号和字母组成的字符串。Kuiper 里支持的符号如下表所示:
+
+| 符号 | 含义     | 示例                                    |
+| -------- | ----------- | ---------------------------------------------- |
+/ G        /  公元        / G(AD)    /
+/ Y        /  年/ YYYY(2004), YY(04) /
+/ M   / 月 / M(1), MM(01), MMM(Jan), MMMM(January) /
+/ d  / 日期 / d(2), dd(02) /
+/ E / 星期几 / EEE(Mon), EEEE(Monday) /
+/ H / 24小时制的小时 / HH(15) /
+/ h / 12小时制的小时 / h(2), hh(03) /
+/ a / AM 或 PM / a(PM) /
+/ m / 分 / m(4), mm(04) /
+/ s / 秒 / s(5), ss(05) /
+/ S / 秒的分数 / S(.0), SS(.00), SSS(.000) /
+/ z / 时区名 / z(MST) /
+/ Z / 4位数的时区 / Z(-0700) /
+/ X / 时区 / X(-07), XX(-0700), XXX(-07:00) /
+
+示例:
+
+- YYYY-MM-dd T HH:mm:ss -> 2006-01-02 T 15:04:05
+- YYYY/MM/dd HH:mm:ssSSS XXX -> 2006/01/02 15:04:05.000 -07:00
+
+
 ## 转换函数
 ## 转换函数
 
 
 | 函数 | 示例     | 说明                                  |
 | 函数 | 示例     | 说明                                  |
 | -------- | ----------- | ---------------------------------------------- |
 | -------- | ----------- | ---------------------------------------------- |
-| cast     | cast(col,  "bigint") | 将值从一种数据类型转换为另一种数据类型。 支持的类型包括:bigint,float,string,boolean 和 datetime(现在不支持)。 |
+| cast     | cast(col,  "bigint") | 将值从一种数据类型转换为另一种数据类型。 支持的类型包括:bigint,float,string,boolean 和 datetime。 |
 | chr      | chr(col1)   | 返回与给定 Int 参数对应的 ASCII 字符                           |
 | chr      | chr(col1)   | 返回与给定 Int 参数对应的 ASCII 字符                           |
 | encode   | encode(col1, "base64") |使用 encode 函数根据编码方案将负载(可能是非 JSON 数据)编码为其字符串表示形式。目前,只支持"base64"econding 类型。                             |
 | encode   | encode(col1, "base64") |使用 encode 函数根据编码方案将负载(可能是非 JSON 数据)编码为其字符串表示形式。目前,只支持"base64"econding 类型。                             |
 | trunc    | trunc(dec, int)| 将第一个参数截断为第二个参数指定的小数位数。 如果第二个参数小于零,则将其设置为零。 如果第二个参数大于34,则将其设置为34。从结果中去除尾随零。 |
 | trunc    | trunc(dec, int)| 将第一个参数截断为第二个参数指定的小数位数。 如果第二个参数小于零,则将其设置为零。 如果第二个参数大于34,则将其设置为34。从结果中去除尾随零。 |
 
 
+### 转换为 datetime 类型
+
+使用 cast 函数转换到 datatime 类型时,转换规则如下:
+
+1. 如果参数为 datatime 类型,则直接返回原值。
+2. 如果参数为 bigint 或者 float 类型,则其数值会作为自 1970年1月1日0时起至今的毫秒值而转换为 datetime 类型。
+3. 如果参数为 string 类型,则会用默认格式 ``"2006-01-02T15:04:05.000Z07:00"``  将其转换为 datetime类型。
+4. 其他类型的参数均不支持转换。
+
 ## 哈希函数
 ## 哈希函数
 | 函数   | 示例         | 说明         |
 | 函数   | 示例         | 说明         |
 | ------ | ------------ | ------------ |
 | ------ | ------------ | ------------ |

+ 2 - 7
docs/zh_CN/sqls/data_types.md

@@ -13,7 +13,7 @@
 | 1    | bigint   | 整数型                                                       |
 | 1    | bigint   | 整数型                                                       |
 | 2    | float    | 浮点型                                                       |
 | 2    | float    | 浮点型                                                       |
 | 3    | string   | 文本值,由 Unicode 字符组成。                                |
 | 3    | string   | 文本值,由 Unicode 字符组成。                                |
-| 4    | datetime | 日期时间类型-*目前尚不支持*。                                |
+| 4    | datetime | 日期时间类型。                                |
 | 5    | boolean  | 布尔类型,值可以是`true` 或者 `false`。                      |
 | 5    | boolean  | 布尔类型,值可以是`true` 或者 `false`。                      |
 | 6    | array    | 数组类型可以是简单数据或结构类型中的任何类型(#1-#5和#7)。 |
 | 6    | array    | 数组类型可以是简单数据或结构类型中的任何类型(#1-#5和#7)。 |
 | 7    | struct   | 复杂类型。 名称/值对的集合。 值必须是受支持的数据类型。      |
 | 7    | struct   | 复杂类型。 名称/值对的集合。 值必须是受支持的数据类型。      |
@@ -41,9 +41,4 @@
 
 
 ## 类型转换
 ## 类型转换
 
 
-这些是控制*数据类型转换*的规则:
-
-- ...
-- 
-
-
+可使用内置函数 `cast(col, targetType)` 在运行时进行显式类型转换。详情请参考 [cast](./built-in_functions.md#转换函数)。