Merge pull request #50709 from arenadata/ADQM-867

Added numeric arguments support to some Date/DateTime conversion functions
This commit is contained in:
Robert Schulze 2023-06-12 17:08:14 +02:00 committed by GitHub
commit 128e8c20d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 600 additions and 34 deletions

View File

@ -33,7 +33,7 @@ SELECT
toTypeName(toNullable('') AS val) AS source_type,
toTypeName(toString(val)) AS to_type_result_type,
toTypeName(CAST(val, 'String')) AS cast_result_type
┌─source_type──────┬─to_type_result_type─┬─cast_result_type─┐
│ Nullable(String) │ Nullable(String) │ String │
└──────────────────┴─────────────────────┴──────────────────┘
@ -203,7 +203,7 @@ Result:
## toDate
Converts the argument to [Date](/docs/en/sql-reference/data-types/date.md) data type.
Converts the argument to [Date](/docs/en/sql-reference/data-types/date.md) data type.
If the argument is [DateTime](/docs/en/sql-reference/data-types/datetime.md) or [DateTime64](/docs/en/sql-reference/data-types/datetime64.md), it truncates it and leaves the date component of the DateTime:
@ -232,7 +232,7 @@ SELECT
│ 2022-12-30 │ Date │
└────────────┴──────────────────────────────────┘
1 row in set. Elapsed: 0.001 sec.
1 row in set. Elapsed: 0.001 sec.
```
```sql
@ -314,20 +314,183 @@ SELECT
└─────────────────────┴───────────────┴─────────────┴─────────────────────┘
```
## toDateOrZero
The same as [toDate](#todate) but returns lower boundary of [Date](/docs/en/sql-reference/data-types/date.md) if an invalid argument is received. Only [String](/docs/en/sql-reference/data-types/string.md) argument is supported.
**Example**
Query:
``` sql
SELECT toDateOrZero('2022-12-30'), toDateOrZero('');
```
Result:
```response
┌─toDateOrZero('2022-12-30')─┬─toDateOrZero('')─┐
│ 2022-12-30 │ 1970-01-01 │
└────────────────────────────┴──────────────────┘
```
## toDateOrNull
The same as [toDate](#todate) but returns `NULL` if an invalid argument is received. Only [String](/docs/en/sql-reference/data-types/string.md) argument is supported.
**Example**
Query:
``` sql
SELECT toDateOrNull('2022-12-30'), toDateOrNull('');
```
Result:
```response
┌─toDateOrNull('2022-12-30')─┬─toDateOrNull('')─┐
│ 2022-12-30 │ ᴺᵁᴸᴸ │
└────────────────────────────┴──────────────────┘
```
## toDateOrDefault
Like [toDate](#todate) but if unsuccessful, returns a default value which is either the second argument (if specified), or otherwise the lower boundary of [Date](/docs/en/sql-reference/data-types/date.md).
**Syntax**
``` sql
toDateOrDefault(expr [, default_value])
```
**Example**
Query:
``` sql
SELECT toDateOrDefault('2022-12-30'), toDateOrDefault('', '2023-01-01'::Date);
```
Result:
```response
┌─toDateOrDefault('2022-12-30')─┬─toDateOrDefault('', CAST('2023-01-01', 'Date'))─┐
│ 2022-12-30 │ 2023-01-01 │
└───────────────────────────────┴─────────────────────────────────────────────────┘
```
## toDateTime
Converts an input value to [DateTime](/docs/en/sql-reference/data-types/datetime.md).
**Syntax**
``` sql
toDateTime(expr[, time_zone ])
```
**Arguments**
- `expr` — The value. [String](/docs/en/sql-reference/data-types/string.md), [Int](/docs/en/sql-reference/data-types/int-uint.md), [Date](/docs/en/sql-reference/data-types/date.md) or [DateTime](/docs/en/sql-reference/data-types/datetime.md).
- `time_zone` — Time zone. [String](/docs/en/sql-reference/data-types/string.md).
If `expr` is a number, it is interpreted as the number of seconds since the beginning of the Unix Epoch (as Unix timestamp).
**Returned value**
- A date time. [DateTime](/docs/en/sql-reference/data-types/datetime.md)
**Example**
Query:
``` sql
SELECT toDateTime('2022-12-30 13:44:17'), toDateTime(1685457500, 'UTC');
```
Result:
```response
┌─toDateTime('2022-12-30 13:44:17')─┬─toDateTime(1685457500, 'UTC')─┐
│ 2022-12-30 13:44:17 │ 2023-05-30 14:38:20 │
└───────────────────────────────────┴───────────────────────────────┘
```
## toDateTimeOrZero
The same as [toDateTime](#todatetime) but returns lower boundary of [DateTime](/docs/en/sql-reference/data-types/datetime.md) if an invalid argument is received. Only [String](/docs/en/sql-reference/data-types/string.md) argument is supported.
**Example**
Query:
``` sql
SELECT toDateTimeOrZero('2022-12-30 13:44:17'), toDateTimeOrZero('');
```
Result:
```response
┌─toDateTimeOrZero('2022-12-30 13:44:17')─┬─toDateTimeOrZero('')─┐
│ 2022-12-30 13:44:17 │ 1970-01-01 00:00:00 │
└─────────────────────────────────────────┴──────────────────────┘
```
## toDateTimeOrNull
The same as [toDateTime](#todatetime) but returns `NULL` if an invalid argument is received. Only [String](/docs/en/sql-reference/data-types/string.md) argument is supported.
**Example**
Query:
``` sql
SELECT toDateTimeOrNull('2022-12-30 13:44:17'), toDateTimeOrNull('');
```
Result:
```response
┌─toDateTimeOrNull('2022-12-30 13:44:17')─┬─toDateTimeOrNull('')─┐
│ 2022-12-30 13:44:17 │ ᴺᵁᴸᴸ │
└─────────────────────────────────────────┴──────────────────────┘
```
## toDateTimeOrDefault
Like [toDateTime](#todatetime) but if unsuccessful, returns a default value which is either the third argument (if specified), or otherwise the lower boundary of [DateTime](/docs/en/sql-reference/data-types/datetime.md).
**Syntax**
``` sql
toDateTimeOrDefault(expr [, time_zone [, default_value]])
```
**Example**
Query:
``` sql
SELECT toDateTimeOrDefault('2022-12-30 13:44:17'), toDateTimeOrDefault('', 'UTC', '2023-01-01'::DateTime('UTC'));
```
Result:
```response
┌─toDateTimeOrDefault('2022-12-30 13:44:17')─┬─toDateTimeOrDefault('', 'UTC', CAST('2023-01-01', 'DateTime(\'UTC\')'))─┐
│ 2022-12-30 13:44:17 │ 2023-01-01 00:00:00 │
└────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────┘
```
## toDate32
Converts the argument to the [Date32](/docs/en/sql-reference/data-types/date32.md) data type. If the value is outside the range, `toDate32` returns the border values supported by [Date32](/docs/en/sql-reference/data-types/date32.md). If the argument has [Date](/docs/en/sql-reference/data-types/date.md) type, it's borders are taken into account.
@ -519,6 +682,11 @@ SELECT toDateTime64('2019-01-01 00:00:00', 3, 'Asia/Istanbul') AS value, toTypeN
└─────────────────────────┴─────────────────────────────────────────────────────────────────────┘
```
## toDateTime64OrZero
## toDateTime64OrNull
## toDateTime64OrDefault
## toDecimal(32\|64\|128\|256)
@ -1247,7 +1415,7 @@ Returns DateTime values parsed from input string according to a MySQL style form
**Supported format specifiers**
All format specifiers listed in [formatDateTime](/docs/en/sql-reference/functions/date-time-functions.md#date_time_functions-formatDateTime) except:
- %Q: Quarter (1-4)
- %Q: Quarter (1-4)
**Example**

View File

@ -165,22 +165,217 @@ SELECT toUInt64(nan), toUInt32(-32), toUInt16('16'), toUInt8(8.8);
## toDate {#todate}
Cиноним: `DATE`.
Конвертирует аргумент в значение [Date](/docs/ru/sql-reference/data-types/date.md).
**Синтаксис**
``` sql
toDate(expr)
```
**Аргументы**
- `expr` — Значение для преобразования. [String](/docs/ru/sql-reference/data-types/string.md), [Int](/docs/ru/sql-reference/data-types/int-uint.md), [Date](/docs/ru/sql-reference/data-types/date.md) или [DateTime](/docs/ru/sql-reference/data-types/datetime.md).
Если `expr` является числом выглядит как UNIX timestamp (больше чем 65535), оно интерпретируется как DateTime, затем обрезается до Date учитывавая текущую часовой пояс. Если `expr` является числом и меньше чем 65536, оно интерпретируется как количество дней с 1970-01-01.
**Возвращаемое значение**
- Календарная дата. [Date](/docs/ru/sql-reference/data-types/date.md).
**Пример**
Запрос:
``` sql
SELECT toDate('2022-12-30'), toDate(1685457500);
```
Результат:
```response
┌─toDate('2022-12-30')─┬─toDate(1685457500)─┐
│ 2022-12-30 │ 2023-05-30 │
└──────────────────────┴────────────────────┘
```
## toDateOrZero {#todateorzero}
Как [toDate](#todate), но в случае неудачи возвращает нижнюю границу [Date](/docs/ru/sql-reference/data-types/date.md)). Поддерживается только аргумент типа [String](/docs/ru/sql-reference/data-types/string.md).
**Пример**
Запрос:
``` sql
SELECT toDateOrZero('2022-12-30'), toDateOrZero('');
```
Результат:
```response
┌─toDateOrZero('2022-12-30')─┬─toDateOrZero('')─┐
│ 2022-12-30 │ 1970-01-01 │
└────────────────────────────┴──────────────────┘
```
## toDateOrNull {#todateornull}
Как [toDate](#todate), но в случае неудачи возвращает `NULL`. Поддерживается только аргумент типа [String](/docs/ru/sql-reference/data-types/string.md).
**Пример**
Запрос:
``` sql
SELECT toDateOrNull('2022-12-30'), toDateOrNull('');
```
Результат:
```response
┌─toDateOrNull('2022-12-30')─┬─toDateOrNull('')─┐
│ 2022-12-30 │ ᴺᵁᴸᴸ │
└────────────────────────────┴──────────────────┘
```
## toDateOrDefault {#todateordefault}
Как [toDate](#todate), но в случае неудачи возвращает значение по умолчанию (или второй аргумент (если указан), или нижняя граница [Date](/docs/ru/sql-reference/data-types/date.md)).
**Синтаксис**
``` sql
toDateOrDefault(expr [, default_value])
```
**Пример**
Запрос:
``` sql
SELECT toDateOrDefault('2022-12-30'), toDateOrDefault('', '2023-01-01'::Date);
```
Результат:
```response
┌─toDateOrDefault('2022-12-30')─┬─toDateOrDefault('', CAST('2023-01-01', 'Date'))─┐
│ 2022-12-30 │ 2023-01-01 │
└───────────────────────────────┴─────────────────────────────────────────────────┘
```
## toDateTime {#todatetime}
Конвертирует аргумент в значение [DateTime](/docs/ru/sql-reference/data-types/datetime.md).
**Синтаксис**
``` sql
toDateTime(expr[, time_zone ])
```
**Аргументы**
- `expr` — Значение для преобразования. [String](/docs/ru/sql-reference/data-types/string.md), [Int](/docs/ru/sql-reference/data-types/int-uint.md), [Date](/docs/ru/sql-reference/data-types/date.md) или [DateTime](/docs/ru/sql-reference/data-types/datetime.md).
- `time_zone` — Часовой пояс. [String](/docs/ru/sql-reference/data-types/string.md).
Если `expr` является числом, оно интерпретируется как количество секунд от начала unix эпохи.
**Возвращаемое значение**
- Время. [DateTime](/docs/ru/sql-reference/data-types/datetime.md)
**Пример**
Запрос:
``` sql
SELECT toDateTime('2022-12-30 13:44:17'), toDateTime(1685457500, 'UTC');
```
Результат:
```response
┌─toDateTime('2022-12-30 13:44:17')─┬─toDateTime(1685457500, 'UTC')─┐
│ 2022-12-30 13:44:17 │ 2023-05-30 14:38:20 │
└───────────────────────────────────┴───────────────────────────────┘
```
## toDateTimeOrZero {#todatetimeorzero}
Как [toDateTime](#todatetime), но в случае неудачи возвращает нижнюю границу [DateTime](/docs/ru/sql-reference/data-types/datetime.md)). Поддерживается только аргумент типа [String](/docs/ru/sql-reference/data-types/string.md).
**Пример**
Запрос:
``` sql
SELECT toDateTimeOrZero('2022-12-30 13:44:17'), toDateTimeOrZero('');
```
Результат:
```response
┌─toDateTimeOrZero('2022-12-30 13:44:17')─┬─toDateTimeOrZero('')─┐
│ 2022-12-30 13:44:17 │ 1970-01-01 00:00:00 │
└─────────────────────────────────────────┴──────────────────────┘
```
## toDateTimeOrNull {#todatetimeornull}
Как [toDateTime](#todatetime), но в случае неудачи возвращает `NULL`. Поддерживается только аргумент типа [String](/docs/ru/sql-reference/data-types/string.md).
**Example**
Query:
``` sql
SELECT toDateTimeOrNull('2022-12-30 13:44:17'), toDateTimeOrNull('');
```
Result:
```response
┌─toDateTimeOrNull('2022-12-30 13:44:17')─┬─toDateTimeOrNull('')─┐
│ 2022-12-30 13:44:17 │ ᴺᵁᴸᴸ │
└─────────────────────────────────────────┴──────────────────────┘
```
## toDateTimeOrDefault {#todatetimeordefault}
Как [toDateTime](#todatetime), но в случае неудачи возвращает значение по умолчанию (или третий аргумент (если указан), или нижняя граница [DateTime](/docs/ru/sql-reference/data-types/datetime.md)).
**Синтаксис**
``` sql
toDateTimeOrDefault(expr, [, time_zone [, default_value]])
```
**Пример**
Запрос:
``` sql
SELECT toDateTimeOrDefault('2022-12-30 13:44:17'), toDateTimeOrDefault('', 'UTC', '2023-01-01'::DateTime('UTC'));
```
Результат:
```response
┌─toDateTimeOrDefault('2022-12-30 13:44:17')─┬─toDateTimeOrDefault('', 'UTC', CAST('2023-01-01', 'DateTime(\'UTC\')'))─┐
│ 2022-12-30 13:44:17 │ 2023-01-01 00:00:00 │
└────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────┘
```
## toDate32 {#todate32}
Конвертирует аргумент в значение типа [Date32](../../sql-reference/data-types/date32.md). Если значение выходит за границы диапазона, возвращается пограничное значение `Date32`. Если аргумент имеет тип [Date](../../sql-reference/data-types/date.md), учитываются границы типа `Date`.
@ -301,6 +496,14 @@ SELECT
└─────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────┘
```
## toDateTime64
## toDateTime64OrZero
## toDateTime64OrNull
## toDateTime64OrDefault
## toDecimal(32\|64\|128\|256) {#todecimal3264128}
Преобразует `value` к типу данных [Decimal](../../sql-reference/functions/type-conversion-functions.md) с точностью `S`. `value` может быть числом или строкой. Параметр `S` (scale) задаёт число десятичных знаков.

View File

@ -5,6 +5,8 @@
#include <Common/Exception.h>
#include <Common/DateLUTImpl.h>
#include <Common/DateLUT.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnVector.h>
#include <Columns/ColumnDecimal.h>
#include <Functions/FunctionHelpers.h>
@ -21,6 +23,7 @@ namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int CANNOT_CONVERT_TYPE;
}
/** Transformations.
@ -1425,12 +1428,15 @@ struct ToDateTimeComponentsImpl
using FactorTransform = ZeroTransform;
};
struct DateTimeAccurateConvertStrategyAdditions {};
struct DateTimeAccurateOrNullConvertStrategyAdditions {};
template <typename FromType, typename ToType, typename Transform, bool is_extended_result = false>
template <typename FromType, typename ToType, typename Transform, bool is_extended_result = false, typename Additions = void *>
struct Transformer
{
template <typename FromTypeVector, typename ToTypeVector>
static void vector(const FromTypeVector & vec_from, ToTypeVector & vec_to, const DateLUTImpl & time_zone, const Transform & transform)
static void vector(const FromTypeVector & vec_from, ToTypeVector & vec_to, const DateLUTImpl & time_zone, const Transform & transform,
[[maybe_unused]] ColumnUInt8::Container * vec_null_map_to)
{
using ValueType = typename ToTypeVector::value_type;
size_t size = vec_from.size();
@ -1438,6 +1444,30 @@ struct Transformer
for (size_t i = 0; i < size; ++i)
{
if constexpr (std::is_same_v<ToType, DataTypeDate> || std::is_same_v<ToType, DataTypeDateTime>)
{
if constexpr (std::is_same_v<Additions, DateTimeAccurateConvertStrategyAdditions>
|| std::is_same_v<Additions, DateTimeAccurateOrNullConvertStrategyAdditions>)
{
bool is_valid_input = vec_from[i] >= 0 && vec_from[i] <= 0xFFFFFFFFL;
if (!is_valid_input)
{
if constexpr (std::is_same_v<Additions, DateTimeAccurateOrNullConvertStrategyAdditions>)
{
vec_to[i] = 0;
(*vec_null_map_to)[i] = true;
continue;
}
else
{
throw Exception(ErrorCodes::CANNOT_CONVERT_TYPE, "Value {} cannot be safely converted into type {}",
vec_from[i], TypeName<ValueType>);
}
}
}
}
if constexpr (is_extended_result)
vec_to[i] = static_cast<ValueType>(transform.executeExtendedResult(vec_from[i], time_zone));
else
@ -1446,18 +1476,26 @@ struct Transformer
}
};
template <typename FromDataType, typename ToDataType, typename Transform, bool is_extended_result = false>
struct DateTimeTransformImpl
{
template <typename Additions = void *>
static ColumnPtr execute(
const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/, const Transform & transform = {})
{
using Op = Transformer<typename FromDataType::FieldType, typename ToDataType::FieldType, Transform, is_extended_result>;
using Op = Transformer<FromDataType, ToDataType, Transform, is_extended_result, Additions>;
const ColumnPtr source_col = arguments[0].column;
if (const auto * sources = checkAndGetColumn<typename FromDataType::ColumnType>(source_col.get()))
{
ColumnUInt8::MutablePtr col_null_map_to;
ColumnUInt8::Container * vec_null_map_to [[maybe_unused]] = nullptr;
if constexpr (std::is_same_v<Additions, DateTimeAccurateOrNullConvertStrategyAdditions>)
{
col_null_map_to = ColumnUInt8::create(sources->getData().size(), false);
vec_null_map_to = &col_null_map_to->getData();
}
auto mutable_result_col = result_type->createColumn();
auto * col_to = assert_cast<typename ToDataType::ColumnType *>(mutable_result_col.get());
@ -1465,7 +1503,7 @@ struct DateTimeTransformImpl
if (result_data_type.isDateTime() || result_data_type.isDateTime64())
{
const auto & time_zone = dynamic_cast<const TimezoneMixin &>(*result_type).getTimeZone();
Op::vector(sources->getData(), col_to->getData(), time_zone, transform);
Op::vector(sources->getData(), col_to->getData(), time_zone, transform, vec_null_map_to);
}
else
{
@ -1474,7 +1512,15 @@ struct DateTimeTransformImpl
time_zone_argument_position = 2;
const DateLUTImpl & time_zone = extractTimeZoneFromFunctionArguments(arguments, time_zone_argument_position, 0);
Op::vector(sources->getData(), col_to->getData(), time_zone, transform);
Op::vector(sources->getData(), col_to->getData(), time_zone, transform, vec_null_map_to);
}
if constexpr (std::is_same_v<Additions, DateTimeAccurateOrNullConvertStrategyAdditions>)
{
if (vec_null_map_to)
{
return ColumnNullable::create(std::move(mutable_result_col), std::move(col_null_map_to));
}
}
return mutable_result_col;

View File

@ -2866,20 +2866,37 @@ private:
using LeftDataType = typename Types::LeftType;
using RightDataType = typename Types::RightType;
if constexpr (IsDataTypeNumber<LeftDataType> && IsDataTypeNumber<RightDataType>)
if constexpr (IsDataTypeNumber<LeftDataType>)
{
if (wrapper_cast_type == CastType::accurate)
if constexpr (IsDataTypeNumber<RightDataType>)
{
result_column = ConvertImpl<LeftDataType, RightDataType, FunctionName>::execute(
arguments, result_type, input_rows_count, AccurateConvertStrategyAdditions());
}
else
{
result_column = ConvertImpl<LeftDataType, RightDataType, FunctionName>::execute(
arguments, result_type, input_rows_count, AccurateOrNullConvertStrategyAdditions());
if (wrapper_cast_type == CastType::accurate)
{
result_column = ConvertImpl<LeftDataType, RightDataType, FunctionName>::execute(
arguments, result_type, input_rows_count, AccurateConvertStrategyAdditions());
}
else
{
result_column = ConvertImpl<LeftDataType, RightDataType, FunctionName>::execute(
arguments, result_type, input_rows_count, AccurateOrNullConvertStrategyAdditions());
}
return true;
}
return true;
if constexpr (std::is_same_v<RightDataType, DataTypeDate> || std::is_same_v<RightDataType, DataTypeDateTime>)
{
if (wrapper_cast_type == CastType::accurate)
{
result_column = ConvertImpl<LeftDataType, RightDataType, FunctionName>::template execute<DateTimeAccurateConvertStrategyAdditions>(
arguments, result_type, input_rows_count);
}
else
{
result_column = ConvertImpl<LeftDataType, RightDataType, FunctionName>::template execute<DateTimeAccurateOrNullConvertStrategyAdditions>(
arguments, result_type, input_rows_count);
}
return true;
}
}
return false;

View File

@ -30,3 +30,15 @@
\N
127
\N
\N
\N
\N
2023-05-30 14:38:20
1970-01-01 00:00:19
1970-01-01 19:26:40
\N
\N
\N
2023-05-30
2149-06-06
1970-01-20

View File

@ -35,3 +35,17 @@ SELECT accurateCastOrNull(nan, 'UInt64');
SELECT accurateCastOrNull(nan, 'UInt256');
SELECT accurateCastOrNull(number + 127, 'Int8') AS x FROM numbers (2) ORDER BY x;
SELECT accurateCastOrNull(-1, 'DateTime');
SELECT accurateCastOrNull(5000000000, 'DateTime');
SELECT accurateCastOrNull('1xxx', 'DateTime');
select toString(accurateCastOrNull('2023-05-30 14:38:20', 'DateTime'), timezone());
SELECT toString(accurateCastOrNull(19, 'DateTime'), 'UTC');
SELECT toString(accurateCastOrNull(70000, 'DateTime'), 'UTC');
SELECT accurateCastOrNull(-1, 'Date');
SELECT accurateCastOrNull(5000000000, 'Date');
SELECT accurateCastOrNull('1xxx', 'Date');
SELECT accurateCastOrNull('2023-05-30', 'Date');
SELECT accurateCastOrNull('2180-01-01', 'Date');
SELECT accurateCastOrNull(19, 'Date');

View File

@ -6,3 +6,7 @@
5
1
12
2023-05-30 14:38:20
1970-01-01 00:00:19
2023-05-30
1970-01-20

View File

@ -1,24 +1,36 @@
SELECT accurateCast(-1, 'UInt8'); -- { serverError 70 }
SELECT accurateCast(-1, 'UInt8'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(5, 'UInt8');
SELECT accurateCast(257, 'UInt8'); -- { serverError 70 }
SELECT accurateCast(-1, 'UInt16'); -- { serverError 70 }
SELECT accurateCast(257, 'UInt8'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(-1, 'UInt16'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(5, 'UInt16');
SELECT accurateCast(65536, 'UInt16'); -- { serverError 70 }
SELECT accurateCast(-1, 'UInt32'); -- { serverError 70 }
SELECT accurateCast(65536, 'UInt16'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(-1, 'UInt32'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(5, 'UInt32');
SELECT accurateCast(4294967296, 'UInt32'); -- { serverError 70 }
SELECT accurateCast(-1, 'UInt64'); -- { serverError 70 }
SELECT accurateCast(4294967296, 'UInt32'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(-1, 'UInt64'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(5, 'UInt64');
SELECT accurateCast(-1, 'UInt256'); -- { serverError 70 }
SELECT accurateCast(-1, 'UInt256'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(5, 'UInt256');
SELECT accurateCast(-129, 'Int8'); -- { serverError 70 }
SELECT accurateCast(-129, 'Int8'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(5, 'Int8');
SELECT accurateCast(128, 'Int8'); -- { serverError 70 }
SELECT accurateCast(128, 'Int8'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(10, 'Decimal32(9)'); -- { serverError 407 }
SELECT accurateCast(10, 'Decimal32(9)'); -- { serverError DECIMAL_OVERFLOW }
SELECT accurateCast(1, 'Decimal32(9)');
SELECT accurateCast(-10, 'Decimal32(9)'); -- { serverError 407 }
SELECT accurateCast(-10, 'Decimal32(9)'); -- { serverError DECIMAL_OVERFLOW }
SELECT accurateCast('123', 'FixedString(2)'); -- { serverError 131 }
SELECT accurateCast('123', 'FixedString(2)'); -- { serverError TOO_LARGE_STRING_SIZE }
SELECT accurateCast('12', 'FixedString(2)');
SELECT accurateCast(-1, 'DateTime'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(0xFFFFFFFF + 1, 'DateTime'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast('1xxx', 'DateTime'); -- { serverError CANNOT_PARSE_DATETIME }
SELECT accurateCast('2023-05-30 14:38:20', 'DateTime');
SELECT toString(accurateCast(19, 'DateTime'), 'UTC');
SELECT accurateCast(-1, 'Date'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast(0xFFFFFFFF + 1, 'Date'); -- { serverError CANNOT_CONVERT_TYPE }
SELECT accurateCast('1xxx', 'Date'); -- { serverError CANNOT_PARSE_DATE }
SELECT accurateCast('2023-05-30', 'Date');
SELECT accurateCast(19, 'Date');

View File

@ -22,3 +22,40 @@
-2
61f0c404-5cb3-11e7-907b-a6006ad3dba0
59f0c404-5cb3-11e7-907b-a6006ad3dba0
1970-01-01
2023-05-30
2023-05-30
2023-05-30
1970-01-01
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
1970-01-20
2149-06-06
1970-01-02
2023-05-30
2023-05-30
2023-05-30 14:38:20
2023-05-30 14:38:20
2023-05-30 14:38:20
2023-05-30 14:38:20
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19
1970-01-01 00:00:19

View File

@ -28,3 +28,56 @@ select toInt256OrDefault('-1xx', cast(-2 as Int256));
SELECT toUUIDOrDefault('61f0c404-5cb3-11e7-907b-a6006ad3dba0', cast('59f0c404-5cb3-11e7-907b-a6006ad3dba0' as UUID));
SELECT toUUIDOrDefault('-----61f0c404-5cb3-11e7-907b-a6006ad3dba0', cast('59f0c404-5cb3-11e7-907b-a6006ad3dba0' as UUID));
select toDateOrDefault('1xxx');
select toDateOrDefault('2023-05-30');
select toDateOrDefault('2023-05-30', '2000-01-01'::Date);
select toDateOrDefault('1xx', '2023-05-30'::Date);
select toDateOrDefault(-1);
select toDateOrDefault(cast(19 as Int8));
select toDateOrDefault(cast(19 as UInt8));
select toDateOrDefault(cast(19 as Int16));
select toDateOrDefault(cast(19 as UInt16));
select toDateOrDefault(cast(19 as Int32));
select toDateOrDefault(cast(19 as UInt32));
select toDateOrDefault(cast(19 as Int64));
select toDateOrDefault(cast(19 as UInt64));
select toDateOrDefault(cast(19 as Int128));
select toDateOrDefault(cast(19 as UInt128));
select toDateOrDefault(cast(19 as Int256));
select toDateOrDefault(cast(19 as UInt256));
select toDateOrDefault(65535);
select toDateOrDefault(122400);
select toDateOrDefault(19507, '2000-01-01'::Date);
select toDateOrDefault(-1, '2023-05-30'::Date);
select toDateTimeOrDefault('2023-05-30 14:38:20', 'UTC');
select toDateTimeOrDefault('1xxx', 'UTC', '2023-05-30 14:38:20'::DateTime('UTC'));
select toDateTimeOrDefault(1685457500, 'UTC');
select toDateTimeOrDefault(-1, 'UTC', '2023-05-30 14:38:20'::DateTime('UTC'));
select toDateTimeOrDefault(cast(19 as Int8), 'UTC');
select toDateTimeOrDefault(cast(19 as UInt8), 'UTC');
select toDateTimeOrDefault(cast(19 as Int16), 'UTC');
select toDateTimeOrDefault(cast(19 as UInt16), 'UTC');
select toDateTimeOrDefault(cast(19 as Int32), 'UTC');
select toDateTimeOrDefault(cast(19 as UInt32), 'UTC');
select toDateTimeOrDefault(cast(19 as Int64), 'UTC');
select toDateTimeOrDefault(cast(19 as UInt64), 'UTC');
select toDateTimeOrDefault(cast(19 as Int128), 'UTC');
select toDateTimeOrDefault(cast(19 as UInt128), 'UTC');
select toDateTimeOrDefault(cast(19 as Int256), 'UTC');
select toDateTimeOrDefault(cast(19 as UInt256), 'UTC');