2022-08-17 16:38:06 +00:00
|
|
|
#pragma once
|
2022-08-24 15:44:53 +00:00
|
|
|
#include <Functions/IFunctionDateOrDateTime.h>
|
2022-08-17 16:38:06 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-09-09 14:17:46 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:38:06 +00:00
|
|
|
template <typename Transform>
|
2022-09-29 09:29:07 +00:00
|
|
|
class FunctionDateOrDateTimeToDateOrDate32 : public IFunctionDateOrDateTime<Transform>
|
2022-08-17 16:38:06 +00:00
|
|
|
{
|
2022-09-29 11:04:13 +00:00
|
|
|
private:
|
2022-09-15 12:48:09 +00:00
|
|
|
const bool enable_extended_results_for_datetime_functions = false;
|
2022-08-24 15:44:53 +00:00
|
|
|
|
2022-09-29 11:04:13 +00:00
|
|
|
public:
|
2022-08-23 12:43:08 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionDateOrDateTimeToDateOrDate32>(context_);
|
|
|
|
}
|
2022-08-17 16:38:06 +00:00
|
|
|
|
2022-09-16 09:37:30 +00:00
|
|
|
explicit FunctionDateOrDateTimeToDateOrDate32(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-16 09:37:30 +00:00
|
|
|
{
|
|
|
|
}
|
2022-08-17 16:38:06 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2022-09-15 10:56:05 +00:00
|
|
|
this->checkArguments(arguments, /*is_result_type_date_or_date32*/ true);
|
2022-08-25 12:38:28 +00:00
|
|
|
|
2022-08-17 16:38:06 +00:00
|
|
|
const IDataType * from_type = arguments[0].type.get();
|
|
|
|
WhichDataType which(from_type);
|
|
|
|
|
|
|
|
/// If the time zone is specified but empty, throw an exception.
|
2022-09-16 09:37:30 +00:00
|
|
|
/// only validate the time_zone part if the number of arguments is 2.
|
|
|
|
if ((which.isDateTime() || which.isDateTime64()) && arguments.size() == 2
|
2023-06-10 12:53:02 +00:00
|
|
|
&& extractTimeZoneNameFromFunctionArguments(arguments, 1, 0, false).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-16 09:37:30 +00:00
|
|
|
|
|
|
|
if ((which.isDate32() || which.isDateTime64()) && enable_extended_results_for_datetime_functions)
|
2022-08-17 16:38:06 +00:00
|
|
|
return std::make_shared<DataTypeDate32>();
|
|
|
|
else
|
|
|
|
return std::make_shared<DataTypeDate>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (which.isDate())
|
|
|
|
return DateTimeTransformImpl<DataTypeDate, DataTypeDate, Transform>::execute(arguments, result_type, input_rows_count);
|
|
|
|
else if (which.isDate32())
|
2022-09-29 11:04:13 +00:00
|
|
|
{
|
2022-09-15 12:48:09 +00:00
|
|
|
if (enable_extended_results_for_datetime_functions)
|
|
|
|
return DateTimeTransformImpl<DataTypeDate32, DataTypeDate32, Transform, /*is_extended_result*/ true>::execute(arguments, result_type, input_rows_count);
|
2022-08-23 12:43:08 +00:00
|
|
|
else
|
|
|
|
return DateTimeTransformImpl<DataTypeDate32, DataTypeDate, Transform>::execute(arguments, result_type, input_rows_count);
|
2022-09-29 11:04:13 +00:00
|
|
|
}
|
2022-08-17 16:38:06 +00:00
|
|
|
else if (which.isDateTime())
|
|
|
|
return DateTimeTransformImpl<DataTypeDateTime, DataTypeDate, 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);
|
2022-09-15 12:48:09 +00:00
|
|
|
if (enable_extended_results_for_datetime_functions)
|
|
|
|
return DateTimeTransformImpl<DataTypeDateTime64, DataTypeDate32, decltype(transformer), /*is_extended_result*/ true>::execute(arguments, result_type, input_rows_count, transformer);
|
2022-08-23 12:43:08 +00:00
|
|
|
else
|
|
|
|
return DateTimeTransformImpl<DataTypeDateTime64, DataTypeDate, decltype(transformer)>::execute(arguments, result_type, input_rows_count, transformer);
|
2022-08-17 16:38:06 +00:00
|
|
|
}
|
|
|
|
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-08-17 16:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|