2011-10-15 23:40:56 +00:00
# pragma once
2021-09-29 16:42:41 +00:00
# include <cstddef>
2016-01-13 00:32:59 +00:00
# include <type_traits>
2017-04-01 09:19:00 +00:00
# include <IO/WriteBufferFromVector.h>
# include <IO/ReadBufferFromMemory.h>
2017-07-21 06:35:58 +00:00
# include <IO/Operators.h>
2018-02-11 23:57:07 +00:00
# include <IO/parseDateTimeBestEffort.h>
2017-04-01 09:19:00 +00:00
# include <DataTypes/DataTypeFactory.h>
# include <DataTypes/DataTypesNumber.h>
2018-08-21 18:25:38 +00:00
# include <DataTypes/DataTypesDecimal.h>
2017-04-01 09:19:00 +00:00
# include <DataTypes/DataTypeString.h>
# include <DataTypes/DataTypeFixedString.h>
# include <DataTypes/DataTypeDate.h>
2021-07-15 11:41:52 +00:00
# include <DataTypes/DataTypeDate32.h>
2017-04-01 09:19:00 +00:00
# include <DataTypes/DataTypeDateTime.h>
2019-10-24 10:44:38 +00:00
# include <DataTypes/DataTypeDateTime64.h>
2017-04-01 09:19:00 +00:00
# include <DataTypes/DataTypeEnum.h>
# include <DataTypes/DataTypeArray.h>
# include <DataTypes/DataTypeTuple.h>
2020-10-10 06:49:03 +00:00
# include <DataTypes/DataTypeMap.h>
2017-04-01 09:19:00 +00:00
# include <DataTypes/DataTypeNullable.h>
2017-12-09 20:56:53 +00:00
# include <DataTypes/DataTypeNothing.h>
2017-07-04 10:42:53 +00:00
# include <DataTypes/DataTypeUUID.h>
2017-10-30 02:18:06 +00:00
# include <DataTypes/DataTypeInterval.h>
2019-09-02 09:16:49 +00:00
# include <DataTypes/DataTypeAggregateFunction.h>
2021-08-10 01:33:57 +00:00
# include <DataTypes/DataTypeObject.h>
2022-01-27 00:24:34 +00:00
# include <DataTypes/ObjectUtils.h>
# include <DataTypes/DataTypeNested.h>
2023-12-19 16:43:30 +00:00
# include <DataTypes/DataTypeVariant.h>
2021-03-09 14:46:52 +00:00
# include <DataTypes/Serializations/SerializationDecimal.h>
2018-06-10 19:22:49 +00:00
# include <Formats/FormatSettings.h>
2017-04-01 09:19:00 +00:00
# include <Columns/ColumnString.h>
# include <Columns/ColumnFixedString.h>
# include <Columns/ColumnConst.h>
2023-09-09 18:38:12 +00:00
# include <Columns/ColumnAggregateFunction.h>
2017-04-01 09:19:00 +00:00
# include <Columns/ColumnArray.h>
# include <Columns/ColumnNullable.h>
# include <Columns/ColumnTuple.h>
2020-10-10 06:49:03 +00:00
# include <Columns/ColumnMap.h>
2021-08-10 01:33:57 +00:00
# include <Columns/ColumnObject.h>
2018-02-22 21:20:02 +00:00
# include <Columns/ColumnsCommon.h>
2023-12-19 16:43:30 +00:00
# include <Columns/ColumnVariant.h>
2021-08-18 20:34:07 +00:00
# include <Columns/ColumnStringHelpers.h>
2019-08-21 02:28:04 +00:00
# include <Common/assert_cast.h>
2023-04-05 12:25:51 +00:00
# include <Common/Concepts.h>
2020-11-18 09:38:03 +00:00
# include <Common/quoteString.h>
2023-01-03 00:24:56 +00:00
# include <Common/Exception.h>
2020-11-05 19:09:17 +00:00
# include <Core/AccurateComparison.h>
2019-12-12 14:16:59 +00:00
# include <Functions/IFunctionAdaptors.h>
2017-07-21 06:35:58 +00:00
# include <Functions/FunctionHelpers.h>
2018-09-26 00:31:40 +00:00
# include <Functions/DateTimeTransforms.h>
2020-12-05 13:18:56 +00:00
# include <Functions/toFixedString.h>
2020-04-17 13:26:44 +00:00
# include <Functions/TransformDateTime64.h>
2022-03-12 17:12:05 +00:00
# include <Functions/FunctionsCodingIP.h>
2023-11-10 04:23:50 +00:00
# include <Functions/CastOverloadResolver.h>
2018-09-27 15:55:22 +00:00
# include <DataTypes/DataTypeLowCardinality.h>
# include <Columns/ColumnLowCardinality.h>
2020-12-05 13:18:56 +00:00
# include <Interpreters/Context.h>
2022-03-25 21:00:00 +00:00
# include <Common/HashTable/HashMap.h>
2022-11-14 14:17:17 +00:00
# include <DataTypes/DataTypeIPv4andIPv6.h>
2023-05-10 23:55:13 +00:00
# include <Common/IPv6ToBinary.h>
2022-09-20 02:05:18 +00:00
# include <Core/Types.h>
2011-10-15 23:40:56 +00:00
namespace DB
{
2016-01-12 02:21:15 +00:00
namespace ErrorCodes
{
2016-08-17 06:55:47 +00:00
extern const int ATTEMPT_TO_READ_AFTER_EOF ;
2016-01-12 02:21:15 +00:00
extern const int CANNOT_PARSE_NUMBER ;
2016-08-17 06:55:47 +00:00
extern const int CANNOT_READ_ARRAY_FROM_TEXT ;
extern const int CANNOT_PARSE_INPUT_ASSERTION_FAILED ;
extern const int CANNOT_PARSE_QUOTED_STRING ;
extern const int CANNOT_PARSE_ESCAPE_SEQUENCE ;
extern const int CANNOT_PARSE_DATE ;
extern const int CANNOT_PARSE_DATETIME ;
2016-08-17 07:40:06 +00:00
extern const int CANNOT_PARSE_TEXT ;
2017-06-15 09:12:32 +00:00
extern const int CANNOT_PARSE_UUID ;
2022-11-14 14:17:17 +00:00
extern const int CANNOT_PARSE_IPV4 ;
extern const int CANNOT_PARSE_IPV6 ;
2018-12-07 03:20:27 +00:00
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION ;
2018-02-18 02:22:32 +00:00
extern const int LOGICAL_ERROR ;
extern const int TYPE_MISMATCH ;
extern const int CANNOT_CONVERT_TYPE ;
extern const int ILLEGAL_COLUMN ;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH ;
extern const int ILLEGAL_TYPE_OF_ARGUMENT ;
extern const int NOT_IMPLEMENTED ;
2018-02-22 21:20:02 +00:00
extern const int CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN ;
2023-09-12 13:34:21 +00:00
extern const int CANNOT_PARSE_BOOL ;
2023-10-23 13:01:45 +00:00
extern const int VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE ;
}
2016-08-30 16:14:05 +00:00
2016-07-30 04:39:51 +00:00
/** Type conversion functions.
* toType - conversion in " natural way " ;
2011-10-15 23:40:56 +00:00
*/
2018-08-21 18:25:38 +00:00
inline UInt32 extractToDecimalScale ( const ColumnWithTypeAndName & named_column )
{
const auto * arg_type = named_column . type . get ( ) ;
bool ok = checkAndGetDataType < DataTypeUInt64 > ( arg_type )
| | checkAndGetDataType < DataTypeUInt32 > ( arg_type )
| | checkAndGetDataType < DataTypeUInt16 > ( arg_type )
| | checkAndGetDataType < DataTypeUInt8 > ( arg_type ) ;
if ( ! ok )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type of toDecimal() scale { } " , named_column.type->getName());
2018-08-21 18:25:38 +00:00
Field field ;
named_column . column - > get ( 0 , field ) ;
2022-10-07 10:46:45 +00:00
return static_cast < UInt32 > ( field . get < UInt32 > ( ) ) ;
2018-08-21 18:25:38 +00:00
}
2020-11-24 21:11:28 +00:00
/// Function toUnixTimestamp has exactly the same implementation as toDateTime of String type.
struct NameToUnixTimestamp { static constexpr auto name = " toUnixTimestamp " ; } ;
2020-12-05 13:18:56 +00:00
struct AccurateConvertStrategyAdditions
2020-11-05 19:09:17 +00:00
{
2020-11-12 11:27:02 +00:00
UInt32 scale { 0 } ;
2020-11-05 19:09:17 +00:00
} ;
2020-12-05 13:18:56 +00:00
struct AccurateOrNullConvertStrategyAdditions
2020-11-05 19:09:17 +00:00
{
2020-12-05 13:18:56 +00:00
UInt32 scale { 0 } ;
2020-11-05 19:09:17 +00:00
} ;
2011-10-15 23:40:56 +00:00
2020-12-23 21:57:33 +00:00
struct ConvertDefaultBehaviorTag { } ;
struct ConvertReturnNullOnErrorTag { } ;
2022-11-21 07:45:42 +00:00
struct ConvertReturnZeroOnErrorTag { } ;
2020-12-23 21:57:33 +00:00
2016-07-30 04:39:51 +00:00
/** Conversion of number types to each other, enums to numbers, dates and datetimes to numbers and back: done by straight assignment.
* ( Date is represented internally as number of days from some day ; DateTime - as unix timestamp )
2011-10-15 23:40:56 +00:00
*/
2023-10-19 09:11:45 +00:00
template < typename FromDataType , typename ToDataType , typename Name ,
2023-10-23 23:49:40 +00:00
typename SpecialTag = ConvertDefaultBehaviorTag ,
FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior >
2011-10-16 01:57:10 +00:00
struct ConvertImpl
2011-10-15 23:40:56 +00:00
{
2016-05-28 10:35:44 +00:00
using FromFieldType = typename FromDataType : : FieldType ;
using ToFieldType = typename ToDataType : : FieldType ;
2017-04-01 07:20:54 +00:00
2018-08-31 08:59:21 +00:00
template < typename Additions = void * >
2020-10-19 18:37:44 +00:00
static ColumnPtr NO_SANITIZE_UNDEFINED execute (
2021-05-03 19:56:40 +00:00
const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type [[maybe_unused]] , size_t input_rows_count ,
2020-10-17 14:23:37 +00:00
Additions additions [[maybe_unused]] = Additions ( ) )
2011-10-15 23:40:56 +00:00
{
2020-10-17 14:23:37 +00:00
const ColumnWithTypeAndName & named_from = arguments [ 0 ] ;
2018-08-21 18:25:38 +00:00
2019-10-24 10:44:38 +00:00
using ColVecFrom = typename FromDataType : : ColumnType ;
using ColVecTo = typename ToDataType : : ColumnType ;
2018-08-27 16:16:16 +00:00
2019-10-24 10:44:38 +00:00
if constexpr ( ( IsDataTypeDecimal < FromDataType > | | IsDataTypeDecimal < ToDataType > )
& & ! ( std : : is_same_v < DataTypeDateTime64 , FromDataType > | | std : : is_same_v < DataTypeDateTime64 , ToDataType > ) )
2018-09-25 16:03:50 +00:00
{
if constexpr ( ! IsDataTypeDecimalOrNumber < FromDataType > | | ! IsDataTypeDecimalOrNumber < ToDataType > )
2019-10-24 10:44:38 +00:00
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
named_from . column - > getName ( ) , Name : : name ) ;
2019-10-24 10:44:38 +00:00
}
2018-09-25 16:03:50 +00:00
}
2018-08-27 16:16:16 +00:00
if ( const ColVecFrom * col_from = checkAndGetColumn < ColVecFrom > ( named_from . column . get ( ) ) )
2011-10-15 23:40:56 +00:00
{
2018-08-27 16:16:16 +00:00
typename ColVecTo : : MutablePtr col_to = nullptr ;
2022-01-27 10:07:53 +00:00
2018-09-10 13:52:18 +00:00
if constexpr ( IsDataTypeDecimal < ToDataType > )
2018-08-21 18:25:38 +00:00
{
2022-01-27 16:02:31 +00:00
UInt32 scale ;
2020-12-05 13:18:56 +00:00
if constexpr ( std : : is_same_v < Additions , AccurateConvertStrategyAdditions >
| | std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions > )
2020-11-05 19:09:17 +00:00
{
scale = additions . scale ;
}
else
{
scale = additions ;
}
2018-08-27 16:16:16 +00:00
col_to = ColVecTo : : create ( 0 , scale ) ;
2018-08-21 18:25:38 +00:00
}
2018-08-27 16:16:16 +00:00
else
col_to = ColVecTo : : create ( ) ;
2017-04-01 07:20:54 +00:00
2018-08-27 16:16:16 +00:00
const auto & vec_from = col_from - > getData ( ) ;
auto & vec_to = col_to - > getData ( ) ;
2021-05-03 19:56:40 +00:00
vec_to . resize ( input_rows_count ) ;
2017-04-01 07:20:54 +00:00
2020-11-05 19:09:17 +00:00
ColumnUInt8 : : MutablePtr col_null_map_to ;
ColumnUInt8 : : Container * vec_null_map_to [[maybe_unused]] = nullptr ;
2020-12-05 13:18:56 +00:00
if constexpr ( std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions > )
2020-11-05 19:09:17 +00:00
{
2021-05-03 19:56:40 +00:00
col_null_map_to = ColumnUInt8 : : create ( input_rows_count , false ) ;
2020-11-05 19:09:17 +00:00
vec_null_map_to = & col_null_map_to - > getData ( ) ;
}
2021-12-23 13:46:16 +00:00
bool result_is_bool = isBool ( result_type ) ;
2021-05-03 19:56:40 +00:00
for ( size_t i = 0 ; i < input_rows_count ; + + i )
2018-08-21 18:25:38 +00:00
{
2022-01-18 11:52:27 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeUInt8 > )
{
if ( result_is_bool )
{
vec_to [ i ] = vec_from [ i ] ! = FromFieldType ( 0 ) ;
continue ;
}
}
2023-08-16 20:12:31 +00:00
if constexpr ( std : : is_same_v < FromDataType , DataTypeUUID > & & std : : is_same_v < ToDataType , DataTypeUInt128 > )
2023-06-30 09:49:29 +00:00
{
2023-08-16 20:12:31 +00:00
static_assert (
std : : is_same_v < DataTypeUInt128 : : FieldType , DataTypeUUID : : FieldType : : UnderlyingType > ,
" UInt128 and UUID types must be same " ) ;
vec_to [ i ] . items [ 1 ] = vec_from [ i ] . toUnderType ( ) . items [ 0 ] ;
vec_to [ i ] . items [ 0 ] = vec_from [ i ] . toUnderType ( ) . items [ 1 ] ;
2023-06-30 09:49:29 +00:00
continue ;
}
2023-12-09 17:57:32 +00:00
if constexpr ( std : : is_same_v < FromDataType , DataTypeIPv6 > & & std : : is_same_v < ToDataType , DataTypeUInt128 > )
{
static_assert (
std : : is_same_v < DataTypeUInt128 : : FieldType , DataTypeUUID : : FieldType : : UnderlyingType > ,
" UInt128 and IPv6 types must be same " ) ;
vec_to [ i ] . items [ 1 ] = std : : byteswap ( vec_from [ i ] . toUnderType ( ) . items [ 0 ] ) ;
vec_to [ i ] . items [ 0 ] = std : : byteswap ( vec_from [ i ] . toUnderType ( ) . items [ 1 ] ) ;
continue ;
}
2021-05-03 15:41:37 +00:00
if constexpr ( std : : is_same_v < FromDataType , DataTypeUUID > ! = std : : is_same_v < ToDataType , DataTypeUUID > )
2021-01-04 22:31:34 +00:00
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : NOT_IMPLEMENTED ,
" Conversion between numeric types and UUID is not supported. "
" Probably the passed UUID is unquoted " ) ;
2021-01-04 22:31:34 +00:00
}
2022-11-18 20:10:27 +00:00
else if constexpr (
( std : : is_same_v < FromDataType , DataTypeIPv4 > ! = std : : is_same_v < ToDataType , DataTypeIPv4 > )
2023-05-11 16:17:52 +00:00
& & ! ( is_any_of < FromDataType , DataTypeUInt8 , DataTypeUInt16 , DataTypeUInt32 , DataTypeUInt64 , DataTypeIPv6 > | | is_any_of < ToDataType , DataTypeUInt32 , DataTypeUInt64 , DataTypeUInt128 , DataTypeUInt256 , DataTypeIPv6 > )
2022-11-18 20:10:27 +00:00
)
2022-11-14 14:17:17 +00:00
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : NOT_IMPLEMENTED , " Conversion from {} to {} is not supported " ,
TypeName < typename FromDataType : : FieldType > , TypeName < typename ToDataType : : FieldType > ) ;
2022-11-14 14:17:17 +00:00
}
2023-05-11 16:17:52 +00:00
else if constexpr ( std : : is_same_v < FromDataType , DataTypeIPv6 > ! = std : : is_same_v < ToDataType , DataTypeIPv6 > & & ! ( std : : is_same_v < ToDataType , DataTypeIPv4 > | | std : : is_same_v < FromDataType , DataTypeIPv4 > ) )
2022-11-14 14:17:17 +00:00
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : NOT_IMPLEMENTED ,
" Conversion between numeric types and IPv6 is not supported. "
" Probably the passed IPv6 is unquoted " ) ;
2022-11-14 14:17:17 +00:00
}
2020-12-05 14:55:37 +00:00
else
2020-08-19 11:52:17 +00:00
{
2020-12-05 13:18:56 +00:00
if constexpr ( IsDataTypeDecimal < FromDataType > | | IsDataTypeDecimal < ToDataType > )
2020-11-12 11:27:02 +00:00
{
2020-12-15 18:54:16 +00:00
if constexpr ( std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions > )
{
ToFieldType result ;
bool convert_result = false ;
if constexpr ( IsDataTypeDecimal < FromDataType > & & IsDataTypeDecimal < ToDataType > )
2022-01-27 16:02:31 +00:00
convert_result = tryConvertDecimals < FromDataType , ToDataType > ( vec_from [ i ] , col_from - > getScale ( ) , col_to - > getScale ( ) , result ) ;
2020-12-15 18:54:16 +00:00
else if constexpr ( IsDataTypeDecimal < FromDataType > & & IsDataTypeNumber < ToDataType > )
2022-01-27 16:02:31 +00:00
convert_result = tryConvertFromDecimal < FromDataType , ToDataType > ( vec_from [ i ] , col_from - > getScale ( ) , result ) ;
2020-12-15 18:54:16 +00:00
else if constexpr ( IsDataTypeNumber < FromDataType > & & IsDataTypeDecimal < ToDataType > )
2022-01-27 16:02:31 +00:00
convert_result = tryConvertToDecimal < FromDataType , ToDataType > ( vec_from [ i ] , col_to - > getScale ( ) , result ) ;
2020-12-15 18:54:16 +00:00
if ( convert_result )
vec_to [ i ] = result ;
else
2021-01-22 13:24:51 +00:00
{
vec_to [ i ] = static_cast < ToFieldType > ( 0 ) ;
2020-12-15 18:54:16 +00:00
( * vec_null_map_to ) [ i ] = true ;
2021-01-22 13:24:51 +00:00
}
2020-12-15 18:54:16 +00:00
}
else
2020-11-12 11:27:02 +00:00
{
2020-12-05 13:18:56 +00:00
if constexpr ( IsDataTypeDecimal < FromDataType > & & IsDataTypeDecimal < ToDataType > )
2022-01-27 16:02:31 +00:00
vec_to [ i ] = convertDecimals < FromDataType , ToDataType > ( vec_from [ i ] , col_from - > getScale ( ) , col_to - > getScale ( ) ) ;
2020-12-05 13:18:56 +00:00
else if constexpr ( IsDataTypeDecimal < FromDataType > & & IsDataTypeNumber < ToDataType > )
2022-01-27 16:02:31 +00:00
vec_to [ i ] = convertFromDecimal < FromDataType , ToDataType > ( vec_from [ i ] , col_from - > getScale ( ) ) ;
2020-12-05 13:18:56 +00:00
else if constexpr ( IsDataTypeNumber < FromDataType > & & IsDataTypeDecimal < ToDataType > )
2022-01-27 16:02:31 +00:00
vec_to [ i ] = convertToDecimal < FromDataType , ToDataType > ( vec_from [ i ] , col_to - > getScale ( ) ) ;
2020-12-05 13:18:56 +00:00
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Unsupported data type in conversion function " ) ;
2020-11-12 11:27:02 +00:00
}
2020-11-05 19:09:17 +00:00
}
2020-08-19 11:52:17 +00:00
else
2020-11-10 13:18:58 +00:00
{
2020-12-08 07:43:18 +00:00
/// If From Data is Nan or Inf and we convert to integer type, throw exception
if constexpr ( std : : is_floating_point_v < FromFieldType > & & ! std : : is_floating_point_v < ToFieldType > )
2020-11-10 13:18:58 +00:00
{
2020-12-05 13:18:56 +00:00
if ( ! isFinite ( vec_from [ i ] ) )
2020-11-10 13:18:58 +00:00
{
2020-12-05 13:18:56 +00:00
if constexpr ( std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions > )
{
2021-01-22 13:24:51 +00:00
vec_to [ i ] = 0 ;
2020-12-08 10:54:33 +00:00
( * vec_null_map_to ) [ i ] = true ;
2020-12-05 13:18:56 +00:00
continue ;
}
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Unexpected inf or nan to integer conversion " ) ;
2020-11-10 13:18:58 +00:00
}
}
2020-11-12 09:28:49 +00:00
2020-12-05 14:55:37 +00:00
if constexpr ( std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions >
2020-12-05 13:18:56 +00:00
| | std : : is_same_v < Additions , AccurateConvertStrategyAdditions > )
2020-11-10 13:18:58 +00:00
{
2020-12-05 13:18:56 +00:00
bool convert_result = accurate : : convertNumeric ( vec_from [ i ] , vec_to [ i ] ) ;
2020-11-12 09:28:49 +00:00
2020-12-05 13:18:56 +00:00
if ( ! convert_result )
{
if ( std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions > )
{
2021-01-22 13:24:51 +00:00
vec_to [ i ] = 0 ;
2020-12-05 13:18:56 +00:00
( * vec_null_map_to ) [ i ] = true ;
}
else
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Value in column {} cannot be safely converted into type {} " ,
named_from . column - > getName ( ) , result_type - > getName ( ) ) ;
2020-12-05 13:18:56 +00:00
}
}
}
else
2020-11-05 19:09:17 +00:00
{
2023-05-10 23:55:13 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeIPv4 > & & std : : is_same_v < FromDataType , DataTypeIPv6 > )
{
const uint8_t ip4_cidr [ ] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xff , 0xff , 0x00 , 0x00 , 0x00 , 0x00 } ;
const uint8_t * src = reinterpret_cast < const uint8_t * > ( & vec_from [ i ] . toUnderType ( ) ) ;
if ( ! matchIPv6Subnet ( src , ip4_cidr , 96 ) )
2023-05-31 15:26:58 +00:00
{
char addr [ IPV6_MAX_TEXT_LENGTH + 1 ] { } ;
char * paddr = addr ;
formatIPv6 ( src , paddr ) ;
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " IPv6 {} in column {} is not in IPv4 mapping block " , addr , named_from . column - > getName ( ) ) ;
}
2023-05-10 23:55:13 +00:00
uint8_t * dst = reinterpret_cast < uint8_t * > ( & vec_to [ i ] . toUnderType ( ) ) ;
if constexpr ( std : : endian : : native = = std : : endian : : little )
{
dst [ 0 ] = src [ 15 ] ;
dst [ 1 ] = src [ 14 ] ;
dst [ 2 ] = src [ 13 ] ;
dst [ 3 ] = src [ 12 ] ;
}
else
{
dst [ 0 ] = src [ 12 ] ;
2023-05-11 16:17:52 +00:00
dst [ 1 ] = src [ 13 ] ;
dst [ 2 ] = src [ 14 ] ;
dst [ 3 ] = src [ 15 ] ;
}
}
else if constexpr ( std : : is_same_v < ToDataType , DataTypeIPv6 > & & std : : is_same_v < FromDataType , DataTypeIPv4 > )
{
const uint8_t * src = reinterpret_cast < const uint8_t * > ( & vec_from [ i ] . toUnderType ( ) ) ;
uint8_t * dst = reinterpret_cast < uint8_t * > ( & vec_to [ i ] . toUnderType ( ) ) ;
std : : memset ( dst , ' \0 ' , IPV6_BINARY_LENGTH ) ;
dst [ 10 ] = dst [ 11 ] = 0xff ;
if constexpr ( std : : endian : : native = = std : : endian : : little )
{
dst [ 12 ] = src [ 3 ] ;
dst [ 13 ] = src [ 2 ] ;
dst [ 14 ] = src [ 1 ] ;
dst [ 15 ] = src [ 0 ] ;
}
else
{
dst [ 12 ] = src [ 0 ] ;
dst [ 13 ] = src [ 1 ] ;
dst [ 14 ] = src [ 2 ] ;
dst [ 15 ] = src [ 3 ] ;
2023-05-10 23:55:13 +00:00
}
}
else if constexpr ( std : : is_same_v < ToDataType , DataTypeIPv4 > & & std : : is_same_v < FromDataType , DataTypeUInt64 > )
2023-01-31 19:04:21 +00:00
vec_to [ i ] = static_cast < ToFieldType > ( static_cast < IPv4 : : UnderlyingType > ( vec_from [ i ] ) ) ;
2023-05-18 09:07:27 +00:00
else if constexpr ( std : : is_same_v < Name , NameToUnixTimestamp > & & ( std : : is_same_v < FromDataType , DataTypeDate > | | std : : is_same_v < FromDataType , DataTypeDate32 > ) )
2023-05-22 09:23:23 +00:00
vec_to [ i ] = static_cast < ToFieldType > ( vec_from [ i ] * DATE_SECONDS_PER_DAY ) ;
2023-01-31 19:04:21 +00:00
else
vec_to [ i ] = static_cast < ToFieldType > ( vec_from [ i ] ) ;
2020-11-05 19:09:17 +00:00
}
}
2020-08-19 11:52:17 +00:00
}
2018-08-21 18:25:38 +00:00
}
2017-12-16 04:59:32 +00:00
2020-12-05 13:18:56 +00:00
if constexpr ( std : : is_same_v < Additions , AccurateOrNullConvertStrategyAdditions > )
2020-11-05 19:09:17 +00:00
return ColumnNullable : : create ( std : : move ( col_to ) , std : : move ( col_null_map_to ) ) ;
else
return col_to ;
2011-10-15 23:40:56 +00:00
}
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
named_from . column - > getName ( ) , Name : : name ) ;
2011-10-15 23:40:56 +00:00
}
2011-10-16 01:57:10 +00:00
} ;
2019-10-30 13:59:38 +00:00
/** Conversion of DateTime to Date: throw off time component.
*/
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDateTime , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDateTime , DataTypeDate , ToDateImpl < date_time_overflow_behavior > , false > { } ;
2019-10-30 13:59:38 +00:00
2021-07-15 11:40:45 +00:00
/** Conversion of DateTime to Date32: throw off time component.
*/
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDateTime , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDateTime , DataTypeDate32 , ToDate32Impl , false > { } ;
2011-10-15 23:40:56 +00:00
2016-07-30 04:39:51 +00:00
/** Conversion of Date to DateTime: adding 00:00:00 time component.
2011-10-16 01:57:10 +00:00
*/
2023-10-23 23:49:40 +00:00
template < FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior >
2017-01-22 08:33:16 +00:00
struct ToDateTimeImpl
2011-10-16 01:57:10 +00:00
{
2017-10-29 04:18:48 +00:00
static constexpr auto name = " toDateTime " ;
2022-10-03 13:31:29 +00:00
static UInt32 execute ( UInt16 d , const DateLUTImpl & time_zone )
2011-10-15 23:40:56 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( d > MAX_DATETIME_DAY_NUM ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Day number {} is out of bounds of type DateTime " , d ) ;
2023-10-19 09:11:45 +00:00
}
2023-10-23 23:49:40 +00:00
else if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Saturate )
2023-10-19 09:11:45 +00:00
{
2023-10-23 23:49:40 +00:00
if ( d > MAX_DATETIME_DAY_NUM )
d = MAX_DATETIME_DAY_NUM ;
2023-10-19 09:11:45 +00:00
}
2022-10-21 20:48:09 +00:00
return static_cast < UInt32 > ( time_zone . fromDayNum ( DayNum ( d ) ) ) ;
2011-10-15 23:40:56 +00:00
}
2019-10-30 13:59:38 +00:00
2023-10-19 09:11:45 +00:00
static UInt32 execute ( Int32 d , const DateLUTImpl & time_zone )
2021-07-15 11:40:45 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Saturate )
2023-10-19 09:11:45 +00:00
{
if ( d < 0 )
return 0 ;
2023-10-23 23:49:40 +00:00
else if ( d > MAX_DATETIME_DAY_NUM )
d = MAX_DATETIME_DAY_NUM ;
2023-10-19 09:11:45 +00:00
}
2023-10-23 23:49:40 +00:00
else if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( d < 0 | | d > MAX_DATETIME_DAY_NUM ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Value {} is out of bounds of type DateTime " , d ) ;
2023-10-19 09:11:45 +00:00
}
return static_cast < UInt32 > ( time_zone . fromDayNum ( ExtendedDayNum ( d ) ) ) ;
2021-07-15 11:40:45 +00:00
}
2022-10-03 13:31:29 +00:00
static UInt32 execute ( UInt32 dt , const DateLUTImpl & /*time_zone*/ )
2019-10-30 13:59:38 +00:00
{
2020-04-17 13:26:44 +00:00
return dt ;
}
2023-10-19 09:11:45 +00:00
static UInt32 execute ( Int64 dt64 , const DateLUTImpl & /*time_zone*/ )
2020-04-17 13:26:44 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Ignore )
2023-10-19 09:11:45 +00:00
return static_cast < UInt32 > ( dt64 ) ;
else
{
2023-10-23 23:49:40 +00:00
if ( dt64 < 0 | | dt64 > = MAX_DATETIME_TIMESTAMP )
2023-10-19 09:11:45 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Saturate )
2023-10-19 09:11:45 +00:00
return dt64 < 0 ? 0 : std : : numeric_limits < UInt32 > : : max ( ) ;
2023-10-26 23:13:52 +00:00
else
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Value {} is out of bounds of type DateTime " , dt64 ) ;
2023-10-19 09:11:45 +00:00
}
else
return static_cast < UInt32 > ( dt64 ) ;
}
2019-10-30 13:59:38 +00:00
}
2011-10-16 01:57:10 +00:00
} ;
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDate , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDate , DataTypeDateTime , ToDateTimeImpl < date_time_overflow_behavior > , false > { } ;
2015-03-16 04:44:53 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDate32 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDate32 , DataTypeDateTime , ToDateTimeImpl < date_time_overflow_behavior > , false > { } ;
2021-07-15 11:40:45 +00:00
2017-01-22 08:33:16 +00:00
/// Implementation of toDate function.
2015-03-16 04:44:53 +00:00
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2015-10-22 15:31:42 +00:00
struct ToDateTransform32Or64
{
2017-10-29 04:18:48 +00:00
static constexpr auto name = " toDate " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & time_zone )
2015-10-22 15:31:42 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from > MAX_DATETIME_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Value {} is out of bounds of type Date " , from ) ;
2023-10-19 09:11:45 +00:00
}
/// if value is smaller (or equal) than maximum day value for Date, than treat it as day num,
/// otherwise treat it as unix timestamp. This is a bit weird, but we leave this behavior.
if ( from < = DATE_LUT_MAX_DAY_NUM )
return from ;
else
2023-10-23 23:49:40 +00:00
return time_zone . toDayNum ( std : : min ( time_t ( from ) , time_t ( MAX_DATETIME_TIMESTAMP ) ) ) ;
2015-10-22 15:31:42 +00:00
}
} ;
2023-10-19 09:11:45 +00:00
/** Conversion of Date32 to Date.
*/
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDate32 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDate32 , DataTypeDate , ToDateImpl < date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2020-08-07 17:38:42 +00:00
struct ToDateTransform32Or64Signed
{
static constexpr auto name = " toDate " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & time_zone )
2020-08-07 17:38:42 +00:00
{
2020-04-17 13:26:44 +00:00
// TODO: decide narrow or extended range based on FromType
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from < 0 | | from > MAX_DATE_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Value {} is out of bounds of type Date " , from ) ;
2023-10-19 09:11:45 +00:00
}
else
{
if ( from < 0 )
return 0 ;
}
2023-02-04 11:16:18 +00:00
return ( from < = DATE_LUT_MAX_DAY_NUM )
2022-10-17 02:57:15 +00:00
? static_cast < ToType > ( from )
2023-10-19 09:11:45 +00:00
: time_zone . toDayNum ( std : : min ( time_t ( from ) , time_t ( MAX_DATE_TIMESTAMP ) ) ) ;
2020-08-07 17:38:42 +00:00
}
} ;
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2020-08-07 17:38:42 +00:00
struct ToDateTransform8Or16Signed
{
static constexpr auto name = " toDate " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & )
2020-08-07 17:38:42 +00:00
{
2020-08-07 19:52:21 +00:00
if ( from < 0 )
2023-10-19 09:11:45 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Value {} is out of bounds of type Date " , from ) ;
2023-10-19 09:11:45 +00:00
else
return 0 ;
}
2020-08-07 17:38:42 +00:00
return from ;
}
} ;
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDateTime64 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDateTime64 , DataTypeDate32 , TransformDateTime64 < ToDate32Impl > , false > { } ;
2021-09-30 07:06:08 +00:00
2021-07-15 11:40:45 +00:00
/// Implementation of toDate32 function.
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2021-07-15 11:40:45 +00:00
struct ToDate32Transform32Or64
{
static constexpr auto name = " toDate32 " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & time_zone )
2021-07-15 11:40:45 +00:00
{
2023-10-19 09:11:45 +00:00
if ( from < DATE_LUT_MAX_EXTEND_DAY_NUM )
return static_cast < ToType > ( from ) ;
else
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from > MAX_DATETIME64_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type Date32 " , from ) ;
2023-10-19 09:11:45 +00:00
}
2023-10-23 23:49:40 +00:00
return time_zone . toDayNum ( std : : min ( time_t ( from ) , time_t ( MAX_DATETIME64_TIMESTAMP ) ) ) ;
2023-10-19 09:11:45 +00:00
}
2021-07-15 11:40:45 +00:00
}
} ;
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2021-07-15 11:40:45 +00:00
struct ToDate32Transform32Or64Signed
{
static constexpr auto name = " toDate32 " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & time_zone )
2021-07-15 11:40:45 +00:00
{
2023-05-09 12:18:04 +00:00
static const Int32 daynum_min_offset = - static_cast < Int32 > ( time_zone . getDayNumOffsetEpoch ( ) ) ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from < daynum_min_offset | | from > MAX_DATETIME64_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type Date32 " , from ) ;
2023-10-19 09:11:45 +00:00
}
2021-07-15 11:40:45 +00:00
if ( from < daynum_min_offset )
return daynum_min_offset ;
2023-10-19 09:11:45 +00:00
2021-07-15 11:40:45 +00:00
return ( from < DATE_LUT_MAX_EXTEND_DAY_NUM )
2022-09-11 01:21:34 +00:00
? static_cast < ToType > ( from )
2023-10-23 23:49:40 +00:00
: time_zone . toDayNum ( std : : min ( time_t ( Int64 ( from ) ) , time_t ( MAX_DATETIME64_TIMESTAMP ) ) ) ;
2021-07-15 11:40:45 +00:00
}
} ;
template < typename FromType , typename ToType >
struct ToDate32Transform8Or16Signed
{
static constexpr auto name = " toDate32 " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & )
2021-07-15 11:40:45 +00:00
{
return from ;
}
} ;
2020-08-07 17:38:42 +00:00
/** Special case of converting Int8, Int16, (U)Int32 or (U)Int64 (and also, for convenience,
2023-10-19 09:11:45 +00:00
* Float32 , Float64 ) to Date . If the
2020-08-07 17:38:42 +00:00
* number is less than 65536 , then it is treated as DayNum , and if it ' s greater or equals to 65536 ,
* then treated as unix timestamp . If the number exceeds UInt32 , saturate to MAX_UINT32 then as DayNum .
2016-07-30 04:39:51 +00:00
* It ' s a bit illogical , as we actually have two functions in one .
* But allows to support frequent case ,
* when user write toDate ( UInt32 ) , expecting conversion of unix timestamp to Date .
* ( otherwise such usage would be frequent mistake ) .
2015-10-22 15:31:42 +00:00
*/
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeUInt32 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeUInt32 , DataTypeDate , ToDateTransform32Or64 < UInt32 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2021-07-15 11:40:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeUInt64 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeUInt64 , DataTypeDate , ToDateTransform32Or64 < UInt64 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2020-08-07 17:38:42 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt8 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt8 , DataTypeDate , ToDateTransform8Or16Signed < Int8 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt16 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt16 , DataTypeDate , ToDateTransform8Or16Signed < Int16 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt32 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt32 , DataTypeDate , ToDateTransform32Or64Signed < Int32 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt64 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt64 , DataTypeDate , ToDateTransform32Or64Signed < Int64 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat32 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat32 , DataTypeDate , ToDateTransform32Or64Signed < Float32 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat64 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat64 , DataTypeDate , ToDateTransform32Or64Signed < Float64 , UInt16 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeUInt32 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeUInt32 , DataTypeDate32 , ToDate32Transform32Or64 < UInt32 , Int32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeUInt64 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeUInt64 , DataTypeDate32 , ToDate32Transform32Or64 < UInt64 , Int32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt8 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2023-10-19 09:11:45 +00:00
: DateTimeTransformImpl < DataTypeInt8 , DataTypeDate32 , ToDate32Transform8Or16Signed < Int8 , Int32 > , false > { } ;
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt16 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2023-10-19 09:11:45 +00:00
: DateTimeTransformImpl < DataTypeInt16 , DataTypeDate32 , ToDate32Transform8Or16Signed < Int16 , Int32 > , false > { } ;
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt32 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt32 , DataTypeDate32 , ToDate32Transform32Or64Signed < Int32 , Int32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt64 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt64 , DataTypeDate32 , ToDate32Transform32Or64Signed < Int64 , Int32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat32 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat32 , DataTypeDate32 , ToDate32Transform32Or64Signed < Float32 , Int32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat64 , DataTypeDate32 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat64 , DataTypeDate32 , ToDate32Transform32Or64Signed < Float64 , Int32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2020-08-07 17:38:42 +00:00
struct ToDateTimeTransform64
{
static constexpr auto name = " toDateTime " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & )
2020-08-07 17:38:42 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from > MAX_DATETIME_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type DateTime " , from ) ;
2023-10-19 09:11:45 +00:00
}
2023-10-23 23:49:40 +00:00
return static_cast < ToType > ( std : : min ( time_t ( from ) , time_t ( MAX_DATETIME_TIMESTAMP ) ) ) ;
2020-08-07 17:38:42 +00:00
}
} ;
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2020-08-07 17:38:42 +00:00
struct ToDateTimeTransformSigned
{
static constexpr auto name = " toDateTime " ;
2022-10-03 13:31:29 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & )
2020-08-07 17:38:42 +00:00
{
2020-08-07 19:53:52 +00:00
if ( from < 0 )
2023-10-19 09:11:45 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type DateTime " , from ) ;
2023-10-19 09:11:45 +00:00
else
return 0 ;
}
2020-08-07 17:38:42 +00:00
return from ;
}
} ;
2023-10-23 23:49:40 +00:00
template < typename FromType , typename ToType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2020-08-07 17:38:42 +00:00
struct ToDateTimeTransform64Signed
{
static constexpr auto name = " toDateTime " ;
2022-10-17 02:57:15 +00:00
static NO_SANITIZE_UNDEFINED ToType execute ( const FromType & from , const DateLUTImpl & )
2020-08-07 17:38:42 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from < 0 | | from > MAX_DATETIME_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type DateTime " , from ) ;
2023-10-19 09:11:45 +00:00
}
2020-08-07 19:53:52 +00:00
if ( from < 0 )
return 0 ;
2023-10-23 23:49:40 +00:00
return static_cast < ToType > ( std : : min ( time_t ( from ) , time_t ( MAX_DATETIME_TIMESTAMP ) ) ) ;
2020-08-07 17:38:42 +00:00
}
} ;
2023-10-19 09:11:45 +00:00
/// Special case of converting Int8, Int16, Int32 or (U)Int64 (and also, for convenience, Float32, Float64) to DateTime.
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt8 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt8 , DataTypeDateTime , ToDateTimeTransformSigned < Int8 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt16 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt16 , DataTypeDateTime , ToDateTimeTransformSigned < Int16 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt32 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt32 , DataTypeDateTime , ToDateTimeTransformSigned < Int32 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt64 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt64 , DataTypeDateTime , ToDateTimeTransform64Signed < Int64 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeUInt64 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeUInt64 , DataTypeDateTime , ToDateTimeTransform64 < UInt64 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat32 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat32 , DataTypeDateTime , ToDateTimeTransform64Signed < Float32 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat64 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat64 , DataTypeDateTime , ToDateTimeTransform64Signed < Float64 , UInt32 , default_date_time_overflow_behavior > , false > { } ;
2019-10-30 13:59:38 +00:00
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
/** Conversion of numeric to DateTime64
*/
2023-10-23 23:49:40 +00:00
template < typename FromType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
struct ToDateTime64TransformUnsigned
{
static constexpr auto name = " toDateTime64 " ;
const DateTime64 : : NativeType scale_multiplier = 1 ;
2022-03-12 18:05:50 +00:00
ToDateTime64TransformUnsigned ( UInt32 scale = 0 ) /// NOLINT
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
: scale_multiplier ( DecimalUtils : : scaleMultiplier < DateTime64 : : NativeType > ( scale ) )
{ }
2022-10-03 13:31:29 +00:00
NO_SANITIZE_UNDEFINED DateTime64 : : NativeType execute ( FromType from , const DateLUTImpl & ) const
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from > MAX_DATETIME64_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type DateTime64 " , from ) ;
2023-10-19 09:11:45 +00:00
else
return DecimalUtils : : decimalFromComponentsWithMultiplier < DateTime64 > ( from , 0 , scale_multiplier ) ;
}
else
2023-10-23 23:49:40 +00:00
return DecimalUtils : : decimalFromComponentsWithMultiplier < DateTime64 > ( std : : min < time_t > ( from , MAX_DATETIME64_TIMESTAMP ) , 0 , scale_multiplier ) ;
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
}
} ;
2023-10-23 23:49:40 +00:00
template < typename FromType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
struct ToDateTime64TransformSigned
{
static constexpr auto name = " toDateTime64 " ;
const DateTime64 : : NativeType scale_multiplier = 1 ;
2022-03-12 18:05:50 +00:00
ToDateTime64TransformSigned ( UInt32 scale = 0 ) /// NOLINT
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
: scale_multiplier ( DecimalUtils : : scaleMultiplier < DateTime64 : : NativeType > ( scale ) )
{ }
2022-10-03 13:31:29 +00:00
NO_SANITIZE_UNDEFINED DateTime64 : : NativeType execute ( FromType from , const DateLUTImpl & ) const
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from < MIN_DATETIME64_TIMESTAMP | | from > MAX_DATETIME64_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type DateTime64 " , from ) ;
2023-10-19 09:11:45 +00:00
}
2023-10-23 23:49:40 +00:00
from = static_cast < FromType > ( std : : max < time_t > ( from , MIN_DATETIME64_TIMESTAMP ) ) ;
from = static_cast < FromType > ( std : : min < time_t > ( from , MAX_DATETIME64_TIMESTAMP ) ) ;
2023-10-19 09:11:45 +00:00
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
return DecimalUtils : : decimalFromComponentsWithMultiplier < DateTime64 > ( from , 0 , scale_multiplier ) ;
}
} ;
2023-10-23 23:49:40 +00:00
template < typename FromDataType , typename FromType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2021-02-21 18:09:51 +00:00
struct ToDateTime64TransformFloat
{
static constexpr auto name = " toDateTime64 " ;
const UInt32 scale = 1 ;
2022-03-12 18:05:50 +00:00
ToDateTime64TransformFloat ( UInt32 scale_ = 0 ) /// NOLINT
2021-02-21 18:09:51 +00:00
: scale ( scale_ )
{ }
2022-10-03 13:31:29 +00:00
NO_SANITIZE_UNDEFINED DateTime64 : : NativeType execute ( FromType from , const DateLUTImpl & ) const
2021-02-21 18:09:51 +00:00
{
2023-10-23 23:49:40 +00:00
if constexpr ( date_time_overflow_behavior = = FormatSettings : : DateTimeOverflowBehavior : : Throw )
2023-10-19 09:11:45 +00:00
{
2023-10-26 21:56:45 +00:00
if ( from < MIN_DATETIME64_TIMESTAMP | | from > MAX_DATETIME64_TIMESTAMP ) [[unlikely]]
2023-10-23 13:01:45 +00:00
throw Exception ( ErrorCodes : : VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE , " Timestamp value {} is out of bounds of type DateTime64 " , from ) ;
2023-10-19 09:11:45 +00:00
}
2023-10-23 23:49:40 +00:00
from = std : : max ( from , static_cast < FromType > ( MIN_DATETIME64_TIMESTAMP ) ) ;
from = std : : min ( from , static_cast < FromType > ( MAX_DATETIME64_TIMESTAMP ) ) ;
2021-02-21 18:09:51 +00:00
return convertToDecimal < FromDataType , DataTypeDateTime64 > ( from , scale ) ;
}
} ;
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt8 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt8 , DataTypeDateTime64 , ToDateTime64TransformSigned < Int8 , date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt16 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt16 , DataTypeDateTime64 , ToDateTime64TransformSigned < Int16 , date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt32 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt32 , DataTypeDateTime64 , ToDateTime64TransformSigned < Int32 , date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeInt64 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeInt64 , DataTypeDateTime64 , ToDateTime64TransformSigned < Int64 , date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeUInt64 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeUInt64 , DataTypeDateTime64 , ToDateTime64TransformUnsigned < UInt64 , date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat32 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat32 , DataTypeDateTime64 , ToDateTime64TransformFloat < DataTypeFloat32 , Float32 , date_time_overflow_behavior > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeFloat64 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeFloat64 , DataTypeDateTime64 , ToDateTime64TransformFloat < DataTypeFloat64 , Float64 , date_time_overflow_behavior > , false > { } ;
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
2020-04-17 13:26:44 +00:00
2019-10-31 08:23:55 +00:00
/** Conversion of DateTime64 to Date or DateTime: discards fractional part.
*/
template < typename Transform >
struct FromDateTime64Transform
{
2019-12-11 08:56:32 +00:00
static constexpr auto name = Transform : : name ;
2019-10-31 08:23:55 +00:00
const DateTime64 : : NativeType scale_multiplier = 1 ;
2022-03-12 18:05:50 +00:00
FromDateTime64Transform ( UInt32 scale ) /// NOLINT
2019-12-06 21:05:31 +00:00
: scale_multiplier ( DecimalUtils : : scaleMultiplier < DateTime64 : : NativeType > ( scale ) )
2019-10-31 08:23:55 +00:00
{ }
2022-10-03 13:31:29 +00:00
auto execute ( DateTime64 : : NativeType dt , const DateLUTImpl & time_zone ) const
2019-10-31 08:23:55 +00:00
{
2019-12-06 21:05:31 +00:00
const auto c = DecimalUtils : : splitWithScaleMultiplier ( DateTime64 ( dt ) , scale_multiplier ) ;
2019-10-31 08:23:55 +00:00
return Transform : : execute ( static_cast < UInt32 > ( c . whole ) , time_zone ) ;
}
} ;
2022-10-17 02:57:15 +00:00
/** Conversion of DateTime64 to Date or DateTime: discards fractional part.
*/
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDateTime64 , DataTypeDate , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDateTime64 , DataTypeDate , TransformDateTime64 < ToDateImpl < date_time_overflow_behavior > > , false > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDateTime64 , DataTypeDateTime , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
: DateTimeTransformImpl < DataTypeDateTime64 , DataTypeDateTime , TransformDateTime64 < ToDateTimeImpl < date_time_overflow_behavior > > , false > { } ;
2020-04-17 13:26:44 +00:00
struct ToDateTime64Transform
{
static constexpr auto name = " toDateTime64 " ;
const DateTime64 : : NativeType scale_multiplier = 1 ;
2022-03-12 18:05:50 +00:00
ToDateTime64Transform ( UInt32 scale = 0 ) /// NOLINT
2020-04-17 13:26:44 +00:00
: scale_multiplier ( DecimalUtils : : scaleMultiplier < DateTime64 : : NativeType > ( scale ) )
{ }
2022-10-03 13:31:29 +00:00
DateTime64 : : NativeType execute ( UInt16 d , const DateLUTImpl & time_zone ) const
2020-04-17 13:26:44 +00:00
{
2023-10-19 09:11:45 +00:00
const auto dt = ToDateTimeImpl < > : : execute ( d , time_zone ) ;
2020-04-17 13:26:44 +00:00
return execute ( dt , time_zone ) ;
}
2022-10-03 13:31:29 +00:00
DateTime64 : : NativeType execute ( Int32 d , const DateLUTImpl & time_zone ) const
2021-09-30 07:06:08 +00:00
{
2023-10-19 09:11:45 +00:00
Int64 dt = static_cast < Int64 > ( time_zone . fromDayNum ( ExtendedDayNum ( d ) ) ) ;
2021-10-20 03:36:07 +00:00
return DecimalUtils : : decimalFromComponentsWithMultiplier < DateTime64 > ( dt , 0 , scale_multiplier ) ;
2021-09-30 07:06:08 +00:00
}
2022-10-03 13:31:29 +00:00
DateTime64 : : NativeType execute ( UInt32 dt , const DateLUTImpl & /*time_zone*/ ) const
2020-04-17 13:26:44 +00:00
{
return DecimalUtils : : decimalFromComponentsWithMultiplier < DateTime64 > ( dt , 0 , scale_multiplier ) ;
}
} ;
/** Conversion of Date or DateTime to DateTime64: add zero sub-second part.
*/
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDate , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2020-04-17 13:26:44 +00:00
: DateTimeTransformImpl < DataTypeDate , DataTypeDateTime64 , ToDateTime64Transform > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDate32 , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2021-09-30 07:06:08 +00:00
: DateTimeTransformImpl < DataTypeDate32 , DataTypeDateTime64 , ToDateTime64Transform > { } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct ConvertImpl < DataTypeDateTime , DataTypeDateTime64 , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2020-04-17 13:26:44 +00:00
: DateTimeTransformImpl < DataTypeDateTime , DataTypeDateTime64 , ToDateTime64Transform > { } ;
2015-03-16 04:44:53 +00:00
2016-07-30 04:39:51 +00:00
/** Transformation of numbers, dates, datetimes to strings: through formatting.
2011-10-16 01:57:10 +00:00
*/
2016-07-30 04:39:51 +00:00
template < typename DataType >
struct FormatImpl
2015-12-22 13:07:40 +00:00
{
2021-07-12 13:02:15 +00:00
template < typename ReturnType = void >
static ReturnType execute ( const typename DataType : : FieldType x , WriteBuffer & wb , const DataType * , const DateLUTImpl * )
2015-12-22 13:07:40 +00:00
{
writeText ( x , wb ) ;
2021-07-12 13:02:15 +00:00
return ReturnType ( true ) ;
2015-12-22 13:07:40 +00:00
}
} ;
2016-07-30 04:39:51 +00:00
template < >
struct FormatImpl < DataTypeDate >
2015-12-22 13:07:40 +00:00
{
2021-07-12 13:02:15 +00:00
template < typename ReturnType = void >
2023-05-05 12:12:14 +00:00
static ReturnType execute ( const DataTypeDate : : FieldType x , WriteBuffer & wb , const DataTypeDate * , const DateLUTImpl * time_zone )
2015-12-22 13:07:40 +00:00
{
2023-05-05 12:12:14 +00:00
writeDateText ( DayNum ( x ) , wb , * time_zone ) ;
2021-07-12 13:02:15 +00:00
return ReturnType ( true ) ;
2015-12-22 13:07:40 +00:00
}
} ;
2021-07-15 11:40:45 +00:00
template < >
struct FormatImpl < DataTypeDate32 >
{
template < typename ReturnType = void >
2023-05-05 12:12:14 +00:00
static ReturnType execute ( const DataTypeDate32 : : FieldType x , WriteBuffer & wb , const DataTypeDate32 * , const DateLUTImpl * time_zone )
2021-07-15 11:40:45 +00:00
{
2023-05-05 12:12:14 +00:00
writeDateText ( ExtendedDayNum ( x ) , wb , * time_zone ) ;
2021-07-15 11:40:45 +00:00
return ReturnType ( true ) ;
}
} ;
2016-07-30 04:39:51 +00:00
template < >
struct FormatImpl < DataTypeDateTime >
2015-12-22 13:07:40 +00:00
{
2021-07-12 13:02:15 +00:00
template < typename ReturnType = void >
static ReturnType execute ( const DataTypeDateTime : : FieldType x , WriteBuffer & wb , const DataTypeDateTime * , const DateLUTImpl * time_zone )
2015-12-22 13:07:40 +00:00
{
2017-01-22 08:33:16 +00:00
writeDateTimeText ( x , wb , * time_zone ) ;
2021-07-12 13:02:15 +00:00
return ReturnType ( true ) ;
2015-12-22 13:07:40 +00:00
}
} ;
2019-05-04 00:07:54 +00:00
template < >
struct FormatImpl < DataTypeDateTime64 >
{
2021-07-12 13:02:15 +00:00
template < typename ReturnType = void >
static ReturnType execute ( const DataTypeDateTime64 : : FieldType x , WriteBuffer & wb , const DataTypeDateTime64 * type , const DateLUTImpl * time_zone )
2019-05-04 00:07:54 +00:00
{
2019-10-30 13:59:38 +00:00
writeDateTimeText ( DateTime64 ( x ) , type - > getScale ( ) , wb , * time_zone ) ;
2021-07-12 13:02:15 +00:00
return ReturnType ( true ) ;
2019-05-04 00:07:54 +00:00
}
} ;
2016-07-30 04:39:51 +00:00
template < typename FieldType >
struct FormatImpl < DataTypeEnum < FieldType > >
2015-12-22 13:07:40 +00:00
{
2021-07-12 13:02:15 +00:00
template < typename ReturnType = void >
static ReturnType execute ( const FieldType x , WriteBuffer & wb , const DataTypeEnum < FieldType > * type , const DateLUTImpl * )
2015-12-22 13:07:40 +00:00
{
2021-07-12 13:02:15 +00:00
static constexpr bool throw_exception = std : : is_same_v < ReturnType , void > ;
2011-10-16 03:05:15 +00:00
2021-07-12 13:02:15 +00:00
if constexpr ( throw_exception )
{
writeString ( type - > getNameForValue ( x ) , wb ) ;
}
else
{
StringRef res ;
bool is_ok = type - > getNameForValue ( x , res ) ;
if ( is_ok )
writeString ( res , wb ) ;
return ReturnType ( is_ok ) ;
}
2015-12-22 13:07:40 +00:00
}
} ;
2011-10-16 03:05:15 +00:00
2018-08-21 18:25:38 +00:00
template < typename FieldType >
struct FormatImpl < DataTypeDecimal < FieldType > >
{
2021-07-12 13:02:15 +00:00
template < typename ReturnType = void >
static ReturnType execute ( const FieldType x , WriteBuffer & wb , const DataTypeDecimal < FieldType > * type , const DateLUTImpl * )
2018-08-21 18:25:38 +00:00
{
2021-08-16 08:03:23 +00:00
writeText ( x , type - > getScale ( ) , wb , false ) ;
2021-07-12 13:02:15 +00:00
return ReturnType ( true ) ;
2018-08-21 18:25:38 +00:00
}
} ;
2015-12-30 11:53:12 +00:00
/// DataTypeEnum<T> to DataType<T> free conversion
template < typename FieldType , typename Name >
2020-12-23 21:57:33 +00:00
struct ConvertImpl < DataTypeEnum < FieldType > , DataTypeNumber < FieldType > , Name , ConvertDefaultBehaviorTag >
2015-12-30 11:53:12 +00:00
{
2020-11-17 13:24:45 +00:00
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & , size_t /*input_rows_count*/ )
2015-12-30 11:53:12 +00:00
{
2020-10-17 14:23:37 +00:00
return arguments [ 0 ] . column ;
2015-12-30 11:53:12 +00:00
}
} ;
2023-03-17 09:33:18 +00:00
static inline ColumnUInt8 : : MutablePtr copyNullMap ( ColumnPtr col )
2021-07-12 09:18:04 +00:00
{
ColumnUInt8 : : MutablePtr null_map = nullptr ;
if ( const auto * col_null = checkAndGetColumn < ColumnNullable > ( col . get ( ) ) )
{
null_map = ColumnUInt8 : : create ( ) ;
null_map - > insertRangeFrom ( col_null - > getNullMapColumn ( ) , 0 , col_null - > size ( ) ) ;
}
return null_map ;
}
2015-12-30 11:53:12 +00:00
2011-10-16 01:57:10 +00:00
template < typename FromDataType , typename Name >
2022-03-17 18:06:50 +00:00
requires ( ! std : : is_same_v < FromDataType , DataTypeString > )
struct ConvertImpl < FromDataType , DataTypeString , Name , ConvertDefaultBehaviorTag >
2011-10-16 01:57:10 +00:00
{
2016-05-28 10:35:44 +00:00
using FromFieldType = typename FromDataType : : FieldType ;
2021-09-10 11:49:22 +00:00
using ColVecType = ColumnVectorOrDecimal < FromFieldType > ;
2017-04-01 07:20:54 +00:00
2020-11-17 13:24:45 +00:00
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & , size_t /*input_rows_count*/ )
2011-10-16 01:57:10 +00:00
{
2023-09-07 12:41:01 +00:00
if constexpr ( IsDataTypeDateOrDateTime < FromDataType > )
2023-09-04 13:30:24 +00:00
{
2023-09-07 12:41:01 +00:00
auto datetime_arg = arguments [ 0 ] ;
2021-07-12 09:18:04 +00:00
2023-09-07 12:41:01 +00:00
const DateLUTImpl * time_zone = nullptr ;
const ColumnConst * time_zone_column = nullptr ;
2017-04-01 07:20:54 +00:00
2023-09-07 12:41:01 +00:00
if ( arguments . size ( ) = = 1 )
2023-09-01 14:01:06 +00:00
{
2023-09-07 12:41:01 +00:00
auto non_null_args = createBlockWithNestedColumns ( arguments ) ;
time_zone = & extractTimeZoneFromFunctionArguments ( non_null_args , 1 , 0 ) ;
}
else /// When we have a column for timezone
{
datetime_arg . column = datetime_arg . column - > convertToFullColumnIfConst ( ) ;
2023-05-09 12:18:04 +00:00
2023-09-07 12:41:01 +00:00
if constexpr ( std : : is_same_v < FromDataType , DataTypeDate > | | std : : is_same_v < FromDataType , DataTypeDate32 > )
time_zone = & DateLUT : : instance ( ) ;
/// For argument of Date or DateTime type, second argument with time zone could be specified.
if constexpr ( std : : is_same_v < FromDataType , DataTypeDateTime > | | std : : is_same_v < FromDataType , DataTypeDateTime64 > )
2023-09-06 13:33:11 +00:00
{
2023-09-07 12:41:01 +00:00
if ( ( time_zone_column = checkAndGetColumnConst < ColumnString > ( arguments [ 1 ] . column . get ( ) ) ) )
{
auto non_null_args = createBlockWithNestedColumns ( arguments ) ;
time_zone = & extractTimeZoneFromFunctionArguments ( non_null_args , 1 , 0 ) ;
}
2023-09-06 13:33:11 +00:00
}
2023-09-01 14:01:06 +00:00
}
2023-09-07 12:41:01 +00:00
const auto & col_with_type_and_name = columnGetNested ( datetime_arg ) ;
2017-04-01 07:20:54 +00:00
2023-09-07 12:41:01 +00:00
if ( const auto col_from = checkAndGetColumn < ColVecType > ( col_with_type_and_name . column . get ( ) ) )
{
auto col_to = ColumnString : : create ( ) ;
const typename ColVecType : : Container & vec_from = col_from - > getData ( ) ;
ColumnString : : Chars & data_to = col_to - > getChars ( ) ;
ColumnString : : Offsets & offsets_to = col_to - > getOffsets ( ) ;
size_t size = vec_from . size ( ) ;
if constexpr ( std : : is_same_v < FromDataType , DataTypeDate > )
data_to . resize ( size * ( strlen ( " YYYY-MM-DD " ) + 1 ) ) ;
else if constexpr ( std : : is_same_v < FromDataType , DataTypeDate32 > )
data_to . resize ( size * ( strlen ( " YYYY-MM-DD " ) + 1 ) ) ;
else if constexpr ( std : : is_same_v < FromDataType , DataTypeDateTime > )
data_to . resize ( size * ( strlen ( " YYYY-MM-DD hh:mm:ss " ) + 1 ) ) ;
else if constexpr ( std : : is_same_v < FromDataType , DataTypeDateTime64 > )
data_to . resize ( size * ( strlen ( " YYYY-MM-DD hh:mm:ss. " ) + col_from - > getScale ( ) + 1 ) ) ;
else
data_to . resize ( size * 3 ) ; /// Arbitrary
2017-04-01 07:20:54 +00:00
2023-09-07 12:41:01 +00:00
offsets_to . resize ( size ) ;
2017-11-17 23:45:49 +00:00
2023-09-07 12:41:01 +00:00
WriteBufferFromVector < ColumnString : : Chars > write_buffer ( data_to ) ;
const auto & type = static_cast < const FromDataType & > ( * col_with_type_and_name . type ) ;
2017-04-01 07:20:54 +00:00
2023-09-07 12:41:01 +00:00
ColumnUInt8 : : MutablePtr null_map = copyNullMap ( datetime_arg . column ) ;
2017-04-01 07:20:54 +00:00
2024-01-24 18:17:19 +00:00
if ( ! null_map & & arguments . size ( ) > 1 )
2024-01-25 14:47:32 +00:00
null_map = copyNullMap ( arguments [ 1 ] . column - > convertToFullColumnIfConst ( ) ) ;
2024-01-24 18:17:19 +00:00
2023-09-07 12:41:01 +00:00
if ( null_map )
2021-07-12 09:18:04 +00:00
{
2023-09-07 12:41:01 +00:00
for ( size_t i = 0 ; i < size ; + + i )
2023-09-01 14:01:06 +00:00
{
2023-09-07 12:41:01 +00:00
if ( ! time_zone_column & & arguments . size ( ) > 1 )
{
if ( ! arguments [ 1 ] . column . get ( ) - > getDataAt ( i ) . toString ( ) . empty ( ) )
time_zone = & DateLUT : : instance ( arguments [ 1 ] . column . get ( ) - > getDataAt ( i ) . toString ( ) ) ;
else
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Provided time zone must be non-empty " ) ;
}
bool is_ok = FormatImpl < FromDataType > : : template execute < bool > ( vec_from [ i ] , write_buffer , & type , time_zone ) ;
null_map - > getData ( ) [ i ] | = ! is_ok ;
writeChar ( 0 , write_buffer ) ;
offsets_to [ i ] = write_buffer . count ( ) ;
}
}
else
{
for ( size_t i = 0 ; i < size ; + + i )
{
if ( ! time_zone_column & & arguments . size ( ) > 1 )
{
if ( ! arguments [ 1 ] . column . get ( ) - > getDataAt ( i ) . toString ( ) . empty ( ) )
2024-01-25 12:01:51 +00:00
time_zone = & DateLUT : : instance ( arguments [ 1 ] . column . get ( ) - > getDataAt ( i ) . toString ( ) ) ;
2023-09-07 12:41:01 +00:00
else
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Provided time zone must be non-empty " ) ;
}
FormatImpl < FromDataType > : : template execute < void > ( vec_from [ i ] , write_buffer , & type , time_zone ) ;
writeChar ( 0 , write_buffer ) ;
offsets_to [ i ] = write_buffer . count ( ) ;
2023-09-01 14:01:06 +00:00
}
2021-07-12 09:18:04 +00:00
}
2023-09-07 12:41:01 +00:00
write_buffer . finalize ( ) ;
if ( null_map )
return ColumnNullable : : create ( std : : move ( col_to ) , std : : move ( null_map ) ) ;
return col_to ;
2021-07-13 06:52:31 +00:00
}
else
2023-09-07 12:41:01 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
arguments [ 0 ] . column - > getName ( ) , Name : : name ) ;
}
else
{
ColumnUInt8 : : MutablePtr null_map = copyNullMap ( arguments [ 0 ] . column ) ;
2023-09-08 10:54:25 +00:00
const auto & col_with_type_and_name = columnGetNested ( arguments [ 0 ] ) ;
2023-09-07 12:41:01 +00:00
const auto & type = static_cast < const FromDataType & > ( * col_with_type_and_name . type ) ;
if ( const auto col_from = checkAndGetColumn < ColVecType > ( col_with_type_and_name . column . get ( ) ) )
2021-07-13 06:52:31 +00:00
{
2023-09-07 12:41:01 +00:00
auto col_to = ColumnString : : create ( ) ;
const typename ColVecType : : Container & vec_from = col_from - > getData ( ) ;
ColumnString : : Chars & data_to = col_to - > getChars ( ) ;
ColumnString : : Offsets & offsets_to = col_to - > getOffsets ( ) ;
size_t size = vec_from . size ( ) ;
2023-09-08 10:54:25 +00:00
data_to . resize ( size * 3 ) ;
2023-09-07 12:41:01 +00:00
offsets_to . resize ( size ) ;
WriteBufferFromVector < ColumnString : : Chars > write_buffer ( data_to ) ;
if ( null_map )
2021-07-12 09:18:04 +00:00
{
2023-09-07 12:41:01 +00:00
for ( size_t i = 0 ; i < size ; + + i )
2023-09-01 14:01:06 +00:00
{
2023-09-08 13:36:33 +00:00
bool is_ok = FormatImpl < FromDataType > : : template execute < bool > ( vec_from [ i ] , write_buffer , & type , nullptr ) ;
/// We don't use timezones in this branch
2023-09-07 12:41:01 +00:00
null_map - > getData ( ) [ i ] | = ! is_ok ;
writeChar ( 0 , write_buffer ) ;
offsets_to [ i ] = write_buffer . count ( ) ;
}
}
else
{
for ( size_t i = 0 ; i < size ; + + i )
{
2023-09-08 13:36:33 +00:00
FormatImpl < FromDataType > : : template execute < void > ( vec_from [ i ] , write_buffer , & type , nullptr ) ;
2023-09-07 12:41:01 +00:00
writeChar ( 0 , write_buffer ) ;
offsets_to [ i ] = write_buffer . count ( ) ;
2023-09-01 14:01:06 +00:00
}
2021-07-12 09:18:04 +00:00
}
2017-04-01 07:20:54 +00:00
2023-09-07 12:41:01 +00:00
write_buffer . finalize ( ) ;
2021-07-12 09:18:04 +00:00
2023-09-07 12:41:01 +00:00
if ( null_map )
return ColumnNullable : : create ( std : : move ( col_to ) , std : : move ( null_map ) ) ;
return col_to ;
}
else
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
arguments [ 0 ] . column - > getName ( ) , Name : : name ) ;
2011-10-16 01:57:10 +00:00
}
}
} ;
2016-07-30 04:39:51 +00:00
2021-08-18 20:34:07 +00:00
/// Generic conversion of any type to String or FixedString via serialization to text.
template < typename StringColumnType >
2016-07-30 04:39:51 +00:00
struct ConvertImplGenericToString
{
2021-09-29 16:42:41 +00:00
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , size_t /*input_rows_count*/ )
2016-07-30 04:39:51 +00:00
{
2021-08-18 20:34:07 +00:00
static_assert ( std : : is_same_v < StringColumnType , ColumnString > | | std : : is_same_v < StringColumnType , ColumnFixedString > ,
" Can be used only to serialize to ColumnString or ColumnFixedString " ) ;
2021-07-12 10:15:30 +00:00
ColumnUInt8 : : MutablePtr null_map = copyNullMap ( arguments [ 0 ] . column ) ;
const auto & col_with_type_and_name = columnGetNested ( arguments [ 0 ] ) ;
2016-07-30 04:39:51 +00:00
const IDataType & type = * col_with_type_and_name . type ;
const IColumn & col_from = * col_with_type_and_name . column ;
2017-04-01 07:20:54 +00:00
2021-09-29 16:42:41 +00:00
size_t size = col_from . size ( ) ;
2022-03-30 11:19:03 +00:00
auto col_to = removeNullable ( result_type ) - > createColumn ( ) ;
2017-04-01 07:20:54 +00:00
2021-08-18 20:34:07 +00:00
{
ColumnStringHelpers : : WriteHelper write_helper (
assert_cast < StringColumnType & > ( * col_to ) ,
2021-09-29 16:42:41 +00:00
size ) ;
2017-04-01 07:20:54 +00:00
2021-08-18 20:34:07 +00:00
auto & write_buffer = write_helper . getWriteBuffer ( ) ;
2017-04-01 07:20:54 +00:00
2021-08-18 20:34:07 +00:00
FormatSettings format_settings ;
auto serialization = type . getDefaultSerialization ( ) ;
2023-11-15 15:53:38 +00:00
for ( size_t row = 0 ; row < size ; + + row )
2021-08-18 20:34:07 +00:00
{
2023-11-15 15:53:38 +00:00
serialization - > serializeText ( col_from , row , write_buffer , format_settings ) ;
2021-08-18 20:34:07 +00:00
write_helper . rowWritten ( ) ;
}
2021-09-29 16:42:41 +00:00
write_helper . finalize ( ) ;
2016-07-30 04:39:51 +00:00
}
2017-12-09 10:14:45 +00:00
2021-07-12 10:15:30 +00:00
if ( result_type - > isNullable ( ) & & null_map )
return ColumnNullable : : create ( std : : move ( col_to ) , std : : move ( null_map ) ) ;
2020-10-17 14:23:37 +00:00
return col_to ;
2016-07-30 04:39:51 +00:00
}
} ;
2022-03-02 07:06:38 +00:00
/** Conversion of time_t to UInt16, Int32, UInt32
*/
template < typename DataType >
void convertFromTime ( typename DataType : : FieldType & x , time_t & time )
{
x = time ;
}
template < >
inline void convertFromTime < DataTypeDate > ( DataTypeDate : : FieldType & x , time_t & time )
{
if ( unlikely ( time < 0 ) )
x = 0 ;
else if ( unlikely ( time > 0xFFFF ) )
x = 0xFFFF ;
else
x = time ;
}
template < >
inline void convertFromTime < DataTypeDate32 > ( DataTypeDate32 : : FieldType & x , time_t & time )
{
2022-10-07 10:46:45 +00:00
x = static_cast < UInt32 > ( time ) ;
2022-03-02 07:06:38 +00:00
}
template < >
inline void convertFromTime < DataTypeDateTime > ( DataTypeDateTime : : FieldType & x , time_t & time )
{
if ( unlikely ( time < 0 ) )
x = 0 ;
2023-10-23 23:49:40 +00:00
else if ( unlikely ( time > MAX_DATETIME_TIMESTAMP ) )
x = MAX_DATETIME_TIMESTAMP ;
2022-03-02 07:06:38 +00:00
else
2022-10-07 10:46:45 +00:00
x = static_cast < UInt32 > ( time ) ;
2022-03-02 07:06:38 +00:00
}
2016-07-30 04:39:51 +00:00
2017-01-22 08:33:16 +00:00
/** Conversion of strings to numbers, dates, datetimes: through parsing.
2015-07-03 11:55:51 +00:00
*/
2023-08-01 16:29:44 +00:00
template < typename DataType >
void parseImpl ( typename DataType : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool precise_float_parsing )
2023-07-30 17:42:10 +00:00
{
if constexpr ( std : : is_floating_point_v < typename DataType : : FieldType > )
2023-07-31 14:26:18 +00:00
{
2023-08-01 16:29:44 +00:00
if ( precise_float_parsing )
2023-07-31 14:26:18 +00:00
readFloatTextPrecise ( x , rb ) ;
else
readFloatTextFast ( x , rb ) ;
}
2023-07-30 17:42:10 +00:00
else
2023-08-01 16:29:44 +00:00
readText ( x , rb ) ;
2015-07-01 16:07:29 +00:00
}
2018-07-11 21:43:09 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline void parseImpl < DataTypeDate > ( DataTypeDate : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * time_zone , bool )
2011-10-16 03:05:15 +00:00
{
2018-05-25 13:29:15 +00:00
DayNum tmp ( 0 ) ;
2023-05-08 20:28:31 +00:00
readDateText ( tmp , rb , * time_zone ) ;
2011-10-16 03:05:15 +00:00
x = tmp ;
}
2021-07-15 11:41:52 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline void parseImpl < DataTypeDate32 > ( DataTypeDate32 : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * time_zone , bool )
2021-07-15 11:41:52 +00:00
{
ExtendedDayNum tmp ( 0 ) ;
2023-05-08 20:28:31 +00:00
readDateText ( tmp , rb , * time_zone ) ;
2021-07-15 11:41:52 +00:00
x = tmp ;
}
2022-03-02 07:06:38 +00:00
2020-04-17 13:26:44 +00:00
// NOTE: no need of extra overload of DateTime64, since readDateTimeText64 has different signature and that case is explicitly handled in the calling code.
2018-07-11 21:43:09 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline void parseImpl < DataTypeDateTime > ( DataTypeDateTime : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * time_zone , bool )
2011-10-16 03:05:15 +00:00
{
2021-03-15 20:40:33 +00:00
time_t time = 0 ;
readDateTimeText ( time , rb , * time_zone ) ;
2022-03-02 07:06:38 +00:00
convertFromTime < DataTypeDateTime > ( x , time ) ;
2011-10-16 03:05:15 +00:00
}
2018-07-16 00:28:44 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline void parseImpl < DataTypeUUID > ( DataTypeUUID : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool )
2017-07-06 14:42:27 +00:00
{
UUID tmp ;
2020-10-25 08:45:29 +00:00
readUUIDText ( tmp , rb ) ;
2021-04-25 09:30:43 +00:00
x = tmp . toUnderType ( ) ;
2017-07-06 14:42:27 +00:00
}
2016-08-02 05:32:09 +00:00
2022-11-14 14:17:17 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline void parseImpl < DataTypeIPv4 > ( DataTypeIPv4 : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool )
2022-11-14 14:17:17 +00:00
{
IPv4 tmp ;
readIPv4Text ( tmp , rb ) ;
x = tmp . toUnderType ( ) ;
}
template < >
2023-08-01 16:29:44 +00:00
inline void parseImpl < DataTypeIPv6 > ( DataTypeIPv6 : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool )
2022-11-14 14:17:17 +00:00
{
IPv6 tmp ;
readIPv6Text ( tmp , rb ) ;
2022-11-22 12:57:19 +00:00
x = tmp ;
2022-11-14 14:17:17 +00:00
}
2023-08-01 16:29:44 +00:00
template < typename DataType >
bool tryParseImpl ( typename DataType : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool precise_float_parsing )
2017-12-25 02:57:54 +00:00
{
2019-06-16 18:12:14 +00:00
if constexpr ( std : : is_floating_point_v < typename DataType : : FieldType > )
2023-07-31 14:26:18 +00:00
{
2023-08-01 16:29:44 +00:00
if ( precise_float_parsing )
2023-07-31 14:26:18 +00:00
return tryReadFloatTextPrecise ( x , rb ) ;
else
return tryReadFloatTextFast ( x , rb ) ;
}
2020-08-19 11:52:17 +00:00
else /*if constexpr (is_integer_v<typename DataType::FieldType>)*/
2023-08-01 16:29:44 +00:00
return tryReadIntText ( x , rb ) ;
2018-07-11 21:43:09 +00:00
}
template < >
2023-08-01 16:29:44 +00:00
inline bool tryParseImpl < DataTypeDate > ( DataTypeDate : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * time_zone , bool )
2018-07-11 21:43:09 +00:00
{
DayNum tmp ( 0 ) ;
2023-05-08 20:28:31 +00:00
if ( ! tryReadDateText ( tmp , rb , * time_zone ) )
2018-07-14 23:39:00 +00:00
return false ;
2018-07-11 21:43:09 +00:00
x = tmp ;
return true ;
}
2021-07-15 11:41:52 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline bool tryParseImpl < DataTypeDate32 > ( DataTypeDate32 : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * time_zone , bool )
2021-07-15 11:41:52 +00:00
{
ExtendedDayNum tmp ( 0 ) ;
2023-05-08 20:28:31 +00:00
if ( ! tryReadDateText ( tmp , rb , * time_zone ) )
2021-07-15 11:41:52 +00:00
return false ;
x = tmp ;
return true ;
}
2018-07-11 21:43:09 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline bool tryParseImpl < DataTypeDateTime > ( DataTypeDateTime : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * time_zone , bool )
2018-07-11 21:43:09 +00:00
{
2023-12-21 21:30:51 +00:00
time_t time = 0 ;
if ( ! tryReadDateTimeText ( time , rb , * time_zone ) )
2018-07-14 23:39:00 +00:00
return false ;
2023-12-21 21:30:51 +00:00
convertFromTime < DataTypeDateTime > ( x , time ) ;
2018-07-11 21:43:09 +00:00
return true ;
2017-12-25 02:57:54 +00:00
}
2020-10-25 08:45:29 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline bool tryParseImpl < DataTypeUUID > ( DataTypeUUID : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool )
2020-10-25 08:45:29 +00:00
{
UUID tmp ;
if ( ! tryReadUUIDText ( tmp , rb ) )
return false ;
2021-04-25 09:30:43 +00:00
x = tmp . toUnderType ( ) ;
2020-10-25 08:45:29 +00:00
return true ;
}
2022-11-14 14:17:17 +00:00
template < >
2023-08-01 16:29:44 +00:00
inline bool tryParseImpl < DataTypeIPv4 > ( DataTypeIPv4 : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool )
2022-11-14 14:17:17 +00:00
{
IPv4 tmp ;
if ( ! tryReadIPv4Text ( tmp , rb ) )
return false ;
x = tmp . toUnderType ( ) ;
return true ;
}
template < >
2023-08-01 16:29:44 +00:00
inline bool tryParseImpl < DataTypeIPv6 > ( DataTypeIPv6 : : FieldType & x , ReadBuffer & rb , const DateLUTImpl * , bool )
2022-11-14 14:17:17 +00:00
{
IPv6 tmp ;
if ( ! tryReadIPv6Text ( tmp , rb ) )
return false ;
2022-11-22 12:57:19 +00:00
x = tmp ;
2022-11-14 14:17:17 +00:00
return true ;
}
2017-12-25 02:57:54 +00:00
2016-08-17 07:40:06 +00:00
/** Throw exception with verbose message when string value is not parsed completely.
*/
2022-05-20 15:04:55 +00:00
[[noreturn]] inline void throwExceptionForIncompletelyParsedValue ( ReadBuffer & read_buffer , const IDataType & result_type )
2019-12-25 19:17:41 +00:00
{
WriteBufferFromOwnString message_buf ;
message_buf < < " Cannot parse string " < < quote < < String ( read_buffer . buffer ( ) . begin ( ) , read_buffer . buffer ( ) . size ( ) )
2022-05-20 15:04:55 +00:00
< < " as " < < result_type . getName ( )
2019-12-25 19:17:41 +00:00
< < " : syntax error " ;
if ( read_buffer . offset ( ) )
message_buf < < " at position " < < read_buffer . offset ( )
< < " (parsed just " < < quote < < String ( read_buffer . buffer ( ) . begin ( ) , read_buffer . offset ( ) ) < < " ) " ;
else
message_buf < < " at begin of string " ;
2021-08-18 20:34:07 +00:00
// Currently there are no functions toIPv{4,6}Or{Null,Zero}
2022-05-20 15:04:55 +00:00
if ( isNativeNumber ( result_type ) & & ! ( result_type . getName ( ) = = " IPv4 " | | result_type . getName ( ) = = " IPv6 " ) )
message_buf < < " . Note: there are to " < < result_type . getName ( ) < < " OrZero and to " < < result_type . getName ( ) < < " OrNull functions, which returns zero/NULL instead of throwing exception. " ;
2019-12-25 19:17:41 +00:00
2023-01-20 14:33:01 +00:00
throw Exception ( PreformattedMessage { message_buf . str ( ) , " Cannot parse string { } as { } : syntax error { } " }, ErrorCodes::CANNOT_PARSE_TEXT);
2019-12-25 19:17:41 +00:00
}
2016-08-17 07:40:06 +00:00
2017-12-25 02:57:54 +00:00
enum class ConvertFromStringExceptionMode
{
Throw , /// Throw exception if value cannot be parsed.
Zero , /// Fill with zero or default if value cannot be parsed.
Null /// Return ColumnNullable with NULLs when value cannot be parsed.
} ;
2018-02-11 23:57:07 +00:00
enum class ConvertFromStringParsingMode
{
Normal ,
2020-07-05 18:09:20 +00:00
BestEffort , /// Only applicable for DateTime. Will use sophisticated method, that is slower.
BestEffortUS
2018-02-11 23:57:07 +00:00
} ;
template < typename FromDataType , typename ToDataType , typename Name ,
ConvertFromStringExceptionMode exception_mode , ConvertFromStringParsingMode parsing_mode >
2017-12-25 02:57:54 +00:00
struct ConvertThroughParsing
2011-10-16 01:57:10 +00:00
{
2017-12-25 02:57:54 +00:00
static_assert ( std : : is_same_v < FromDataType , DataTypeString > | | std : : is_same_v < FromDataType , DataTypeFixedString > ,
2017-12-25 04:10:43 +00:00
" ConvertThroughParsing is only applicable for String or FixedString data types " ) ;
2017-12-25 02:57:54 +00:00
2019-11-20 22:05:09 +00:00
static constexpr bool to_datetime64 = std : : is_same_v < ToDataType , DataTypeDateTime64 > ;
2017-12-25 02:57:54 +00:00
static bool isAllRead ( ReadBuffer & in )
{
/// In case of FixedString, skip zero bytes at end.
if constexpr ( std : : is_same_v < FromDataType , DataTypeFixedString > )
while ( ! in . eof ( ) & & * in . position ( ) = = 0 )
+ + in . position ( ) ;
if ( in . eof ( ) )
return true ;
2022-08-21 22:23:10 +00:00
/// Special case, that allows to parse string with DateTime or DateTime64 as Date or Date32.
if constexpr ( std : : is_same_v < ToDataType , DataTypeDate > | | std : : is_same_v < ToDataType , DataTypeDate32 > )
{
if ( ! in . eof ( ) & & ( * in . position ( ) = = ' ' | | * in . position ( ) = = ' T ' ) )
{
if ( in . buffer ( ) . size ( ) = = strlen ( " YYYY-MM-DD hh:mm:ss " ) )
return true ;
if ( in . buffer ( ) . size ( ) > = strlen ( " YYYY-MM-DD hh:mm:ss.x " )
& & in . buffer ( ) . begin ( ) [ 19 ] = = ' . ' )
{
in . position ( ) = in . buffer ( ) . begin ( ) + 20 ;
while ( ! in . eof ( ) & & isNumericASCII ( * in . position ( ) ) )
+ + in . position ( ) ;
if ( in . eof ( ) )
return true ;
}
}
}
2017-12-25 02:57:54 +00:00
return false ;
}
2018-08-31 08:59:21 +00:00
template < typename Additions = void * >
2020-11-17 13:24:45 +00:00
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & res_type , size_t input_rows_count ,
2018-08-31 08:59:21 +00:00
Additions additions [[maybe_unused]] = Additions ( ) )
2011-10-16 01:57:10 +00:00
{
2019-10-24 10:44:38 +00:00
using ColVecTo = typename ToDataType : : ColumnType ;
2018-08-27 16:16:16 +00:00
2018-02-11 23:57:07 +00:00
const DateLUTImpl * local_time_zone [[maybe_unused]] = nullptr ;
const DateLUTImpl * utc_time_zone [[maybe_unused]] = nullptr ;
2017-04-01 07:20:54 +00:00
2023-05-09 12:18:04 +00:00
/// For conversion to Date or DateTime type, second argument with time zone could be specified.
2019-11-20 22:05:09 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime > | | to_datetime64 )
2018-02-11 23:57:07 +00:00
{
2020-10-17 14:23:37 +00:00
const auto result_type = removeNullable ( res_type ) ;
2020-08-08 00:47:03 +00:00
// Time zone is already figured out during result type resolution, no need to do it here.
2019-11-05 11:59:46 +00:00
if ( const auto dt_col = checkAndGetDataType < ToDataType > ( result_type . get ( ) ) )
2019-11-03 05:10:11 +00:00
local_time_zone = & dt_col - > getTimeZone ( ) ;
else
2020-10-17 14:23:37 +00:00
local_time_zone = & extractTimeZoneFromFunctionArguments ( arguments , 1 , 0 ) ;
2018-02-11 23:57:07 +00:00
2020-07-05 18:09:20 +00:00
if constexpr ( parsing_mode = = ConvertFromStringParsingMode : : BestEffort | | parsing_mode = = ConvertFromStringParsingMode : : BestEffortUS )
2018-02-11 23:57:07 +00:00
utc_time_zone = & DateLUT : : instance ( " UTC " ) ;
}
2023-05-09 12:18:04 +00:00
else if constexpr ( std : : is_same_v < ToDataType , DataTypeDate > | | std : : is_same_v < ToDataType , DataTypeDate32 > )
{
// Timezone is more or less dummy when parsing Date/Date32 from string.
local_time_zone = & DateLUT : : instance ( ) ;
utc_time_zone = & DateLUT : : instance ( " UTC " ) ;
}
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
const IColumn * col_from = arguments [ 0 ] . column . get ( ) ;
2017-12-25 02:57:54 +00:00
const ColumnString * col_from_string = checkAndGetColumn < ColumnString > ( col_from ) ;
const ColumnFixedString * col_from_fixed_string = checkAndGetColumn < ColumnFixedString > ( col_from ) ;
if ( std : : is_same_v < FromDataType , DataTypeString > & & ! col_from_string )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
col_from - > getName ( ) , Name : : name ) ;
2017-12-25 02:57:54 +00:00
if ( std : : is_same_v < FromDataType , DataTypeFixedString > & & ! col_from_fixed_string )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
col_from - > getName ( ) , Name : : name ) ;
2017-12-25 02:57:54 +00:00
2018-04-24 07:16:39 +00:00
size_t size = input_rows_count ;
2018-08-27 16:16:16 +00:00
typename ColVecTo : : MutablePtr col_to = nullptr ;
2017-12-25 02:57:54 +00:00
2018-09-10 13:52:18 +00:00
if constexpr ( IsDataTypeDecimal < ToDataType > )
2018-08-21 18:25:38 +00:00
{
2018-08-31 08:59:21 +00:00
UInt32 scale = additions ;
2019-11-20 22:05:09 +00:00
if constexpr ( to_datetime64 )
2019-10-12 07:03:38 +00:00
{
ToDataType check_bounds_in_ctor ( scale , local_time_zone ? local_time_zone - > getTimeZone ( ) : String { } ) ;
}
else
{
ToDataType check_bounds_in_ctor ( ToDataType : : maxPrecision ( ) , scale ) ;
}
2018-08-31 08:59:21 +00:00
col_to = ColVecTo : : create ( size , scale ) ;
2018-08-21 18:25:38 +00:00
}
2018-08-27 16:16:16 +00:00
else
col_to = ColVecTo : : create ( size ) ;
typename ColVecTo : : Container & vec_to = col_to - > getData ( ) ;
2017-12-25 02:57:54 +00:00
ColumnUInt8 : : MutablePtr col_null_map_to ;
2017-12-25 07:18:27 +00:00
ColumnUInt8 : : Container * vec_null_map_to [[maybe_unused]] = nullptr ;
2018-02-11 23:57:07 +00:00
if constexpr ( exception_mode = = ConvertFromStringExceptionMode : : Null )
2011-10-16 03:05:15 +00:00
{
2017-12-25 02:57:54 +00:00
col_null_map_to = ColumnUInt8 : : create ( size ) ;
vec_null_map_to = & col_null_map_to - > getData ( ) ;
}
2017-04-01 07:20:54 +00:00
2018-11-25 00:08:50 +00:00
const ColumnString : : Chars * chars = nullptr ;
2017-12-25 02:57:54 +00:00
const IColumn : : Offsets * offsets = nullptr ;
size_t fixed_string_size = 0 ;
if constexpr ( std : : is_same_v < FromDataType , DataTypeString > )
{
chars = & col_from_string - > getChars ( ) ;
offsets = & col_from_string - > getOffsets ( ) ;
}
else
{
chars = & col_from_fixed_string - > getChars ( ) ;
fixed_string_size = col_from_fixed_string - > getN ( ) ;
}
2017-04-01 07:20:54 +00:00
2017-12-25 02:57:54 +00:00
size_t current_offset = 0 ;
2017-04-01 07:20:54 +00:00
2023-07-30 17:42:10 +00:00
bool precise_float_parsing = false ;
if ( DB : : CurrentThread : : isInitialized ( ) )
{
const DB : : ContextPtr query_context = DB : : CurrentThread : : get ( ) . getQueryContext ( ) ;
if ( query_context )
precise_float_parsing = query_context - > getSettingsRef ( ) . precise_float_parsing ;
}
2017-12-25 02:57:54 +00:00
for ( size_t i = 0 ; i < size ; + + i )
{
size_t next_offset = std : : is_same_v < FromDataType , DataTypeString > ? ( * offsets ) [ i ] : ( current_offset + fixed_string_size ) ;
size_t string_size = std : : is_same_v < FromDataType , DataTypeString > ? next_offset - current_offset - 1 : fixed_string_size ;
2017-04-01 07:20:54 +00:00
2017-12-25 02:57:54 +00:00
ReadBufferFromMemory read_buffer ( & ( * chars ) [ current_offset ] , string_size ) ;
2017-04-01 07:20:54 +00:00
2019-05-15 18:50:35 +00:00
if constexpr ( exception_mode = = ConvertFromStringExceptionMode : : Throw )
2017-12-25 02:57:54 +00:00
{
2018-02-11 23:57:07 +00:00
if constexpr ( parsing_mode = = ConvertFromStringParsingMode : : BestEffort )
{
2019-11-20 22:05:09 +00:00
if constexpr ( to_datetime64 )
2019-10-12 07:03:38 +00:00
{
2019-11-11 05:18:34 +00:00
DateTime64 res = 0 ;
2022-01-27 10:07:53 +00:00
parseDateTime64BestEffort ( res , col_to - > getScale ( ) , read_buffer , * local_time_zone , * utc_time_zone ) ;
2019-10-12 07:03:38 +00:00
vec_to [ i ] = res ;
}
else
{
time_t res ;
parseDateTimeBestEffort ( res , read_buffer , * local_time_zone , * utc_time_zone ) ;
2022-03-02 07:06:38 +00:00
convertFromTime < ToDataType > ( vec_to [ i ] , res ) ;
2019-10-12 07:03:38 +00:00
}
2018-02-11 23:57:07 +00:00
}
2020-07-05 18:09:20 +00:00
else if constexpr ( parsing_mode = = ConvertFromStringParsingMode : : BestEffortUS )
{
2020-04-17 13:26:44 +00:00
if constexpr ( to_datetime64 )
{
DateTime64 res = 0 ;
2022-01-27 10:07:53 +00:00
parseDateTime64BestEffortUS ( res , col_to - > getScale ( ) , read_buffer , * local_time_zone , * utc_time_zone ) ;
2020-04-17 13:26:44 +00:00
vec_to [ i ] = res ;
}
else
{
time_t res ;
parseDateTimeBestEffortUS ( res , read_buffer , * local_time_zone , * utc_time_zone ) ;
2022-03-02 07:06:38 +00:00
convertFromTime < ToDataType > ( vec_to [ i ] , res ) ;
2020-04-17 13:26:44 +00:00
}
2020-07-05 18:09:20 +00:00
}
2018-02-11 23:57:07 +00:00
else
{
2019-11-20 22:05:09 +00:00
if constexpr ( to_datetime64 )
2019-10-12 07:03:38 +00:00
{
2019-11-11 05:18:34 +00:00
DateTime64 value = 0 ;
2022-01-27 10:07:53 +00:00
readDateTime64Text ( value , col_to - > getScale ( ) , read_buffer , * local_time_zone ) ;
2019-10-14 08:38:03 +00:00
vec_to [ i ] = value ;
2019-10-12 07:03:38 +00:00
}
else if constexpr ( IsDataTypeDecimal < ToDataType > )
2022-08-21 22:23:10 +00:00
{
2021-06-17 22:42:33 +00:00
SerializationDecimal < typename ToDataType : : FieldType > : : readText (
2022-01-27 10:07:53 +00:00
vec_to [ i ] , read_buffer , ToDataType : : maxPrecision ( ) , col_to - > getScale ( ) ) ;
2022-08-21 22:23:10 +00:00
}
2019-05-15 18:50:35 +00:00
else
2021-07-15 11:40:45 +00:00
{
2022-11-29 01:07:25 +00:00
/// we want to utilize constexpr condition here, which is not mixable with value comparison
2022-11-24 05:00:28 +00:00
do
{
if constexpr ( std : : is_same_v < FromDataType , DataTypeFixedString > & & std : : is_same_v < ToDataType , DataTypeIPv6 > )
{
if ( fixed_string_size = = IPV6_BINARY_LENGTH )
{
readBinary ( vec_to [ i ] , read_buffer ) ;
break ;
}
}
2023-08-01 16:29:44 +00:00
parseImpl < ToDataType > ( vec_to [ i ] , read_buffer , local_time_zone , precise_float_parsing ) ;
2022-11-29 01:07:25 +00:00
} while ( false ) ;
2021-07-15 11:40:45 +00:00
}
2018-02-11 23:57:07 +00:00
}
2017-04-01 07:20:54 +00:00
2017-12-25 02:57:54 +00:00
if ( ! isAllRead ( read_buffer ) )
2022-05-20 15:04:55 +00:00
throwExceptionForIncompletelyParsedValue ( read_buffer , * res_type ) ;
2017-12-25 02:57:54 +00:00
}
else
{
2018-02-11 23:57:07 +00:00
bool parsed ;
if constexpr ( parsing_mode = = ConvertFromStringParsingMode : : BestEffort )
{
2019-11-20 22:05:09 +00:00
if constexpr ( to_datetime64 )
2019-10-12 07:03:38 +00:00
{
2019-11-11 05:18:34 +00:00
DateTime64 res = 0 ;
2022-01-27 10:07:53 +00:00
parsed = tryParseDateTime64BestEffort ( res , col_to - > getScale ( ) , read_buffer , * local_time_zone , * utc_time_zone ) ;
2019-10-12 07:03:38 +00:00
vec_to [ i ] = res ;
}
else
{
time_t res ;
parsed = tryParseDateTimeBestEffort ( res , read_buffer , * local_time_zone , * utc_time_zone ) ;
2022-03-02 07:06:38 +00:00
convertFromTime < ToDataType > ( vec_to [ i ] , res ) ;
2019-10-12 07:03:38 +00:00
}
2018-02-11 23:57:07 +00:00
}
2021-01-27 14:10:06 +00:00
else if constexpr ( parsing_mode = = ConvertFromStringParsingMode : : BestEffortUS )
{
2022-08-09 02:32:34 +00:00
if constexpr ( to_datetime64 )
{
DateTime64 res = 0 ;
parsed = tryParseDateTime64BestEffortUS ( res , col_to - > getScale ( ) , read_buffer , * local_time_zone , * utc_time_zone ) ;
vec_to [ i ] = res ;
}
else
{
time_t res ;
parsed = tryParseDateTimeBestEffortUS ( res , read_buffer , * local_time_zone , * utc_time_zone ) ;
convertFromTime < ToDataType > ( vec_to [ i ] , res ) ;
}
2021-01-27 14:10:06 +00:00
}
2018-02-11 23:57:07 +00:00
else
{
2019-11-20 22:05:09 +00:00
if constexpr ( to_datetime64 )
2019-10-12 07:03:38 +00:00
{
2019-11-11 05:18:34 +00:00
DateTime64 value = 0 ;
2022-01-27 10:07:53 +00:00
parsed = tryReadDateTime64Text ( value , col_to - > getScale ( ) , read_buffer , * local_time_zone ) ;
2019-10-12 07:03:38 +00:00
vec_to [ i ] = value ;
}
else if constexpr ( IsDataTypeDecimal < ToDataType > )
2022-08-21 22:23:10 +00:00
{
2021-06-17 22:42:33 +00:00
parsed = SerializationDecimal < typename ToDataType : : FieldType > : : tryReadText (
2022-01-27 10:07:53 +00:00
vec_to [ i ] , read_buffer , ToDataType : : maxPrecision ( ) , col_to - > getScale ( ) ) ;
2022-08-21 22:23:10 +00:00
}
2019-05-15 18:50:35 +00:00
else
2022-11-24 05:00:28 +00:00
{
2022-11-29 01:07:25 +00:00
/// we want to utilize constexpr condition here, which is not mixable with value comparison
2022-11-24 05:00:28 +00:00
do
{
if constexpr ( std : : is_same_v < FromDataType , DataTypeFixedString > & & std : : is_same_v < ToDataType , DataTypeIPv6 > )
{
if ( fixed_string_size = = IPV6_BINARY_LENGTH )
{
readBinary ( vec_to [ i ] , read_buffer ) ;
parsed = true ;
break ;
}
}
2023-08-01 16:29:44 +00:00
parsed = tryParseImpl < ToDataType > ( vec_to [ i ] , read_buffer , local_time_zone , precise_float_parsing ) ;
2022-11-29 01:07:25 +00:00
} while ( false ) ;
2022-11-24 05:00:28 +00:00
}
2018-02-11 23:57:07 +00:00
}
2017-12-25 05:22:33 +00:00
2021-06-17 22:42:33 +00:00
if ( ! isAllRead ( read_buffer ) )
parsed = false ;
2020-06-14 01:45:03 +00:00
2017-12-25 05:22:33 +00:00
if ( ! parsed )
2021-07-15 11:40:45 +00:00
{
if constexpr ( std : : is_same_v < ToDataType , DataTypeDate32 > )
{
vec_to [ i ] = - static_cast < Int32 > ( DateLUT : : instance ( ) . getDayNumOffsetEpoch ( ) ) ;
}
else
{
vec_to [ i ] = static_cast < typename ToDataType : : FieldType > ( 0 ) ;
}
}
2017-12-25 05:22:33 +00:00
2018-02-11 23:57:07 +00:00
if constexpr ( exception_mode = = ConvertFromStringExceptionMode : : Null )
2017-12-25 05:22:33 +00:00
( * vec_null_map_to ) [ i ] = ! parsed ;
2011-10-16 03:05:15 +00:00
}
2017-12-16 04:59:32 +00:00
2017-12-25 02:57:54 +00:00
current_offset = next_offset ;
2011-10-16 03:05:15 +00:00
}
2017-12-25 02:57:54 +00:00
2018-02-11 23:57:07 +00:00
if constexpr ( exception_mode = = ConvertFromStringExceptionMode : : Null )
2020-10-17 14:23:37 +00:00
return ColumnNullable : : create ( std : : move ( col_to ) , std : : move ( col_null_map_to ) ) ;
2016-08-02 05:32:09 +00:00
else
2020-10-20 15:56:05 +00:00
return col_to ;
2016-08-02 05:32:09 +00:00
}
} ;
2023-10-23 23:49:40 +00:00
template < typename ToDataType , typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2022-03-17 18:06:50 +00:00
requires ( ! std : : is_same_v < ToDataType , DataTypeString > )
2023-10-23 23:49:40 +00:00
struct ConvertImpl < DataTypeString , ToDataType , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2018-02-11 23:57:07 +00:00
: ConvertThroughParsing < DataTypeString , ToDataType , Name , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : Normal > { } ;
2017-12-25 02:57:54 +00:00
2023-10-23 23:49:40 +00:00
template < typename ToDataType , typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2022-03-17 18:06:50 +00:00
requires ( ! std : : is_same_v < ToDataType , DataTypeFixedString > )
2023-10-23 23:49:40 +00:00
struct ConvertImpl < DataTypeFixedString , ToDataType , Name , ConvertDefaultBehaviorTag , date_time_overflow_behavior >
2018-02-11 23:57:07 +00:00
: ConvertThroughParsing < DataTypeFixedString , ToDataType , Name , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : Normal > { } ;
2011-10-16 01:57:10 +00:00
2023-10-23 23:49:40 +00:00
template < typename ToDataType , typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2022-03-17 18:06:50 +00:00
requires ( ! std : : is_same_v < ToDataType , DataTypeString > )
2023-10-23 23:49:40 +00:00
struct ConvertImpl < DataTypeString , ToDataType , Name , ConvertReturnNullOnErrorTag , date_time_overflow_behavior >
2020-12-23 21:57:33 +00:00
: ConvertThroughParsing < DataTypeString , ToDataType , Name , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : Normal > { } ;
2023-10-23 23:49:40 +00:00
template < typename ToDataType , typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2022-03-17 18:06:50 +00:00
requires ( ! std : : is_same_v < ToDataType , DataTypeFixedString > )
2023-10-23 23:49:40 +00:00
struct ConvertImpl < DataTypeFixedString , ToDataType , Name , ConvertReturnNullOnErrorTag , date_time_overflow_behavior >
2020-12-23 21:57:33 +00:00
: ConvertThroughParsing < DataTypeFixedString , ToDataType , Name , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : Normal > { } ;
2023-10-23 23:49:40 +00:00
template < typename FromDataType , typename ToDataType , typename Name , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
2022-11-21 07:45:42 +00:00
requires ( is_any_of < FromDataType , DataTypeString , DataTypeFixedString > & & is_any_of < ToDataType , DataTypeIPv4 , DataTypeIPv6 > )
2023-10-23 23:49:40 +00:00
struct ConvertImpl < FromDataType , ToDataType , Name , ConvertReturnZeroOnErrorTag , date_time_overflow_behavior >
2022-11-21 07:45:42 +00:00
: ConvertThroughParsing < FromDataType , ToDataType , Name , ConvertFromStringExceptionMode : : Zero , ConvertFromStringParsingMode : : Normal > { } ;
2021-08-18 20:34:07 +00:00
/// Generic conversion of any type from String. Used for complex types: Array and Tuple or types with custom serialization.
template < typename StringColumnType >
2016-08-02 03:30:34 +00:00
struct ConvertImplGenericFromString
{
2022-05-16 19:15:18 +00:00
static ColumnPtr execute ( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * column_nullable , size_t input_rows_count )
2016-08-02 03:30:34 +00:00
{
2021-08-18 20:34:07 +00:00
static_assert ( std : : is_same_v < StringColumnType , ColumnString > | | std : : is_same_v < StringColumnType , ColumnFixedString > ,
" Can be used only to parse from ColumnString or ColumnFixedString " ) ;
2016-08-02 03:30:34 +00:00
2022-05-20 15:04:55 +00:00
const IColumn & column_from = * arguments [ 0 ] . column ;
2020-10-17 14:23:37 +00:00
const IDataType & data_type_to = * result_type ;
2022-05-20 15:04:55 +00:00
auto res = data_type_to . createColumn ( ) ;
auto serialization = data_type_to . getDefaultSerialization ( ) ;
const auto * null_map = column_nullable ? & column_nullable - > getNullMapData ( ) : nullptr ;
2016-08-02 03:30:34 +00:00
2022-05-20 15:04:55 +00:00
executeImpl ( column_from , * res , * serialization , input_rows_count , null_map , result_type . get ( ) ) ;
return res ;
}
static void executeImpl (
const IColumn & column_from ,
IColumn & column_to ,
const ISerialization & serialization_from ,
size_t input_rows_count ,
const PaddedPODArray < UInt8 > * null_map = nullptr ,
const IDataType * result_type = nullptr )
{
static_assert ( std : : is_same_v < StringColumnType , ColumnString > | | std : : is_same_v < StringColumnType , ColumnFixedString > ,
" Can be used only to parse from ColumnString or ColumnFixedString " ) ;
if ( const StringColumnType * col_from_string = checkAndGetColumn < StringColumnType > ( & column_from ) )
{
2021-08-18 20:34:07 +00:00
column_to . reserve ( input_rows_count ) ;
2016-08-02 03:30:34 +00:00
2018-06-08 01:51:55 +00:00
FormatSettings format_settings ;
2021-08-18 20:34:07 +00:00
for ( size_t i = 0 ; i < input_rows_count ; + + i )
2016-08-02 03:30:34 +00:00
{
2022-05-16 19:15:18 +00:00
if ( null_map & & ( * null_map ) [ i ] )
{
column_to . insertDefault ( ) ;
continue ;
}
2021-08-18 20:34:07 +00:00
const auto & val = col_from_string - > getDataAt ( i ) ;
ReadBufferFromMemory read_buffer ( val . data , val . size ) ;
2023-09-12 13:34:21 +00:00
try
{
serialization_from . deserializeWholeText ( column_to , read_buffer , format_settings ) ;
}
catch ( const Exception & e )
{
2023-09-29 09:16:56 +00:00
auto * nullable_column = typeid_cast < ColumnNullable * > ( & column_to ) ;
if ( e . code ( ) = = ErrorCodes : : CANNOT_PARSE_BOOL & & nullable_column )
2023-09-12 13:34:21 +00:00
{
2023-09-29 09:16:56 +00:00
auto & col_nullmap = nullable_column - > getNullMapData ( ) ;
if ( col_nullmap . size ( ) ! = nullable_column - > size ( ) )
2023-10-23 11:01:53 +00:00
col_nullmap . resize_fill ( nullable_column - > size ( ) ) ;
if ( nullable_column - > size ( ) = = ( i + 1 ) )
2023-09-29 09:16:56 +00:00
nullable_column - > popBack ( 1 ) ;
nullable_column - > insertDefault ( ) ;
2023-09-12 13:34:21 +00:00
continue ;
}
throw ;
}
2016-08-02 03:30:34 +00:00
if ( ! read_buffer . eof ( ) )
2022-05-20 15:04:55 +00:00
{
if ( result_type )
throwExceptionForIncompletelyParsedValue ( read_buffer , * result_type ) ;
else
throw Exception ( ErrorCodes : : CANNOT_PARSE_TEXT ,
" Cannot parse string to column {}. Expected eof " , column_to . getName ( ) ) ;
}
2016-08-02 03:30:34 +00:00
}
}
else
2022-05-20 15:04:55 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN ,
" Illegal column {} of first argument of conversion function from string " ,
column_from . getName ( ) ) ;
2016-08-02 03:30:34 +00:00
}
2022-05-20 15:04:55 +00:00
2016-08-02 03:30:34 +00:00
} ;
2016-07-31 03:53:16 +00:00
template < >
2020-12-23 21:57:33 +00:00
struct ConvertImpl < DataTypeString , DataTypeUInt32 , NameToUnixTimestamp , ConvertDefaultBehaviorTag >
: ConvertImpl < DataTypeString , DataTypeDateTime , NameToUnixTimestamp , ConvertDefaultBehaviorTag > { } ;
2015-07-01 17:32:04 +00:00
2020-12-24 00:13:53 +00:00
template < >
struct ConvertImpl < DataTypeString , DataTypeUInt32 , NameToUnixTimestamp , ConvertReturnNullOnErrorTag >
: ConvertImpl < DataTypeString , DataTypeDateTime , NameToUnixTimestamp , ConvertReturnNullOnErrorTag > { } ;
2015-07-03 00:09:57 +00:00
2016-07-31 03:53:16 +00:00
/** If types are identical, just take reference to column.
2011-10-16 03:05:15 +00:00
*/
2017-10-30 02:18:06 +00:00
template < typename T , typename Name >
2022-03-17 18:06:50 +00:00
requires ( ! T : : is_parametric )
struct ConvertImpl < T , T , Name , ConvertDefaultBehaviorTag >
2011-10-16 01:57:10 +00:00
{
2020-11-05 19:09:17 +00:00
template < typename Additions = void * >
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & , size_t /*input_rows_count*/ ,
Additions additions [[maybe_unused]] = Additions ( ) )
2011-10-16 01:57:10 +00:00
{
2020-10-17 14:23:37 +00:00
return arguments [ 0 ] . column ;
2011-10-16 01:57:10 +00:00
}
} ;
2021-12-20 23:59:08 +00:00
template < typename Name >
struct ConvertImpl < DataTypeUInt8 , DataTypeUInt8 , Name , ConvertDefaultBehaviorTag >
{
template < typename Additions = void * >
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & , size_t /*input_rows_count*/ ,
Additions additions [[maybe_unused]] = Additions ( ) )
{
return arguments [ 0 ] . column ;
}
} ;
2011-10-16 01:57:10 +00:00
2016-07-31 03:53:16 +00:00
/** Conversion from FixedString to String.
* Cutting sequences of zero bytes from end of strings .
2014-04-13 13:58:54 +00:00
*/
template < typename Name >
2020-12-23 21:57:33 +00:00
struct ConvertImpl < DataTypeFixedString , DataTypeString , Name , ConvertDefaultBehaviorTag >
2014-04-13 13:58:54 +00:00
{
2022-09-20 02:05:18 +00:00
static ColumnPtr execute ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & return_type , size_t /*input_rows_count*/ )
2014-04-13 13:58:54 +00:00
{
2022-09-20 02:05:18 +00:00
ColumnUInt8 : : MutablePtr null_map = copyNullMap ( arguments [ 0 ] . column ) ;
const auto & nested = columnGetNested ( arguments [ 0 ] ) ;
if ( const ColumnFixedString * col_from = checkAndGetColumn < ColumnFixedString > ( nested . column . get ( ) ) )
2014-04-13 13:58:54 +00:00
{
2017-12-14 01:43:19 +00:00
auto col_to = ColumnString : : create ( ) ;
2017-04-01 07:20:54 +00:00
2018-11-25 00:08:50 +00:00
const ColumnFixedString : : Chars & data_from = col_from - > getChars ( ) ;
ColumnString : : Chars & data_to = col_to - > getChars ( ) ;
2017-12-15 21:32:25 +00:00
ColumnString : : Offsets & offsets_to = col_to - > getOffsets ( ) ;
2014-04-13 13:58:54 +00:00
size_t size = col_from - > size ( ) ;
size_t n = col_from - > getN ( ) ;
2017-10-30 02:18:06 +00:00
data_to . resize ( size * ( n + 1 ) ) ; /// + 1 - zero terminator
2014-04-13 13:58:54 +00:00
offsets_to . resize ( size ) ;
2017-04-01 07:20:54 +00:00
2014-04-13 13:58:54 +00:00
size_t offset_from = 0 ;
size_t offset_to = 0 ;
for ( size_t i = 0 ; i < size ; + + i )
{
2022-09-20 02:05:18 +00:00
if ( ! null_map | | ! null_map - > getData ( ) [ i ] )
{
size_t bytes_to_copy = n ;
while ( bytes_to_copy > 0 & & data_from [ offset_from + bytes_to_copy - 1 ] = = 0 )
- - bytes_to_copy ;
2017-04-01 07:20:54 +00:00
2022-09-20 02:05:18 +00:00
memcpy ( & data_to [ offset_to ] , & data_from [ offset_from ] , bytes_to_copy ) ;
offset_to + = bytes_to_copy ;
}
2014-04-13 13:58:54 +00:00
data_to [ offset_to ] = 0 ;
+ + offset_to ;
offsets_to [ i ] = offset_to ;
2022-09-20 02:05:18 +00:00
offset_from + = n ;
2014-04-13 13:58:54 +00:00
}
2017-04-01 07:20:54 +00:00
2014-04-13 13:58:54 +00:00
data_to . resize ( offset_to ) ;
2022-09-20 02:05:18 +00:00
if ( return_type - > isNullable ( ) & & null_map )
return ColumnNullable : : create ( std : : move ( col_to ) , std : : move ( null_map ) ) ;
2020-10-17 14:23:37 +00:00
return col_to ;
2014-04-13 13:58:54 +00:00
}
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Illegal column {} of first argument of function {} " ,
arguments [ 0 ] . column - > getName ( ) , Name : : name ) ;
2014-04-13 13:58:54 +00:00
}
} ;
2016-07-31 03:53:16 +00:00
/// Declared early because used below.
2017-10-30 02:18:06 +00:00
struct NameToDate { static constexpr auto name = " toDate " ; } ;
2021-07-15 11:41:52 +00:00
struct NameToDate32 { static constexpr auto name = " toDate32 " ; } ;
2017-11-05 05:32:22 +00:00
struct NameToDateTime { static constexpr auto name = " toDateTime " ; } ;
2020-08-15 13:29:24 +00:00
struct NameToDateTime32 { static constexpr auto name = " toDateTime32 " ; } ;
2019-10-12 07:03:38 +00:00
struct NameToDateTime64 { static constexpr auto name = " toDateTime64 " ; } ;
2017-10-30 02:18:06 +00:00
struct NameToString { static constexpr auto name = " toString " ; } ;
2018-08-22 16:16:39 +00:00
struct NameToDecimal32 { static constexpr auto name = " toDecimal32 " ; } ;
struct NameToDecimal64 { static constexpr auto name = " toDecimal64 " ; } ;
struct NameToDecimal128 { static constexpr auto name = " toDecimal128 " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToDecimal256 { static constexpr auto name = " toDecimal256 " ; } ;
2017-10-30 02:18:06 +00:00
# define DEFINE_NAME_TO_INTERVAL(INTERVAL_KIND) \
struct NameToInterval # # INTERVAL_KIND \
{ \
static constexpr auto name = " toInterval " # INTERVAL_KIND ; \
2024-02-27 13:37:21 +00:00
static constexpr auto kind = IntervalKind : : Kind : : INTERVAL_KIND ; \
2017-10-30 02:18:06 +00:00
} ;
2022-02-07 18:44:14 +00:00
DEFINE_NAME_TO_INTERVAL ( Nanosecond )
DEFINE_NAME_TO_INTERVAL ( Microsecond )
DEFINE_NAME_TO_INTERVAL ( Millisecond )
2017-10-30 02:18:06 +00:00
DEFINE_NAME_TO_INTERVAL ( Second )
DEFINE_NAME_TO_INTERVAL ( Minute )
DEFINE_NAME_TO_INTERVAL ( Hour )
DEFINE_NAME_TO_INTERVAL ( Day )
DEFINE_NAME_TO_INTERVAL ( Week )
DEFINE_NAME_TO_INTERVAL ( Month )
2018-12-18 13:16:48 +00:00
DEFINE_NAME_TO_INTERVAL ( Quarter )
2017-10-30 02:18:06 +00:00
DEFINE_NAME_TO_INTERVAL ( Year )
# undef DEFINE_NAME_TO_INTERVAL
2011-10-16 01:57:10 +00:00
2020-08-20 14:41:03 +00:00
struct NameParseDateTimeBestEffort ;
struct NameParseDateTimeBestEffortOrZero ;
struct NameParseDateTimeBestEffortOrNull ;
2020-08-20 11:18:29 +00:00
template < typename Name , typename ToDataType >
2020-10-17 14:23:37 +00:00
static inline bool isDateTime64 ( const ColumnsWithTypeAndName & arguments )
2020-08-20 11:18:29 +00:00
{
if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime64 > )
return true ;
2020-08-20 14:41:03 +00:00
else if constexpr ( std : : is_same_v < Name , NameToDateTime > | | std : : is_same_v < Name , NameParseDateTimeBestEffort >
| | std : : is_same_v < Name , NameParseDateTimeBestEffortOrZero > | | std : : is_same_v < Name , NameParseDateTimeBestEffortOrNull > )
2020-08-20 11:18:29 +00:00
{
2023-11-06 10:29:29 +00:00
return ( arguments . size ( ) = = 2 & & isUInt ( arguments [ 1 ] . type ) ) | | arguments . size ( ) = = 3 ;
2020-08-20 11:18:29 +00:00
}
return false ;
}
2016-07-30 04:39:51 +00:00
2015-12-29 13:51:12 +00:00
template < typename ToDataType , typename Name , typename MonotonicityImpl >
2011-10-16 01:57:10 +00:00
class FunctionConvert : public IFunction
{
2011-10-15 23:40:56 +00:00
public :
2015-12-29 13:51:12 +00:00
using Monotonic = MonotonicityImpl ;
2017-04-01 07:20:54 +00:00
2014-11-12 17:23:26 +00:00
static constexpr auto name = Name : : name ;
2018-08-21 18:25:38 +00:00
static constexpr bool to_decimal =
2020-08-19 11:52:17 +00:00
std : : is_same_v < Name , NameToDecimal32 > | | std : : is_same_v < Name , NameToDecimal64 >
| | std : : is_same_v < Name , NameToDecimal128 > | | std : : is_same_v < Name , NameToDecimal256 > ;
2018-08-21 18:25:38 +00:00
2019-11-20 22:05:09 +00:00
static constexpr bool to_datetime64 = std : : is_same_v < ToDataType , DataTypeDateTime64 > ;
2020-12-23 21:57:33 +00:00
static constexpr bool to_string_or_fixed_string = std : : is_same_v < ToDataType , DataTypeFixedString > | |
std : : is_same_v < ToDataType , DataTypeString > ;
2021-08-16 11:30:56 +00:00
static constexpr bool to_date_or_datetime = std : : is_same_v < ToDataType , DataTypeDate > | |
std : : is_same_v < ToDataType , DataTypeDate32 > | |
std : : is_same_v < ToDataType , DataTypeDateTime > ;
2022-11-21 07:45:42 +00:00
static FunctionPtr create ( ContextPtr context ) { return std : : make_shared < FunctionConvert > ( context ) ; }
2019-12-25 19:17:41 +00:00
static FunctionPtr create ( ) { return std : : make_shared < FunctionConvert > ( ) ; }
2017-04-01 07:20:54 +00:00
2022-11-21 07:45:42 +00:00
FunctionConvert ( ) = default ;
explicit FunctionConvert ( ContextPtr context_ ) : context ( context_ ) { }
2015-10-11 23:36:45 +00:00
String getName ( ) const override
2011-10-15 23:40:56 +00:00
{
2014-11-12 17:23:26 +00:00
return name ;
2011-10-15 23:40:56 +00:00
}
2017-04-01 07:20:54 +00:00
2016-12-29 19:38:10 +00:00
bool isVariadic ( ) const override { return true ; }
size_t getNumberOfArguments ( ) const override { return 0 ; }
2020-10-09 07:41:28 +00:00
bool isInjective ( const ColumnsWithTypeAndName & ) const override { return std : : is_same_v < Name , NameToString > ; }
2021-08-16 11:30:56 +00:00
bool isSuitableForShortCircuitArgumentsExecution ( const DataTypesWithConstInfo & arguments ) const override
{
/// TODO: We can make more optimizations here.
return ! ( to_date_or_datetime & & isNumber ( * arguments [ 0 ] . type ) ) ;
}
2017-04-01 07:20:54 +00:00
2021-05-16 15:17:05 +00:00
using DefaultReturnTypeGetter = std : : function < DataTypePtr ( const ColumnsWithTypeAndName & ) > ;
static DataTypePtr getReturnTypeDefaultImplementationForNulls ( const ColumnsWithTypeAndName & arguments , const DefaultReturnTypeGetter & getter )
2020-12-23 21:57:33 +00:00
{
2021-05-15 17:33:15 +00:00
NullPresence null_presence = getNullPresense ( arguments ) ;
if ( null_presence . has_null_constant )
{
2021-05-16 15:17:05 +00:00
return makeNullable ( std : : make_shared < DataTypeNothing > ( ) ) ;
2021-05-15 17:33:15 +00:00
}
if ( null_presence . has_nullable )
{
auto nested_columns = Block ( createBlockWithNestedColumns ( arguments ) ) ;
2021-05-16 15:17:05 +00:00
auto return_type = getter ( ColumnsWithTypeAndName ( nested_columns . begin ( ) , nested_columns . end ( ) ) ) ;
return makeNullable ( return_type ) ;
2021-05-15 17:33:15 +00:00
}
2021-05-16 15:17:05 +00:00
return getter ( arguments ) ;
}
2021-05-15 17:33:15 +00:00
2021-05-16 15:17:05 +00:00
DataTypePtr getReturnTypeImpl ( const ColumnsWithTypeAndName & arguments ) const override
{
auto getter = [ & ] ( const auto & args ) { return getReturnTypeImplRemovedNullable ( args ) ; } ;
auto res = getReturnTypeDefaultImplementationForNulls ( arguments , getter ) ;
2020-12-23 21:57:33 +00:00
to_nullable = res - > isNullable ( ) ;
2020-12-24 00:13:53 +00:00
checked_return_type = true ;
2020-12-23 21:57:33 +00:00
return res ;
}
DataTypePtr getReturnTypeImplRemovedNullable ( const ColumnsWithTypeAndName & arguments ) const
2011-10-15 23:40:56 +00:00
{
2019-12-25 14:50:13 +00:00
FunctionArgumentDescriptors mandatory_args = { { " Value " , nullptr , nullptr , nullptr } } ;
2019-12-23 14:54:06 +00:00
FunctionArgumentDescriptors optional_args ;
2019-10-14 10:15:10 +00:00
2020-08-20 11:18:29 +00:00
if constexpr ( to_decimal )
2018-08-21 18:25:38 +00:00
{
2024-03-07 16:16:13 +00:00
mandatory_args . push_back ( { " scale " , static_cast < FunctionArgumentDescriptor : : TypeValidator > ( & isNativeInteger ) , & isColumnConst , " const Integer " } ) ;
2019-10-14 10:15:10 +00:00
}
2020-08-15 13:29:24 +00:00
2020-08-20 11:18:29 +00:00
if ( ! to_decimal & & isDateTime64 < Name , ToDataType > ( arguments ) )
2020-08-15 13:29:24 +00:00
{
2024-03-07 16:16:13 +00:00
mandatory_args . push_back ( { " scale " , static_cast < FunctionArgumentDescriptor : : TypeValidator > ( & isNativeInteger ) , & isColumnConst , " const Integer " } ) ;
2020-08-15 13:29:24 +00:00
}
2019-12-23 14:54:06 +00:00
// toString(DateTime or DateTime64, [timezone: String])
2020-10-17 14:23:37 +00:00
if ( ( std : : is_same_v < Name , NameToString > & & ! arguments . empty ( ) & & ( isDateTime64 ( arguments [ 0 ] . type ) | | isDateTime ( arguments [ 0 ] . type ) ) )
2019-12-25 14:50:13 +00:00
// toUnixTimestamp(value[, timezone : String])
| | std : : is_same_v < Name , NameToUnixTimestamp >
// toDate(value[, timezone : String])
2020-06-27 19:05:00 +00:00
| | std : : is_same_v < ToDataType , DataTypeDate > // TODO: shall we allow timestamp argument for toDate? DateTime knows nothing about timezones and this argument is ignored below.
2023-05-09 20:37:25 +00:00
// toDate32(value[, timezone : String])
2021-07-15 11:40:45 +00:00
| | std : : is_same_v < ToDataType , DataTypeDate32 >
2019-12-25 14:50:13 +00:00
// toDateTime(value[, timezone: String])
| | std : : is_same_v < ToDataType , DataTypeDateTime >
// toDateTime64(value, scale : Integer[, timezone: String])
| | std : : is_same_v < ToDataType , DataTypeDateTime64 > )
2018-08-21 18:25:38 +00:00
{
2024-03-07 16:16:13 +00:00
optional_args . push_back ( { " timezone " , static_cast < FunctionArgumentDescriptor : : TypeValidator > ( & isString ) , nullptr , " String " } ) ;
2018-08-21 18:25:38 +00:00
}
2019-10-14 10:15:10 +00:00
validateFunctionArgumentTypes ( * this , arguments , mandatory_args , optional_args ) ;
2018-02-11 23:57:07 +00:00
2017-12-25 04:01:46 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeInterval > )
2017-11-05 05:32:22 +00:00
{
2019-11-16 13:54:52 +00:00
return std : : make_shared < DataTypeInterval > ( Name : : kind ) ;
2017-12-02 02:47:12 +00:00
}
2018-08-21 18:25:38 +00:00
else if constexpr ( to_decimal )
{
UInt64 scale = extractToDecimalScale ( arguments [ 1 ] ) ;
2018-08-22 16:16:39 +00:00
if constexpr ( std : : is_same_v < Name , NameToDecimal32 > )
2020-08-26 12:26:12 +00:00
return createDecimalMaxPrecision < Decimal32 > ( scale ) ;
2018-08-22 16:16:39 +00:00
else if constexpr ( std : : is_same_v < Name , NameToDecimal64 > )
2020-08-26 12:26:12 +00:00
return createDecimalMaxPrecision < Decimal64 > ( scale ) ;
2018-11-24 01:48:06 +00:00
else if constexpr ( std : : is_same_v < Name , NameToDecimal128 > )
2020-08-26 12:26:12 +00:00
return createDecimalMaxPrecision < Decimal128 > ( scale ) ;
2020-08-19 11:52:17 +00:00
else if constexpr ( std : : is_same_v < Name , NameToDecimal256 > )
2020-08-26 12:26:12 +00:00
return createDecimalMaxPrecision < Decimal256 > ( scale ) ;
2018-08-21 18:25:38 +00:00
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Unexpected branch in code of conversion function: it is a bug. " ) ;
2018-08-21 18:25:38 +00:00
}
2017-12-02 02:47:12 +00:00
else
{
2019-12-11 08:56:32 +00:00
// Optional second argument with time zone for DateTime.
2019-10-14 10:15:10 +00:00
UInt8 timezone_arg_position = 1 ;
2019-11-07 10:56:13 +00:00
UInt32 scale [[maybe_unused]] = DataTypeDateTime64 : : default_scale ;
2017-11-05 05:32:22 +00:00
2019-10-08 04:59:38 +00:00
// DateTime64 requires more arguments: scale and timezone. Since timezone is optional, scale should be first.
2020-08-20 11:18:29 +00:00
if ( isDateTime64 < Name , ToDataType > ( arguments ) )
2017-11-05 05:32:22 +00:00
{
2019-10-14 10:15:10 +00:00
timezone_arg_position + = 1 ;
scale = static_cast < UInt32 > ( arguments [ 1 ] . column - > get64 ( 0 ) ) ;
2017-11-05 05:32:22 +00:00
2020-08-20 11:18:29 +00:00
if ( to_datetime64 | | scale ! = 0 ) /// toDateTime('xxxx-xx-xx xx:xx:xx', 0) return DateTime
return std : : make_shared < DataTypeDateTime64 > ( scale ,
2023-06-10 12:53:02 +00:00
extractTimeZoneNameFromFunctionArguments ( arguments , timezone_arg_position , 0 , false ) ) ;
2020-08-20 11:18:29 +00:00
2023-06-10 12:53:02 +00:00
return std : : make_shared < DataTypeDateTime > ( extractTimeZoneNameFromFunctionArguments ( arguments , timezone_arg_position , 0 , false ) ) ;
2017-11-05 05:32:22 +00:00
}
2019-10-14 10:15:10 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime > )
2023-06-10 12:53:02 +00:00
return std : : make_shared < DataTypeDateTime > ( extractTimeZoneNameFromFunctionArguments ( arguments , timezone_arg_position , 0 , false ) ) ;
2020-08-20 11:18:29 +00:00
else if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime64 > )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Unexpected branch in code of conversion function: it is a bug. " ) ;
2017-12-02 02:47:12 +00:00
else
2018-02-02 08:33:36 +00:00
return std : : make_shared < ToDataType > ( ) ;
2017-12-02 02:47:12 +00:00
}
2017-11-05 05:32:22 +00:00
}
2020-12-24 00:13:53 +00:00
/// Function actually uses default implementation for nulls,
/// but we need to know if return type is Nullable or not,
/// so we use checked_return_type only to intercept the first call to getReturnTypeImpl(...).
2021-07-12 09:18:04 +00:00
bool useDefaultImplementationForNulls ( ) const override
{
bool to_nullable_string = to_nullable & & std : : is_same_v < ToDataType , DataTypeString > ;
return checked_return_type & & ! to_nullable_string ;
}
2020-12-24 00:13:53 +00:00
2017-07-23 08:40:43 +00:00
bool useDefaultImplementationForConstants ( ) const override { return true ; }
2021-02-05 19:06:23 +00:00
ColumnNumbers getArgumentsThatAreAlwaysConstant ( ) const override
{
2023-09-01 16:30:39 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeString > )
return { } ;
else if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime64 > )
2021-02-05 19:06:23 +00:00
return { 2 } ;
return { 1 } ;
}
2018-08-08 16:05:50 +00:00
bool canBeExecutedOnDefaultArguments ( ) const override { return false ; }
2017-07-23 08:40:43 +00:00
2020-11-17 13:24:45 +00:00
ColumnPtr executeImpl ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , size_t input_rows_count ) const override
2016-08-17 06:55:47 +00:00
{
try
{
2020-10-17 14:23:37 +00:00
return executeInternal ( arguments , result_type , input_rows_count ) ;
2016-08-17 06:55:47 +00:00
}
2016-08-17 07:02:00 +00:00
catch ( Exception & e )
2016-08-17 06:55:47 +00:00
{
/// More convenient error message.
if ( e . code ( ) = = ErrorCodes : : ATTEMPT_TO_READ_AFTER_EOF )
{
e . addMessage ( " Cannot parse "
2020-10-17 14:23:37 +00:00
+ result_type - > getName ( ) + " from "
+ arguments [ 0 ] . type - > getName ( )
2016-08-17 06:55:47 +00:00
+ " , because value is too short " ) ;
}
else if ( e . code ( ) = = ErrorCodes : : CANNOT_PARSE_NUMBER
| | e . code ( ) = = ErrorCodes : : CANNOT_READ_ARRAY_FROM_TEXT
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_INPUT_ASSERTION_FAILED
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_QUOTED_STRING
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_ESCAPE_SEQUENCE
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_DATE
2017-06-15 09:12:32 +00:00
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_DATETIME
2022-11-14 14:17:17 +00:00
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_UUID
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_IPV4
| | e . code ( ) = = ErrorCodes : : CANNOT_PARSE_IPV6 )
2016-08-17 06:55:47 +00:00
{
e . addMessage ( " Cannot parse "
2020-10-17 14:23:37 +00:00
+ result_type - > getName ( ) + " from "
+ arguments [ 0 ] . type - > getName ( ) ) ;
2016-08-17 06:55:47 +00:00
}
2017-04-01 07:20:54 +00:00
2016-08-17 06:55:47 +00:00
throw ;
}
}
2017-04-01 07:20:54 +00:00
2016-08-17 06:55:47 +00:00
bool hasInformationAboutMonotonicity ( ) const override
{
return Monotonic : : has ( ) ;
}
2017-04-01 07:20:54 +00:00
2016-08-17 06:55:47 +00:00
Monotonicity getMonotonicityForRange ( const IDataType & type , const Field & left , const Field & right ) const override
{
return Monotonic : : get ( type , left , right ) ;
}
private :
2022-11-21 07:45:42 +00:00
ContextPtr context ;
2020-12-24 00:13:53 +00:00
mutable bool checked_return_type = false ;
2020-12-23 21:57:33 +00:00
mutable bool to_nullable = false ;
2020-11-17 13:24:45 +00:00
ColumnPtr executeInternal ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , size_t input_rows_count ) const
2011-10-15 23:40:56 +00:00
{
2020-10-17 14:23:37 +00:00
if ( arguments . empty ( ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : TOO_FEW_ARGUMENTS_FOR_FUNCTION , " Function {} expects at least 1 argument " , getName ( ) ) ;
2017-12-13 21:09:31 +00:00
2021-07-12 13:04:59 +00:00
if ( result_type - > onlyNull ( ) )
return result_type - > createColumnConstWithDefaultValue ( input_rows_count ) ;
2021-07-12 09:18:04 +00:00
const DataTypePtr from_type = removeNullable ( arguments [ 0 ] . type ) ;
2020-10-17 14:23:37 +00:00
ColumnPtr result_column ;
2017-07-21 06:35:58 +00:00
2023-10-23 23:49:40 +00:00
[[maybe_unused]] FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior ;
2023-10-19 09:11:45 +00:00
if ( context )
2023-10-23 23:49:40 +00:00
date_time_overflow_behavior = context - > getSettingsRef ( ) . date_time_overflow_behavior . value ;
2023-10-19 09:11:45 +00:00
2020-12-23 21:57:33 +00:00
auto call = [ & ] ( const auto & types , const auto & tag ) - > bool
2018-08-20 15:17:55 +00:00
{
using Types = std : : decay_t < decltype ( types ) > ;
using LeftDataType = typename Types : : LeftType ;
using RightDataType = typename Types : : RightType ;
2020-12-23 21:57:33 +00:00
using SpecialTag = std : : decay_t < decltype ( tag ) > ;
2018-08-20 15:17:55 +00:00
2018-09-10 13:52:18 +00:00
if constexpr ( IsDataTypeDecimal < RightDataType > )
2018-08-31 08:59:21 +00:00
{
2019-10-12 07:03:38 +00:00
if constexpr ( std : : is_same_v < RightDataType , DataTypeDateTime64 > )
{
2021-05-03 19:56:40 +00:00
/// Account for optional timezone argument.
2019-10-12 07:03:38 +00:00
if ( arguments . size ( ) ! = 2 & & arguments . size ( ) ! = 3 )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : TOO_FEW_ARGUMENTS_FOR_FUNCTION , " Function {} expects 2 or 3 arguments for DataTypeDateTime64. " , getName ( ) ) ;
2019-10-12 07:03:38 +00:00
}
else if ( arguments . size ( ) ! = 2 )
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : TOO_FEW_ARGUMENTS_FOR_FUNCTION , " Function {} expects 2 arguments for Decimal. " , getName ( ) ) ;
2019-10-12 07:03:38 +00:00
}
2018-08-31 08:59:21 +00:00
2020-10-17 14:23:37 +00:00
const ColumnWithTypeAndName & scale_column = arguments [ 1 ] ;
2018-08-31 08:59:21 +00:00
UInt32 scale = extractToDecimalScale ( scale_column ) ;
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : Throw :
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : Throw > : : execute ( arguments , result_type , input_rows_count , scale ) ;
2023-10-19 09:11:45 +00:00
break ;
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : Ignore :
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : Ignore > : : execute ( arguments , result_type , input_rows_count , scale ) ;
2023-10-19 09:11:45 +00:00
break ;
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : Saturate :
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : Saturate > : : execute ( arguments , result_type , input_rows_count , scale ) ;
2023-10-19 09:11:45 +00:00
break ;
}
2018-08-31 08:59:21 +00:00
}
2019-10-31 08:23:55 +00:00
else if constexpr ( IsDataTypeDateOrDateTime < RightDataType > & & std : : is_same_v < LeftDataType , DataTypeDateTime64 > )
{
2020-10-17 14:23:37 +00:00
const auto * dt64 = assert_cast < const DataTypeDateTime64 * > ( arguments [ 0 ] . type . get ( ) ) ;
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : Throw :
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : Throw > : : execute ( arguments , result_type , input_rows_count , dt64 - > getScale ( ) ) ;
2023-10-19 09:11:45 +00:00
break ;
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : Ignore :
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : Ignore > : : execute ( arguments , result_type , input_rows_count , dt64 - > getScale ( ) ) ;
2023-10-19 09:11:45 +00:00
break ;
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : Saturate :
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : Saturate > : : execute ( arguments , result_type , input_rows_count , dt64 - > getScale ( ) ) ;
2023-10-19 09:11:45 +00:00
break ;
}
2019-10-31 08:23:55 +00:00
}
2023-10-19 09:11:45 +00:00
# define GENERATE_OVERFLOW_MODE_CASE(OVERFLOW_MODE) \
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : OVERFLOW_MODE : \
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag , FormatSettings : : DateTimeOverflowBehavior : : OVERFLOW_MODE > : : execute ( \
2023-10-19 09:11:45 +00:00
arguments , result_type , input_rows_count ) ; \
break ;
2020-08-19 11:52:17 +00:00
else if constexpr ( IsDataTypeDecimalOrNumber < LeftDataType > & & IsDataTypeDecimalOrNumber < RightDataType > )
{
using LeftT = typename LeftDataType : : FieldType ;
using RightT = typename RightDataType : : FieldType ;
static constexpr bool bad_left =
2021-09-10 11:49:22 +00:00
is_decimal < LeftT > | | std : : is_floating_point_v < LeftT > | | is_big_int_v < LeftT > | | is_signed_v < LeftT > ;
2020-08-19 11:52:17 +00:00
static constexpr bool bad_right =
2021-09-10 11:49:22 +00:00
is_decimal < RightT > | | std : : is_floating_point_v < RightT > | | is_big_int_v < RightT > | | is_signed_v < RightT > ;
2020-08-19 11:52:17 +00:00
/// Disallow int vs UUID conversion (but support int vs UInt128 conversion)
if constexpr ( ( bad_left & & std : : is_same_v < RightDataType , DataTypeUUID > ) | |
( bad_right & & std : : is_same_v < LeftDataType , DataTypeUUID > ) )
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Wrong UUID conversion " ) ;
2020-08-19 11:52:17 +00:00
}
else
2021-12-20 23:59:08 +00:00
{
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 13:01:45 +00:00
GENERATE_OVERFLOW_MODE_CASE ( Throw )
GENERATE_OVERFLOW_MODE_CASE ( Ignore )
GENERATE_OVERFLOW_MODE_CASE ( Saturate )
2023-10-19 09:11:45 +00:00
}
2021-12-20 23:59:08 +00:00
}
2020-08-19 11:52:17 +00:00
}
2023-10-19 09:11:45 +00:00
else if constexpr ( ( IsDataTypeNumber < LeftDataType > | | IsDataTypeDateOrDateTime < LeftDataType > )
& & IsDataTypeDateOrDateTime < RightDataType > )
2021-07-15 11:40:45 +00:00
{
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 13:01:45 +00:00
GENERATE_OVERFLOW_MODE_CASE ( Throw )
GENERATE_OVERFLOW_MODE_CASE ( Ignore )
GENERATE_OVERFLOW_MODE_CASE ( Saturate )
2023-10-19 09:11:45 +00:00
}
2021-07-15 11:40:45 +00:00
}
2023-10-19 09:11:45 +00:00
# undef GENERATE_OVERFLOW_MODE_CASE
else
result_column = ConvertImpl < LeftDataType , RightDataType , Name , SpecialTag > : : execute ( arguments , result_type , input_rows_count ) ;
2019-05-04 00:07:54 +00:00
2018-08-20 15:17:55 +00:00
return true ;
} ;
2020-10-17 14:23:37 +00:00
if ( isDateTime64 < Name , ToDataType > ( arguments ) )
2020-08-15 13:29:24 +00:00
{
/// For toDateTime('xxxx-xx-xx xx:xx:xx.00', 2[, 'timezone']) we need to it convert to DateTime64
2020-10-17 14:23:37 +00:00
const ColumnWithTypeAndName & scale_column = arguments [ 1 ] ;
2020-08-20 11:18:29 +00:00
UInt32 scale = extractToDecimalScale ( scale_column ) ;
2020-08-15 17:08:03 +00:00
2020-08-21 05:16:50 +00:00
if ( to_datetime64 | | scale ! = 0 ) /// When scale = 0, the data type is DateTime otherwise the data type is DateTime64
2020-08-20 11:18:29 +00:00
{
2020-12-23 21:57:33 +00:00
if ( ! callOnIndexAndDataType < DataTypeDateTime64 > ( from_type - > getTypeId ( ) , call , ConvertDefaultBehaviorTag { } ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of argument of function {} " ,
arguments [ 0 ] . type - > getName ( ) , getName ( ) ) ;
2020-08-15 17:08:03 +00:00
2020-10-17 14:23:37 +00:00
return result_column ;
2020-08-15 13:29:24 +00:00
}
}
2022-01-25 09:32:21 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeString > )
{
if ( from_type - > getCustomSerialization ( ) )
return ConvertImplGenericToString < ColumnString > : : execute ( arguments , result_type , input_rows_count ) ;
}
2022-01-25 07:09:17 +00:00
2022-11-21 07:45:42 +00:00
bool done = false ;
2020-12-23 21:57:33 +00:00
if constexpr ( to_string_or_fixed_string )
{
done = callOnIndexAndDataType < ToDataType > ( from_type - > getTypeId ( ) , call , ConvertDefaultBehaviorTag { } ) ;
}
else
{
2022-11-21 07:45:42 +00:00
bool cast_ipv4_ipv6_default_on_conversion_error = false ;
if constexpr ( is_any_of < ToDataType , DataTypeIPv4 , DataTypeIPv6 > )
2022-11-25 15:57:49 +00:00
if ( context & & ( cast_ipv4_ipv6_default_on_conversion_error = context - > getSettingsRef ( ) . cast_ipv4_ipv6_default_on_conversion_error ) )
2022-11-21 07:45:42 +00:00
done = callOnIndexAndDataType < ToDataType > ( from_type - > getTypeId ( ) , call , ConvertReturnZeroOnErrorTag { } ) ;
if ( ! cast_ipv4_ipv6_default_on_conversion_error )
{
/// We should use ConvertFromStringExceptionMode::Null mode when converting from String (or FixedString)
/// to Nullable type, to avoid 'value is too short' error on attempt to parse empty string from NULL values.
if ( to_nullable & & WhichDataType ( from_type ) . isStringOrFixedString ( ) )
done = callOnIndexAndDataType < ToDataType > ( from_type - > getTypeId ( ) , call , ConvertReturnNullOnErrorTag { } ) ;
else
done = callOnIndexAndDataType < ToDataType > ( from_type - > getTypeId ( ) , call , ConvertDefaultBehaviorTag { } ) ;
}
2020-12-23 21:57:33 +00:00
}
2018-08-20 15:17:55 +00:00
if ( ! done )
2016-07-30 04:39:51 +00:00
{
/// Generic conversion of any type to String.
2017-12-25 04:01:46 +00:00
if ( std : : is_same_v < ToDataType , DataTypeString > )
2016-07-30 04:39:51 +00:00
{
2021-08-18 20:34:07 +00:00
return ConvertImplGenericToString < ColumnString > : : execute ( arguments , result_type , input_rows_count ) ;
2016-07-30 04:39:51 +00:00
}
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of argument of function {} " ,
arguments [ 0 ] . type - > getName ( ) , getName ( ) ) ;
2016-07-30 04:39:51 +00:00
}
2020-10-19 18:37:44 +00:00
return result_column ;
2011-10-15 23:40:56 +00:00
}
} ;
2018-02-11 23:57:07 +00:00
/** Function toTOrZero (where T is number of date or datetime type):
2016-08-02 05:32:09 +00:00
* try to convert from String to type T through parsing ,
* if cannot parse , return default value instead of throwing exception .
2018-02-11 23:57:07 +00:00
* Function toTOrNull will return Nullable type with NULL when cannot parse .
* NOTE Also need to implement tryToUnixTimestamp with timezone .
2016-08-02 05:32:09 +00:00
*/
2018-02-11 23:57:07 +00:00
template < typename ToDataType , typename Name ,
ConvertFromStringExceptionMode exception_mode ,
ConvertFromStringParsingMode parsing_mode = ConvertFromStringParsingMode : : Normal >
class FunctionConvertFromString : public IFunction
2016-08-02 05:32:09 +00:00
{
public :
static constexpr auto name = Name : : name ;
2018-08-31 08:59:21 +00:00
static constexpr bool to_decimal =
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal32 > > | |
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal64 > > | |
2020-08-19 11:52:17 +00:00
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal128 > > | |
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal256 > > ;
2018-08-31 08:59:21 +00:00
2020-05-16 12:11:17 +00:00
static constexpr bool to_datetime64 = std : : is_same_v < ToDataType , DataTypeDateTime64 > ;
2021-04-10 23:33:54 +00:00
static FunctionPtr create ( ContextPtr ) { return std : : make_shared < FunctionConvertFromString > ( ) ; }
2019-12-25 19:17:41 +00:00
static FunctionPtr create ( ) { return std : : make_shared < FunctionConvertFromString > ( ) ; }
2017-04-01 07:20:54 +00:00
2016-08-02 05:32:09 +00:00
String getName ( ) const override
{
return name ;
}
2017-04-01 07:20:54 +00:00
2018-02-11 23:57:07 +00:00
bool isVariadic ( ) const override { return true ; }
2021-06-28 10:23:07 +00:00
bool isSuitableForShortCircuitArgumentsExecution ( const DataTypesWithConstInfo & /*arguments*/ ) const override { return true ; }
2018-02-11 23:57:07 +00:00
size_t getNumberOfArguments ( ) const override { return 0 ; }
2017-04-01 07:20:54 +00:00
2017-07-23 08:40:43 +00:00
bool useDefaultImplementationForConstants ( ) const override { return true ; }
2021-12-29 10:34:54 +00:00
bool canBeExecutedOnDefaultArguments ( ) const override { return false ; }
2018-02-11 23:57:07 +00:00
ColumnNumbers getArgumentsThatAreAlwaysConstant ( ) const override { return { 1 } ; }
2017-07-23 08:40:43 +00:00
2018-02-11 23:57:07 +00:00
DataTypePtr getReturnTypeImpl ( const ColumnsWithTypeAndName & arguments ) const override
2016-08-02 05:32:09 +00:00
{
2020-05-16 12:11:17 +00:00
DataTypePtr res ;
2020-08-20 14:41:03 +00:00
if ( isDateTime64 < Name , ToDataType > ( arguments ) )
2019-11-21 15:46:25 +00:00
{
2020-05-16 12:11:17 +00:00
validateFunctionArgumentTypes ( * this , arguments ,
2024-03-07 16:16:13 +00:00
FunctionArgumentDescriptors { { " string " , static_cast < FunctionArgumentDescriptor : : TypeValidator > ( & isStringOrFixedString ) , nullptr , " String or FixedString " } } ,
2020-05-16 12:11:17 +00:00
// optional
FunctionArgumentDescriptors {
2024-03-07 16:16:13 +00:00
{ " precision " , static_cast < FunctionArgumentDescriptor : : TypeValidator > ( & isUInt8 ) , isColumnConst , " const UInt8 " } ,
{ " timezone " , static_cast < FunctionArgumentDescriptor : : TypeValidator > ( & isStringOrFixedString ) , isColumnConst , " const String or FixedString " } ,
2020-05-16 12:11:17 +00:00
} ) ;
2017-12-25 05:22:33 +00:00
2020-08-20 14:41:03 +00:00
UInt64 scale = to_datetime64 ? DataTypeDateTime64 : : default_scale : 0 ;
2020-05-16 12:11:17 +00:00
if ( arguments . size ( ) > 1 )
scale = extractToDecimalScale ( arguments [ 1 ] ) ;
2023-06-10 12:53:02 +00:00
const auto timezone = extractTimeZoneNameFromFunctionArguments ( arguments , 2 , 0 , false ) ;
2020-08-20 14:41:03 +00:00
res = scale = = 0 ? res = std : : make_shared < DataTypeDateTime > ( timezone ) : std : : make_shared < DataTypeDateTime64 > ( scale , timezone ) ;
2020-05-16 12:11:17 +00:00
}
else
2018-02-11 23:57:07 +00:00
{
2020-05-16 12:11:17 +00:00
if ( ( arguments . size ( ) ! = 1 & & arguments . size ( ) ! = 2 ) | | ( to_decimal & & arguments . size ( ) ! = 2 ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ,
" Number of arguments for function {} doesn't match: passed {}, should be 1 or 2. "
" Second argument only make sense for DateTime (time zone, optional) and Decimal (scale). " ,
getName ( ) , arguments . size ( ) ) ;
2020-05-16 12:11:17 +00:00
if ( ! isStringOrFixedString ( arguments [ 0 ] . type ) )
2019-05-15 18:50:35 +00:00
{
2020-05-16 12:11:17 +00:00
if ( this - > getName ( ) . find ( " OrZero " ) ! = std : : string : : npos | |
this - > getName ( ) . find ( " OrNull " ) ! = std : : string : : npos )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of first argument of function {}. "
" Conversion functions with postfix 'OrZero' or 'OrNull' should take String argument " ,
arguments [ 0 ] . type - > getName ( ) , getName ( ) ) ;
2020-05-16 12:11:17 +00:00
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of first argument of function {} " ,
arguments [ 0 ] . type - > getName ( ) , getName ( ) ) ;
2019-05-15 18:50:35 +00:00
}
2020-05-16 12:11:17 +00:00
if ( arguments . size ( ) = = 2 )
2019-05-15 18:50:35 +00:00
{
2020-05-16 12:11:17 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime > )
{
if ( ! isString ( arguments [ 1 ] . type ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of 2nd argument of function {} " ,
arguments [ 1 ] . type - > getName ( ) , getName ( ) ) ;
2020-05-16 12:11:17 +00:00
}
else if constexpr ( to_decimal )
{
if ( ! isInteger ( arguments [ 1 ] . type ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of 2nd argument of function {} " ,
arguments [ 1 ] . type - > getName ( ) , getName ( ) ) ;
2020-05-16 12:11:17 +00:00
if ( ! arguments [ 1 ] . column )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_COLUMN , " Second argument for function {} must be constant " , getName ( ) ) ;
2020-05-16 12:11:17 +00:00
}
else
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : NUMBER_OF_ARGUMENTS_DOESNT_MATCH ,
" Number of arguments for function {} doesn't match: passed {}, should be 1. "
" Second argument makes sense only for DateTime and Decimal. " ,
getName ( ) , arguments . size ( ) ) ;
2020-05-16 12:11:17 +00:00
}
2019-05-15 18:50:35 +00:00
}
2017-12-25 05:22:33 +00:00
2020-05-16 12:11:17 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime > )
2023-06-10 12:53:02 +00:00
res = std : : make_shared < DataTypeDateTime > ( extractTimeZoneNameFromFunctionArguments ( arguments , 1 , 0 , false ) ) ;
2020-08-20 14:41:03 +00:00
else if constexpr ( std : : is_same_v < ToDataType , DataTypeDateTime64 > )
2024-02-26 22:34:50 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " MaterializedMySQL is a bug. " ) ;
2020-05-16 12:11:17 +00:00
else if constexpr ( to_decimal )
{
UInt64 scale = extractToDecimalScale ( arguments [ 1 ] ) ;
2020-08-26 12:26:12 +00:00
res = createDecimalMaxPrecision < typename ToDataType : : FieldType > ( scale ) ;
2020-05-16 12:11:17 +00:00
if ( ! res )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Something wrong with toDecimalNNOrZero() or toDecimalNNOrNull ( ) " ) ;
2020-05-16 12:11:17 +00:00
}
else
res = std : : make_shared < ToDataType > ( ) ;
2019-12-11 08:56:32 +00:00
}
2018-02-12 00:55:46 +00:00
if constexpr ( exception_mode = = ConvertFromStringExceptionMode : : Null )
res = std : : make_shared < DataTypeNullable > ( res ) ;
return res ;
2017-12-25 05:22:33 +00:00
}
2020-08-20 14:41:03 +00:00
template < typename ConvertToDataType >
2020-11-17 13:24:45 +00:00
ColumnPtr executeInternal ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , size_t input_rows_count , UInt32 scale = 0 ) const
2017-12-25 05:22:33 +00:00
{
2020-10-17 14:23:37 +00:00
const IDataType * from_type = arguments [ 0 ] . type . get ( ) ;
2017-12-25 05:22:33 +00:00
2020-08-20 14:41:03 +00:00
if ( checkAndGetDataType < DataTypeString > ( from_type ) )
2019-05-15 18:50:35 +00:00
{
2020-10-17 14:23:37 +00:00
return ConvertThroughParsing < DataTypeString , ConvertToDataType , Name , exception_mode , parsing_mode > : : execute (
arguments , result_type , input_rows_count , scale ) ;
2019-05-15 18:50:35 +00:00
}
2020-08-20 14:41:03 +00:00
else if ( checkAndGetDataType < DataTypeFixedString > ( from_type ) )
{
2020-10-17 14:23:37 +00:00
return ConvertThroughParsing < DataTypeFixedString , ConvertToDataType , Name , exception_mode , parsing_mode > : : execute (
arguments , result_type , input_rows_count , scale ) ;
2019-05-15 18:50:35 +00:00
}
2020-08-20 14:41:03 +00:00
2020-10-17 14:23:37 +00:00
return nullptr ;
2020-08-20 14:41:03 +00:00
}
2020-11-17 13:24:45 +00:00
ColumnPtr executeImpl ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , size_t input_rows_count ) const override
2020-08-20 14:41:03 +00:00
{
2020-10-17 14:23:37 +00:00
ColumnPtr result_column ;
2020-08-20 14:41:03 +00:00
if constexpr ( to_decimal )
2020-10-17 14:23:37 +00:00
result_column = executeInternal < ToDataType > ( arguments , result_type , input_rows_count ,
assert_cast < const ToDataType & > ( * removeNullable ( result_type ) ) . getScale ( ) ) ;
2017-12-25 05:22:33 +00:00
else
2019-05-15 18:50:35 +00:00
{
2020-10-17 14:23:37 +00:00
if ( isDateTime64 < Name , ToDataType > ( arguments ) )
2019-05-15 18:50:35 +00:00
{
2020-08-20 14:41:03 +00:00
UInt64 scale = to_datetime64 ? DataTypeDateTime64 : : default_scale : 0 ;
if ( arguments . size ( ) > 1 )
2020-10-17 14:23:37 +00:00
scale = extractToDecimalScale ( arguments [ 1 ] ) ;
2020-08-20 14:41:03 +00:00
if ( scale = = 0 )
2020-10-17 14:23:37 +00:00
result_column = executeInternal < DataTypeDateTime > ( arguments , result_type , input_rows_count ) ;
2020-08-20 14:41:03 +00:00
else
{
2020-10-17 14:23:37 +00:00
result_column = executeInternal < DataTypeDateTime64 > ( arguments , result_type , input_rows_count , static_cast < UInt32 > ( scale ) ) ;
2020-08-20 14:41:03 +00:00
}
2019-05-15 18:50:35 +00:00
}
2020-08-20 14:41:03 +00:00
else
2019-05-15 18:50:35 +00:00
{
2020-10-17 14:23:37 +00:00
result_column = executeInternal < ToDataType > ( arguments , result_type , input_rows_count ) ;
2019-05-15 18:50:35 +00:00
}
}
2020-10-17 14:23:37 +00:00
if ( ! result_column )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : ILLEGAL_TYPE_OF_ARGUMENT , " Illegal type {} of argument of function {}. "
" Only String or FixedString argument is accepted for try-conversion function. For other arguments, "
" use function without 'orZero' or 'orNull'. " , arguments [ 0 ] . type - > getName ( ) , getName ( ) ) ;
2020-10-17 14:23:37 +00:00
return result_column ;
2016-08-02 05:32:09 +00:00
}
} ;
2013-12-03 10:19:57 +00:00
2016-07-23 23:02:14 +00:00
/// Monotonicity.
2015-11-29 08:06:29 +00:00
2015-11-29 11:58:44 +00:00
struct PositiveMonotonicity
2015-11-29 08:06:29 +00:00
{
static bool has ( ) { return true ; }
2017-12-02 02:47:12 +00:00
static IFunction : : Monotonicity get ( const IDataType & , const Field & , const Field & )
2015-11-29 08:06:29 +00:00
{
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true } ;
2015-11-29 08:06:29 +00:00
}
} ;
2018-08-22 17:26:11 +00:00
struct UnknownMonotonicity
{
static bool has ( ) { return false ; }
static IFunction : : Monotonicity get ( const IDataType & , const Field & , const Field & )
{
2021-09-29 16:01:26 +00:00
return { } ;
2018-08-22 17:26:11 +00:00
}
} ;
2015-11-29 11:58:44 +00:00
template < typename T >
2019-08-08 08:41:38 +00:00
struct ToNumberMonotonicity
2015-11-29 08:06:29 +00:00
{
2015-11-29 11:58:44 +00:00
static bool has ( ) { return true ; }
2017-04-01 07:20:54 +00:00
2017-12-02 02:47:12 +00:00
static UInt64 divideByRangeOfType ( UInt64 x )
{
if constexpr ( sizeof ( T ) < sizeof ( UInt64 ) )
return x > > ( sizeof ( T ) * 8 ) ;
else
return 0 ;
}
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
static IFunction : : Monotonicity get ( const IDataType & type , const Field & left , const Field & right )
2015-11-29 08:06:29 +00:00
{
2018-12-20 11:42:45 +00:00
if ( ! type . isValueRepresentedByNumber ( ) )
2018-12-19 06:57:37 +00:00
return { } ;
2018-12-24 15:46:55 +00:00
/// If type is same, the conversion is always monotonic.
/// (Enum has separate case, because it is different data type)
2018-12-21 11:37:33 +00:00
if ( checkAndGetDataType < DataTypeNumber < T > > ( & type ) | |
2018-09-07 14:37:26 +00:00
checkAndGetDataType < DataTypeEnum < T > > ( & type ) )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2017-04-01 07:20:54 +00:00
2018-12-24 15:46:55 +00:00
/// Float cases.
2018-12-24 13:25:48 +00:00
2018-12-24 15:46:55 +00:00
/// When converting to Float, the conversion is always monotonic.
2021-09-29 16:01:26 +00:00
if constexpr ( std : : is_floating_point_v < T > )
return { . is_monotonic = true , . is_always_monotonic = true } ;
2018-12-24 15:46:55 +00:00
2023-01-01 19:41:18 +00:00
const auto * low_cardinality = typeid_cast < const DataTypeLowCardinality * > ( & type ) ;
const IDataType * low_cardinality_dictionary_type = nullptr ;
if ( low_cardinality )
low_cardinality_dictionary_type = low_cardinality - > getDictionaryType ( ) . get ( ) ;
2022-11-27 10:40:26 +00:00
2023-01-01 19:41:18 +00:00
WhichDataType which_type ( type ) ;
WhichDataType which_inner_type = low_cardinality
? WhichDataType ( low_cardinality_dictionary_type )
: WhichDataType ( type ) ;
/// If converting from Float, for monotonicity, arguments must fit in range of result type.
if ( which_inner_type . isFloat ( ) )
2015-11-29 11:58:44 +00:00
{
2018-12-24 15:46:55 +00:00
if ( left . isNull ( ) | | right . isNull ( ) )
return { } ;
2015-11-29 11:58:44 +00:00
Float64 left_float = left . get < Float64 > ( ) ;
Float64 right_float = right . get < Float64 > ( ) ;
2017-04-01 07:20:54 +00:00
2020-08-19 11:52:17 +00:00
if ( left_float > = static_cast < Float64 > ( std : : numeric_limits < T > : : min ( ) )
& & left_float < = static_cast < Float64 > ( std : : numeric_limits < T > : : max ( ) )
& & right_float > = static_cast < Float64 > ( std : : numeric_limits < T > : : min ( ) )
& & right_float < = static_cast < Float64 > ( std : : numeric_limits < T > : : max ( ) ) )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true } ;
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
return { } ;
}
2017-04-01 07:20:54 +00:00
2018-12-24 15:46:55 +00:00
/// Integer cases.
2023-01-03 00:17:05 +00:00
/// Only support types represented by native integers.
/// It can be extended to big integers, decimals and DateTime64 later.
/// By the way, NULLs are representing unbounded ranges.
if ( ! ( ( left . isNull ( ) | | left . getType ( ) = = Field : : Types : : UInt64 | | left . getType ( ) = = Field : : Types : : Int64 )
& & ( right . isNull ( ) | | right . getType ( ) = = Field : : Types : : UInt64 | | right . getType ( ) = = Field : : Types : : Int64 ) ) )
return { } ;
2018-12-25 18:40:47 +00:00
const bool from_is_unsigned = type . isValueRepresentedByUnsignedInteger ( ) ;
2019-11-02 05:55:06 +00:00
const bool to_is_unsigned = is_unsigned_v < T > ;
2018-12-24 15:46:55 +00:00
2018-12-25 18:40:47 +00:00
const size_t size_of_from = type . getSizeOfValueInMemory ( ) ;
const size_t size_of_to = sizeof ( T ) ;
2018-12-24 15:46:55 +00:00
2018-12-25 18:40:47 +00:00
const bool left_in_first_half = left . isNull ( )
2018-12-24 15:46:55 +00:00
? from_is_unsigned
2018-12-25 18:40:47 +00:00
: ( left . get < Int64 > ( ) > = 0 ) ;
2018-12-24 15:46:55 +00:00
2018-12-25 18:40:47 +00:00
const bool right_in_first_half = right . isNull ( )
2018-12-24 15:46:55 +00:00
? ! from_is_unsigned
2018-12-25 18:40:47 +00:00
: ( right . get < Int64 > ( ) > = 0 ) ;
2018-12-25 18:06:38 +00:00
2018-12-24 15:46:55 +00:00
/// Size of type is the same.
if ( size_of_from = = size_of_to )
{
if ( from_is_unsigned = = to_is_unsigned )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2018-12-24 15:46:55 +00:00
if ( left_in_first_half = = right_in_first_half )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true } ;
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
return { } ;
2018-12-24 15:46:55 +00:00
}
2017-04-01 07:20:54 +00:00
2018-12-24 15:46:55 +00:00
/// Size of type is expanded.
if ( size_of_from < size_of_to )
{
if ( from_is_unsigned = = to_is_unsigned )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2018-12-24 15:46:55 +00:00
if ( ! to_is_unsigned )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2018-12-24 15:46:55 +00:00
/// signed -> unsigned. If arguments from the same half, then function is monotonic.
if ( left_in_first_half = = right_in_first_half )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true } ;
2018-12-25 18:40:47 +00:00
return { } ;
2018-12-24 15:46:55 +00:00
}
2020-08-08 00:47:03 +00:00
/// Size of type is shrunk.
2018-12-24 15:46:55 +00:00
if ( size_of_from > size_of_to )
{
/// Function cannot be monotonic on unbounded ranges.
if ( left . isNull ( ) | | right . isNull ( ) )
return { } ;
2020-09-14 03:34:14 +00:00
/// Function cannot be monotonic when left and right are not on the same ranges.
if ( divideByRangeOfType ( left . get < UInt64 > ( ) ) ! = divideByRangeOfType ( right . get < UInt64 > ( ) ) )
2018-12-24 15:46:55 +00:00
return { } ;
2020-09-14 03:34:14 +00:00
if ( to_is_unsigned )
2021-09-29 16:01:26 +00:00
return { . is_monotonic = true } ;
2020-09-14 03:34:14 +00:00
else
2021-09-29 16:01:26 +00:00
{
2020-09-14 03:34:14 +00:00
// If To is signed, it's possible that the signedness is different after conversion. So we check it explicitly.
2021-09-29 16:01:26 +00:00
const bool is_monotonic = ( T ( left . get < UInt64 > ( ) ) > = 0 ) = = ( T ( right . get < UInt64 > ( ) ) > = 0 ) ;
return { . is_monotonic = is_monotonic } ;
}
2018-12-24 15:46:55 +00:00
}
2022-10-07 19:20:14 +00:00
UNREACHABLE ( ) ;
2015-11-29 08:06:29 +00:00
}
} ;
2020-08-07 17:38:42 +00:00
struct ToDateMonotonicity
{
static bool has ( ) { return true ; }
static IFunction : : Monotonicity get ( const IDataType & type , const Field & left , const Field & right )
{
auto which = WhichDataType ( type ) ;
2023-10-19 09:11:45 +00:00
if ( which . isDateOrDate32 ( ) | | which . isDateTime ( ) | | which . isDateTime64 ( ) | | which . isInt8 ( ) | | which . isInt16 ( ) | | which . isUInt8 ( )
| | which . isUInt16 ( ) )
2022-10-22 07:02:20 +00:00
{
2023-10-19 09:11:45 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2022-10-22 07:02:20 +00:00
}
2020-08-07 17:38:42 +00:00
else if (
2022-10-22 07:02:20 +00:00
( ( left . getType ( ) = = Field : : Types : : UInt64 | | left . isNull ( ) ) & & ( right . getType ( ) = = Field : : Types : : UInt64 | | right . isNull ( ) )
2023-10-19 09:11:45 +00:00
& & ( ( left . isNull ( ) | | left . get < UInt64 > ( ) < 0xFFFF ) & & ( right . isNull ( ) | | right . get < UInt64 > ( ) > = 0xFFFF ) ) )
2022-10-22 07:02:20 +00:00
| | ( ( left . getType ( ) = = Field : : Types : : Int64 | | left . isNull ( ) ) & & ( right . getType ( ) = = Field : : Types : : Int64 | | right . isNull ( ) )
& & ( ( left . isNull ( ) | | left . get < Int64 > ( ) < 0xFFFF ) & & ( right . isNull ( ) | | right . get < Int64 > ( ) > = 0xFFFF ) ) )
2023-10-19 09:11:45 +00:00
| | ( (
( left . getType ( ) = = Field : : Types : : Float64 | | left . isNull ( ) )
& & ( right . getType ( ) = = Field : : Types : : Float64 | | right . isNull ( ) )
2022-10-22 07:02:20 +00:00
& & ( ( left . isNull ( ) | | left . get < Float64 > ( ) < 0xFFFF ) & & ( right . isNull ( ) | | right . get < Float64 > ( ) > = 0xFFFF ) ) ) )
| | ! isNativeNumber ( type ) )
{
2020-08-07 17:38:42 +00:00
return { } ;
2022-10-22 07:02:20 +00:00
}
2020-08-07 17:38:42 +00:00
else
2022-10-22 07:02:20 +00:00
{
2023-10-19 09:11:45 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2022-10-22 07:02:20 +00:00
}
2020-08-07 17:38:42 +00:00
}
} ;
2020-08-08 06:30:50 +00:00
struct ToDateTimeMonotonicity
{
static bool has ( ) { return true ; }
static IFunction : : Monotonicity get ( const IDataType & type , const Field & , const Field & )
{
if ( type . isValueRepresentedByNumber ( ) )
2023-10-19 09:11:45 +00:00
return { . is_monotonic = true , . is_always_monotonic = true } ;
2020-08-08 06:30:50 +00:00
else
return { } ;
}
} ;
2017-05-13 22:19:04 +00:00
/** The monotonicity for the `toString` function is mainly determined for test purposes.
* It is doubtful that anyone is looking to optimize queries with conditions ` toString ( CounterID ) = 34 ` .
2015-11-29 11:58:44 +00:00
*/
struct ToStringMonotonicity
2015-11-29 08:06:29 +00:00
{
static bool has ( ) { return true ; }
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
static IFunction : : Monotonicity get ( const IDataType & type , const Field & left , const Field & right )
2015-11-29 08:06:29 +00:00
{
2021-09-29 16:01:26 +00:00
IFunction : : Monotonicity positive { . is_monotonic = true } ;
2015-11-29 11:58:44 +00:00
IFunction : : Monotonicity not_monotonic ;
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
const auto * type_ptr = & type ;
if ( const auto * low_cardinality_type = checkAndGetDataType < DataTypeLowCardinality > ( type_ptr ) )
2020-04-08 05:27:46 +00:00
type_ptr = low_cardinality_type - > getDictionaryType ( ) . get ( ) ;
2017-04-01 07:20:54 +00:00
2022-11-23 22:08:14 +00:00
/// Order on enum values (which is the order on integers) is completely arbitrary in respect to the order on strings.
if ( WhichDataType ( type ) . isEnum ( ) )
return not_monotonic ;
2021-07-15 11:40:45 +00:00
/// `toString` function is monotonous if the argument is Date or Date32 or DateTime or String, or non-negative numbers with the same number of symbols.
if ( checkDataTypes < DataTypeDate , DataTypeDate32 , DataTypeDateTime , DataTypeString > ( type_ptr ) )
2015-11-29 11:58:44 +00:00
return positive ;
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
if ( left . isNull ( ) | | right . isNull ( ) )
2015-11-29 08:06:29 +00:00
return { } ;
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
if ( left . getType ( ) = = Field : : Types : : UInt64
& & right . getType ( ) = = Field : : Types : : UInt64 )
{
return ( left . get < Int64 > ( ) = = 0 & & right . get < Int64 > ( ) = = 0 )
| | ( floor ( log10 ( left . get < UInt64 > ( ) ) ) = = floor ( log10 ( right . get < UInt64 > ( ) ) ) )
? positive : not_monotonic ;
}
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
if ( left . getType ( ) = = Field : : Types : : Int64
& & right . getType ( ) = = Field : : Types : : Int64 )
{
return ( left . get < Int64 > ( ) = = 0 & & right . get < Int64 > ( ) = = 0 )
| | ( left . get < Int64 > ( ) > 0 & & right . get < Int64 > ( ) > 0 & & floor ( log10 ( left . get < Int64 > ( ) ) ) = = floor ( log10 ( right . get < Int64 > ( ) ) ) )
? positive : not_monotonic ;
}
2017-04-01 07:20:54 +00:00
2015-11-29 11:58:44 +00:00
return not_monotonic ;
2015-11-29 08:06:29 +00:00
}
} ;
2017-10-30 02:18:06 +00:00
struct NameToUInt8 { static constexpr auto name = " toUInt8 " ; } ;
struct NameToUInt16 { static constexpr auto name = " toUInt16 " ; } ;
struct NameToUInt32 { static constexpr auto name = " toUInt32 " ; } ;
struct NameToUInt64 { static constexpr auto name = " toUInt64 " ; } ;
2021-05-03 15:41:37 +00:00
struct NameToUInt128 { static constexpr auto name = " toUInt128 " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToUInt256 { static constexpr auto name = " toUInt256 " ; } ;
2017-10-30 02:18:06 +00:00
struct NameToInt8 { static constexpr auto name = " toInt8 " ; } ;
struct NameToInt16 { static constexpr auto name = " toInt16 " ; } ;
struct NameToInt32 { static constexpr auto name = " toInt32 " ; } ;
struct NameToInt64 { static constexpr auto name = " toInt64 " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToInt128 { static constexpr auto name = " toInt128 " ; } ;
struct NameToInt256 { static constexpr auto name = " toInt256 " ; } ;
2017-10-30 02:18:06 +00:00
struct NameToFloat32 { static constexpr auto name = " toFloat32 " ; } ;
struct NameToFloat64 { static constexpr auto name = " toFloat64 " ; } ;
struct NameToUUID { static constexpr auto name = " toUUID " ; } ;
2022-11-14 14:17:17 +00:00
struct NameToIPv4 { static constexpr auto name = " toIPv4 " ; } ;
struct NameToIPv6 { static constexpr auto name = " toIPv6 " ; } ;
2017-10-30 02:18:06 +00:00
2019-08-08 08:41:38 +00:00
using FunctionToUInt8 = FunctionConvert < DataTypeUInt8 , NameToUInt8 , ToNumberMonotonicity < UInt8 > > ;
using FunctionToUInt16 = FunctionConvert < DataTypeUInt16 , NameToUInt16 , ToNumberMonotonicity < UInt16 > > ;
using FunctionToUInt32 = FunctionConvert < DataTypeUInt32 , NameToUInt32 , ToNumberMonotonicity < UInt32 > > ;
using FunctionToUInt64 = FunctionConvert < DataTypeUInt64 , NameToUInt64 , ToNumberMonotonicity < UInt64 > > ;
2021-05-03 15:41:37 +00:00
using FunctionToUInt128 = FunctionConvert < DataTypeUInt128 , NameToUInt128 , ToNumberMonotonicity < UInt128 > > ;
2020-09-01 09:54:50 +00:00
using FunctionToUInt256 = FunctionConvert < DataTypeUInt256 , NameToUInt256 , ToNumberMonotonicity < UInt256 > > ;
2019-08-08 08:41:38 +00:00
using FunctionToInt8 = FunctionConvert < DataTypeInt8 , NameToInt8 , ToNumberMonotonicity < Int8 > > ;
using FunctionToInt16 = FunctionConvert < DataTypeInt16 , NameToInt16 , ToNumberMonotonicity < Int16 > > ;
using FunctionToInt32 = FunctionConvert < DataTypeInt32 , NameToInt32 , ToNumberMonotonicity < Int32 > > ;
using FunctionToInt64 = FunctionConvert < DataTypeInt64 , NameToInt64 , ToNumberMonotonicity < Int64 > > ;
2020-08-19 11:52:17 +00:00
using FunctionToInt128 = FunctionConvert < DataTypeInt128 , NameToInt128 , ToNumberMonotonicity < Int128 > > ;
2020-09-01 09:54:50 +00:00
using FunctionToInt256 = FunctionConvert < DataTypeInt256 , NameToInt256 , ToNumberMonotonicity < Int256 > > ;
2019-08-08 08:41:38 +00:00
using FunctionToFloat32 = FunctionConvert < DataTypeFloat32 , NameToFloat32 , ToNumberMonotonicity < Float32 > > ;
using FunctionToFloat64 = FunctionConvert < DataTypeFloat64 , NameToFloat64 , ToNumberMonotonicity < Float64 > > ;
2023-10-19 09:11:45 +00:00
2020-08-07 17:38:42 +00:00
using FunctionToDate = FunctionConvert < DataTypeDate , NameToDate , ToDateMonotonicity > ;
2023-10-19 09:11:45 +00:00
2021-07-15 11:41:52 +00:00
using FunctionToDate32 = FunctionConvert < DataTypeDate32 , NameToDate32 , ToDateMonotonicity > ;
2023-10-19 09:11:45 +00:00
2020-08-08 06:30:50 +00:00
using FunctionToDateTime = FunctionConvert < DataTypeDateTime , NameToDateTime , ToDateTimeMonotonicity > ;
2023-10-19 09:11:45 +00:00
2020-08-15 13:29:24 +00:00
using FunctionToDateTime32 = FunctionConvert < DataTypeDateTime , NameToDateTime32 , ToDateTimeMonotonicity > ;
2023-10-19 09:11:45 +00:00
2022-08-03 13:36:36 +00:00
using FunctionToDateTime64 = FunctionConvert < DataTypeDateTime64 , NameToDateTime64 , ToDateTimeMonotonicity > ;
2023-10-19 09:11:45 +00:00
2019-08-08 08:41:38 +00:00
using FunctionToUUID = FunctionConvert < DataTypeUUID , NameToUUID , ToNumberMonotonicity < UInt128 > > ;
2022-11-14 14:17:17 +00:00
using FunctionToIPv4 = FunctionConvert < DataTypeIPv4 , NameToIPv4 , ToNumberMonotonicity < UInt32 > > ;
using FunctionToIPv6 = FunctionConvert < DataTypeIPv6 , NameToIPv6 , ToNumberMonotonicity < UInt128 > > ;
2017-10-30 02:18:06 +00:00
using FunctionToString = FunctionConvert < DataTypeString , NameToString , ToStringMonotonicity > ;
2019-08-08 08:41:38 +00:00
using FunctionToUnixTimestamp = FunctionConvert < DataTypeUInt32 , NameToUnixTimestamp , ToNumberMonotonicity < UInt32 > > ;
2018-08-22 17:26:11 +00:00
using FunctionToDecimal32 = FunctionConvert < DataTypeDecimal < Decimal32 > , NameToDecimal32 , UnknownMonotonicity > ;
using FunctionToDecimal64 = FunctionConvert < DataTypeDecimal < Decimal64 > , NameToDecimal64 , UnknownMonotonicity > ;
using FunctionToDecimal128 = FunctionConvert < DataTypeDecimal < Decimal128 > , NameToDecimal128 , UnknownMonotonicity > ;
2020-08-19 11:52:17 +00:00
using FunctionToDecimal256 = FunctionConvert < DataTypeDecimal < Decimal256 > , NameToDecimal256 , UnknownMonotonicity > ;
2017-10-30 02:18:06 +00:00
2023-10-23 23:49:40 +00:00
template < typename DataType , FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior > struct FunctionTo ;
2017-10-30 02:18:06 +00:00
2015-12-29 13:51:12 +00:00
template < > struct FunctionTo < DataTypeUInt8 > { using Type = FunctionToUInt8 ; } ;
template < > struct FunctionTo < DataTypeUInt16 > { using Type = FunctionToUInt16 ; } ;
template < > struct FunctionTo < DataTypeUInt32 > { using Type = FunctionToUInt32 ; } ;
template < > struct FunctionTo < DataTypeUInt64 > { using Type = FunctionToUInt64 ; } ;
2021-05-03 15:41:37 +00:00
template < > struct FunctionTo < DataTypeUInt128 > { using Type = FunctionToUInt128 ; } ;
2020-08-19 11:52:17 +00:00
template < > struct FunctionTo < DataTypeUInt256 > { using Type = FunctionToUInt256 ; } ;
2015-12-29 13:51:12 +00:00
template < > struct FunctionTo < DataTypeInt8 > { using Type = FunctionToInt8 ; } ;
template < > struct FunctionTo < DataTypeInt16 > { using Type = FunctionToInt16 ; } ;
template < > struct FunctionTo < DataTypeInt32 > { using Type = FunctionToInt32 ; } ;
template < > struct FunctionTo < DataTypeInt64 > { using Type = FunctionToInt64 ; } ;
2020-08-19 11:52:17 +00:00
template < > struct FunctionTo < DataTypeInt128 > { using Type = FunctionToInt128 ; } ;
template < > struct FunctionTo < DataTypeInt256 > { using Type = FunctionToInt256 ; } ;
2015-12-29 13:51:12 +00:00
template < > struct FunctionTo < DataTypeFloat32 > { using Type = FunctionToFloat32 ; } ;
template < > struct FunctionTo < DataTypeFloat64 > { using Type = FunctionToFloat64 ; } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct FunctionTo < DataTypeDate , date_time_overflow_behavior > { using Type = FunctionToDate ; } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct FunctionTo < DataTypeDate32 , date_time_overflow_behavior > { using Type = FunctionToDate32 ; } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct FunctionTo < DataTypeDateTime , date_time_overflow_behavior > { using Type = FunctionToDateTime ; } ;
2023-10-19 09:11:45 +00:00
2023-10-23 23:49:40 +00:00
template < FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior >
struct FunctionTo < DataTypeDateTime64 , date_time_overflow_behavior > { using Type = FunctionToDateTime64 ; } ;
2023-10-19 09:11:45 +00:00
2017-07-04 10:42:53 +00:00
template < > struct FunctionTo < DataTypeUUID > { using Type = FunctionToUUID ; } ;
2022-11-14 14:17:17 +00:00
template < > struct FunctionTo < DataTypeIPv4 > { using Type = FunctionToIPv4 ; } ;
template < > struct FunctionTo < DataTypeIPv6 > { using Type = FunctionToIPv6 ; } ;
2015-12-29 13:51:12 +00:00
template < > struct FunctionTo < DataTypeString > { using Type = FunctionToString ; } ;
template < > struct FunctionTo < DataTypeFixedString > { using Type = FunctionToFixedString ; } ;
2018-08-22 16:16:39 +00:00
template < > struct FunctionTo < DataTypeDecimal < Decimal32 > > { using Type = FunctionToDecimal32 ; } ;
template < > struct FunctionTo < DataTypeDecimal < Decimal64 > > { using Type = FunctionToDecimal64 ; } ;
template < > struct FunctionTo < DataTypeDecimal < Decimal128 > > { using Type = FunctionToDecimal128 ; } ;
2020-08-19 11:52:17 +00:00
template < > struct FunctionTo < DataTypeDecimal < Decimal256 > > { using Type = FunctionToDecimal256 ; } ;
2017-10-30 02:18:06 +00:00
2015-12-29 13:51:12 +00:00
template < typename FieldType > struct FunctionTo < DataTypeEnum < FieldType > >
2017-03-12 10:13:45 +00:00
: FunctionTo < DataTypeNumber < FieldType > >
2015-12-29 13:51:12 +00:00
{
} ;
2015-12-25 15:43:38 +00:00
2017-10-30 02:18:06 +00:00
struct NameToUInt8OrZero { static constexpr auto name = " toUInt8OrZero " ; } ;
struct NameToUInt16OrZero { static constexpr auto name = " toUInt16OrZero " ; } ;
struct NameToUInt32OrZero { static constexpr auto name = " toUInt32OrZero " ; } ;
struct NameToUInt64OrZero { static constexpr auto name = " toUInt64OrZero " ; } ;
2021-05-03 15:41:37 +00:00
struct NameToUInt128OrZero { static constexpr auto name = " toUInt128OrZero " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToUInt256OrZero { static constexpr auto name = " toUInt256OrZero " ; } ;
2017-10-30 02:18:06 +00:00
struct NameToInt8OrZero { static constexpr auto name = " toInt8OrZero " ; } ;
struct NameToInt16OrZero { static constexpr auto name = " toInt16OrZero " ; } ;
struct NameToInt32OrZero { static constexpr auto name = " toInt32OrZero " ; } ;
struct NameToInt64OrZero { static constexpr auto name = " toInt64OrZero " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToInt128OrZero { static constexpr auto name = " toInt128OrZero " ; } ;
struct NameToInt256OrZero { static constexpr auto name = " toInt256OrZero " ; } ;
2017-10-30 02:18:06 +00:00
struct NameToFloat32OrZero { static constexpr auto name = " toFloat32OrZero " ; } ;
struct NameToFloat64OrZero { static constexpr auto name = " toFloat64OrZero " ; } ;
2018-02-11 23:57:07 +00:00
struct NameToDateOrZero { static constexpr auto name = " toDateOrZero " ; } ;
2021-07-15 11:41:52 +00:00
struct NameToDate32OrZero { static constexpr auto name = " toDate32OrZero " ; } ;
2018-02-11 23:57:07 +00:00
struct NameToDateTimeOrZero { static constexpr auto name = " toDateTimeOrZero " ; } ;
2019-10-12 07:03:38 +00:00
struct NameToDateTime64OrZero { static constexpr auto name = " toDateTime64OrZero " ; } ;
2019-05-15 18:50:35 +00:00
struct NameToDecimal32OrZero { static constexpr auto name = " toDecimal32OrZero " ; } ;
struct NameToDecimal64OrZero { static constexpr auto name = " toDecimal64OrZero " ; } ;
struct NameToDecimal128OrZero { static constexpr auto name = " toDecimal128OrZero " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToDecimal256OrZero { static constexpr auto name = " toDecimal256OrZero " ; } ;
2020-10-25 08:45:29 +00:00
struct NameToUUIDOrZero { static constexpr auto name = " toUUIDOrZero " ; } ;
2022-11-14 14:17:17 +00:00
struct NameToIPv4OrZero { static constexpr auto name = " toIPv4OrZero " ; } ;
struct NameToIPv6OrZero { static constexpr auto name = " toIPv6OrZero " ; } ;
2018-02-11 23:57:07 +00:00
using FunctionToUInt8OrZero = FunctionConvertFromString < DataTypeUInt8 , NameToUInt8OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToUInt16OrZero = FunctionConvertFromString < DataTypeUInt16 , NameToUInt16OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToUInt32OrZero = FunctionConvertFromString < DataTypeUInt32 , NameToUInt32OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToUInt64OrZero = FunctionConvertFromString < DataTypeUInt64 , NameToUInt64OrZero , ConvertFromStringExceptionMode : : Zero > ;
2021-05-03 15:41:37 +00:00
using FunctionToUInt128OrZero = FunctionConvertFromString < DataTypeUInt128 , NameToUInt128OrZero , ConvertFromStringExceptionMode : : Zero > ;
2020-08-19 11:52:17 +00:00
using FunctionToUInt256OrZero = FunctionConvertFromString < DataTypeUInt256 , NameToUInt256OrZero , ConvertFromStringExceptionMode : : Zero > ;
2018-02-11 23:57:07 +00:00
using FunctionToInt8OrZero = FunctionConvertFromString < DataTypeInt8 , NameToInt8OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToInt16OrZero = FunctionConvertFromString < DataTypeInt16 , NameToInt16OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToInt32OrZero = FunctionConvertFromString < DataTypeInt32 , NameToInt32OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToInt64OrZero = FunctionConvertFromString < DataTypeInt64 , NameToInt64OrZero , ConvertFromStringExceptionMode : : Zero > ;
2020-08-19 11:52:17 +00:00
using FunctionToInt128OrZero = FunctionConvertFromString < DataTypeInt128 , NameToInt128OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToInt256OrZero = FunctionConvertFromString < DataTypeInt256 , NameToInt256OrZero , ConvertFromStringExceptionMode : : Zero > ;
2018-02-11 23:57:07 +00:00
using FunctionToFloat32OrZero = FunctionConvertFromString < DataTypeFloat32 , NameToFloat32OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToFloat64OrZero = FunctionConvertFromString < DataTypeFloat64 , NameToFloat64OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToDateOrZero = FunctionConvertFromString < DataTypeDate , NameToDateOrZero , ConvertFromStringExceptionMode : : Zero > ;
2021-07-15 11:41:52 +00:00
using FunctionToDate32OrZero = FunctionConvertFromString < DataTypeDate32 , NameToDate32OrZero , ConvertFromStringExceptionMode : : Zero > ;
2018-02-11 23:57:07 +00:00
using FunctionToDateTimeOrZero = FunctionConvertFromString < DataTypeDateTime , NameToDateTimeOrZero , ConvertFromStringExceptionMode : : Zero > ;
2019-10-12 07:03:38 +00:00
using FunctionToDateTime64OrZero = FunctionConvertFromString < DataTypeDateTime64 , NameToDateTime64OrZero , ConvertFromStringExceptionMode : : Zero > ;
2019-05-15 18:50:35 +00:00
using FunctionToDecimal32OrZero = FunctionConvertFromString < DataTypeDecimal < Decimal32 > , NameToDecimal32OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToDecimal64OrZero = FunctionConvertFromString < DataTypeDecimal < Decimal64 > , NameToDecimal64OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToDecimal128OrZero = FunctionConvertFromString < DataTypeDecimal < Decimal128 > , NameToDecimal128OrZero , ConvertFromStringExceptionMode : : Zero > ;
2020-08-19 11:52:17 +00:00
using FunctionToDecimal256OrZero = FunctionConvertFromString < DataTypeDecimal < Decimal256 > , NameToDecimal256OrZero , ConvertFromStringExceptionMode : : Zero > ;
2020-10-25 08:45:29 +00:00
using FunctionToUUIDOrZero = FunctionConvertFromString < DataTypeUUID , NameToUUIDOrZero , ConvertFromStringExceptionMode : : Zero > ;
2022-11-14 14:17:17 +00:00
using FunctionToIPv4OrZero = FunctionConvertFromString < DataTypeIPv4 , NameToIPv4OrZero , ConvertFromStringExceptionMode : : Zero > ;
using FunctionToIPv6OrZero = FunctionConvertFromString < DataTypeIPv6 , NameToIPv6OrZero , ConvertFromStringExceptionMode : : Zero > ;
2016-08-02 05:32:09 +00:00
2017-12-25 05:22:33 +00:00
struct NameToUInt8OrNull { static constexpr auto name = " toUInt8OrNull " ; } ;
struct NameToUInt16OrNull { static constexpr auto name = " toUInt16OrNull " ; } ;
struct NameToUInt32OrNull { static constexpr auto name = " toUInt32OrNull " ; } ;
struct NameToUInt64OrNull { static constexpr auto name = " toUInt64OrNull " ; } ;
2021-05-03 15:41:37 +00:00
struct NameToUInt128OrNull { static constexpr auto name = " toUInt128OrNull " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToUInt256OrNull { static constexpr auto name = " toUInt256OrNull " ; } ;
2017-12-25 05:22:33 +00:00
struct NameToInt8OrNull { static constexpr auto name = " toInt8OrNull " ; } ;
struct NameToInt16OrNull { static constexpr auto name = " toInt16OrNull " ; } ;
struct NameToInt32OrNull { static constexpr auto name = " toInt32OrNull " ; } ;
struct NameToInt64OrNull { static constexpr auto name = " toInt64OrNull " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToInt128OrNull { static constexpr auto name = " toInt128OrNull " ; } ;
struct NameToInt256OrNull { static constexpr auto name = " toInt256OrNull " ; } ;
2017-12-25 05:22:33 +00:00
struct NameToFloat32OrNull { static constexpr auto name = " toFloat32OrNull " ; } ;
struct NameToFloat64OrNull { static constexpr auto name = " toFloat64OrNull " ; } ;
2018-02-11 23:57:07 +00:00
struct NameToDateOrNull { static constexpr auto name = " toDateOrNull " ; } ;
2021-07-15 11:41:52 +00:00
struct NameToDate32OrNull { static constexpr auto name = " toDate32OrNull " ; } ;
2018-02-11 23:57:07 +00:00
struct NameToDateTimeOrNull { static constexpr auto name = " toDateTimeOrNull " ; } ;
2019-10-12 07:03:38 +00:00
struct NameToDateTime64OrNull { static constexpr auto name = " toDateTime64OrNull " ; } ;
2019-05-15 18:50:35 +00:00
struct NameToDecimal32OrNull { static constexpr auto name = " toDecimal32OrNull " ; } ;
struct NameToDecimal64OrNull { static constexpr auto name = " toDecimal64OrNull " ; } ;
struct NameToDecimal128OrNull { static constexpr auto name = " toDecimal128OrNull " ; } ;
2020-08-19 11:52:17 +00:00
struct NameToDecimal256OrNull { static constexpr auto name = " toDecimal256OrNull " ; } ;
2020-10-25 08:45:29 +00:00
struct NameToUUIDOrNull { static constexpr auto name = " toUUIDOrNull " ; } ;
2022-11-14 14:17:17 +00:00
struct NameToIPv4OrNull { static constexpr auto name = " toIPv4OrNull " ; } ;
struct NameToIPv6OrNull { static constexpr auto name = " toIPv6OrNull " ; } ;
2018-02-11 23:57:07 +00:00
using FunctionToUInt8OrNull = FunctionConvertFromString < DataTypeUInt8 , NameToUInt8OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToUInt16OrNull = FunctionConvertFromString < DataTypeUInt16 , NameToUInt16OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToUInt32OrNull = FunctionConvertFromString < DataTypeUInt32 , NameToUInt32OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToUInt64OrNull = FunctionConvertFromString < DataTypeUInt64 , NameToUInt64OrNull , ConvertFromStringExceptionMode : : Null > ;
2021-05-03 15:41:37 +00:00
using FunctionToUInt128OrNull = FunctionConvertFromString < DataTypeUInt128 , NameToUInt128OrNull , ConvertFromStringExceptionMode : : Null > ;
2020-08-19 11:52:17 +00:00
using FunctionToUInt256OrNull = FunctionConvertFromString < DataTypeUInt256 , NameToUInt256OrNull , ConvertFromStringExceptionMode : : Null > ;
2018-02-11 23:57:07 +00:00
using FunctionToInt8OrNull = FunctionConvertFromString < DataTypeInt8 , NameToInt8OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToInt16OrNull = FunctionConvertFromString < DataTypeInt16 , NameToInt16OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToInt32OrNull = FunctionConvertFromString < DataTypeInt32 , NameToInt32OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToInt64OrNull = FunctionConvertFromString < DataTypeInt64 , NameToInt64OrNull , ConvertFromStringExceptionMode : : Null > ;
2020-08-19 11:52:17 +00:00
using FunctionToInt128OrNull = FunctionConvertFromString < DataTypeInt128 , NameToInt128OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToInt256OrNull = FunctionConvertFromString < DataTypeInt256 , NameToInt256OrNull , ConvertFromStringExceptionMode : : Null > ;
2018-02-11 23:57:07 +00:00
using FunctionToFloat32OrNull = FunctionConvertFromString < DataTypeFloat32 , NameToFloat32OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToFloat64OrNull = FunctionConvertFromString < DataTypeFloat64 , NameToFloat64OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToDateOrNull = FunctionConvertFromString < DataTypeDate , NameToDateOrNull , ConvertFromStringExceptionMode : : Null > ;
2021-07-15 11:41:52 +00:00
using FunctionToDate32OrNull = FunctionConvertFromString < DataTypeDate32 , NameToDate32OrNull , ConvertFromStringExceptionMode : : Null > ;
2018-02-11 23:57:07 +00:00
using FunctionToDateTimeOrNull = FunctionConvertFromString < DataTypeDateTime , NameToDateTimeOrNull , ConvertFromStringExceptionMode : : Null > ;
2019-10-12 07:03:38 +00:00
using FunctionToDateTime64OrNull = FunctionConvertFromString < DataTypeDateTime64 , NameToDateTime64OrNull , ConvertFromStringExceptionMode : : Null > ;
2019-05-15 18:50:35 +00:00
using FunctionToDecimal32OrNull = FunctionConvertFromString < DataTypeDecimal < Decimal32 > , NameToDecimal32OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToDecimal64OrNull = FunctionConvertFromString < DataTypeDecimal < Decimal64 > , NameToDecimal64OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToDecimal128OrNull = FunctionConvertFromString < DataTypeDecimal < Decimal128 > , NameToDecimal128OrNull , ConvertFromStringExceptionMode : : Null > ;
2020-08-19 11:52:17 +00:00
using FunctionToDecimal256OrNull = FunctionConvertFromString < DataTypeDecimal < Decimal256 > , NameToDecimal256OrNull , ConvertFromStringExceptionMode : : Null > ;
2020-10-25 08:45:29 +00:00
using FunctionToUUIDOrNull = FunctionConvertFromString < DataTypeUUID , NameToUUIDOrNull , ConvertFromStringExceptionMode : : Null > ;
2022-11-14 14:17:17 +00:00
using FunctionToIPv4OrNull = FunctionConvertFromString < DataTypeIPv4 , NameToIPv4OrNull , ConvertFromStringExceptionMode : : Null > ;
using FunctionToIPv6OrNull = FunctionConvertFromString < DataTypeIPv6 , NameToIPv6OrNull , ConvertFromStringExceptionMode : : Null > ;
2018-02-11 23:57:07 +00:00
struct NameParseDateTimeBestEffort { static constexpr auto name = " parseDateTimeBestEffort " ; } ;
struct NameParseDateTimeBestEffortOrZero { static constexpr auto name = " parseDateTimeBestEffortOrZero " ; } ;
struct NameParseDateTimeBestEffortOrNull { static constexpr auto name = " parseDateTimeBestEffortOrNull " ; } ;
2021-01-27 14:10:06 +00:00
struct NameParseDateTimeBestEffortUS { static constexpr auto name = " parseDateTimeBestEffortUS " ; } ;
struct NameParseDateTimeBestEffortUSOrZero { static constexpr auto name = " parseDateTimeBestEffortUSOrZero " ; } ;
struct NameParseDateTimeBestEffortUSOrNull { static constexpr auto name = " parseDateTimeBestEffortUSOrNull " ; } ;
2020-08-20 14:41:03 +00:00
struct NameParseDateTime32BestEffort { static constexpr auto name = " parseDateTime32BestEffort " ; } ;
struct NameParseDateTime32BestEffortOrZero { static constexpr auto name = " parseDateTime32BestEffortOrZero " ; } ;
struct NameParseDateTime32BestEffortOrNull { static constexpr auto name = " parseDateTime32BestEffortOrNull " ; } ;
2019-10-12 07:03:38 +00:00
struct NameParseDateTime64BestEffort { static constexpr auto name = " parseDateTime64BestEffort " ; } ;
struct NameParseDateTime64BestEffortOrZero { static constexpr auto name = " parseDateTime64BestEffortOrZero " ; } ;
struct NameParseDateTime64BestEffortOrNull { static constexpr auto name = " parseDateTime64BestEffortOrNull " ; } ;
2022-08-09 02:32:34 +00:00
struct NameParseDateTime64BestEffortUS { static constexpr auto name = " parseDateTime64BestEffortUS " ; } ;
struct NameParseDateTime64BestEffortUSOrZero { static constexpr auto name = " parseDateTime64BestEffortUSOrZero " ; } ;
struct NameParseDateTime64BestEffortUSOrNull { static constexpr auto name = " parseDateTime64BestEffortUSOrNull " ; } ;
2019-10-12 07:03:38 +00:00
2018-02-11 23:57:07 +00:00
using FunctionParseDateTimeBestEffort = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTimeBestEffort , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : BestEffort > ;
using FunctionParseDateTimeBestEffortOrZero = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTimeBestEffortOrZero , ConvertFromStringExceptionMode : : Zero , ConvertFromStringParsingMode : : BestEffort > ;
using FunctionParseDateTimeBestEffortOrNull = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTimeBestEffortOrNull , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : BestEffort > ;
2017-12-25 05:22:33 +00:00
2021-01-27 14:10:06 +00:00
using FunctionParseDateTimeBestEffortUS = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTimeBestEffortUS , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : BestEffortUS > ;
using FunctionParseDateTimeBestEffortUSOrZero = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTimeBestEffortUSOrZero , ConvertFromStringExceptionMode : : Zero , ConvertFromStringParsingMode : : BestEffortUS > ;
using FunctionParseDateTimeBestEffortUSOrNull = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTimeBestEffortUSOrNull , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : BestEffortUS > ;
2020-08-21 05:06:06 +00:00
using FunctionParseDateTime32BestEffort = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTime32BestEffort , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : BestEffort > ;
using FunctionParseDateTime32BestEffortOrZero = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTime32BestEffortOrZero , ConvertFromStringExceptionMode : : Zero , ConvertFromStringParsingMode : : BestEffort > ;
using FunctionParseDateTime32BestEffortOrNull = FunctionConvertFromString <
DataTypeDateTime , NameParseDateTime32BestEffortOrNull , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : BestEffort > ;
2019-10-12 07:03:38 +00:00
using FunctionParseDateTime64BestEffort = FunctionConvertFromString <
DataTypeDateTime64 , NameParseDateTime64BestEffort , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : BestEffort > ;
using FunctionParseDateTime64BestEffortOrZero = FunctionConvertFromString <
DataTypeDateTime64 , NameParseDateTime64BestEffortOrZero , ConvertFromStringExceptionMode : : Zero , ConvertFromStringParsingMode : : BestEffort > ;
using FunctionParseDateTime64BestEffortOrNull = FunctionConvertFromString <
DataTypeDateTime64 , NameParseDateTime64BestEffortOrNull , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : BestEffort > ;
2015-12-25 15:43:38 +00:00
2022-08-09 02:32:34 +00:00
using FunctionParseDateTime64BestEffortUS = FunctionConvertFromString <
DataTypeDateTime64 , NameParseDateTime64BestEffortUS , ConvertFromStringExceptionMode : : Throw , ConvertFromStringParsingMode : : BestEffortUS > ;
using FunctionParseDateTime64BestEffortUSOrZero = FunctionConvertFromString <
DataTypeDateTime64 , NameParseDateTime64BestEffortUSOrZero , ConvertFromStringExceptionMode : : Zero , ConvertFromStringParsingMode : : BestEffortUS > ;
using FunctionParseDateTime64BestEffortUSOrNull = FunctionConvertFromString <
DataTypeDateTime64 , NameParseDateTime64BestEffortUSOrNull , ConvertFromStringExceptionMode : : Null , ConvertFromStringParsingMode : : BestEffortUS > ;
2021-05-15 17:33:15 +00:00
class ExecutableFunctionCast : public IExecutableFunction
2015-12-23 08:11:11 +00:00
{
2017-12-07 12:09:55 +00:00
public :
2020-10-20 13:11:57 +00:00
using WrapperType = std : : function < ColumnPtr ( ColumnsWithTypeAndName & , const DataTypePtr & , const ColumnNullable * , size_t ) > ;
2018-02-02 08:33:36 +00:00
2020-11-18 09:35:32 +00:00
explicit ExecutableFunctionCast (
2023-11-10 04:23:50 +00:00
WrapperType & & wrapper_function_ , const char * name_ , std : : optional < CastDiagnostic > diagnostic_ )
2020-11-18 09:35:32 +00:00
: wrapper_function ( std : : move ( wrapper_function_ ) ) , name ( name_ ) , diagnostic ( std : : move ( diagnostic_ ) ) { }
2018-02-02 08:33:36 +00:00
String getName ( ) const override { return name ; }
protected :
2021-05-15 17:33:15 +00:00
ColumnPtr executeImpl ( const ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , size_t input_rows_count ) const override
2018-02-02 08:33:36 +00:00
{
/// drop second argument, pass others
2020-10-17 14:23:37 +00:00
ColumnsWithTypeAndName new_arguments { arguments . front ( ) } ;
2018-02-02 08:33:36 +00:00
if ( arguments . size ( ) > 2 )
new_arguments . insert ( std : : end ( new_arguments ) , std : : next ( std : : begin ( arguments ) , 2 ) , std : : end ( arguments ) ) ;
2020-11-18 09:35:32 +00:00
try
{
return wrapper_function ( new_arguments , result_type , nullptr , input_rows_count ) ;
}
catch ( Exception & e )
{
if ( diagnostic )
e . addMessage ( " while converting source column " + backQuoteIfNeed ( diagnostic - > column_from ) +
" to destination column " + backQuoteIfNeed ( diagnostic - > column_to ) ) ;
throw ;
}
2018-02-02 08:33:36 +00:00
}
bool useDefaultImplementationForNulls ( ) const override { return false ; }
2022-05-19 10:13:44 +00:00
/// CAST(Nothing, T) -> T
2022-05-12 15:15:31 +00:00
bool useDefaultImplementationForNothing ( ) const override { return false ; }
2018-02-02 08:33:36 +00:00
bool useDefaultImplementationForConstants ( ) const override { return true ; }
2018-09-27 15:55:22 +00:00
bool useDefaultImplementationForLowCardinalityColumns ( ) const override { return false ; }
2018-02-02 08:33:36 +00:00
ColumnNumbers getArgumentsThatAreAlwaysConstant ( ) const override { return { 1 } ; }
2017-12-07 12:09:55 +00:00
private :
2018-02-02 08:33:36 +00:00
WrapperType wrapper_function ;
const char * name ;
2023-11-10 04:23:50 +00:00
std : : optional < CastDiagnostic > diagnostic ;
2018-02-02 08:33:36 +00:00
} ;
2021-08-07 08:11:40 +00:00
struct CastName { static constexpr auto name = " CAST " ; } ;
struct CastInternalName { static constexpr auto name = " _CAST " ; } ;
2018-07-11 19:51:18 +00:00
2021-08-07 08:11:40 +00:00
class FunctionCastBase : public IFunctionBase
2018-02-02 08:33:36 +00:00
{
public :
2018-02-06 19:34:53 +00:00
using MonotonicityForRange = std : : function < Monotonicity ( const IDataType & , const Field & , const Field & ) > ;
2021-08-07 08:11:40 +00:00
} ;
2018-02-02 08:33:36 +00:00
2021-08-07 08:11:40 +00:00
template < typename FunctionName >
class FunctionCast final : public FunctionCastBase
{
public :
using WrapperType = std : : function < ColumnPtr ( ColumnsWithTypeAndName & , const DataTypePtr & , const ColumnNullable * , size_t ) > ;
2022-11-25 15:57:49 +00:00
FunctionCast ( ContextPtr context_
, const char * cast_name_
2021-08-07 08:11:40 +00:00
, MonotonicityForRange & & monotonicity_for_range_
, const DataTypes & argument_types_
, const DataTypePtr & return_type_
2023-11-10 04:23:50 +00:00
, std : : optional < CastDiagnostic > diagnostic_
2022-11-25 15:57:49 +00:00
, CastType cast_type_ )
2021-08-07 08:11:40 +00:00
: cast_name ( cast_name_ ) , monotonicity_for_range ( std : : move ( monotonicity_for_range_ ) )
2020-11-18 09:35:32 +00:00
, argument_types ( argument_types_ ) , return_type ( return_type_ ) , diagnostic ( std : : move ( diagnostic_ ) )
2020-12-05 13:18:56 +00:00
, cast_type ( cast_type_ )
2022-11-25 15:57:49 +00:00
, context ( context_ )
2018-02-02 08:33:36 +00:00
{
}
const DataTypes & getArgumentTypes ( ) const override { return argument_types ; }
2020-10-17 14:23:37 +00:00
const DataTypePtr & getResultType ( ) const override { return return_type ; }
2018-02-02 08:33:36 +00:00
2021-05-15 17:33:15 +00:00
ExecutableFunctionPtr prepare ( const ColumnsWithTypeAndName & /*sample_columns*/ ) const override
2018-02-02 08:33:36 +00:00
{
2020-11-18 09:45:46 +00:00
try
{
return std : : make_unique < ExecutableFunctionCast > (
2021-08-07 08:11:40 +00:00
prepareUnpackDictionaries ( getArgumentTypes ( ) [ 0 ] , getResultType ( ) ) , cast_name , diagnostic ) ;
2020-11-18 09:45:46 +00:00
}
catch ( Exception & e )
{
if ( diagnostic )
e . addMessage ( " while converting source column " + backQuoteIfNeed ( diagnostic - > column_from ) +
" to destination column " + backQuoteIfNeed ( diagnostic - > column_to ) ) ;
throw ;
}
2018-02-02 08:33:36 +00:00
}
2021-08-07 08:11:40 +00:00
String getName ( ) const override { return cast_name ; }
2018-02-02 08:33:36 +00:00
2021-06-28 10:23:07 +00:00
bool isSuitableForShortCircuitArgumentsExecution ( const DataTypesWithConstInfo & /*arguments*/ ) const override { return true ; }
2019-10-10 14:38:08 +00:00
2018-02-02 08:33:36 +00:00
bool hasInformationAboutMonotonicity ( ) const override
{
return static_cast < bool > ( monotonicity_for_range ) ;
}
Monotonicity getMonotonicityForRange ( const IDataType & type , const Field & left , const Field & right ) const override
{
return monotonicity_for_range ( type , left , right ) ;
}
private :
2021-08-07 08:11:40 +00:00
const char * cast_name ;
2018-02-02 08:33:36 +00:00
MonotonicityForRange monotonicity_for_range ;
DataTypes argument_types ;
DataTypePtr return_type ;
2015-12-23 08:11:11 +00:00
2023-11-10 04:23:50 +00:00
std : : optional < CastDiagnostic > diagnostic ;
2020-12-05 13:18:56 +00:00
CastType cast_type ;
2022-11-25 15:57:49 +00:00
ContextPtr context ;
2020-11-18 09:35:32 +00:00
2021-04-10 23:33:54 +00:00
static WrapperType createFunctionAdaptor ( FunctionPtr function , const DataTypePtr & from_type )
2015-12-23 08:11:11 +00:00
{
2021-05-15 17:33:15 +00:00
auto function_adaptor = std : : make_unique < FunctionToOverloadResolverAdaptor > ( function ) - > build ( { ColumnWithTypeAndName { nullptr , from_type , " " } } ) ;
2017-04-01 07:20:54 +00:00
2020-11-10 13:18:58 +00:00
return [ function_adaptor ]
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count )
2018-07-11 19:51:18 +00:00
{
2020-10-17 14:23:37 +00:00
return function_adaptor - > execute ( arguments , result_type , input_rows_count ) ;
2018-07-11 19:51:18 +00:00
} ;
}
2020-12-05 13:18:56 +00:00
static WrapperType createToNullableColumnWrapper ( )
2018-07-11 19:51:18 +00:00
{
2020-11-05 19:09:17 +00:00
return [ ] ( ColumnsWithTypeAndName & , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count )
2017-11-05 05:32:22 +00:00
{
2020-11-05 19:09:17 +00:00
ColumnPtr res = result_type - > createColumn ( ) ;
ColumnUInt8 : : Ptr col_null_map_to = ColumnUInt8 : : create ( input_rows_count , true ) ;
return ColumnNullable : : create ( res - > cloneResized ( input_rows_count ) , std : : move ( col_null_map_to ) ) ;
2015-12-23 08:11:11 +00:00
} ;
}
2017-04-01 07:20:54 +00:00
2020-11-05 19:09:17 +00:00
template < typename ToDataType >
2020-11-12 11:27:02 +00:00
WrapperType createWrapper ( const DataTypePtr & from_type , const ToDataType * const to_type , bool requested_result_is_nullable ) const
2015-12-23 08:11:11 +00:00
{
2020-11-05 19:09:17 +00:00
TypeIndex from_type_index = from_type - > getTypeId ( ) ;
WhichDataType which ( from_type_index ) ;
2020-12-05 13:18:56 +00:00
bool can_apply_accurate_cast = ( cast_type = = CastType : : accurate | | cast_type = = CastType : : accurateOrNull )
& & ( which . isInt ( ) | | which . isUInt ( ) | | which . isFloat ( ) ) ;
2017-04-01 07:20:54 +00:00
2023-10-23 23:49:40 +00:00
FormatSettings : : DateTimeOverflowBehavior date_time_overflow_behavior = default_date_time_overflow_behavior ;
2023-10-19 09:11:45 +00:00
if ( context )
2023-10-23 23:49:40 +00:00
date_time_overflow_behavior = context - > getSettingsRef ( ) . date_time_overflow_behavior ;
2023-10-19 09:11:45 +00:00
2020-11-05 19:09:17 +00:00
if ( requested_result_is_nullable & & checkAndGetDataType < DataTypeString > ( from_type . get ( ) ) )
{
/// In case when converting to Nullable type, we apply different parsing rule,
/// that will not throw an exception but return NULL in case of malformed input.
2021-08-07 08:11:40 +00:00
FunctionPtr function = FunctionConvertFromString < ToDataType , FunctionName , ConvertFromStringExceptionMode : : Null > : : create ( ) ;
2020-11-05 19:09:17 +00:00
return createFunctionAdaptor ( function , from_type ) ;
}
else if ( ! can_apply_accurate_cast )
2015-12-23 08:11:11 +00:00
{
2022-11-25 15:57:49 +00:00
FunctionPtr function = FunctionTo < ToDataType > : : Type : : create ( context ) ;
2020-11-05 19:09:17 +00:00
return createFunctionAdaptor ( function , from_type ) ;
}
2017-04-01 07:20:54 +00:00
2020-12-05 13:18:56 +00:00
auto wrapper_cast_type = cast_type ;
2020-11-12 11:27:02 +00:00
2023-10-23 23:49:40 +00:00
return [ wrapper_cast_type , from_type_index , to_type , date_time_overflow_behavior ]
2020-11-15 19:24:15 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * column_nullable , size_t input_rows_count )
2015-12-23 08:11:11 +00:00
{
2020-11-05 19:09:17 +00:00
ColumnPtr result_column ;
2020-11-12 15:56:17 +00:00
auto res = callOnIndexAndDataType < ToDataType > ( from_type_index , [ & ] ( const auto & types ) - > bool {
2020-11-05 19:09:17 +00:00
using Types = std : : decay_t < decltype ( types ) > ;
using LeftDataType = typename Types : : LeftType ;
using RightDataType = typename Types : : RightType ;
2023-06-09 12:06:43 +00:00
if constexpr ( IsDataTypeNumber < LeftDataType > )
2020-11-05 19:09:17 +00:00
{
2023-06-09 12:06:43 +00:00
if constexpr ( IsDataTypeNumber < RightDataType > )
2020-12-05 13:18:56 +00:00
{
2023-10-19 09:11:45 +00:00
# define GENERATE_OVERFLOW_MODE_CASE(OVERFLOW_MODE, ADDITIONS) \
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : OVERFLOW_MODE : \
result_column = ConvertImpl < LeftDataType , RightDataType , FunctionName , ConvertDefaultBehaviorTag , FormatSettings : : DateTimeOverflowBehavior : : OVERFLOW_MODE > : : execute ( \
2023-10-19 09:11:45 +00:00
arguments , result_type , input_rows_count , ADDITIONS ( ) ) ; \
break ;
2023-06-09 12:06:43 +00:00
if ( wrapper_cast_type = = CastType : : accurate )
{
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 13:01:45 +00:00
GENERATE_OVERFLOW_MODE_CASE ( Throw , AccurateConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Ignore , AccurateConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Saturate , AccurateConvertStrategyAdditions )
2023-10-19 09:11:45 +00:00
}
2023-06-09 12:06:43 +00:00
}
else
{
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 13:01:45 +00:00
GENERATE_OVERFLOW_MODE_CASE ( Throw , AccurateOrNullConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Ignore , AccurateOrNullConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Saturate , AccurateOrNullConvertStrategyAdditions )
2023-10-19 09:11:45 +00:00
}
2023-06-09 12:06:43 +00:00
}
2023-10-19 09:11:45 +00:00
# undef GENERATE_OVERFLOW_MODE_CASE
2023-06-09 12:06:43 +00:00
return true ;
2020-12-05 13:18:56 +00:00
}
2023-06-09 12:06:43 +00:00
if constexpr ( std : : is_same_v < RightDataType , DataTypeDate > | | std : : is_same_v < RightDataType , DataTypeDateTime > )
2020-12-05 13:18:56 +00:00
{
2023-10-19 09:11:45 +00:00
# define GENERATE_OVERFLOW_MODE_CASE(OVERFLOW_MODE, ADDITIONS) \
2023-10-23 23:49:40 +00:00
case FormatSettings : : DateTimeOverflowBehavior : : OVERFLOW_MODE : \
result_column = ConvertImpl < LeftDataType , RightDataType , FunctionName , ConvertDefaultBehaviorTag , FormatSettings : : DateTimeOverflowBehavior : : OVERFLOW_MODE > : : template execute < ADDITIONS > ( \
2023-10-19 09:11:45 +00:00
arguments , result_type , input_rows_count ) ; \
break ;
2023-06-09 12:06:43 +00:00
if ( wrapper_cast_type = = CastType : : accurate )
{
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 13:01:45 +00:00
GENERATE_OVERFLOW_MODE_CASE ( Throw , DateTimeAccurateConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Ignore , DateTimeAccurateConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Saturate , DateTimeAccurateConvertStrategyAdditions )
2023-10-19 09:11:45 +00:00
}
2023-06-09 12:06:43 +00:00
}
else
{
2023-10-23 23:49:40 +00:00
switch ( date_time_overflow_behavior )
2023-10-19 09:11:45 +00:00
{
2023-10-23 13:01:45 +00:00
GENERATE_OVERFLOW_MODE_CASE ( Throw , DateTimeAccurateOrNullConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Ignore , DateTimeAccurateOrNullConvertStrategyAdditions )
GENERATE_OVERFLOW_MODE_CASE ( Saturate , DateTimeAccurateOrNullConvertStrategyAdditions )
2023-10-19 09:11:45 +00:00
}
2023-06-09 12:06:43 +00:00
}
2023-10-19 09:11:45 +00:00
# undef GENERATE_OVERFLOW_MODE_CASE
2023-06-09 12:06:43 +00:00
return true ;
2020-12-05 13:18:56 +00:00
}
2020-11-05 19:09:17 +00:00
}
return false ;
} ) ;
/// Additionally check if callOnIndexAndDataType wasn't called at all.
if ( ! res )
{
2020-12-05 13:18:56 +00:00
if ( wrapper_cast_type = = CastType : : accurateOrNull )
{
2021-08-07 08:11:40 +00:00
auto nullable_column_wrapper = FunctionCast < FunctionName > : : createToNullableColumnWrapper ( ) ;
2020-11-12 11:27:02 +00:00
return nullable_column_wrapper ( arguments , result_type , column_nullable , input_rows_count ) ;
2020-12-05 13:18:56 +00:00
}
2020-11-12 11:27:02 +00:00
else
{
2021-09-06 15:59:46 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE ,
" Conversion from {} to {} is not supported " ,
from_type_index , to_type - > getName ( ) ) ;
2020-11-12 11:27:02 +00:00
}
2020-11-05 19:09:17 +00:00
}
return result_column ;
2015-12-23 08:11:11 +00:00
} ;
}
2017-04-01 07:20:54 +00:00
2021-12-20 20:24:16 +00:00
template < typename ToDataType >
WrapperType createBoolWrapper ( const DataTypePtr & from_type , const ToDataType * const to_type , bool requested_result_is_nullable ) const
{
if ( checkAndGetDataType < DataTypeString > ( from_type . get ( ) ) )
{
2021-12-23 17:14:54 +00:00
return & ConvertImplGenericFromString < ColumnString > : : execute ;
2021-12-20 20:24:16 +00:00
}
return createWrapper < ToDataType > ( from_type , to_type , requested_result_is_nullable ) ;
}
2022-03-23 18:59:26 +00:00
WrapperType createUInt8ToBoolWrapper ( const DataTypePtr from_type , const DataTypePtr to_type ) const
2021-12-20 23:59:08 +00:00
{
return [ from_type , to_type ] ( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * , size_t /*input_rows_count*/ ) - > ColumnPtr
{
/// Special case when we convert UInt8 column to Bool column.
/// both columns have type UInt8, but we shouldn't use identity wrapper,
/// because Bool column can contain only 0 and 1.
auto res_column = to_type - > createColumn ( ) ;
const auto & data_from = checkAndGetColumn < ColumnUInt8 > ( arguments [ 0 ] . column . get ( ) ) - > getData ( ) ;
auto & data_to = assert_cast < ColumnUInt8 * > ( res_column . get ( ) ) - > getData ( ) ;
data_to . resize ( data_from . size ( ) ) ;
for ( size_t i = 0 ; i ! = data_from . size ( ) ; + + i )
data_to [ i ] = static_cast < bool > ( data_from [ i ] ) ;
return res_column ;
} ;
}
2021-04-10 23:33:54 +00:00
static WrapperType createStringWrapper ( const DataTypePtr & from_type )
2018-09-07 16:38:40 +00:00
{
2020-11-05 19:09:17 +00:00
FunctionPtr function = FunctionToString : : create ( ) ;
return createFunctionAdaptor ( function , from_type ) ;
}
2018-09-07 16:38:40 +00:00
2020-11-05 19:09:17 +00:00
WrapperType createFixedStringWrapper ( const DataTypePtr & from_type , const size_t N ) const
{
if ( ! isStringOrFixedString ( from_type ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : NOT_IMPLEMENTED , " CAST AS FixedString is only implemented for types String and FixedString " ) ;
2018-09-07 16:38:40 +00:00
2020-12-05 13:18:56 +00:00
bool exception_mode_null = cast_type = = CastType : : accurateOrNull ;
2020-11-05 19:09:17 +00:00
return [ exception_mode_null , N ] ( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * , size_t /*input_rows_count*/ )
2018-09-07 16:38:40 +00:00
{
2020-11-05 19:09:17 +00:00
if ( exception_mode_null )
return FunctionToFixedString : : executeForN < ConvertToFixedStringExceptionMode : : Null > ( arguments , N ) ;
else
return FunctionToFixedString : : executeForN < ConvertToFixedStringExceptionMode : : Throw > ( arguments , N ) ;
2018-09-07 16:38:40 +00:00
} ;
}
2022-11-12 22:58:09 +00:00
# define GENERATE_INTERVAL_CASE(INTERVAL_KIND) \
2024-02-27 13:37:21 +00:00
case IntervalKind : : Kind : : INTERVAL_KIND : \
2022-11-12 22:58:09 +00:00
return createFunctionAdaptor ( FunctionConvert < DataTypeInterval , NameToInterval # # INTERVAL_KIND , PositiveMonotonicity > : : create ( ) , from_type ) ;
2022-11-12 19:47:06 +00:00
static WrapperType createIntervalWrapper ( const DataTypePtr & from_type , IntervalKind kind )
{
switch ( kind )
{
GENERATE_INTERVAL_CASE ( Nanosecond )
GENERATE_INTERVAL_CASE ( Microsecond )
GENERATE_INTERVAL_CASE ( Millisecond )
GENERATE_INTERVAL_CASE ( Second )
GENERATE_INTERVAL_CASE ( Minute )
GENERATE_INTERVAL_CASE ( Hour )
GENERATE_INTERVAL_CASE ( Day )
GENERATE_INTERVAL_CASE ( Week )
GENERATE_INTERVAL_CASE ( Month )
GENERATE_INTERVAL_CASE ( Quarter )
GENERATE_INTERVAL_CASE ( Year )
}
throw Exception { ErrorCodes : : CANNOT_CONVERT_TYPE , " Conversion to unexpected IntervalKind: {} " , kind . toString ( ) } ;
}
# undef GENERATE_INTERVAL_CASE
2019-10-24 10:44:38 +00:00
template < typename ToDataType >
2022-03-17 18:06:50 +00:00
requires IsDataTypeDecimal < ToDataType >
WrapperType createDecimalWrapper ( const DataTypePtr & from_type , const ToDataType * to_type , bool requested_result_is_nullable ) const
2018-08-31 08:59:21 +00:00
{
TypeIndex type_index = from_type - > getTypeId ( ) ;
UInt32 scale = to_type - > getScale ( ) ;
2019-08-02 14:31:15 +00:00
WhichDataType which ( type_index ) ;
2021-07-15 11:40:45 +00:00
bool ok = which . isNativeInt ( ) | | which . isNativeUInt ( ) | | which . isDecimal ( ) | | which . isFloat ( ) | | which . isDateOrDate32 ( ) | | which . isDateTime ( ) | | which . isDateTime64 ( )
2020-11-12 15:56:17 +00:00
| | which . isStringOrFixedString ( ) ;
2019-08-02 14:31:15 +00:00
if ( ! ok )
2020-11-10 13:18:58 +00:00
{
2020-12-05 13:18:56 +00:00
if ( cast_type = = CastType : : accurateOrNull )
2020-11-05 19:09:17 +00:00
return createToNullableColumnWrapper ( ) ;
2020-11-10 13:18:58 +00:00
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Conversion from {} to {} is not supported " ,
from_type - > getName ( ) , to_type - > getName ( ) ) ;
2020-11-05 19:09:17 +00:00
}
2019-08-02 14:31:15 +00:00
2020-12-05 13:18:56 +00:00
auto wrapper_cast_type = cast_type ;
2019-08-02 14:31:15 +00:00
2021-01-04 14:52:07 +00:00
return [ wrapper_cast_type , type_index , scale , to_type , requested_result_is_nullable ]
2020-11-12 11:27:02 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * column_nullable , size_t input_rows_count )
2018-08-31 08:59:21 +00:00
{
2020-10-17 14:23:37 +00:00
ColumnPtr result_column ;
2021-01-04 14:52:07 +00:00
auto res = callOnIndexAndDataType < ToDataType > ( type_index , [ & ] ( const auto & types ) - > bool
2021-01-04 12:40:33 +00:00
{
2018-08-31 08:59:21 +00:00
using Types = std : : decay_t < decltype ( types ) > ;
using LeftDataType = typename Types : : LeftType ;
using RightDataType = typename Types : : RightType ;
Fix DateTime64 initialization (to match DateTime behaviour)
There was no specializations for toDateTime64(<numeric>), and because of
this default decimal conversion was used, however this is not enough for
DateTime/DateTime64 types, since the date may overflow and the proper
check is required (like DateTime has), and this what UBsan found [1]:
../src/IO/WriteHelpers.h:812:33: runtime error: index 508 out of bounds for type 'const char [201]' Received signal -3 Received signal Unknown signal (-3)
Backtrace:
(gdb) bt
0 LocalDateTime::LocalDateTime (this=0x7fffffff8418, year_=1970, month_=1 '\001', day_=1 '\001', hour_=2 '\002', minute_=0 '\000', second_=254 '\376') at LocalDateTime.h:83
1 0x00000000138a5edb in DB::writeDateTimeText<(char)45, (char)58, (char)32, (char)46> (datetime64=..., scale=7, buf=..., date_lut=...) at WriteHelpers.h:852
2 0x0000000019c379b4 in DB::DataTypeDateTime64::serializeText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at DataTypeDateTime64.cpp:66
3 0x0000000019d297e4 in DB::IDataType::serializeAsText (this=0x7ffff5c4b0d8, column=..., row_num=0, ostr=..., settings=...) at IDataType.cpp:387
[1]: https://clickhouse-test-reports.s3.yandex.net/19527/cea8ae162ffbf92e5ed29304ab010704c5d611c8/fuzzer_ubsan/report.html#fail1
Also fix CAST for DateTime64
2021-02-05 19:06:23 +00:00
if constexpr ( IsDataTypeDecimalOrNumber < LeftDataType > & & IsDataTypeDecimalOrNumber < RightDataType > & & ! std : : is_same_v < DataTypeDateTime64 , RightDataType > )
2020-11-12 11:27:02 +00:00
{
2020-12-05 13:18:56 +00:00
if ( wrapper_cast_type = = CastType : : accurate )
2020-11-12 11:27:02 +00:00
{
2020-12-05 13:18:56 +00:00
AccurateConvertStrategyAdditions additions ;
2020-11-12 11:27:02 +00:00
additions . scale = scale ;
2021-08-07 08:11:40 +00:00
result_column = ConvertImpl < LeftDataType , RightDataType , FunctionName > : : execute (
2020-12-05 13:18:56 +00:00
arguments , result_type , input_rows_count , additions ) ;
return true ;
}
else if ( wrapper_cast_type = = CastType : : accurateOrNull )
{
AccurateOrNullConvertStrategyAdditions additions ;
additions . scale = scale ;
2021-08-07 08:11:40 +00:00
result_column = ConvertImpl < LeftDataType , RightDataType , FunctionName > : : execute (
2020-12-05 13:18:56 +00:00
arguments , result_type , input_rows_count , additions ) ;
2020-11-12 11:27:02 +00:00
return true ;
}
}
2021-01-04 14:52:07 +00:00
else if constexpr ( std : : is_same_v < LeftDataType , DataTypeString > )
2020-12-05 13:18:56 +00:00
{
2021-01-04 14:52:07 +00:00
if ( requested_result_is_nullable )
{
/// Consistent with CAST(Nullable(String) AS Nullable(Numbers))
/// In case when converting to Nullable type, we apply different parsing rule,
/// that will not throw an exception but return NULL in case of malformed input.
2021-08-07 08:11:40 +00:00
result_column = ConvertImpl < LeftDataType , RightDataType , FunctionName , ConvertReturnNullOnErrorTag > : : execute (
2021-01-04 14:52:07 +00:00
arguments , result_type , input_rows_count , scale ) ;
return true ;
}
2020-12-05 13:18:56 +00:00
}
2020-11-12 11:27:02 +00:00
2021-08-07 08:11:40 +00:00
result_column = ConvertImpl < LeftDataType , RightDataType , FunctionName > : : execute ( arguments , result_type , input_rows_count , scale ) ;
2020-11-12 11:27:02 +00:00
2018-08-31 08:59:21 +00:00
return true ;
} ) ;
2019-08-05 15:23:32 +00:00
/// Additionally check if callOnIndexAndDataType wasn't called at all.
if ( ! res )
{
2020-12-05 13:18:56 +00:00
if ( wrapper_cast_type = = CastType : : accurateOrNull )
{
2021-08-07 08:11:40 +00:00
auto nullable_column_wrapper = FunctionCast < FunctionName > : : createToNullableColumnWrapper ( ) ;
2020-11-12 11:27:02 +00:00
return nullable_column_wrapper ( arguments , result_type , column_nullable , input_rows_count ) ;
2020-12-05 13:18:56 +00:00
}
2020-11-10 13:18:58 +00:00
else
2021-09-06 15:59:46 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE ,
" Conversion from {} to {} is not supported " ,
type_index , to_type - > getName ( ) ) ;
2019-08-05 15:23:32 +00:00
}
2020-10-17 14:23:37 +00:00
return result_column ;
2018-08-31 08:59:21 +00:00
} ;
}
2019-09-03 14:36:17 +00:00
2020-11-05 19:09:17 +00:00
WrapperType createAggregateFunctionWrapper ( const DataTypePtr & from_type_untyped , const DataTypeAggregateFunction * to_type ) const
2019-09-02 09:16:49 +00:00
{
/// Conversion from String through parsing.
if ( checkAndGetDataType < DataTypeString > ( from_type_untyped . get ( ) ) )
{
2021-08-18 20:34:07 +00:00
return & ConvertImplGenericFromString < ColumnString > : : execute ;
2019-09-02 09:16:49 +00:00
}
2023-09-09 18:38:12 +00:00
else if ( const auto * agg_type = checkAndGetDataType < DataTypeAggregateFunction > ( from_type_untyped . get ( ) ) )
2020-11-05 19:09:17 +00:00
{
2023-09-09 18:38:12 +00:00
if ( agg_type - > getFunction ( ) - > haveSameStateRepresentation ( * to_type - > getFunction ( ) ) )
{
2023-09-11 19:54:19 +00:00
return [ function = to_type - > getFunction ( ) ] (
2023-09-09 18:38:12 +00:00
ColumnsWithTypeAndName & arguments ,
const DataTypePtr & /* result_type */ ,
const ColumnNullable * /* nullable_source */ ,
size_t /*input_rows_count*/ ) - > ColumnPtr
{
const auto & argument_column = arguments . front ( ) ;
const auto * col_agg = checkAndGetColumn < ColumnAggregateFunction > ( argument_column . column . get ( ) ) ;
if ( col_agg )
{
auto new_col_agg = ColumnAggregateFunction : : create ( * col_agg ) ;
2023-09-11 18:49:27 +00:00
new_col_agg - > set ( function ) ;
2023-09-09 18:38:12 +00:00
return new_col_agg ;
}
else
{
throw Exception (
ErrorCodes : : LOGICAL_ERROR ,
" Illegal column {} for function CAST AS AggregateFunction " ,
argument_column . column - > getName ( ) ) ;
}
} ;
}
2020-11-05 19:09:17 +00:00
}
2023-09-09 18:38:12 +00:00
if ( cast_type = = CastType : : accurateOrNull )
return createToNullableColumnWrapper ( ) ;
else
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Conversion from {} to {} is not supported " ,
from_type_untyped - > getName ( ) , to_type - > getName ( ) ) ;
2019-09-02 09:16:49 +00:00
}
2018-08-31 08:59:21 +00:00
2021-04-21 21:19:01 +00:00
WrapperType createArrayWrapper ( const DataTypePtr & from_type_untyped , const DataTypeArray & to_type ) const
2015-12-23 08:11:11 +00:00
{
2016-08-02 03:30:34 +00:00
/// Conversion from String through parsing.
2017-07-21 06:35:58 +00:00
if ( checkAndGetDataType < DataTypeString > ( from_type_untyped . get ( ) ) )
2016-08-02 03:30:34 +00:00
{
2021-08-18 20:34:07 +00:00
return & ConvertImplGenericFromString < ColumnString > : : execute ;
2016-08-02 03:30:34 +00:00
}
2017-04-01 07:20:54 +00:00
2023-03-30 15:54:25 +00:00
DataTypePtr from_type_holder ;
2020-10-17 14:23:37 +00:00
const auto * from_type = checkAndGetDataType < DataTypeArray > ( from_type_untyped . get ( ) ) ;
2022-01-23 00:09:29 +00:00
const auto * from_type_map = checkAndGetDataType < DataTypeMap > ( from_type_untyped . get ( ) ) ;
2022-01-21 15:14:31 +00:00
2022-01-23 00:09:29 +00:00
/// Convert from Map
if ( from_type_map )
2023-03-30 15:54:25 +00:00
{
/// Recreate array of unnamed tuples because otherwise it may work
/// unexpectedly while converting to array of named tuples.
2023-03-31 02:20:17 +00:00
from_type_holder = from_type_map - > getNestedTypeWithUnnamedTuple ( ) ;
2023-03-30 15:54:25 +00:00
from_type = assert_cast < const DataTypeArray * > ( from_type_holder . get ( ) ) ;
}
2022-01-21 15:14:31 +00:00
2021-04-21 21:19:01 +00:00
if ( ! from_type )
2015-12-23 08:11:11 +00:00
{
2021-04-21 21:19:01 +00:00
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
2022-01-21 15:14:31 +00:00
" CAST AS Array can only be performed between same-dimensional Array, Map or String types " ) ;
2021-04-21 21:19:01 +00:00
}
2017-04-01 07:20:54 +00:00
2021-04-21 21:19:01 +00:00
DataTypePtr from_nested_type = from_type - > getNestedType ( ) ;
2017-04-01 07:20:54 +00:00
2021-04-21 21:19:01 +00:00
/// In query SELECT CAST([] AS Array(Array(String))) from type is Array(Nothing)
bool from_empty_array = isNothing ( from_nested_type ) ;
if ( from_type - > getNumberOfDimensions ( ) ! = to_type . getNumberOfDimensions ( ) & & ! from_empty_array )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
2021-06-07 11:52:54 +00:00
" CAST AS Array can only be performed between same-dimensional array types " ) ;
2021-04-21 21:19:01 +00:00
const DataTypePtr & to_nested_type = to_type . getNestedType ( ) ;
2017-04-01 07:20:54 +00:00
2015-12-23 09:52:21 +00:00
/// Prepare nested type conversion
2018-06-07 10:37:06 +00:00
const auto nested_function = prepareUnpackDictionaries ( from_nested_type , to_nested_type ) ;
2017-04-01 07:20:54 +00:00
2017-12-08 03:52:33 +00:00
return [ nested_function , from_nested_type , to_nested_type ] (
2020-10-20 13:11:57 +00:00
ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t /*input_rows_count*/ ) - > ColumnPtr
2015-12-23 08:11:11 +00:00
{
2022-01-21 15:14:31 +00:00
const auto & argument_column = arguments . front ( ) ;
2017-04-01 07:20:54 +00:00
2022-01-21 15:14:31 +00:00
const ColumnArray * col_array = nullptr ;
if ( const ColumnMap * col_map = checkAndGetColumn < ColumnMap > ( argument_column . column . get ( ) ) )
col_array = & col_map - > getNestedColumn ( ) ;
else
col_array = checkAndGetColumn < ColumnArray > ( argument_column . column . get ( ) ) ;
if ( col_array )
2015-12-23 08:11:11 +00:00
{
2020-10-14 14:04:50 +00:00
/// create columns for converting nested column containing original and result columns
2020-10-17 14:23:37 +00:00
ColumnsWithTypeAndName nested_columns { { col_array - > getDataPtr ( ) , from_nested_type , " " } } ;
2017-04-01 07:20:54 +00:00
2015-12-23 08:11:11 +00:00
/// convert nested column
2020-10-20 13:11:57 +00:00
auto result_column = nested_function ( nested_columns , to_nested_type , nullable_source , nested_columns . front ( ) . column - > size ( ) ) ;
2017-04-01 07:20:54 +00:00
2015-12-23 08:11:11 +00:00
/// set converted nested column to result
2020-10-17 14:23:37 +00:00
return ColumnArray : : create ( result_column , col_array - > getOffsetsPtr ( ) ) ;
2015-12-23 08:11:11 +00:00
}
else
2022-01-21 15:14:31 +00:00
{
throw Exception ( ErrorCodes : : LOGICAL_ERROR ,
" Illegal column {} for function CAST AS Array " ,
argument_column . column - > getName ( ) ) ;
}
2015-12-23 08:11:11 +00:00
} ;
}
2017-04-01 07:20:54 +00:00
2020-12-14 22:16:04 +00:00
using ElementWrappers = std : : vector < WrapperType > ;
ElementWrappers getElementWrappers ( const DataTypes & from_element_types , const DataTypes & to_element_types ) const
{
ElementWrappers element_wrappers ;
element_wrappers . reserve ( from_element_types . size ( ) ) ;
/// Create conversion wrapper for each element in tuple
2021-06-15 19:55:21 +00:00
for ( size_t i = 0 ; i < from_element_types . size ( ) ; + + i )
{
const DataTypePtr & from_element_type = from_element_types [ i ] ;
const DataTypePtr & to_element_type = to_element_types [ i ] ;
element_wrappers . push_back ( prepareUnpackDictionaries ( from_element_type , to_element_type ) ) ;
}
2020-12-14 22:16:04 +00:00
return element_wrappers ;
}
2018-02-02 08:33:36 +00:00
WrapperType createTupleWrapper ( const DataTypePtr & from_type_untyped , const DataTypeTuple * to_type ) const
2015-12-23 09:52:21 +00:00
{
2016-08-02 03:30:34 +00:00
/// Conversion from String through parsing.
2017-07-21 06:35:58 +00:00
if ( checkAndGetDataType < DataTypeString > ( from_type_untyped . get ( ) ) )
2016-08-02 03:30:34 +00:00
{
2021-08-18 20:34:07 +00:00
return & ConvertImplGenericFromString < ColumnString > : : execute ;
2016-08-02 03:30:34 +00:00
}
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
const auto * from_type = checkAndGetDataType < DataTypeTuple > ( from_type_untyped . get ( ) ) ;
2015-12-23 09:52:21 +00:00
if ( ! from_type )
2023-01-23 13:16:14 +00:00
throw Exception ( ErrorCodes : : TYPE_MISMATCH , " CAST AS Tuple can only be performed between tuple types or from String. \n "
" Left type: {}, right type: {} " , from_type_untyped - > getName ( ) , to_type - > getName ( ) ) ;
2017-04-01 07:20:54 +00:00
2015-12-23 09:52:21 +00:00
const auto & from_element_types = from_type - > getElements ( ) ;
const auto & to_element_types = to_type - > getElements ( ) ;
2017-04-01 07:20:54 +00:00
2021-05-03 00:56:19 +00:00
std : : vector < WrapperType > element_wrappers ;
std : : vector < std : : optional < size_t > > to_reverse_index ;
2022-03-01 16:32:55 +00:00
/// For named tuples allow conversions for tuples with
/// different sets of elements. If element exists in @to_type
/// and doesn't exist in @to_type it will be filled by default values.
2022-03-28 18:44:53 +00:00
if ( from_type - > haveExplicitNames ( ) & & to_type - > haveExplicitNames ( ) )
2021-05-03 00:56:19 +00:00
{
const auto & from_names = from_type - > getElementNames ( ) ;
std : : unordered_map < String , size_t > from_positions ;
from_positions . reserve ( from_names . size ( ) ) ;
for ( size_t i = 0 ; i < from_names . size ( ) ; + + i )
from_positions [ from_names [ i ] ] = i ;
const auto & to_names = to_type - > getElementNames ( ) ;
element_wrappers . reserve ( to_names . size ( ) ) ;
to_reverse_index . reserve ( from_names . size ( ) ) ;
for ( size_t i = 0 ; i < to_names . size ( ) ; + + i )
{
auto it = from_positions . find ( to_names [ i ] ) ;
if ( it ! = from_positions . end ( ) )
{
element_wrappers . emplace_back ( prepareUnpackDictionaries ( from_element_types [ it - > second ] , to_element_types [ i ] ) ) ;
to_reverse_index . emplace_back ( it - > second ) ;
}
else
{
element_wrappers . emplace_back ( ) ;
to_reverse_index . emplace_back ( ) ;
}
}
}
else
{
if ( from_element_types . size ( ) ! = to_element_types . size ( ) )
2023-01-23 13:16:14 +00:00
throw Exception ( ErrorCodes : : TYPE_MISMATCH , " CAST AS Tuple can only be performed between tuple types "
" with the same number of elements or from String. \n Left type: {}, right type: {} " ,
from_type - > getName ( ) , to_type - > getName ( ) ) ;
2021-05-03 00:56:19 +00:00
element_wrappers = getElementWrappers ( from_element_types , to_element_types ) ;
to_reverse_index . reserve ( to_element_types . size ( ) ) ;
for ( size_t i = 0 ; i < to_element_types . size ( ) ; + + i )
to_reverse_index . emplace_back ( i ) ;
}
return [ element_wrappers , from_element_types , to_element_types , to_reverse_index ]
2020-10-20 13:11:57 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t input_rows_count ) - > ColumnPtr
2015-12-23 09:52:21 +00:00
{
2020-10-17 14:23:37 +00:00
const auto * col = arguments . front ( ) . column . get ( ) ;
2017-04-01 07:20:54 +00:00
2021-05-03 00:56:19 +00:00
size_t tuple_size = to_element_types . size ( ) ;
2017-12-08 00:50:25 +00:00
const ColumnTuple & column_tuple = typeid_cast < const ColumnTuple & > ( * col ) ;
2020-10-17 14:23:37 +00:00
Columns converted_columns ( tuple_size ) ;
2020-10-09 07:41:28 +00:00
2015-12-23 09:52:21 +00:00
/// invoke conversion for each element
2017-12-08 00:50:25 +00:00
for ( size_t i = 0 ; i < tuple_size ; + + i )
2020-10-17 14:23:37 +00:00
{
2021-05-03 00:56:19 +00:00
if ( to_reverse_index [ i ] )
{
size_t from_idx = * to_reverse_index [ i ] ;
ColumnsWithTypeAndName element = { { column_tuple . getColumns ( ) [ from_idx ] , from_element_types [ from_idx ] , " " } } ;
converted_columns [ i ] = element_wrappers [ i ] ( element , to_element_types [ i ] , nullable_source , input_rows_count ) ;
}
else
{
converted_columns [ i ] = to_element_types [ i ] - > createColumn ( ) - > cloneResized ( input_rows_count ) ;
}
2020-10-17 14:23:37 +00:00
}
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
return ColumnTuple : : create ( converted_columns ) ;
2015-12-23 09:52:21 +00:00
} ;
}
2017-04-01 07:20:54 +00:00
2020-12-15 22:22:21 +00:00
/// The case of: tuple([key1, key2, ..., key_n], [value1, value2, ..., value_n])
2020-12-14 22:16:04 +00:00
WrapperType createTupleToMapWrapper ( const DataTypes & from_kv_types , const DataTypes & to_kv_types ) const
{
return [ element_wrappers = getElementWrappers ( from_kv_types , to_kv_types ) , from_kv_types , to_kv_types ]
2021-02-23 10:42:33 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t /*input_rows_count*/ ) - > ColumnPtr
2020-12-14 22:16:04 +00:00
{
const auto * col = arguments . front ( ) . column . get ( ) ;
const auto & column_tuple = assert_cast < const ColumnTuple & > ( * col ) ;
2020-12-25 17:12:47 +00:00
Columns offsets ( 2 ) ;
2020-12-14 22:16:04 +00:00
Columns converted_columns ( 2 ) ;
for ( size_t i = 0 ; i < 2 ; + + i )
{
const auto & column_array = assert_cast < const ColumnArray & > ( column_tuple . getColumn ( i ) ) ;
ColumnsWithTypeAndName element = { { column_array . getDataPtr ( ) , from_kv_types [ i ] , " " } } ;
2021-02-23 10:42:33 +00:00
converted_columns [ i ] = element_wrappers [ i ] ( element , to_kv_types [ i ] , nullable_source , ( element [ 0 ] . column ) - > size ( ) ) ;
2020-12-25 17:12:47 +00:00
offsets [ i ] = column_array . getOffsetsPtr ( ) ;
2020-12-14 22:16:04 +00:00
}
2020-12-25 17:12:47 +00:00
const auto & keys_offsets = assert_cast < const ColumnArray : : ColumnOffsets & > ( * offsets [ 0 ] ) . getData ( ) ;
const auto & values_offsets = assert_cast < const ColumnArray : : ColumnOffsets & > ( * offsets [ 1 ] ) . getData ( ) ;
if ( keys_offsets ! = values_offsets )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
" CAST AS Map can only be performed from tuple of arrays with equal sizes. " ) ;
return ColumnMap : : create ( converted_columns [ 0 ] , converted_columns [ 1 ] , offsets [ 0 ] ) ;
2020-12-14 22:16:04 +00:00
} ;
}
2023-04-26 18:45:11 +00:00
WrapperType createMapToMapWrapper ( const DataTypes & from_kv_types , const DataTypes & to_kv_types ) const
2020-12-14 22:16:04 +00:00
{
return [ element_wrappers = getElementWrappers ( from_kv_types , to_kv_types ) , from_kv_types , to_kv_types ]
2021-04-09 17:50:10 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t /*input_rows_count*/ ) - > ColumnPtr
2020-12-14 22:16:04 +00:00
{
const auto * col = arguments . front ( ) . column . get ( ) ;
const auto & column_map = typeid_cast < const ColumnMap & > ( * col ) ;
const auto & nested_data = column_map . getNestedData ( ) ;
Columns converted_columns ( 2 ) ;
for ( size_t i = 0 ; i < 2 ; + + i )
{
ColumnsWithTypeAndName element = { { nested_data . getColumnPtr ( i ) , from_kv_types [ i ] , " " } } ;
2021-04-09 17:50:10 +00:00
converted_columns [ i ] = element_wrappers [ i ] ( element , to_kv_types [ i ] , nullable_source , ( element [ 0 ] . column ) - > size ( ) ) ;
2020-12-14 22:16:04 +00:00
}
return ColumnMap : : create ( converted_columns [ 0 ] , converted_columns [ 1 ] , column_map . getNestedColumn ( ) . getOffsetsPtr ( ) ) ;
} ;
}
2020-12-15 22:22:21 +00:00
/// The case of: [(key1, value1), (key2, value2), ...]
2023-04-26 18:45:11 +00:00
WrapperType createArrayToMapWrapper ( const DataTypes & from_kv_types , const DataTypes & to_kv_types ) const
2020-12-14 22:16:04 +00:00
{
return [ element_wrappers = getElementWrappers ( from_kv_types , to_kv_types ) , from_kv_types , to_kv_types ]
2021-04-09 17:50:10 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t /*input_rows_count*/ ) - > ColumnPtr
2020-12-14 22:16:04 +00:00
{
const auto * col = arguments . front ( ) . column . get ( ) ;
const auto & column_array = typeid_cast < const ColumnArray & > ( * col ) ;
const auto & nested_data = typeid_cast < const ColumnTuple & > ( column_array . getData ( ) ) ;
Columns converted_columns ( 2 ) ;
for ( size_t i = 0 ; i < 2 ; + + i )
{
ColumnsWithTypeAndName element = { { nested_data . getColumnPtr ( i ) , from_kv_types [ i ] , " " } } ;
2021-04-09 17:50:10 +00:00
converted_columns [ i ] = element_wrappers [ i ] ( element , to_kv_types [ i ] , nullable_source , ( element [ 0 ] . column ) - > size ( ) ) ;
2020-12-14 22:16:04 +00:00
}
return ColumnMap : : create ( converted_columns [ 0 ] , converted_columns [ 1 ] , column_array . getOffsetsPtr ( ) ) ;
} ;
}
2020-10-10 06:49:03 +00:00
WrapperType createMapWrapper ( const DataTypePtr & from_type_untyped , const DataTypeMap * to_type ) const
{
2020-12-03 03:52:41 +00:00
if ( const auto * from_tuple = checkAndGetDataType < DataTypeTuple > ( from_type_untyped . get ( ) ) )
2020-10-26 18:20:45 +00:00
{
2020-12-03 03:52:41 +00:00
if ( from_tuple - > getElements ( ) . size ( ) ! = 2 )
2023-04-26 18:45:11 +00:00
throw Exception (
ErrorCodes : : TYPE_MISMATCH ,
" CAST AS Map from tuple requires 2 elements. "
" Left type: {}, right type: {} " ,
from_tuple - > getName ( ) ,
to_type - > getName ( ) ) ;
2020-10-26 18:20:45 +00:00
2020-12-03 03:52:41 +00:00
DataTypes from_kv_types ;
const auto & to_kv_types = to_type - > getKeyValueTypes ( ) ;
for ( const auto & elem : from_tuple - > getElements ( ) )
{
const auto * type_array = checkAndGetDataType < DataTypeArray > ( elem . get ( ) ) ;
if ( ! type_array )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
" CAST AS Map can only be performed from tuples of array. Got: {} " , from_tuple - > getName ( ) ) ;
from_kv_types . push_back ( type_array - > getNestedType ( ) ) ;
}
2020-12-14 22:16:04 +00:00
return createTupleToMapWrapper ( from_kv_types , to_kv_types ) ;
}
else if ( const auto * from_array = typeid_cast < const DataTypeArray * > ( from_type_untyped . get ( ) ) )
{
const auto * nested_tuple = typeid_cast < const DataTypeTuple * > ( from_array - > getNestedType ( ) . get ( ) ) ;
if ( ! nested_tuple | | nested_tuple - > getElements ( ) . size ( ) ! = 2 )
2023-04-26 18:45:11 +00:00
throw Exception (
ErrorCodes : : TYPE_MISMATCH ,
" CAST AS Map from array requires nested tuple of 2 elements. "
" Left type: {}, right type: {} " ,
from_array - > getName ( ) ,
to_type - > getName ( ) ) ;
2020-12-03 03:52:41 +00:00
2023-04-26 18:45:11 +00:00
return createArrayToMapWrapper ( nested_tuple - > getElements ( ) , to_type - > getKeyValueTypes ( ) ) ;
2020-10-26 18:20:45 +00:00
}
2020-12-03 03:52:41 +00:00
else if ( const auto * from_type = checkAndGetDataType < DataTypeMap > ( from_type_untyped . get ( ) ) )
2020-10-10 06:49:03 +00:00
{
2023-04-26 18:45:11 +00:00
return createMapToMapWrapper ( from_type - > getKeyValueTypes ( ) , to_type - > getKeyValueTypes ( ) ) ;
2020-11-02 09:23:02 +00:00
}
else
{
2023-01-23 13:16:14 +00:00
throw Exception ( ErrorCodes : : TYPE_MISMATCH , " Unsupported types to CAST AS Map. "
" Left type: {}, right type: {} " , from_type_untyped - > getName ( ) , to_type - > getName ( ) ) ;
2020-11-02 09:23:02 +00:00
}
2020-10-10 06:49:03 +00:00
}
2022-03-25 21:00:00 +00:00
WrapperType createTupleToObjectWrapper ( const DataTypeTuple & from_tuple , bool has_nullable_subcolumns ) const
2021-08-10 01:33:57 +00:00
{
2022-03-25 21:00:00 +00:00
if ( ! from_tuple . haveExplicitNames ( ) )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
" Cast to Object can be performed only from flatten Named Tuple. Got: {} " , from_tuple . getName ( ) ) ;
PathsInData paths ;
DataTypes from_types ;
std : : tie ( paths , from_types ) = flattenTuple ( from_tuple . getPtr ( ) ) ;
auto to_types = from_types ;
for ( auto & type : to_types )
2021-08-10 01:33:57 +00:00
{
2022-03-25 21:00:00 +00:00
if ( isTuple ( type ) | | isNested ( type ) )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
" Cast to Object can be performed only from flatten Named Tuple. Got: {} " ,
from_tuple . getName ( ) ) ;
2021-08-10 01:33:57 +00:00
2022-03-25 21:00:00 +00:00
type = recursiveRemoveLowCardinality ( type ) ;
}
2022-01-27 00:24:34 +00:00
2022-03-25 21:00:00 +00:00
return [ element_wrappers = getElementWrappers ( from_types , to_types ) ,
has_nullable_subcolumns , from_types , to_types , paths ]
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t input_rows_count )
{
size_t tuple_size = to_types . size ( ) ;
auto flattened_column = flattenTuple ( arguments . front ( ) . column ) ;
const auto & column_tuple = assert_cast < const ColumnTuple & > ( * flattened_column ) ;
2021-08-10 01:33:57 +00:00
2022-03-25 21:00:00 +00:00
if ( tuple_size ! = column_tuple . getColumns ( ) . size ( ) )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
" Expected tuple with {} subcolumn, but got {} subcolumns " ,
tuple_size , column_tuple . getColumns ( ) . size ( ) ) ;
2021-08-10 01:33:57 +00:00
2022-03-25 21:00:00 +00:00
auto res = ColumnObject : : create ( has_nullable_subcolumns ) ;
for ( size_t i = 0 ; i < tuple_size ; + + i )
{
ColumnsWithTypeAndName element = { { column_tuple . getColumns ( ) [ i ] , from_types [ i ] , " " } } ;
auto converted_column = element_wrappers [ i ] ( element , to_types [ i ] , nullable_source , input_rows_count ) ;
res - > addSubcolumn ( paths [ i ] , converted_column - > assumeMutable ( ) ) ;
2021-08-10 01:33:57 +00:00
}
2022-03-25 21:00:00 +00:00
return res ;
} ;
}
WrapperType createMapToObjectWrapper ( const DataTypeMap & from_map , bool has_nullable_subcolumns ) const
{
auto key_value_types = from_map . getKeyValueTypes ( ) ;
if ( ! isStringOrFixedString ( key_value_types [ 0 ] ) )
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
" Cast to Object from Map can be performed only from Map "
" with String or FixedString key. Got: {} " , from_map . getName ( ) ) ;
const auto & value_type = key_value_types [ 1 ] ;
auto to_value_type = value_type ;
if ( ! has_nullable_subcolumns & & value_type - > isNullable ( ) )
to_value_type = removeNullable ( value_type ) ;
if ( has_nullable_subcolumns & & ! value_type - > isNullable ( ) )
to_value_type = makeNullable ( value_type ) ;
DataTypes to_key_value_types { std : : make_shared < DataTypeString > ( ) , std : : move ( to_value_type ) } ;
auto element_wrappers = getElementWrappers ( key_value_types , to_key_value_types ) ;
return [ has_nullable_subcolumns , element_wrappers , key_value_types , to_key_value_types ]
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * nullable_source , size_t ) - > ColumnPtr
{
const auto & column_map = assert_cast < const ColumnMap & > ( * arguments . front ( ) . column ) ;
const auto & offsets = column_map . getNestedColumn ( ) . getOffsets ( ) ;
auto key_value_columns = column_map . getNestedData ( ) . getColumnsCopy ( ) ;
for ( size_t i = 0 ; i < 2 ; + + i )
2021-08-10 01:33:57 +00:00
{
2022-03-25 21:00:00 +00:00
ColumnsWithTypeAndName element { { key_value_columns [ i ] , key_value_types [ i ] , " " } } ;
key_value_columns [ i ] = element_wrappers [ i ] ( element , to_key_value_types [ i ] , nullable_source , key_value_columns [ i ] - > size ( ) ) ;
}
2022-01-27 00:24:34 +00:00
2022-03-25 21:00:00 +00:00
const auto & key_column_str = assert_cast < const ColumnString & > ( * key_value_columns [ 0 ] ) ;
const auto & value_column = * key_value_columns [ 1 ] ;
2021-08-10 01:33:57 +00:00
2022-03-25 21:00:00 +00:00
using SubcolumnsMap = HashMap < StringRef , MutableColumnPtr , StringRefHash > ;
SubcolumnsMap subcolumns ;
for ( size_t row = 0 ; row < offsets . size ( ) ; + + row )
{
for ( size_t i = offsets [ static_cast < ssize_t > ( row ) - 1 ] ; i < offsets [ row ] ; + + i )
2021-08-10 01:33:57 +00:00
{
2022-03-25 21:00:00 +00:00
auto ref = key_column_str . getDataAt ( i ) ;
bool inserted ;
SubcolumnsMap : : LookupResult it ;
subcolumns . emplace ( ref , it , inserted ) ;
auto & subcolumn = it - > getMapped ( ) ;
if ( inserted )
subcolumn = value_column . cloneEmpty ( ) - > cloneResized ( row ) ;
/// Map can have duplicated keys. We insert only first one.
if ( subcolumn - > size ( ) = = row )
subcolumn - > insertFrom ( value_column , i ) ;
2021-08-10 01:33:57 +00:00
}
2022-03-25 21:00:00 +00:00
/// Insert default values for keys missed in current row.
for ( const auto & [ _ , subcolumn ] : subcolumns )
if ( subcolumn - > size ( ) = = row )
subcolumn - > insertDefault ( ) ;
}
auto column_object = ColumnObject : : create ( has_nullable_subcolumns ) ;
for ( auto & & [ key , subcolumn ] : subcolumns )
{
PathInData path ( key . toView ( ) ) ;
column_object - > addSubcolumn ( path , std : : move ( subcolumn ) ) ;
}
return column_object ;
} ;
}
WrapperType createObjectWrapper ( const DataTypePtr & from_type , const DataTypeObject * to_type ) const
{
if ( const auto * from_tuple = checkAndGetDataType < DataTypeTuple > ( from_type . get ( ) ) )
{
return createTupleToObjectWrapper ( * from_tuple , to_type - > hasNullableSubcolumns ( ) ) ;
}
else if ( const auto * from_map = checkAndGetDataType < DataTypeMap > ( from_type . get ( ) ) )
{
return createMapToObjectWrapper ( * from_map , to_type - > hasNullableSubcolumns ( ) ) ;
2021-08-10 01:33:57 +00:00
}
else if ( checkAndGetDataType < DataTypeString > ( from_type . get ( ) ) )
{
2022-01-21 17:11:18 +00:00
return [ ] ( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * nullable_source , size_t input_rows_count )
2021-08-10 01:33:57 +00:00
{
2022-05-06 14:44:00 +00:00
auto res = ConvertImplGenericFromString < ColumnString > : : execute ( arguments , result_type , nullable_source , input_rows_count ) - > assumeMutable ( ) ;
res - > finalize ( ) ;
2021-08-21 00:31:44 +00:00
return res ;
2021-08-10 01:33:57 +00:00
} ;
}
2022-04-23 06:13:44 +00:00
else if ( checkAndGetDataType < DataTypeObject > ( from_type . get ( ) ) )
{
return [ is_nullable = to_type - > hasNullableSubcolumns ( ) ] ( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * , size_t ) - > ColumnPtr
{
auto & column_object = assert_cast < const ColumnObject & > ( * arguments . front ( ) . column ) ;
auto res = ColumnObject : : create ( is_nullable ) ;
for ( size_t i = 0 ; i < column_object . size ( ) ; i + + )
res - > insert ( column_object [ i ] ) ;
res - > finalize ( ) ;
return res ;
} ;
}
2021-08-10 01:33:57 +00:00
throw Exception ( ErrorCodes : : TYPE_MISMATCH ,
2022-03-25 21:00:00 +00:00
" Cast to Object can be performed only from flatten named Tuple, Map or String. Got: {} " , from_type - > getName ( ) ) ;
2021-08-10 01:33:57 +00:00
}
2023-12-19 16:43:30 +00:00
WrapperType createVariantToVariantWrapper ( const DataTypeVariant & from_variant , const DataTypeVariant & to_variant ) const
{
/// We support only extension of variant type, so, only new types can be added.
/// For example: Variant(T1, T2) -> Variant(T1, T2, T3) is supported, but Variant(T1, T2) -> Variant(T1, T3) is not supported.
/// We want to extend Variant type for free without rewriting the data, but we sort data types inside Variant during type creation
/// (we do it because we want Variant(T1, T2) to be the same as Variant(T2, T1)), but after extension the order of variant types
/// (and so their discriminators) can be different. For example: Variant(T1, T3) -> Variant(T1, T2, T3).
/// To avoid full rewrite of discriminators column, ColumnVariant supports it's local order of variant columns (and so local
/// discriminators) and stores mapping global order -> local order.
/// So, to extend Variant with new types for free, we should keep old local order for old variants, append new variants and change
/// mapping global order -> local order according to the new global order.
/// Create map (new variant type) -> (it's global discriminator in new order).
const auto & new_variants = to_variant . getVariants ( ) ;
std : : unordered_map < String , ColumnVariant : : Discriminator > new_variant_types_to_new_global_discriminator ;
new_variant_types_to_new_global_discriminator . reserve ( new_variants . size ( ) ) ;
for ( size_t i = 0 ; i ! = new_variants . size ( ) ; + + i )
new_variant_types_to_new_global_discriminator [ new_variants [ i ] - > getName ( ) ] = i ;
/// Create set of old variant types.
const auto & old_variants = from_variant . getVariants ( ) ;
std : : unordered_map < String , ColumnVariant : : Discriminator > old_variant_types_to_old_global_discriminator ;
old_variant_types_to_old_global_discriminator . reserve ( old_variants . size ( ) ) ;
for ( size_t i = 0 ; i ! = old_variants . size ( ) ; + + i )
old_variant_types_to_old_global_discriminator [ old_variants [ i ] - > getName ( ) ] = i ;
/// Check that the set of old variants types is a subset of new variant types and collect new global discriminator for each old global discriminator.
std : : unordered_map < ColumnVariant : : Discriminator , ColumnVariant : : Discriminator > old_global_discriminator_to_new ;
old_global_discriminator_to_new . reserve ( old_variants . size ( ) ) ;
for ( const auto & [ old_variant_type , old_discriminator ] : old_variant_types_to_old_global_discriminator )
{
auto it = new_variant_types_to_new_global_discriminator . find ( old_variant_type ) ;
if ( it = = new_variant_types_to_new_global_discriminator . end ( ) )
throw Exception (
ErrorCodes : : CANNOT_CONVERT_TYPE ,
" Cannot convert type {} to {}. Conversion between Variant types is allowed only when new Variant type is an extension "
" of an initial one " , from_variant . getName ( ) , to_variant . getName ( ) ) ;
old_global_discriminator_to_new [ old_discriminator ] = it - > second ;
}
/// Collect variant types and their global discriminators that should be added to the old Variant to get the new Variant.
std : : vector < std : : pair < DataTypePtr , ColumnVariant : : Discriminator > > variant_types_and_discriminators_to_add ;
variant_types_and_discriminators_to_add . reserve ( new_variants . size ( ) - old_variants . size ( ) ) ;
for ( size_t i = 0 ; i ! = new_variants . size ( ) ; + + i )
{
if ( ! old_variant_types_to_old_global_discriminator . contains ( new_variants [ i ] - > getName ( ) ) )
variant_types_and_discriminators_to_add . emplace_back ( new_variants [ i ] , i ) ;
}
return [ old_global_discriminator_to_new , variant_types_and_discriminators_to_add ]
( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * , size_t ) - > ColumnPtr
{
const auto & column_variant = assert_cast < const ColumnVariant & > ( * arguments . front ( ) . column . get ( ) ) ;
size_t num_old_variants = column_variant . getNumVariants ( ) ;
Columns new_variant_columns ;
new_variant_columns . reserve ( num_old_variants + variant_types_and_discriminators_to_add . size ( ) ) ;
std : : vector < ColumnVariant : : Discriminator > new_local_to_global_discriminators ;
new_local_to_global_discriminators . reserve ( num_old_variants + variant_types_and_discriminators_to_add . size ( ) ) ;
for ( size_t i = 0 ; i ! = num_old_variants ; + + i )
{
new_variant_columns . push_back ( column_variant . getVariantPtrByLocalDiscriminator ( i ) ) ;
new_local_to_global_discriminators . push_back ( old_global_discriminator_to_new . at ( column_variant . globalDiscriminatorByLocal ( i ) ) ) ;
}
for ( const auto & [ new_variant_type , new_global_discriminator ] : variant_types_and_discriminators_to_add )
{
new_variant_columns . push_back ( new_variant_type - > createColumn ( ) ) ;
new_local_to_global_discriminators . push_back ( new_global_discriminator ) ;
}
return ColumnVariant : : create ( column_variant . getLocalDiscriminatorsPtr ( ) , column_variant . getOffsetsPtr ( ) , new_variant_columns , new_local_to_global_discriminators ) ;
} ;
}
WrapperType createVariantToColumnWrapper ( const DataTypeVariant & from_variant , const DataTypePtr & to_type ) const
{
const auto & variant_types = from_variant . getVariants ( ) ;
std : : vector < WrapperType > variant_wrappers ;
variant_wrappers . reserve ( variant_types . size ( ) ) ;
/// Create conversion wrapper for each variant.
for ( const auto & variant_type : variant_types )
variant_wrappers . push_back ( prepareUnpackDictionaries ( variant_type , to_type ) ) ;
return [ variant_wrappers , variant_types , to_type ]
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count ) - > ColumnPtr
{
const auto & column_variant = assert_cast < const ColumnVariant & > ( * arguments . front ( ) . column . get ( ) ) ;
/// First, cast each variant to the result type.
std : : vector < ColumnPtr > casted_variant_columns ;
casted_variant_columns . reserve ( variant_types . size ( ) ) ;
for ( size_t i = 0 ; i ! = variant_types . size ( ) ; + + i )
{
auto variant_col = column_variant . getVariantPtrByLocalDiscriminator ( i ) ;
ColumnsWithTypeAndName variant = { { variant_col , variant_types [ i ] , " " } } ;
const auto & variant_wrapper = variant_wrappers [ column_variant . globalDiscriminatorByLocal ( i ) ] ;
casted_variant_columns . push_back ( variant_wrapper ( variant , result_type , nullptr , variant_col - > size ( ) ) ) ;
}
/// Second, construct resulting column from casted variant columns according to discriminators.
const auto & local_discriminators = column_variant . getLocalDiscriminators ( ) ;
auto res = result_type - > createColumn ( ) ;
res - > reserve ( input_rows_count ) ;
for ( size_t i = 0 ; i ! = input_rows_count ; + + i )
{
auto local_discr = local_discriminators [ i ] ;
if ( local_discr = = ColumnVariant : : NULL_DISCRIMINATOR )
res - > insertDefault ( ) ;
else
res - > insertFrom ( * casted_variant_columns [ local_discr ] , column_variant . offsetAt ( i ) ) ;
}
return res ;
} ;
}
static ColumnPtr createVariantFromDescriptorsAndOneNonEmptyVariant ( const DataTypes & variant_types , const ColumnPtr & discriminators , const ColumnPtr & variant , ColumnVariant : : Discriminator variant_discr )
{
Columns variants ;
variants . reserve ( variant_types . size ( ) ) ;
for ( size_t i = 0 ; i ! = variant_types . size ( ) ; + + i )
{
if ( i = = variant_discr )
variants . emplace_back ( variant ) ;
else
variants . push_back ( variant_types [ i ] - > createColumn ( ) ) ;
}
return ColumnVariant : : create ( discriminators , variants ) ;
}
WrapperType createColumnToVariantWrapper ( const DataTypePtr & from_type , const DataTypeVariant & to_variant ) const
{
/// We allow converting NULL to Variant(...) as Variant can store NULLs.
if ( from_type - > onlyNull ( ) )
{
return [ ] ( ColumnsWithTypeAndName & , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count ) - > ColumnPtr
{
auto result_column = result_type - > createColumn ( ) ;
result_column - > insertManyDefaults ( input_rows_count ) ;
return result_column ;
} ;
}
2024-02-20 18:58:06 +00:00
auto variant_discr_opt = to_variant . tryGetVariantDiscriminator ( * removeNullableOrLowCardinalityNullable ( from_type ) ) ;
2023-12-19 16:43:30 +00:00
if ( ! variant_discr_opt )
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Cannot convert type {} to {}. Conversion to Variant allowed only for types from this Variant " , from_type - > getName ( ) , to_variant . getName ( ) ) ;
return [ variant_discr = * variant_discr_opt ]
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * , size_t ) - > ColumnPtr
{
const auto & result_variant_type = assert_cast < const DataTypeVariant & > ( * result_type ) ;
const auto & variant_types = result_variant_type . getVariants ( ) ;
if ( const ColumnNullable * col_nullable = typeid_cast < const ColumnNullable * > ( arguments . front ( ) . column . get ( ) ) )
{
const auto & column = col_nullable - > getNestedColumnPtr ( ) ;
const auto & null_map = col_nullable - > getNullMapData ( ) ;
IColumn : : Filter filter ;
filter . reserve ( column - > size ( ) ) ;
auto discriminators = ColumnVariant : : ColumnDiscriminators : : create ( ) ;
auto & discriminators_data = discriminators - > getData ( ) ;
discriminators_data . reserve ( column - > size ( ) ) ;
size_t variant_size_hint = 0 ;
for ( size_t i = 0 ; i ! = column - > size ( ) ; + + i )
{
if ( null_map [ i ] )
{
discriminators_data . push_back ( ColumnVariant : : NULL_DISCRIMINATOR ) ;
filter . push_back ( 0 ) ;
}
else
{
discriminators_data . push_back ( variant_discr ) ;
filter . push_back ( 1 ) ;
+ + variant_size_hint ;
}
}
ColumnPtr variant_column ;
/// If there were no NULLs, just use the column.
if ( variant_size_hint = = column - > size ( ) )
variant_column = column ;
/// Otherwise we should use filtered column.
else
variant_column = column - > filter ( filter , variant_size_hint ) ;
return createVariantFromDescriptorsAndOneNonEmptyVariant ( variant_types , std : : move ( discriminators ) , variant_column , variant_discr ) ;
}
else if ( isColumnLowCardinalityNullable ( * arguments . front ( ) . column ) )
{
const auto & column = arguments . front ( ) . column ;
/// Variant column cannot have LowCardinality(Nullable(...)) variant, as Variant column stores NULLs itself.
/// We should create a null-map, insert NULL_DISCRIMINATOR on NULL values and filter initial column.
const auto & col_lc = assert_cast < const ColumnLowCardinality & > ( * column ) ;
const auto & indexes = col_lc . getIndexes ( ) ;
auto null_index = col_lc . getDictionary ( ) . getNullValueIndex ( ) ;
IColumn : : Filter filter ;
filter . reserve ( col_lc . size ( ) ) ;
auto discriminators = ColumnVariant : : ColumnDiscriminators : : create ( ) ;
auto & discriminators_data = discriminators - > getData ( ) ;
discriminators_data . reserve ( col_lc . size ( ) ) ;
size_t variant_size_hint = 0 ;
for ( size_t i = 0 ; i ! = col_lc . size ( ) ; + + i )
{
if ( indexes . getUInt ( i ) = = null_index )
{
discriminators_data . push_back ( ColumnVariant : : NULL_DISCRIMINATOR ) ;
filter . push_back ( 0 ) ;
}
else
{
discriminators_data . push_back ( variant_discr ) ;
filter . push_back ( 1 ) ;
+ + variant_size_hint ;
}
}
MutableColumnPtr variant_column ;
/// If there were no NULLs, we can just clone the column.
if ( variant_size_hint = = col_lc . size ( ) )
variant_column = IColumn : : mutate ( column ) ;
/// Otherwise we should filter column.
else
variant_column = column - > filter ( filter , variant_size_hint ) - > assumeMutable ( ) ;
assert_cast < ColumnLowCardinality & > ( * variant_column ) . nestedRemoveNullable ( ) ;
return createVariantFromDescriptorsAndOneNonEmptyVariant ( variant_types , std : : move ( discriminators ) , std : : move ( variant_column ) , variant_discr ) ;
}
else
{
const auto & column = arguments . front ( ) . column ;
auto discriminators = ColumnVariant : : ColumnDiscriminators : : create ( ) ;
discriminators - > getData ( ) . resize_fill ( column - > size ( ) , variant_discr ) ;
return createVariantFromDescriptorsAndOneNonEmptyVariant ( variant_types , std : : move ( discriminators ) , column , variant_discr ) ;
}
} ;
}
/// Wrapper for conversion to/from Variant type
WrapperType createVariantWrapper ( const DataTypePtr & from_type , const DataTypePtr & to_type ) const
{
if ( const auto * from_variant = checkAndGetDataType < DataTypeVariant > ( from_type . get ( ) ) )
{
if ( const auto * to_variant = checkAndGetDataType < DataTypeVariant > ( to_type . get ( ) ) )
return createVariantToVariantWrapper ( * from_variant , * to_variant ) ;
return createVariantToColumnWrapper ( * from_variant , to_type ) ;
}
return createColumnToVariantWrapper ( from_type , assert_cast < const DataTypeVariant & > ( * to_type ) ) ;
}
2015-12-29 13:51:12 +00:00
template < typename FieldType >
2020-10-20 13:11:57 +00:00
WrapperType createEnumWrapper ( const DataTypePtr & from_type , const DataTypeEnum < FieldType > * to_type ) const
2015-12-25 14:45:58 +00:00
{
using EnumType = DataTypeEnum < FieldType > ;
2015-12-29 13:51:12 +00:00
using Function = typename FunctionTo < EnumType > : : Type ;
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
if ( const auto * from_enum8 = checkAndGetDataType < DataTypeEnum8 > ( from_type . get ( ) ) )
2015-12-25 15:43:38 +00:00
checkEnumToEnumConversion ( from_enum8 , to_type ) ;
2020-10-17 14:23:37 +00:00
else if ( const auto * from_enum16 = checkAndGetDataType < DataTypeEnum16 > ( from_type . get ( ) ) )
2015-12-25 15:43:38 +00:00
checkEnumToEnumConversion ( from_enum16 , to_type ) ;
2017-04-01 07:20:54 +00:00
2017-07-21 06:35:58 +00:00
if ( checkAndGetDataType < DataTypeString > ( from_type . get ( ) ) )
2020-10-20 13:11:57 +00:00
return createStringToEnumWrapper < ColumnString , EnumType > ( ) ;
2017-07-21 06:35:58 +00:00
else if ( checkAndGetDataType < DataTypeFixedString > ( from_type . get ( ) ) )
2020-10-20 13:11:57 +00:00
return createStringToEnumWrapper < ColumnFixedString , EnumType > ( ) ;
2019-05-24 12:11:03 +00:00
else if ( isNativeNumber ( from_type ) | | isEnum ( from_type ) )
2015-12-25 14:45:58 +00:00
{
2019-12-25 19:17:41 +00:00
auto function = Function : : create ( ) ;
2020-11-05 19:09:17 +00:00
return createFunctionAdaptor ( function , from_type ) ;
2015-12-25 14:45:58 +00:00
}
2015-12-25 15:43:38 +00:00
else
2020-11-05 19:09:17 +00:00
{
2020-12-05 13:18:56 +00:00
if ( cast_type = = CastType : : accurateOrNull )
2020-11-05 19:09:17 +00:00
return createToNullableColumnWrapper ( ) ;
2020-11-12 15:56:17 +00:00
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Conversion from {} to {} is not supported " ,
from_type - > getName ( ) , to_type - > getName ( ) ) ;
2020-11-05 19:09:17 +00:00
}
2015-12-25 15:43:38 +00:00
}
2017-04-01 07:20:54 +00:00
2015-12-25 15:43:38 +00:00
template < typename EnumTypeFrom , typename EnumTypeTo >
2018-02-02 08:33:36 +00:00
void checkEnumToEnumConversion ( const EnumTypeFrom * from_type , const EnumTypeTo * to_type ) const
2015-12-25 15:43:38 +00:00
{
const auto & from_values = from_type - > getValues ( ) ;
const auto & to_values = to_type - > getValues ( ) ;
2017-04-01 07:20:54 +00:00
2015-12-25 15:43:38 +00:00
using ValueType = std : : common_type_t < typename EnumTypeFrom : : FieldType , typename EnumTypeTo : : FieldType > ;
using NameValuePair = std : : pair < std : : string , ValueType > ;
2015-12-29 12:57:11 +00:00
using EnumValues = std : : vector < NameValuePair > ;
2017-04-01 07:20:54 +00:00
2015-12-29 12:57:11 +00:00
EnumValues name_intersection ;
2015-12-25 15:43:38 +00:00
std : : set_intersection ( std : : begin ( from_values ) , std : : end ( from_values ) ,
2015-12-29 12:57:11 +00:00
std : : begin ( to_values ) , std : : end ( to_values ) , std : : back_inserter ( name_intersection ) ,
[ ] ( auto & & from , auto & & to ) { return from . first < to . first ; } ) ;
2017-04-01 07:20:54 +00:00
2015-12-29 12:57:11 +00:00
for ( const auto & name_value : name_intersection )
{
const auto & old_value = name_value . second ;
const auto & new_value = to_type - > getValue ( name_value . first ) ;
if ( old_value ! = new_value )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Enum conversion changes value for element '{}' from {} to {} " ,
name_value . first , toString ( old_value ) , toString ( new_value ) ) ;
2015-12-29 12:57:11 +00:00
}
2018-08-26 00:50:48 +00:00
}
2017-04-01 07:20:54 +00:00
2015-12-25 15:43:38 +00:00
template < typename ColumnStringType , typename EnumType >
2020-10-20 13:11:57 +00:00
WrapperType createStringToEnumWrapper ( ) const
2015-12-25 15:43:38 +00:00
{
2021-08-07 08:11:40 +00:00
const char * function_name = cast_name ;
2020-10-20 13:11:57 +00:00
return [ function_name ] (
ColumnsWithTypeAndName & arguments , const DataTypePtr & res_type , const ColumnNullable * nullable_col , size_t /*input_rows_count*/ )
2016-02-01 18:32:34 +00:00
{
2020-10-17 14:23:37 +00:00
const auto & first_col = arguments . front ( ) . column . get ( ) ;
const auto & result_type = typeid_cast < const EnumType & > ( * res_type ) ;
2017-04-01 07:20:54 +00:00
2020-07-23 22:10:14 +00:00
const ColumnStringType * col = typeid_cast < const ColumnStringType * > ( first_col ) ;
2020-10-20 13:11:57 +00:00
if ( col & & nullable_col & & nullable_col - > size ( ) ! = col - > size ( ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " ColumnNullable is not compatible with original " ) ;
2020-07-23 22:10:14 +00:00
if ( col )
2015-12-25 15:43:38 +00:00
{
const auto size = col - > size ( ) ;
2017-04-01 07:20:54 +00:00
2016-02-01 18:32:34 +00:00
auto res = result_type . createColumn ( ) ;
auto & out_data = static_cast < typename EnumType : : ColumnType & > ( * res ) . getData ( ) ;
2015-12-25 15:43:38 +00:00
out_data . resize ( size ) ;
2017-04-01 07:20:54 +00:00
2021-03-23 11:58:00 +00:00
auto default_enum_value = result_type . getValues ( ) . front ( ) . second ;
2020-07-23 22:10:14 +00:00
if ( nullable_col )
{
2021-06-15 19:55:21 +00:00
for ( size_t i = 0 ; i < size ; + + i )
2020-07-23 22:10:14 +00:00
{
if ( ! nullable_col - > isNullAt ( i ) )
out_data [ i ] = result_type . getValue ( col - > getDataAt ( i ) ) ;
2021-03-23 11:58:00 +00:00
else
out_data [ i ] = default_enum_value ;
2020-07-23 22:10:14 +00:00
}
}
else
{
2021-06-15 19:55:21 +00:00
for ( size_t i = 0 ; i < size ; + + i )
2020-07-23 22:10:14 +00:00
out_data [ i ] = result_type . getValue ( col - > getDataAt ( i ) ) ;
}
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
return res ;
2015-12-25 15:43:38 +00:00
}
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Unexpected column {} as first argument of function {} " ,
first_col - > getName ( ) , function_name ) ;
2015-12-25 14:45:58 +00:00
} ;
}
2017-04-01 07:20:54 +00:00
2023-11-12 19:02:42 +00:00
template < typename EnumType >
WrapperType createEnumToStringWrapper ( ) const
{
const char * function_name = cast_name ;
return [ function_name ] (
ColumnsWithTypeAndName & arguments , const DataTypePtr & res_type , const ColumnNullable * nullable_col , size_t /*input_rows_count*/ )
{
using ColumnEnumType = EnumType : : ColumnType ;
const auto & first_col = arguments . front ( ) . column . get ( ) ;
const auto & first_type = arguments . front ( ) . type . get ( ) ;
const ColumnEnumType * enum_col = typeid_cast < const ColumnEnumType * > ( first_col ) ;
const EnumType * enum_type = typeid_cast < const EnumType * > ( first_type ) ;
if ( enum_col & & nullable_col & & nullable_col - > size ( ) ! = enum_col - > size ( ) )
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " ColumnNullable is not compatible with original " ) ;
if ( enum_col & & enum_type )
{
const auto size = enum_col - > size ( ) ;
const auto & enum_data = enum_col - > getData ( ) ;
auto res = res_type - > createColumn ( ) ;
if ( nullable_col )
{
for ( size_t i = 0 ; i < size ; + + i )
{
if ( ! nullable_col - > isNullAt ( i ) )
{
const auto & value = enum_type - > getNameForValue ( enum_data [ i ] ) ;
res - > insertData ( value . data , value . size ) ;
}
else
res - > insertDefault ( ) ;
}
}
else
{
for ( size_t i = 0 ; i < size ; + + i )
{
const auto & value = enum_type - > getNameForValue ( enum_data [ i ] ) ;
res - > insertData ( value . data , value . size ) ;
}
}
return res ;
}
else
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Unexpected column {} as first argument of function {} " ,
first_col - > getName ( ) , function_name ) ;
} ;
}
2020-10-17 14:23:37 +00:00
static WrapperType createIdentityWrapper ( const DataTypePtr & )
2017-02-02 13:15:04 +00:00
{
2020-10-20 13:11:57 +00:00
return [ ] ( ColumnsWithTypeAndName & arguments , const DataTypePtr & , const ColumnNullable * , size_t /*input_rows_count*/ )
2017-02-02 13:15:04 +00:00
{
2020-10-17 14:23:37 +00:00
return arguments . front ( ) . column ;
2017-02-02 13:15:04 +00:00
} ;
}
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
static WrapperType createNothingWrapper ( const IDataType * to_type )
2017-12-09 20:56:53 +00:00
{
2017-12-18 04:07:26 +00:00
ColumnPtr res = to_type - > createColumnConstWithDefaultValue ( 1 ) ;
2020-10-20 13:11:57 +00:00
return [ res ] ( ColumnsWithTypeAndName & , const DataTypePtr & , const ColumnNullable * , size_t input_rows_count )
2017-12-09 20:56:53 +00:00
{
/// Column of Nothing type is trivially convertible to any other column
2020-10-17 14:23:37 +00:00
return res - > cloneResized ( input_rows_count ) - > convertToFullColumnIfConst ( ) ;
2017-12-09 20:56:53 +00:00
} ;
}
2018-06-07 10:37:06 +00:00
WrapperType prepareUnpackDictionaries ( const DataTypePtr & from_type , const DataTypePtr & to_type ) const
2016-08-29 16:57:59 +00:00
{
2023-12-19 16:43:30 +00:00
/// Conversion from/to Variant data type is processed in a special way.
/// We don't need to remove LowCardinality/Nullable.
if ( isVariant ( to_type ) | | isVariant ( from_type ) )
return createVariantWrapper ( from_type , to_type ) ;
2018-09-27 15:55:22 +00:00
const auto * from_low_cardinality = typeid_cast < const DataTypeLowCardinality * > ( from_type . get ( ) ) ;
const auto * to_low_cardinality = typeid_cast < const DataTypeLowCardinality * > ( to_type . get ( ) ) ;
const auto & from_nested = from_low_cardinality ? from_low_cardinality - > getDictionaryType ( ) : from_type ;
const auto & to_nested = to_low_cardinality ? to_low_cardinality - > getDictionaryType ( ) : to_type ;
2018-09-19 11:11:52 +00:00
2017-12-09 06:32:22 +00:00
if ( from_type - > onlyNull ( ) )
2017-12-08 03:52:33 +00:00
{
2023-12-19 16:43:30 +00:00
if ( ! to_nested - > isNullable ( ) & & ! isVariant ( to_type ) )
2020-11-05 19:09:17 +00:00
{
2020-12-05 13:18:56 +00:00
if ( cast_type = = CastType : : accurateOrNull )
2020-11-05 19:09:17 +00:00
{
return createToNullableColumnWrapper ( ) ;
}
else
{
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Cannot convert NULL to a non-nullable type " ) ;
2020-11-05 19:09:17 +00:00
}
}
2018-02-22 21:20:02 +00:00
2020-10-20 13:11:57 +00:00
return [ ] ( ColumnsWithTypeAndName & , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count )
2017-12-08 03:52:33 +00:00
{
2020-10-17 14:23:37 +00:00
return result_type - > createColumnConstWithDefaultValue ( input_rows_count ) - > convertToFullColumnIfConst ( ) ;
2017-12-08 03:52:33 +00:00
} ;
}
2019-07-22 15:41:52 +00:00
bool skip_not_null_check = false ;
if ( from_low_cardinality & & from_nested - > isNullable ( ) & & ! to_nested - > isNullable ( ) )
/// Disable check for dictionary. Will check that column doesn't contain NULL in wrapper below.
skip_not_null_check = true ;
auto wrapper = prepareRemoveNullable ( from_nested , to_nested , skip_not_null_check ) ;
2018-09-27 15:55:22 +00:00
if ( ! from_low_cardinality & & ! to_low_cardinality )
2018-06-07 10:37:06 +00:00
return wrapper ;
2019-07-22 15:41:52 +00:00
return [ wrapper , from_low_cardinality , to_low_cardinality , skip_not_null_check ]
2020-10-20 13:11:57 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * nullable_source , size_t input_rows_count ) - > ColumnPtr
2018-06-07 10:37:06 +00:00
{
2020-10-17 14:23:37 +00:00
ColumnsWithTypeAndName args = { arguments [ 0 ] } ;
auto & arg = args . front ( ) ;
auto res_type = result_type ;
ColumnPtr converted_column ;
2018-06-07 10:37:06 +00:00
ColumnPtr res_indexes ;
2018-08-08 16:05:50 +00:00
/// For some types default can't be casted (for example, String to Int). In that case convert column to full.
bool src_converted_to_full_column = false ;
2018-06-07 10:37:06 +00:00
{
auto tmp_rows_count = input_rows_count ;
2018-09-27 15:55:22 +00:00
if ( to_low_cardinality )
2020-10-17 14:23:37 +00:00
res_type = to_low_cardinality - > getDictionaryType ( ) ;
2018-08-08 16:05:50 +00:00
2018-09-27 15:55:22 +00:00
if ( from_low_cardinality )
2018-06-07 10:37:06 +00:00
{
2024-03-11 19:38:30 +00:00
const auto * col_low_cardinality = assert_cast < const ColumnLowCardinality * > ( arguments [ 0 ] . column . get ( ) ) ;
2019-07-22 15:41:52 +00:00
if ( skip_not_null_check & & col_low_cardinality - > containsNull ( ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN , " Cannot convert NULL value to non-Nullable type " ) ;
2019-07-22 15:41:52 +00:00
2018-09-27 15:55:22 +00:00
arg . column = col_low_cardinality - > getDictionary ( ) . getNestedColumn ( ) ;
arg . type = from_low_cardinality - > getDictionaryType ( ) ;
2018-06-07 10:37:06 +00:00
2018-08-08 16:05:50 +00:00
/// TODO: Make map with defaults conversion.
2020-10-17 14:23:37 +00:00
src_converted_to_full_column = ! removeNullable ( arg . type ) - > equals ( * removeNullable ( res_type ) ) ;
2018-08-08 16:05:50 +00:00
if ( src_converted_to_full_column )
2018-09-27 15:55:22 +00:00
arg . column = arg . column - > index ( col_low_cardinality - > getIndexes ( ) , 0 ) ;
2018-08-08 16:05:50 +00:00
else
2018-09-27 15:55:22 +00:00
res_indexes = col_low_cardinality - > getIndexesPtr ( ) ;
2018-08-08 16:05:50 +00:00
2018-06-07 10:37:06 +00:00
tmp_rows_count = arg . column - > size ( ) ;
}
/// Perform the requested conversion.
2020-10-20 13:11:57 +00:00
converted_column = wrapper ( args , res_type , nullable_source , tmp_rows_count ) ;
2018-06-07 10:37:06 +00:00
}
2018-09-27 15:55:22 +00:00
if ( to_low_cardinality )
2018-06-07 10:37:06 +00:00
{
2018-09-27 15:55:22 +00:00
auto res_column = to_low_cardinality - > createColumn ( ) ;
2024-03-11 19:38:30 +00:00
auto * col_low_cardinality = assert_cast < ColumnLowCardinality * > ( res_column . get ( ) ) ;
2018-06-07 10:37:06 +00:00
2018-09-27 15:55:22 +00:00
if ( from_low_cardinality & & ! src_converted_to_full_column )
2018-06-07 10:37:06 +00:00
{
2020-10-17 14:23:37 +00:00
col_low_cardinality - > insertRangeFromDictionaryEncodedColumn ( * converted_column , * res_indexes ) ;
2018-06-07 10:37:06 +00:00
}
else
2020-10-17 14:23:37 +00:00
col_low_cardinality - > insertRangeFromFullColumn ( * converted_column , 0 , converted_column - > size ( ) ) ;
2018-06-07 10:37:06 +00:00
2020-10-17 14:23:37 +00:00
return res_column ;
2018-06-07 10:37:06 +00:00
}
2018-08-08 16:05:50 +00:00
else if ( ! src_converted_to_full_column )
2020-10-17 14:23:37 +00:00
return converted_column - > index ( * res_indexes , 0 ) ;
else
return converted_column ;
2018-06-07 10:37:06 +00:00
} ;
}
2019-07-22 15:41:52 +00:00
WrapperType prepareRemoveNullable ( const DataTypePtr & from_type , const DataTypePtr & to_type , bool skip_not_null_check ) const
2018-06-07 10:37:06 +00:00
{
/// Determine whether pre-processing and/or post-processing must take place during conversion.
2018-07-20 11:08:54 +00:00
bool source_is_nullable = from_type - > isNullable ( ) ;
bool result_is_nullable = to_type - > isNullable ( ) ;
2017-12-08 03:52:33 +00:00
2020-10-20 13:11:57 +00:00
auto wrapper = prepareImpl ( removeNullable ( from_type ) , removeNullable ( to_type ) , result_is_nullable ) ;
2017-04-01 07:20:54 +00:00
2018-07-11 19:51:18 +00:00
if ( result_is_nullable )
2016-08-29 16:57:59 +00:00
{
2018-07-11 19:51:18 +00:00
return [ wrapper , source_is_nullable ]
2020-10-20 13:11:57 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count ) - > ColumnPtr
2016-08-29 16:57:59 +00:00
{
2020-10-14 14:04:50 +00:00
/// Create a temporary columns on which to perform the operation.
2020-10-17 14:23:37 +00:00
const auto & nullable_type = static_cast < const DataTypeNullable & > ( * result_type ) ;
2016-08-29 16:57:59 +00:00
const auto & nested_type = nullable_type . getNestedType ( ) ;
2017-04-01 07:20:54 +00:00
2020-10-17 14:23:37 +00:00
ColumnsWithTypeAndName tmp_args ;
2018-07-11 19:51:18 +00:00
if ( source_is_nullable )
2020-10-17 14:23:37 +00:00
tmp_args = createBlockWithNestedColumns ( arguments ) ;
2016-08-30 16:14:05 +00:00
else
2020-10-17 14:23:37 +00:00
tmp_args = arguments ;
2017-04-01 07:20:54 +00:00
2020-10-20 13:11:57 +00:00
const ColumnNullable * nullable_source = nullptr ;
2020-07-23 22:10:14 +00:00
/// Add original ColumnNullable for createStringToEnumWrapper()
if ( source_is_nullable )
{
if ( arguments . size ( ) ! = 1 )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Invalid number of arguments " ) ;
2020-10-20 13:11:57 +00:00
nullable_source = typeid_cast < const ColumnNullable * > ( arguments . front ( ) . column . get ( ) ) ;
2020-07-23 22:10:14 +00:00
}
2017-04-01 07:20:54 +00:00
2016-08-29 16:57:59 +00:00
/// Perform the requested conversion.
2020-10-20 13:11:57 +00:00
auto tmp_res = wrapper ( tmp_args , nested_type , nullable_source , input_rows_count ) ;
2018-07-11 21:43:09 +00:00
2019-08-05 15:23:32 +00:00
/// May happen in fuzzy tests. For debug purpose.
2020-10-17 14:23:37 +00:00
if ( ! tmp_res )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : LOGICAL_ERROR , " Couldn't convert {} to {} in prepareRemoveNullable wrapper. " ,
arguments [ 0 ] . type - > getName ( ) , nested_type - > getName ( ) ) ;
2019-08-05 15:23:32 +00:00
2020-10-17 14:23:37 +00:00
return wrapInNullable ( tmp_res , arguments , nested_type , input_rows_count ) ;
2016-08-29 16:57:59 +00:00
} ;
}
2018-07-11 19:51:18 +00:00
else if ( source_is_nullable )
2018-02-22 21:20:02 +00:00
{
/// Conversion from Nullable to non-Nullable.
2020-10-17 14:23:37 +00:00
return [ wrapper , skip_not_null_check ]
2020-10-20 13:11:57 +00:00
( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count ) - > ColumnPtr
2018-02-22 21:20:02 +00:00
{
2020-10-17 14:23:37 +00:00
auto tmp_args = createBlockWithNestedColumns ( arguments ) ;
2020-10-20 13:11:57 +00:00
auto nested_type = removeNullable ( result_type ) ;
2018-02-22 21:20:02 +00:00
/// Check that all values are not-NULL.
2019-07-22 15:41:52 +00:00
/// Check can be skipped in case if LowCardinality dictionary is transformed.
/// In that case, correctness will be checked beforehand.
if ( ! skip_not_null_check )
{
2020-10-17 14:23:37 +00:00
const auto & col = arguments [ 0 ] . column ;
2019-08-21 02:28:04 +00:00
const auto & nullable_col = assert_cast < const ColumnNullable & > ( * col ) ;
2019-07-22 15:41:52 +00:00
const auto & null_map = nullable_col . getNullMapData ( ) ;
2018-02-22 21:20:02 +00:00
2022-03-07 13:48:38 +00:00
if ( ! memoryIsZero ( null_map . data ( ) , 0 , null_map . size ( ) ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN , " Cannot convert NULL value to non-Nullable type " ) ;
2019-07-22 15:41:52 +00:00
}
2020-10-20 13:11:57 +00:00
const ColumnNullable * nullable_source = typeid_cast < const ColumnNullable * > ( arguments . front ( ) . column . get ( ) ) ;
return wrapper ( tmp_args , nested_type , nullable_source , input_rows_count ) ;
2018-02-22 21:20:02 +00:00
} ;
}
2016-08-29 16:57:59 +00:00
else
return wrapper ;
}
2017-04-01 07:20:54 +00:00
2018-08-31 08:59:21 +00:00
/// 'from_type' and 'to_type' are nested types in case of Nullable.
/// 'requested_result_is_nullable' is true if CAST to Nullable type is requested.
2020-10-20 13:11:57 +00:00
WrapperType prepareImpl ( const DataTypePtr & from_type , const DataTypePtr & to_type , bool requested_result_is_nullable ) const
2015-12-29 13:51:12 +00:00
{
2022-03-23 18:59:26 +00:00
if ( isUInt8 ( from_type ) & & isBool ( to_type ) )
return createUInt8ToBoolWrapper ( from_type , to_type ) ;
2022-03-14 09:14:41 +00:00
2022-03-23 14:22:08 +00:00
/// We can cast IPv6 into IPv6, IPv4 into IPv4, but we should not allow to cast FixedString(16) into IPv6 as part of identity cast
2022-03-23 18:59:26 +00:00
bool safe_convert_custom_types = true ;
2021-12-20 23:59:08 +00:00
2022-03-23 14:22:08 +00:00
if ( const auto * to_type_custom_name = to_type - > getCustomName ( ) )
2022-03-23 18:59:26 +00:00
safe_convert_custom_types = from_type - > getCustomName ( ) & & from_type - > getCustomName ( ) - > getName ( ) = = to_type_custom_name - > getName ( ) ;
else if ( const auto * from_type_custom_name = from_type - > getCustomName ( ) )
safe_convert_custom_types = to_type - > getCustomName ( ) & & from_type_custom_name - > getName ( ) = = to_type - > getCustomName ( ) - > getName ( ) ;
2021-12-20 23:59:08 +00:00
2022-03-23 18:59:26 +00:00
if ( from_type - > equals ( * to_type ) & & safe_convert_custom_types )
2023-09-11 19:54:19 +00:00
{
/// We can only use identity conversion for DataTypeAggregateFunction when they are strictly equivalent.
if ( typeid_cast < const DataTypeAggregateFunction * > ( from_type . get ( ) ) )
{
if ( DataTypeAggregateFunction : : strictEquals ( from_type , to_type ) )
return createIdentityWrapper ( from_type ) ;
}
else
return createIdentityWrapper ( from_type ) ;
}
2018-09-07 14:37:26 +00:00
else if ( WhichDataType ( from_type ) . isNothing ( ) )
2018-02-22 21:20:02 +00:00
return createNothingWrapper ( to_type . get ( ) ) ;
2018-08-31 08:59:21 +00:00
WrapperType ret ;
auto make_default_wrapper = [ & ] ( const auto & types ) - > bool
{
using Types = std : : decay_t < decltype ( types ) > ;
using ToDataType = typename Types : : LeftType ;
if constexpr (
std : : is_same_v < ToDataType , DataTypeUInt16 > | |
std : : is_same_v < ToDataType , DataTypeUInt32 > | |
std : : is_same_v < ToDataType , DataTypeUInt64 > | |
2021-05-03 15:41:37 +00:00
std : : is_same_v < ToDataType , DataTypeUInt128 > | |
2020-08-19 11:52:17 +00:00
std : : is_same_v < ToDataType , DataTypeUInt256 > | |
2018-08-31 08:59:21 +00:00
std : : is_same_v < ToDataType , DataTypeInt8 > | |
std : : is_same_v < ToDataType , DataTypeInt16 > | |
std : : is_same_v < ToDataType , DataTypeInt32 > | |
std : : is_same_v < ToDataType , DataTypeInt64 > | |
2020-08-19 11:52:17 +00:00
std : : is_same_v < ToDataType , DataTypeInt128 > | |
std : : is_same_v < ToDataType , DataTypeInt256 > | |
2018-08-31 08:59:21 +00:00
std : : is_same_v < ToDataType , DataTypeFloat32 > | |
std : : is_same_v < ToDataType , DataTypeFloat64 > | |
std : : is_same_v < ToDataType , DataTypeDate > | |
2021-07-15 11:40:45 +00:00
std : : is_same_v < ToDataType , DataTypeDate32 > | |
2020-11-05 19:09:17 +00:00
std : : is_same_v < ToDataType , DataTypeDateTime > | |
2022-11-14 14:17:17 +00:00
std : : is_same_v < ToDataType , DataTypeUUID > | |
std : : is_same_v < ToDataType , DataTypeIPv4 > | |
std : : is_same_v < ToDataType , DataTypeIPv6 > )
2018-08-31 08:59:21 +00:00
{
ret = createWrapper ( from_type , checkAndGetDataType < ToDataType > ( to_type . get ( ) ) , requested_result_is_nullable ) ;
return true ;
}
2021-12-20 20:24:16 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeUInt8 > )
{
2021-12-20 23:59:08 +00:00
if ( isBool ( to_type ) )
2021-12-20 20:24:16 +00:00
ret = createBoolWrapper < ToDataType > ( from_type , checkAndGetDataType < ToDataType > ( to_type . get ( ) ) , requested_result_is_nullable ) ;
else
ret = createWrapper ( from_type , checkAndGetDataType < ToDataType > ( to_type . get ( ) ) , requested_result_is_nullable ) ;
return true ;
}
2018-08-31 08:59:21 +00:00
if constexpr (
std : : is_same_v < ToDataType , DataTypeEnum8 > | |
std : : is_same_v < ToDataType , DataTypeEnum16 > )
{
2020-10-20 13:11:57 +00:00
ret = createEnumWrapper ( from_type , checkAndGetDataType < ToDataType > ( to_type . get ( ) ) ) ;
2018-08-31 08:59:21 +00:00
return true ;
}
if constexpr (
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal32 > > | |
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal64 > > | |
2019-10-30 13:59:38 +00:00
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal128 > > | |
2020-08-19 11:52:17 +00:00
std : : is_same_v < ToDataType , DataTypeDecimal < Decimal256 > > | |
2019-10-24 10:44:38 +00:00
std : : is_same_v < ToDataType , DataTypeDateTime64 > )
2018-08-31 08:59:21 +00:00
{
2021-01-04 12:40:33 +00:00
ret = createDecimalWrapper ( from_type , checkAndGetDataType < ToDataType > ( to_type . get ( ) ) , requested_result_is_nullable ) ;
2018-08-31 08:59:21 +00:00
return true ;
}
return false ;
} ;
2022-11-25 15:57:49 +00:00
bool cast_ipv4_ipv6_default_on_conversion_error_value = context & & context - > getSettingsRef ( ) . cast_ipv4_ipv6_default_on_conversion_error ;
bool input_format_ipv4_default_on_conversion_error_value = context & & context - > getSettingsRef ( ) . input_format_ipv4_default_on_conversion_error ;
bool input_format_ipv6_default_on_conversion_error_value = context & & context - > getSettingsRef ( ) . input_format_ipv6_default_on_conversion_error ;
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
auto make_custom_serialization_wrapper = [ & , cast_ipv4_ipv6_default_on_conversion_error_value , input_format_ipv4_default_on_conversion_error_value , input_format_ipv6_default_on_conversion_error_value ] ( const auto & types ) - > bool
2021-08-18 20:34:07 +00:00
{
using Types = std : : decay_t < decltype ( types ) > ;
using ToDataType = typename Types : : RightType ;
using FromDataType = typename Types : : LeftType ;
if constexpr ( WhichDataType ( FromDataType : : type_id ) . isStringOrFixedString ( ) )
{
2022-11-18 20:10:27 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeIPv4 > )
2021-08-18 20:34:07 +00:00
{
2023-09-12 13:34:21 +00:00
ret = [ cast_ipv4_ipv6_default_on_conversion_error_value ,
input_format_ipv4_default_on_conversion_error_value ,
requested_result_is_nullable ] (
ColumnsWithTypeAndName & arguments ,
const DataTypePtr & result_type ,
const ColumnNullable * column_nullable ,
size_t ) - > ColumnPtr
2022-11-18 20:10:27 +00:00
{
if ( ! WhichDataType ( result_type ) . isIPv4 ( ) )
throw Exception ( ErrorCodes : : TYPE_MISMATCH , " Wrong result type {}. Expected IPv4 " , result_type - > getName ( ) ) ;
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
const auto * null_map = column_nullable ? & column_nullable - > getNullMapData ( ) : nullptr ;
2023-09-12 13:34:21 +00:00
if ( requested_result_is_nullable )
return convertToIPv4 < IPStringToNumExceptionMode : : Null > ( arguments [ 0 ] . column , null_map ) ;
else if ( cast_ipv4_ipv6_default_on_conversion_error_value | | input_format_ipv4_default_on_conversion_error_value )
2022-11-18 20:10:27 +00:00
return convertToIPv4 < IPStringToNumExceptionMode : : Default > ( arguments [ 0 ] . column , null_map ) ;
else
return convertToIPv4 < IPStringToNumExceptionMode : : Throw > ( arguments [ 0 ] . column , null_map ) ;
} ;
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
return true ;
}
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
if constexpr ( std : : is_same_v < ToDataType , DataTypeIPv6 > )
{
2023-09-12 13:34:21 +00:00
ret = [ cast_ipv4_ipv6_default_on_conversion_error_value ,
input_format_ipv6_default_on_conversion_error_value ,
requested_result_is_nullable ] (
ColumnsWithTypeAndName & arguments ,
const DataTypePtr & result_type ,
const ColumnNullable * column_nullable ,
size_t ) - > ColumnPtr
2022-11-18 20:10:27 +00:00
{
if ( ! WhichDataType ( result_type ) . isIPv6 ( ) )
throw Exception (
ErrorCodes : : TYPE_MISMATCH , " Wrong result type {}. Expected IPv6 " , result_type - > getName ( ) ) ;
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
const auto * null_map = column_nullable ? & column_nullable - > getNullMapData ( ) : nullptr ;
2023-09-12 13:34:21 +00:00
if ( requested_result_is_nullable )
return convertToIPv6 < IPStringToNumExceptionMode : : Null > ( arguments [ 0 ] . column , null_map ) ;
else if ( cast_ipv4_ipv6_default_on_conversion_error_value | | input_format_ipv6_default_on_conversion_error_value )
2022-11-18 20:10:27 +00:00
return convertToIPv6 < IPStringToNumExceptionMode : : Default > ( arguments [ 0 ] . column , null_map ) ;
else
return convertToIPv6 < IPStringToNumExceptionMode : : Throw > ( arguments [ 0 ] . column , null_map ) ;
} ;
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
return true ;
}
2022-03-12 17:12:05 +00:00
2022-11-18 20:10:27 +00:00
if ( to_type - > getCustomSerialization ( ) & & to_type - > getCustomName ( ) )
{
2023-09-12 13:34:21 +00:00
ret = [ requested_result_is_nullable ] (
ColumnsWithTypeAndName & arguments ,
const DataTypePtr & result_type ,
const ColumnNullable * column_nullable ,
size_t input_rows_count ) - > ColumnPtr
{
auto wrapped_result_type = result_type ;
if ( requested_result_is_nullable )
wrapped_result_type = makeNullable ( result_type ) ;
return ConvertImplGenericFromString < typename FromDataType : : ColumnType > : : execute (
arguments , wrapped_result_type , column_nullable , input_rows_count ) ;
} ;
2021-08-18 20:34:07 +00:00
return true ;
}
}
2023-05-12 15:54:50 +00:00
else if constexpr ( WhichDataType ( FromDataType : : type_id ) . isIPv6 ( ) & & WhichDataType ( ToDataType : : type_id ) . isIPv4 ( ) )
{
ret = [ cast_ipv4_ipv6_default_on_conversion_error_value , requested_result_is_nullable ] (
ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * column_nullable , size_t )
- > ColumnPtr
{
if ( ! WhichDataType ( result_type ) . isIPv4 ( ) )
throw Exception (
ErrorCodes : : TYPE_MISMATCH , " Wrong result type {}. Expected IPv4 " , result_type - > getName ( ) ) ;
const auto * null_map = column_nullable ? & column_nullable - > getNullMapData ( ) : nullptr ;
2023-09-12 13:34:21 +00:00
if ( requested_result_is_nullable )
return convertIPv6ToIPv4 < IPStringToNumExceptionMode : : Null > ( arguments [ 0 ] . column , null_map ) ;
else if ( cast_ipv4_ipv6_default_on_conversion_error_value )
2023-05-12 15:54:50 +00:00
return convertIPv6ToIPv4 < IPStringToNumExceptionMode : : Default > ( arguments [ 0 ] . column , null_map ) ;
else
return convertIPv6ToIPv4 < IPStringToNumExceptionMode : : Throw > ( arguments [ 0 ] . column , null_map ) ;
} ;
return true ;
}
2021-08-18 20:34:07 +00:00
if constexpr ( WhichDataType ( ToDataType : : type_id ) . isStringOrFixedString ( ) )
{
2023-11-12 19:02:42 +00:00
if constexpr ( WhichDataType ( FromDataType : : type_id ) . isEnum ( ) )
{
ret = createEnumToStringWrapper < FromDataType > ( ) ;
return true ;
}
else if ( from_type - > getCustomSerialization ( ) )
2021-08-18 20:34:07 +00:00
{
ret = [ ] ( ColumnsWithTypeAndName & arguments , const DataTypePtr & result_type , const ColumnNullable * , size_t input_rows_count ) - > ColumnPtr
{
return ConvertImplGenericToString < typename ToDataType : : ColumnType > : : execute ( arguments , result_type , input_rows_count ) ;
} ;
return true ;
}
}
return false ;
} ;
if ( callOnTwoTypeIndexes ( from_type - > getTypeId ( ) , to_type - > getTypeId ( ) , make_custom_serialization_wrapper ) )
return ret ;
2018-08-31 08:59:21 +00:00
if ( callOnIndexAndDataType < void > ( to_type - > getTypeId ( ) , make_default_wrapper ) )
return ret ;
switch ( to_type - > getTypeId ( ) )
{
case TypeIndex : : String :
return createStringWrapper ( from_type ) ;
case TypeIndex : : FixedString :
return createFixedStringWrapper ( from_type , checkAndGetDataType < DataTypeFixedString > ( to_type . get ( ) ) - > getN ( ) ) ;
case TypeIndex : : Array :
2021-04-21 21:19:01 +00:00
return createArrayWrapper ( from_type , static_cast < const DataTypeArray & > ( * to_type ) ) ;
2018-08-31 08:59:21 +00:00
case TypeIndex : : Tuple :
return createTupleWrapper ( from_type , checkAndGetDataType < DataTypeTuple > ( to_type . get ( ) ) ) ;
2020-10-10 06:49:03 +00:00
case TypeIndex : : Map :
return createMapWrapper ( from_type , checkAndGetDataType < DataTypeMap > ( to_type . get ( ) ) ) ;
2021-08-10 01:33:57 +00:00
case TypeIndex : : Object :
return createObjectWrapper ( from_type , checkAndGetDataType < DataTypeObject > ( to_type . get ( ) ) ) ;
2019-09-02 09:16:49 +00:00
case TypeIndex : : AggregateFunction :
return createAggregateFunctionWrapper ( from_type , checkAndGetDataType < DataTypeAggregateFunction > ( to_type . get ( ) ) ) ;
2022-11-12 19:47:06 +00:00
case TypeIndex : : Interval :
return createIntervalWrapper ( from_type , checkAndGetDataType < DataTypeInterval > ( to_type . get ( ) ) - > getKind ( ) ) ;
2018-08-31 08:59:21 +00:00
default :
break ;
}
2017-04-01 07:20:54 +00:00
2020-12-05 13:18:56 +00:00
if ( cast_type = = CastType : : accurateOrNull )
2020-11-05 19:09:17 +00:00
return createToNullableColumnWrapper ( ) ;
else
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : CANNOT_CONVERT_TYPE , " Conversion from {} to {} is not supported " ,
from_type - > getName ( ) , to_type - > getName ( ) ) ;
2015-12-23 09:52:21 +00:00
}
2018-02-02 08:33:36 +00:00
} ;
2020-11-05 19:09:17 +00:00
class MonotonicityHelper
{
public :
2021-08-07 08:11:40 +00:00
using MonotonicityForRange = FunctionCastBase : : MonotonicityForRange ;
2020-11-05 19:09:17 +00:00
2020-12-17 18:32:25 +00:00
template < typename DataType >
2020-11-05 19:09:17 +00:00
static auto monotonicityForType ( const DataType * const )
{
return FunctionTo < DataType > : : Type : : Monotonic : : get ;
}
static MonotonicityForRange getMonotonicityInformation ( const DataTypePtr & from_type , const IDataType * to_type )
{
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeUInt8 > ( to_type ) )
return monotonicityForType ( type ) ;
if ( const auto * type = checkAndGetDataType < DataTypeUInt16 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeUInt32 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeUInt64 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeUInt128 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeUInt256 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeInt8 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeInt16 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeInt32 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeInt64 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeInt128 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeInt256 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeFloat32 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeFloat64 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeDate > ( to_type ) )
2021-07-15 11:40:45 +00:00
return monotonicityForType ( type ) ;
if ( const auto * type = checkAndGetDataType < DataTypeDate32 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeDateTime > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeString > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
if ( isEnum ( from_type ) )
{
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeEnum8 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
2021-05-03 15:41:37 +00:00
if ( const auto * type = checkAndGetDataType < DataTypeEnum16 > ( to_type ) )
2020-11-05 19:09:17 +00:00
return monotonicityForType ( type ) ;
}
/// other types like Null, FixedString, Array and Tuple have no monotonicity defined
return { } ;
2015-12-23 09:52:21 +00:00
}
2018-02-02 08:33:36 +00:00
} ;
2011-10-15 23:40:56 +00:00
}