Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/data/sql_functions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ temporal:
description: Converts a date string string1 with format string2 (by default 'yyyy-MM-dd') to a date.
- sql: TO_TIMESTAMP_LTZ(numeric[, precision])
table: toTimestampLtz(NUMERIC, PRECISION)
description: Converts an epoch seconds or epoch milliseconds to a TIMESTAMP_LTZ, the valid precision is 0 or 3, the 0 represents TO_TIMESTAMP_LTZ(epochSeconds, 0), the 3 represents TO_TIMESTAMP_LTZ(epochMilliseconds, 3). If no precision is provided, the default precision is 3. If any input is null, the function will return null.
description: Converts a numeric epoch value to a TIMESTAMP_LTZ. The precision parameter (0-9) specifies the unit of the epoch value, where 0 represents seconds, 3 represents milliseconds, 6 represents microseconds, and 9 represents nanoseconds. Other values between 0 and 9 are also supported, representing 10^(-precision) seconds. If no precision is provided, the default precision is 3 (milliseconds). If any input is null, the function will return null.
- sql: TO_TIMESTAMP_LTZ(string1[, string2[, string3]])
table: toTimestampLtz(STRING1[, STRING2[, STRING3]])
description: Converts a timestamp string string1 with format string2 (by default 'yyyy-MM-dd HH:mm:ss.SSS') in time zone string3 (by default 'UTC') to a TIMESTAMP_LTZ. If any input is null, the function will return null.
Expand Down
2 changes: 1 addition & 1 deletion docs/data/sql_functions_zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ temporal:
description: 将格式为 string2(默认为 'yyyy-MM-dd')的字符串 string1 转换为日期。
- sql: TO_TIMESTAMP_LTZ(numeric[, precision])
table: toTimestampLtz(NUMERIC, PRECISION)
description: Converts an epoch seconds or epoch milliseconds to a TIMESTAMP_LTZ, the valid precision is 0 or 3, the 0 represents TO_TIMESTAMP_LTZ(epochSeconds, 0), the 3 represents TO_TIMESTAMP_LTZ(epochMilliseconds, 3). If no precision is provided, the default precision is 3. If any input is null, the function will return null.
description: Converts a numeric epoch value to a TIMESTAMP_LTZ. The precision parameter (0-9) specifies the unit of the epoch value, where 0 represents seconds, 3 represents milliseconds, 6 represents microseconds, and 9 represents nanoseconds. Other values between 0 and 9 are also supported, representing 10^(-precision) seconds. If no precision is provided, the default precision is 3 (milliseconds). If any input is null, the function will return null.
- sql: TO_TIMESTAMP_LTZ(string1[, string2[, string3]])
table: toTimestampLtz(STRING1[, STRING2[, STRING3]])
description: Converts a timestamp string string1 with format string2 (by default 'yyyy-MM-dd HH:mm:ss.SSS') in time zone string3 (by default 'UTC') to a TIMESTAMP_LTZ. If any input is null, the function will return null.
Expand Down
36 changes: 18 additions & 18 deletions flink-table/flink-sql-client/src/test/resources/sql/select.q
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ FROM (VALUES
(1, TIMESTAMP '2021-04-13 20:12:11', TIMESTAMP '2021-04-13 20:12:11.123', TIMESTAMP '2021-04-13 20:12:11.123456789'),
(2, TIMESTAMP '2021-04-13 21:12:11', TIMESTAMP '2021-04-13 21:12:11.001', TIMESTAMP '2021-04-13 21:12:11.1'))
as T(id, ts0, ts3, ts9);
+----+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| op | time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| +I | 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 08:00:01.000 | 1970-01-01 08:00:00.001 | 2021-04-13 20:12:11.123456789 |
| +I | 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 08:00:01.000 | 1970-01-01 08:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
+----+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| op | time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| +I | 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 08:00:01 | 1970-01-01 08:00:00.001 | 2021-04-13 20:12:11.123456789 |
| +I | 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 08:00:01 | 1970-01-01 08:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
Received a total of 2 rows
!ok

Expand All @@ -106,12 +106,12 @@ FROM (VALUES
(1, TIMESTAMP '2021-04-13 20:12:11', TIMESTAMP '2021-04-13 20:12:11.123', TIMESTAMP '2021-04-13 20:12:11.123456789'),
(2, TIMESTAMP '2021-04-13 21:12:11', TIMESTAMP '2021-04-13 21:12:11.001', TIMESTAMP '2021-04-13 21:12:11.1'))
as T(id, ts0, ts3, ts9);
+----+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| op | time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| +I | 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 00:00:01.000 | 1970-01-01 00:00:00.001 | 2021-04-13 20:12:11.123456789 |
| +I | 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 00:00:01.000 | 1970-01-01 00:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
+----+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| op | time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| +I | 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 00:00:01 | 1970-01-01 00:00:00.001 | 2021-04-13 20:12:11.123456789 |
| +I | 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 00:00:01 | 1970-01-01 00:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
Received a total of 2 rows
!ok

