Scaffold for toDateTime64(scale, [timezone])

This commit is contained in:
Vasily Nemkov 2019-10-08 07:59:38 +03:00
parent 6fe59aea14
commit 1a84580005
2 changed files with 25 additions and 14 deletions

View File

@ -565,6 +565,14 @@ inline bool isFloat(const T & data_type)
return which.isFloat(); return which.isFloat();
} }
template <typename T>
inline bool isNativeInteger(const T & data_type)
{
WhichDataType which(data_type);
return which.isNativeInt() || which.isNativeUInt();
}
template <typename T> template <typename T>
inline bool isNativeNumber(const T & data_type) inline bool isNativeNumber(const T & data_type)
{ {

View File

@ -828,20 +828,21 @@ public:
else else
{ {
UInt8 max_args = 2; UInt8 max_args = 2;
// UInt8 scale = 3; UInt32 scale = DataTypeDateTime64::default_scale;
// if constexpr (std::is_same_v<ToDataType, DataTypeDateTime64>) // DateTime64 requires more arguments: scale and timezone. Since timezone is optional, scale should be first.
// { if constexpr (std::is_same_v<ToDataType, DataTypeDateTime64>)
// max_args += 1; {
// if (arguments.size() == max_args - 1) max_args += 1;
// { if (isNativeInteger(*arguments[max_args - 1].type))
{
scale = static_cast<UInt32>(arguments[max_args - 1].column->get64(0));
}
}
// } /** Optional (could be first or second) argument with time zone is supported:
// }
/** Optional second argument with time zone is supported:
* - for functions toDateTime, toUnixTimestamp, toDate; * - for functions toDateTime, toUnixTimestamp, toDate;
* - for function toString of DateTime argument. * - for function toString of DateTime argument.
*/ */
if (arguments.size() == max_args) if (arguments.size() == max_args)
{ {
if (!checkAndGetDataType<DataTypeString>(arguments[max_args - 1].type.get())) if (!checkAndGetDataType<DataTypeString>(arguments[max_args - 1].type.get()))
@ -856,15 +857,15 @@ public:
|| (std::is_same_v<Name, NameToString> && (WhichDataType(arguments[0].type).isDateTime() || WhichDataType(arguments[0].type).isDateTime64())))) || (std::is_same_v<Name, NameToString> && (WhichDataType(arguments[0].type).isDateTime() || WhichDataType(arguments[0].type).isDateTime64()))))
{ {
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed " throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
+ toString(arguments.size()) + ", should be 1.", + toString(arguments.size()) + ", should be " + std::to_string(max_args) + ".",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
} }
} }
if (std::is_same_v<ToDataType, DataTypeDateTime>) if (std::is_same_v<ToDataType, DataTypeDateTime>)
return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, 1, 0)); return std::make_shared<DataTypeDateTime>(extractTimeZoneNameFromFunctionArguments(arguments, max_args - 1, 0));
// else if (std::is_same_v<ToDataType, DataTypeDateTime64>) else if (std::is_same_v<ToDataType, DataTypeDateTime64>)
// return std::make_shared<DataTypeDateTime64>(extractTimeZoneNameFromFunctionArguments(arguments, 1, 0)); return std::make_shared<DataTypeDateTime64>(scale, extractTimeZoneNameFromFunctionArguments(arguments, max_args - 1, 0));
else else
return std::make_shared<ToDataType>(); return std::make_shared<ToDataType>();
} }
@ -1408,6 +1409,8 @@ using FunctionToFloat32 = FunctionConvert<DataTypeFloat32, NameToFloat32, ToNumb
using FunctionToFloat64 = FunctionConvert<DataTypeFloat64, NameToFloat64, ToNumberMonotonicity<Float64>>; using FunctionToFloat64 = FunctionConvert<DataTypeFloat64, NameToFloat64, ToNumberMonotonicity<Float64>>;
using FunctionToDate = FunctionConvert<DataTypeDate, NameToDate, ToNumberMonotonicity<UInt16>>; using FunctionToDate = FunctionConvert<DataTypeDate, NameToDate, ToNumberMonotonicity<UInt16>>;
using FunctionToDateTime = FunctionConvert<DataTypeDateTime, NameToDateTime, ToNumberMonotonicity<UInt32>>; using FunctionToDateTime = FunctionConvert<DataTypeDateTime, NameToDateTime, ToNumberMonotonicity<UInt32>>;
// TODO (vnemkov): enable and test toDateTime64 function
//using FunctionToDateTime64 = FunctionConvert<DataTypeDateTime, NameToDateTime, UnknownMonotonicity>;
using FunctionToUUID = FunctionConvert<DataTypeUUID, NameToUUID, ToNumberMonotonicity<UInt128>>; using FunctionToUUID = FunctionConvert<DataTypeUUID, NameToUUID, ToNumberMonotonicity<UInt128>>;
using FunctionToString = FunctionConvert<DataTypeString, NameToString, ToStringMonotonicity>; using FunctionToString = FunctionConvert<DataTypeString, NameToString, ToStringMonotonicity>;
using FunctionToUnixTimestamp = FunctionConvert<DataTypeUInt32, NameToUnixTimestamp, ToNumberMonotonicity<UInt32>>; using FunctionToUnixTimestamp = FunctionConvert<DataTypeUInt32, NameToUnixTimestamp, ToNumberMonotonicity<UInt32>>;