Merge branch 'master' into ADQM-808

This commit is contained in:
Alexey Gerasimchuk 2023-05-23 17:03:54 +10:00 committed by GitHub
commit 30f3b3ba04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 23 deletions

View File

@ -357,14 +357,14 @@ Alias: `SECOND`.
## toUnixTimestamp
For DateTime arguments: converts the value to the number with type UInt32 -- Unix Timestamp (https://en.wikipedia.org/wiki/Unix_time).
Converts a string, a date or a date with time to the [Unix Timestamp](https://en.wikipedia.org/wiki/Unix_time) in `UInt32` representation.
For String argument: converts the input string to the datetime according to the timezone (optional second argument, server timezone is used by default) and returns the corresponding unix timestamp.
If the function is called with a string, it accepts an optional timezone argument.
**Syntax**
``` sql
toUnixTimestamp(datetime)
toUnixTimestamp(date)
toUnixTimestamp(str, [timezone])
```
@ -377,15 +377,29 @@ Type: `UInt32`.
**Example**
``` sql
SELECT toUnixTimestamp('2017-11-05 08:07:47', 'Asia/Tokyo') AS unix_timestamp
SELECT
'2017-11-05 08:07:47' AS dt_str,
toUnixTimestamp(dt_str) AS from_str,
toUnixTimestamp(dt_str, 'Asia/Tokyo') AS from_str_tokyo,
toUnixTimestamp(toDateTime(dt_str)) AS from_datetime,
toUnixTimestamp(toDateTime64(dt_str, 0)) AS from_datetime64,
toUnixTimestamp(toDate(dt_str)) AS from_date,
toUnixTimestamp(toDate32(dt_str)) AS from_date32
FORMAT Vertical;
```
Result:
``` text
┌─unix_timestamp─┐
│ 1509836867 │
└────────────────┘
Row 1:
──────
dt_str: 2017-11-05 08:07:47
from_str: 1509869267
from_str_tokyo: 1509836867
from_datetime: 1509869267
from_datetime64: 1509869267
from_date: 1509840000
from_date32: 1509840000
```
:::note

View File

@ -235,13 +235,13 @@ SELECT toDateTime('2021-04-21 10:20:30', 'Europe/Moscow') AS Time, toTypeName(Ti
## toUnixTimestamp {#to-unix-timestamp}
Переводит дату-с-временем в число типа UInt32 -- Unix Timestamp (https://en.wikipedia.org/wiki/Unix_time).
Для аргумента String, строка конвертируется в дату и время в соответствии с часовым поясом (необязательный второй аргумент, часовой пояс сервера используется по умолчанию).
Переводит строку, дату или дату-с-временем в [Unix Timestamp](https://en.wikipedia.org/wiki/Unix_time), имеющий тип `UInt32`.
Строка может сопровождаться вторым (необязательным) аргументом, указывающим часовой пояс.
**Синтаксис**
``` sql
toUnixTimestamp(datetime)
toUnixTimestamp(date)
toUnixTimestamp(str, [timezone])
```
@ -256,15 +256,29 @@ toUnixTimestamp(str, [timezone])
Запрос:
``` sql
SELECT toUnixTimestamp('2017-11-05 08:07:47', 'Asia/Tokyo') AS unix_timestamp;
SELECT
'2017-11-05 08:07:47' AS dt_str,
toUnixTimestamp(dt_str) AS from_str,
toUnixTimestamp(dt_str, 'Asia/Tokyo') AS from_str_tokyo,
toUnixTimestamp(toDateTime(dt_str)) AS from_datetime,
toUnixTimestamp(toDateTime64(dt_str, 0)) AS from_datetime64,
toUnixTimestamp(toDate(dt_str)) AS from_date,
toUnixTimestamp(toDate32(dt_str)) AS from_date32
FORMAT Vertical;
```
Результат:
``` text
┌─unix_timestamp─┐
│ 1509836867 │
└────────────────┘
Row 1:
──────
dt_str: 2017-11-05 08:07:47
from_str: 1509869267
from_str_tokyo: 1509836867
from_datetime: 1509869267
from_datetime64: 1509869267
from_date: 1509840000
from_date32: 1509840000
```
:::note

View File

@ -10,6 +10,8 @@
#include <type_traits>
#define DATE_SECONDS_PER_DAY 86400 /// Number of seconds in a day, 60 * 60 * 24
#define DATE_LUT_MIN_YEAR 1900 /// 1900 since majority of financial organizations consider 1900 as an initial year.
#define DATE_LUT_MAX_YEAR 2299 /// Last supported year (complete)
#define DATE_LUT_YEARS (1 + DATE_LUT_MAX_YEAR - DATE_LUT_MIN_YEAR) /// Number of years in lookup table

View File

@ -145,13 +145,6 @@ struct ConvertImpl
using ColVecFrom = typename FromDataType::ColumnType;
using ColVecTo = typename ToDataType::ColumnType;
if (std::is_same_v<Name, NameToUnixTimestamp>)
{
if (isDateOrDate32(named_from.type))
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal type {} of first argument of function {}",
named_from.type->getName(), Name::name);
}
if constexpr ((IsDataTypeDecimal<FromDataType> || IsDataTypeDecimal<ToDataType>)
&& !(std::is_same_v<DataTypeDateTime64, FromDataType> || std::is_same_v<DataTypeDateTime64, ToDataType>))
{
@ -306,6 +299,8 @@ struct ConvertImpl
{
if constexpr (std::is_same_v<ToDataType, DataTypeIPv4> && std::is_same_v<FromDataType, DataTypeUInt64>)
vec_to[i] = static_cast<ToFieldType>(static_cast<IPv4::UnderlyingType>(vec_from[i]));
else if constexpr (std::is_same_v<Name, NameToUnixTimestamp> && (std::is_same_v<FromDataType, DataTypeDate> || std::is_same_v<FromDataType, DataTypeDate32>))
vec_to[i] = static_cast<ToFieldType>(vec_from[i] * DATE_SECONDS_PER_DAY);
else
vec_to[i] = static_cast<ToFieldType>(vec_from[i]);
}

View File

@ -49,7 +49,7 @@ Code: 43
"UInt8",11
------------------------------------------
SELECT toUnixTimestamp(N)
Code: 44
"UInt32",1568592000
"UInt32",1568650811
"UInt32",1568650811
------------------------------------------

View File

@ -0,0 +1,4 @@
1683676800
1683676800
1683676800
1683676800

View File

@ -1 +1,4 @@
select toUnixTimestamp(today()); -- { serverError 44 }
select toUnixTimestamp(makeDate(2023, 5, 10));
select toUnixTimestamp(makeDate32(2023, 5, 10));
select toUnixTimestamp(makeDate(2023, 5, 10), 'Pacific/Auckland');
select toUnixTimestamp(makeDate32(2023, 5, 10), 'Pacific/Auckland');