ClickHouse/src/Functions/FunctionDateOrDateTimeToDateOrDate32.h

87 lines
3.6 KiB
C++
Raw Normal View History

#pragma once
2022-08-24 15:44:53 +00:00
#include <Functions/IFunctionDateOrDateTime.h>
namespace DB
{
2022-09-09 14:17:46 +00:00
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
template <typename Transform>
2022-08-24 15:44:53 +00:00
class FunctionDateOrDateTimeToDateOrDate32 : public IFunctionDateOrDateTime<Transform>, WithContext
{
public:
bool enable_date32_results = false;
2022-08-24 15:44:53 +00:00
static FunctionPtr create(ContextPtr context_)
{
return std::make_shared<FunctionDateOrDateTimeToDateOrDate32>(context_);
}
2022-08-24 15:44:53 +00:00
explicit FunctionDateOrDateTimeToDateOrDate32(ContextPtr context_) : WithContext(context_)
{
2022-08-24 15:44:53 +00:00
enable_date32_results = context_->getSettingsRef().enable_date32_results;
}
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
this->checkArguments(arguments, true);
const IDataType * from_type = arguments[0].type.get();
WhichDataType which(from_type);
/// If the time zone is specified but empty, throw an exception.
if (which.isDateTime() || which.isDateTime64())
{
std::string time_zone = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0);
/// only validate the time_zone part if the number of arguments is 2.
if (arguments.size() == 2 && time_zone.empty())
throw Exception(
"Function " + this->getName() + " supports a 2nd argument (optional) that must be non-empty and be a valid time zone",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (which.isDateTime64() && enable_date32_results)
return std::make_shared<DataTypeDate32>();
else
return std::make_shared<DataTypeDate>();
}
if (which.isDate32() && enable_date32_results)
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())
if (enable_date32_results)
return DateTimeTransformImpl<DataTypeDate32, DataTypeDate32, Transform, true>::execute(arguments, result_type, input_rows_count);
else
return DateTimeTransformImpl<DataTypeDate32, DataTypeDate, Transform>::execute(arguments, result_type, input_rows_count);
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);
if (enable_date32_results)
return DateTimeTransformImpl<DataTypeDateTime64, DataTypeDate32, decltype(transformer), true>::execute(arguments, result_type, input_rows_count, transformer);
else
return DateTimeTransformImpl<DataTypeDateTime64, DataTypeDate, decltype(transformer)>::execute(arguments, result_type, input_rows_count, transformer);
}
else
throw Exception("Illegal type " + arguments[0].type->getName() + " of argument of function " + this->getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
};
}