Expand Down Expand Up @@ -250,12 +250,12 @@ FROM (VALUES
(1, TIMESTAMP '2021-04-13 20:12:11', TIMESTAMP '2021-04-13 20:12:11.123', TIMESTAMP '2021-04-13 20:12:11.123456789'),
(2, TIMESTAMP '2021-04-13 21:12:11', TIMESTAMP '2021-04-13 21:12:11.001', TIMESTAMP '2021-04-13 21:12:11.1'))
as T(id, ts0, ts3, ts9);
+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 00:00:01.000 | 1970-01-01 00:00:00.001 | 2021-04-13 20:12:11.123456789 |
| 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 00:00:01.000 | 1970-01-01 00:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 00:00:01 | 1970-01-01 00:00:00.001 | 2021-04-13 20:12:11.123456789 |
| 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 00:00:01 | 1970-01-01 00:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
2 rows in set
!ok

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ FROM (VALUES
(1, TIMESTAMP '2021-04-13 20:12:11', TIMESTAMP '2021-04-13 20:12:11.123', TIMESTAMP '2021-04-13 20:12:11.123456789'),
(2, TIMESTAMP '2021-04-13 21:12:11', TIMESTAMP '2021-04-13 21:12:11.001', TIMESTAMP '2021-04-13 21:12:11.1'))
as T(id, ts0, ts3, ts9);
+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
| 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 00:00:01.000 | 1970-01-01 00:00:00.001 | 2021-04-13 20:12:11.123456789 |
| 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 00:00:01.000 | 1970-01-01 00:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----------+---------------------+-------------------------+-------------------------------+-------------------------+-------------------------+-------------------------------+
+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| time0 | ts0 | ts3 | ts9 | ts_ltz0 | ts_ltz3 | ts_ltz9 |
+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
| 20:12:11 | 2021-04-13 20:12:11 | 2021-04-13 20:12:11.123 | 2021-04-13 20:12:11.123456789 | 1970-01-01 00:00:01 | 1970-01-01 00:00:00.001 | 2021-04-13 20:12:11.123456789 |
| 20:12:11 | 2021-04-13 21:12:11 | 2021-04-13 21:12:11.001 | 2021-04-13 21:12:11.100000000 | 1970-01-01 00:00:01 | 1970-01-01 00:00:00.001 | 2021-04-13 21:12:11.100000000 |
+----------+---------------------+-------------------------+-------------------------------+---------------------+-------------------------+-------------------------------+
2 rows in set
!ok

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import org.apache.flink.table.types.DataType;
import org.apache.flink.table.types.inference.CallContext;
import org.apache.flink.table.types.logical.DecimalType;
import org.apache.flink.table.types.logical.LocalZonedTimestampType;
import org.apache.flink.table.types.logical.LogicalType;
import org.apache.flink.table.types.logical.LogicalTypeFamily;
import org.apache.flink.table.types.logical.LogicalTypeRoot;
import org.apache.flink.table.types.utils.ValueDataTypeConverter;
import org.apache.flink.table.utils.DateTimeUtils;
import org.apache.flink.table.utils.EncodingUtils;
import org.apache.flink.types.ColumnList;
import org.apache.flink.util.Preconditions;
Expand Down Expand Up @@ -283,11 +285,10 @@ public String asSerializableString(SqlFactory sqlFactory) {
localDateTime.toLocalTime().format(DateTimeFormatter.ISO_LOCAL_TIME));
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
final Instant instant = getValueAs(Instant.class).get();
if (instant.getNano() % 1_000_000 != 0) {
throw new TableException(
"Maximum precision for TIMESTAMP_WITH_LOCAL_TIME_ZONE literals is '3'");
}
return String.format("TO_TIMESTAMP_LTZ(%d, %d)", instant.toEpochMilli(), 3);
final int precision =
((LocalZonedTimestampType) dataType.getLogicalType()).getPrecision();
long epochValue = DateTimeUtils.toEpochValue(instant, precision);
return String.format("TO_TIMESTAMP_LTZ(%d, %d)", epochValue, precision);
case INTERVAL_YEAR_MONTH:
final Period period = getValueAs(Period.class).get().normalized();
return String.format(
Expand Down
Loading