2022-09-21 12:58:57 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Functions/IFunctionDateOrDateTime.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Transform>
|
2022-09-29 09:29:07 +00:00
|
|
|
class FunctionDateOrDateTimeToDateTimeOrDateTime64 : public IFunctionDateOrDateTime<Transform>
|
2022-09-21 12:58:57 +00:00
|
|
|
{
|
2022-09-29 11:04:13 +00:00
|
|
|
private:
|
2022-09-21 12:58:57 +00:00
|
|
|
const bool enable_extended_results_for_datetime_functions = false;
|
|
|
|
|
2022-09-29 11:04:13 +00:00
|
|
|
public:
|
2022-09-21 12:58:57 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionDateOrDateTimeToDateTimeOrDateTime64>(context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit FunctionDateOrDateTimeToDateTimeOrDateTime64(ContextPtr context_)
|
2022-09-29 09:29:07 +00:00
|
|
|
: enable_extended_results_for_datetime_functions(context_->getSettingsRef().enable_extended_results_for_datetime_functions)
|
2022-09-21 12:58:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2022-09-28 16:05:59 +00:00
|
|
|
this->checkArguments(arguments, /*is_result_type_date_or_date32*/ false);
|
2022-09-21 12:58:57 +00:00
|
|
|
|
|
|
|
const IDataType * from_type = arguments[0].type.get();
|
2022-09-29 09:29:07 +00:00
|
|
|
|
2022-09-21 12:58:57 +00:00
|
|
|
WhichDataType which(from_type);
|
|
|
|
|
2023-06-10 12:53:02 +00:00
|
|
|
std::string time_zone = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0, false);
|
2022-09-28 16:05:59 +00:00
|
|
|
|
2022-09-21 12:58:57 +00:00
|
|
|
/// If the time zone is specified but empty, throw an exception.
|
|
|
|
/// only validate the time_zone part if the number of arguments is 2.
|
2022-09-28 16:05:59 +00:00
|
|
|
if (arguments.size() == 2 && time_zone.empty())
|
2022-09-29 14:07:02 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Function {} supports a 2nd argument (optional) that must be a valid time zone",
|
|
|
|
this->getName());
|
2022-09-21 12:58:57 +00:00
|
|
|
|
|
|
|
if ((which.isDate32() || which.isDateTime64()) && enable_extended_results_for_datetime_functions)
|
|
|
|
{
|
|
|
|
Int64 scale = DataTypeDateTime64::default_scale;
|
|
|
|
if (which.isDateTime64())
|
|
|
|
{
|
|
|
|
if (const auto * dt64 = checkAndGetDataType<DataTypeDateTime64>(arguments[0].type.get()))
|
|
|
|
scale = dt64->getScale();
|
|
|
|
}
|
2022-09-28 16:05:59 +00:00
|
|
|
return std::make_shared<DataTypeDateTime64>(scale, time_zone);
|
2022-09-21 12:58:57 +00:00
|
|
|
}
|
|
|
|
else
|
2022-09-28 16:05:59 +00:00
|
|
|
return std::make_shared<DataTypeDateTime>(time_zone);
|
2022-09-21 12:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
const IDataType * from_type = arguments[0].type.get();
|
|
|
|
WhichDataType which(from_type);
|
2022-09-22 07:07:04 +00:00
|
|
|
if (which.isDate())
|
|
|
|
return DateTimeTransformImpl<DataTypeDate, DataTypeDateTime, Transform>::execute(arguments, result_type, input_rows_count);
|
|
|
|
else if (which.isDate32())
|
2022-09-29 11:04:13 +00:00
|
|
|
{
|
2022-09-22 07:07:04 +00:00
|
|
|
if (enable_extended_results_for_datetime_functions)
|
|
|
|
return DateTimeTransformImpl<DataTypeDate32, DataTypeDateTime64, Transform, /*is_extended_result*/ true>::execute(arguments, result_type, input_rows_count);
|
|
|
|
else
|
|
|
|
return DateTimeTransformImpl<DataTypeDate32, DataTypeDateTime, Transform>::execute(arguments, result_type, input_rows_count);
|
2022-09-29 11:04:13 +00:00
|
|
|
}
|
|
|
|
else if (which.isDateTime())
|
2022-09-21 12:58:57 +00:00
|
|
|
return DateTimeTransformImpl<DataTypeDateTime, DataTypeDateTime, Transform>::execute(arguments, result_type, input_rows_count);
|
|
|
|
else if (which.isDateTime64())
|
|
|
|
{
|
|
|
|
const auto scale = static_cast<const DataTypeDateTime64 *>(from_type)->getScale();
|
|
|
|
|
|
|
|
const TransformDateTime64<Transform> transformer(scale);
|
|
|
|
if (enable_extended_results_for_datetime_functions)
|
|
|
|
return DateTimeTransformImpl<DataTypeDateTime64, DataTypeDateTime64, decltype(transformer), /*is_extended_result*/ true>::execute(arguments, result_type, input_rows_count, transformer);
|
|
|
|
else
|
|
|
|
return DateTimeTransformImpl<DataTypeDateTime64, DataTypeDateTime, decltype(transformer)>::execute(arguments, result_type, input_rows_count, transformer);
|
|
|
|
}
|
|
|
|
else
|
2022-09-29 14:07:02 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument of function {}",
|
|
|
|
arguments[0].type->getName(), this->getName());
|
2022-09-21 12:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|