2022-08-25 08:11:21 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Functions/IFunctionCustomWeek.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Transform>
|
|
|
|
class FunctionCustomWeekToDateOrDate32 : public IFunctionCustomWeek<Transform>, WithContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool enable_date32_results = false;
|
|
|
|
|
|
|
|
static FunctionPtr create(ContextPtr context_)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionCustomWeekToDateOrDate32>(context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit FunctionCustomWeekToDateOrDate32(ContextPtr context_) : WithContext(context_)
|
|
|
|
{
|
|
|
|
enable_date32_results = context_->getSettingsRef().enable_date32_results;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2022-08-25 08:30:23 +00:00
|
|
|
this->checkArguments(arguments, true);
|
2022-08-25 08:11:21 +00:00
|
|
|
|
|
|
|
const IDataType * from_type = arguments[0].type.get();
|
|
|
|
WhichDataType which(from_type);
|
|
|
|
if ((which.isDate32() || which.isDateTime64()) && 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 CustomWeekTransformImpl<DataTypeDate, DataTypeDate>::execute(
|
|
|
|
arguments, result_type, input_rows_count, Transform{});
|
|
|
|
else if (which.isDate32())
|
|
|
|
if (enable_date32_results)
|
2022-09-09 13:45:51 +00:00
|
|
|
return CustomWeekTransformImpl<DataTypeDate32, DataTypeDate32, true>::execute(
|
2022-08-25 08:11:21 +00:00
|
|
|
arguments, result_type, input_rows_count, Transform{});
|
|
|
|
else
|
2022-08-25 08:54:26 +00:00
|
|
|
return CustomWeekTransformImpl<DataTypeDate32, DataTypeDate>::execute(
|
2022-08-25 08:11:21 +00:00
|
|
|
arguments, result_type, input_rows_count, Transform{});
|
|
|
|
else if (which.isDateTime())
|
|
|
|
return CustomWeekTransformImpl<DataTypeDateTime, DataTypeDate>::execute(
|
|
|
|
arguments, result_type, input_rows_count, Transform{});
|
|
|
|
else if (which.isDateTime64())
|
|
|
|
{
|
|
|
|
if (enable_date32_results)
|
2022-09-09 13:45:51 +00:00
|
|
|
return CustomWeekTransformImpl<DataTypeDateTime64, DataTypeDate32, true>::execute(
|
2022-08-25 08:11:21 +00:00
|
|
|
arguments, result_type, input_rows_count,
|
|
|
|
TransformDateTime64<Transform>{assert_cast<const DataTypeDateTime64 *>(from_type)->getScale()});
|
|
|
|
else
|
|
|
|
return CustomWeekTransformImpl<DataTypeDateTime64, DataTypeDate>::execute(
|
|
|
|
arguments, result_type, input_rows_count,
|
|
|
|
TransformDateTime64<Transform>{assert_cast<const DataTypeDateTime64 *>(from_type)->getScale()});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
2022-08-25 12:38:28 +00:00
|
|
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + this->getName(),
|
2022-08-25 08:11:21 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|