Add function fromUnixTimestamp64Second

This commit is contained in:
Robert Schulze 2024-12-11 16:30:36 +00:00
parent 2bc96dc2da
commit c927b4e617
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A
7 changed files with 131 additions and 66 deletions

View File

@ -7189,45 +7189,6 @@ Result:
└───────────────────────┘
```
## toUnixTimestamp64Milli
Converts a `DateTime64` to a `Int64` value with fixed millisecond precision. The input value is scaled up or down appropriately depending on its precision.
:::note
The output value is a timestamp in UTC, not in the timezone of `DateTime64`.
:::
**Syntax**
```sql
toUnixTimestamp64Milli(value)
```
**Arguments**
- `value` — DateTime64 value with any precision. [DateTime64](../data-types/datetime64.md).
**Returned value**
- `value` converted to the `Int64` data type. [Int64](../data-types/int-uint.md).
**Example**
Query:
```sql
WITH toDateTime64('2009-02-13 23:31:31.011', 3, 'UTC') AS dt64
SELECT toUnixTimestamp64Milli(dt64);
```
Result:
```response
┌─toUnixTimestamp64Milli(dt64)─┐
│ 1234567891011 │
└──────────────────────────────┘
```
## toUnixTimestamp64Second
Converts a `DateTime64` to a `Int64` value with fixed second precision. The input value is scaled up or down appropriately depending on its precision.
@ -7267,6 +7228,45 @@ Result:
└───────────────────────────────┘
```
## toUnixTimestamp64Milli
Converts a `DateTime64` to a `Int64` value with fixed millisecond precision. The input value is scaled up or down appropriately depending on its precision.
:::note
The output value is a timestamp in UTC, not in the timezone of `DateTime64`.
:::
**Syntax**
```sql
toUnixTimestamp64Milli(value)
```
**Arguments**
- `value` — DateTime64 value with any precision. [DateTime64](../data-types/datetime64.md).
**Returned value**
- `value` converted to the `Int64` data type. [Int64](../data-types/int-uint.md).
**Example**
Query:
```sql
WITH toDateTime64('2009-02-13 23:31:31.011', 3, 'UTC') AS dt64
SELECT toUnixTimestamp64Milli(dt64);
```
Result:
```response
┌─toUnixTimestamp64Milli(dt64)─┐
│ 1234567891011 │
└──────────────────────────────┘
```
## toUnixTimestamp64Micro
Converts a `DateTime64` to a `Int64` value with fixed microsecond precision. The input value is scaled up or down appropriately depending on its precision.
@ -7345,6 +7345,48 @@ Result:
└─────────────────────────────┘
```
## fromUnixTimestamp64Second
Converts an `Int64` to a `DateTime64` value with fixed second precision and optional timezone. The input value is scaled up or down appropriately depending on its precision.
:::note
Please note that input value is treated as a UTC timestamp, not timestamp at the given (or implicit) timezone.
:::
**Syntax**
``` sql
fromUnixTimestamp64Second(value[, timezone])
```
**Arguments**
- `value` — value with any precision. [Int64](../data-types/int-uint.md).
- `timezone` — (optional) timezone name of the result. [String](../data-types/string.md).
**Returned value**
- `value` converted to DateTime64 with precision `0`. [DateTime64](../data-types/datetime64.md).
**Example**
Query:
``` sql
WITH CAST(1733935988, 'Int64') AS i64
SELECT
fromUnixTimestamp64Second(i64, 'UTC') AS x,
toTypeName(x);
```
Result:
```response
┌───────────────────x─┬─toTypeName(x)────────┐
│ 2024-12-11 16:53:08 │ DateTime64(0, 'UTC') │
└─────────────────────┴──────────────────────┘
```
## fromUnixTimestamp64Milli
Converts an `Int64` to a `DateTime64` value with fixed millisecond precision and optional timezone. The input value is scaled up or down appropriately depending on its precision.
@ -7373,7 +7415,7 @@ fromUnixTimestamp64Milli(value[, timezone])
Query:
``` sql
WITH CAST(1234567891011, 'Int64') AS i64
WITH CAST(1733935988123, 'Int64') AS i64
SELECT
fromUnixTimestamp64Milli(i64, 'UTC') AS x,
toTypeName(x);
@ -7383,7 +7425,7 @@ Result:
```response
┌───────────────────────x─┬─toTypeName(x)────────┐
│ 2009-02-13 23:31:31.011 │ DateTime64(3, 'UTC') │
│ 2024-12-11 16:53:08.123 │ DateTime64(3, 'UTC') │
└─────────────────────────┴──────────────────────┘
```
@ -7415,7 +7457,7 @@ fromUnixTimestamp64Micro(value[, timezone])
Query:
``` sql
WITH CAST(1234567891011, 'Int64') AS i64
WITH CAST(1733935988123456, 'Int64') AS i64
SELECT
fromUnixTimestamp64Micro(i64, 'UTC') AS x,
toTypeName(x);
@ -7425,7 +7467,7 @@ Result:
```response
┌──────────────────────────x─┬─toTypeName(x)────────┐
1970-01-15 06:56:07.891011 │ DateTime64(6, 'UTC') │
2024-12-11 16:53:08.123456 │ DateTime64(6, 'UTC') │
└────────────────────────────┴──────────────────────┘
```
@ -7457,7 +7499,7 @@ fromUnixTimestamp64Nano(value[, timezone])
Query:
``` sql
WITH CAST(1234567891011, 'Int64') AS i64
WITH CAST(1733935988123456789, 'Int64') AS i64
SELECT
fromUnixTimestamp64Nano(i64, 'UTC') AS x,
toTypeName(x);
@ -7467,7 +7509,7 @@ Result:
```response
┌─────────────────────────────x─┬─toTypeName(x)────────┐
1970-01-01 00:20:34.567891011 │ DateTime64(9, 'UTC') │
2024-12-11 16:53:08.123456789 │ DateTime64(9, 'UTC') │
└───────────────────────────────┴──────────────────────┘
```

