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-08-24 15:44:53 +00:00
|
|
|
class FunctionDateOrDateTimeToDateOrDate32 : public IFunctionDateOrDateTime<Transform>, WithContext
|
2022-08-17 16:38:06 +00:00
|
|
|
{
|
|
|
|
public:
|
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-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-15 12:48:09 +00:00
|
|
|
explicit FunctionDateOrDateTimeToDateOrDate32(ContextPtr context_) : WithContext(context_), enable_extended_results_for_datetime_functions(context_->getSettingsRef().enable_extended_results_for_datetime_functions) { }
|
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-08-22 16:24:45 +00:00
|
|
|
if (which.isDateTime() || which.isDateTime64())
|
2022-08-17 16:38:06 +00:00
|
|
|
{
|
2022-08-22 16:24:45 +00:00
|
|
|
/// only validate the time_zone part if the number of arguments is 2.
|
2022-09-15 10:56:05 +00:00
|
|
|
if (arguments.size() == 2 && extractTimeZoneNameFromFunctionArguments(arguments, 1, 0).empty())
|
2022-08-17 16:38:06 +00:00
|
|
|
throw Exception(
|
2022-08-25 12:38:28 +00:00
|
|
|
"Function " + this->getName() + " supports a 2nd argument (optional) that must be non-empty and be a valid time zone",
|
2022-08-17 16:38:06 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2022-09-15 12:48:09 +00:00
|
|
|
if (which.isDateTime64() && enable_extended_results_for_datetime_functions)
|
2022-08-22 16:24:45 +00:00
|
|
|
return std::make_shared<DataTypeDate32>();
|
|
|
|
else
|
|
|
|
return std::make_shared<DataTypeDate>();
|
2022-08-17 16:38:06 +00:00
|
|
|
}
|
2022-09-15 12:48:09 +00:00
|
|
|
if (which.isDate32() && 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-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-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-08-25 12:38:28 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0].type->getName() + " of argument of function " + this->getName(),
|
2022-08-17 16:38:06 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|