View File

@ -0,0 +1,13 @@
#include <Functions/FunctionUnixTimestamp64.h>
#include <Functions/FunctionFactory.h>
namespace DB
{
REGISTER_FUNCTION(FromUnixTimestamp64)
{
factory.registerFunction("fromUnixTimestamp64Second",
[](ContextPtr context){ return std::make_shared<FunctionFromUnixTimestamp64>(0, "fromUnixTimestamp64Second", context); });
}
}

View File

@ -1,9 +1,9 @@
const column
UTC 1234567891011 2009-02-13 23:31:31.011 1970-01-15 06:56:07.891011 1970-01-01 00:20:34.567891011 DateTime64(9, \'UTC\')
Asia/Makassar 1234567891011 2009-02-14 07:31:31.011 1970-01-15 14:56:07.891011 1970-01-01 08:20:34.567891011 DateTime64(9, \'Asia/Makassar\')
UTC 1234567891011 2299-12-31 23:16:51 2009-02-13 23:31:31.011 1970-01-15 06:56:07.891011 1970-01-01 00:20:34.567891011 DateTime64(9, \'UTC\')
Asia/Makassar 1234567891011 2299-12-31 23:16:51 2009-02-14 07:31:31.011 1970-01-15 14:56:07.891011 1970-01-01 08:20:34.567891011 DateTime64(9, \'Asia/Makassar\')
non-const column
1234567891011 2009-02-13 23:31:31.011 1970-01-15 06:56:07.891011 1970-01-01 00:20:34.567891011
1234567891011 2299-12-31 23:16:51 2009-02-13 23:31:31.011 1970-01-15 06:56:07.891011 1970-01-01 00:20:34.567891011
upper range bound
10413688942 2299-12-30 19:22:22.123 2299-12-30 19:22:22.123456 1900-01-01 00:00:00.413905173
10413688942 2299-12-30 19:22:22 2299-12-30 19:22:22.123 2299-12-30 19:22:22.123456 1900-01-01 00:00:00.413905173
lower range bound
-2208985199 1900-01-01 01:00:00.877 1900-01-01 01:00:00.876544 1900-01-01 01:00:00.876543211
-2208985199 1900-01-01 00:00:00 1900-01-01 01:00:00.877 1900-01-01 01:00:00.876544 1900-01-01 01:00:00.876543211

View File

@ -1,12 +1,15 @@
-- -- Error cases
SELECT fromUnixTimestamp64Second(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT fromUnixTimestamp64Milli(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT fromUnixTimestamp64Micro(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT fromUnixTimestamp64Nano(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT fromUnixTimestamp64Second('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Milli('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Micro('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Nano('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Second('abc', 123); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Milli('abc', 123); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Micro('abc', 123); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT fromUnixTimestamp64Nano('abc', 123); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
@ -18,6 +21,7 @@ WITH
SELECT
tz,
i64,
fromUnixTimestamp64Second(i64, tz),
fromUnixTimestamp64Milli(i64, tz),
fromUnixTimestamp64Micro(i64, tz),
fromUnixTimestamp64Nano(i64, tz) as dt64,
@ -29,6 +33,7 @@ WITH
SELECT
tz,
i64,
fromUnixTimestamp64Second(i64, tz),
fromUnixTimestamp64Milli(i64, tz),
fromUnixTimestamp64Micro(i64, tz),
fromUnixTimestamp64Nano(i64, tz) as dt64,
@ -40,6 +45,7 @@ WITH
'UTC' AS tz
SELECT
i64,
fromUnixTimestamp64Second(i64, tz),
fromUnixTimestamp64Milli(i64, tz),
fromUnixTimestamp64Micro(i64, tz),
fromUnixTimestamp64Nano(i64, tz) as dt64;
@ -47,12 +53,14 @@ SELECT
SELECT 'upper range bound';
WITH
10413688942 AS timestamp,
CAST(10413688942 AS Int64) AS second,
CAST(10413688942123 AS Int64) AS milli,
CAST(10413688942123456 AS Int64) AS micro,
CAST(10413688942123456789 AS Int64) AS nano,
'UTC' AS tz
SELECT
timestamp,
fromUnixTimestamp64Second(second, tz),
fromUnixTimestamp64Milli(milli, tz),
fromUnixTimestamp64Micro(micro, tz),
fromUnixTimestamp64Nano(nano, tz);
@ -60,12 +68,14 @@ SELECT
SELECT 'lower range bound';
WITH
-2208985199 AS timestamp,
CAST(-2208985199 AS Int64) AS second,
CAST(-2208985199123 AS Int64) AS milli,
CAST(-2208985199123456 AS Int64) AS micro,
CAST(-2208985199123456789 AS Int64) AS nano,
'UTC' AS tz
SELECT
timestamp,
fromUnixTimestamp64Second(milli, tz),
fromUnixTimestamp64Milli(milli, tz),
fromUnixTimestamp64Micro(micro, tz),
fromUnixTimestamp64Nano(nano, tz);

View File

@ -1,8 +1,8 @@
const column
2019-09-16 19:20:12.345 1568650812345 1568650812345000 1568650812345000000 1568650812
2019-09-16 19:20:12.345678 1568650812345 1568650812345678 1568650812345678000 1568650812
2019-09-16 19:20:12.345678910 1568650812345 1568650812345678 1568650812345678910 1568650812
2019-09-16 19:20:12.345 1568650812 1568650812345 1568650812345000 1568650812345000000
2019-09-16 19:20:12.345678 1568650812 1568650812345 1568650812345678 1568650812345678000
2019-09-16 19:20:12.345678910 1568650812 1568650812345 1568650812345678 1568650812345678910
non-const column
2019-09-16 19:20:12.345 1568650812345 1568650812345000 1568650812345000000 1568650812
2019-09-16 19:20:12.345678 1568650812345 1568650812345678 1568650812345678000 1568650812
2019-09-16 19:20:12.345678910 1568650812345 1568650812345678 1568650812345678910 1568650812
2019-09-16 19:20:12.345 1568650812 1568650812345 1568650812345000 1568650812345000000
2019-09-16 19:20:12.345678 1568650812 1568650812345 1568650812345678 1568650812345678000
2019-09-16 19:20:12.345678910 1568650812 1568650812345 1568650812345678 1568650812345678910

View File

@ -1,36 +1,35 @@
-- Error cases
SELECT toUnixTimestamp64Second(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Milli(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Micro(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Nano(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Second(); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Second('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT toUnixTimestamp64Milli('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT toUnixTimestamp64Micro('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT toUnixTimestamp64Nano('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT toUnixTimestamp64Second('abc'); -- {serverError ILLEGAL_TYPE_OF_ARGUMENT}
SELECT toUnixTimestamp64Second('abc', 123); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Milli('abc', 123); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Micro('abc', 123); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Nano('abc', 123); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT toUnixTimestamp64Second('abc', 123); -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
SELECT 'const column';
WITH toDateTime64('2019-09-16 19:20:12.345678910', 3, 'Asia/Istanbul') AS dt64
SELECT dt64, toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64), toUnixTimestamp64Second(dt64);
SELECT dt64, toUnixTimestamp64Second(dt64), toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64);
WITH toDateTime64('2019-09-16 19:20:12.345678910', 6, 'Asia/Istanbul') AS dt64
SELECT dt64, toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64), toUnixTimestamp64Second(dt64);
SELECT dt64, toUnixTimestamp64Second(dt64), toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64);
WITH toDateTime64('2019-09-16 19:20:12.345678910', 9, 'Asia/Istanbul') AS dt64
SELECT dt64, toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64), toUnixTimestamp64Second(dt64);
SELECT dt64, toUnixTimestamp64Second(dt64), toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64);
SELECT 'non-const column';
WITH toDateTime64('2019-09-16 19:20:12.345678910', 3, 'Asia/Istanbul') AS x
SELECT materialize(x) as dt64, toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64), toUnixTimestamp64Second(dt64);
SELECT materialize(x) as dt64, toUnixTimestamp64Second(dt64), toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64);
WITH toDateTime64('2019-09-16 19:20:12.345678910', 6, 'Asia/Istanbul') AS x
SELECT materialize(x) as dt64, toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64), toUnixTimestamp64Second(dt64);
SELECT materialize(x) as dt64, toUnixTimestamp64Second(dt64), toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64);
WITH toDateTime64('2019-09-16 19:20:12.345678910', 9, 'Asia/Istanbul') AS x
SELECT materialize(x) as dt64, toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64), toUnixTimestamp64Second(dt64);
SELECT materialize(x) as dt64, toUnixTimestamp64Second(dt64), toUnixTimestamp64Milli(dt64), toUnixTimestamp64Micro(dt64), toUnixTimestamp64Nano(dt64);

View File

@ -310,6 +310,7 @@ fromUnixTimestamp
fromUnixTimestamp64Micro
fromUnixTimestamp64Milli
fromUnixTimestamp64Nano
fromUnixTimestamp64Second
fromUnixTimestampInJodaSyntax
fuzzBits
gccMurmurHash