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>
|
2022-09-29 11:04:13 +00:00
|
|
|
class FunctionCustomWeekToDateOrDate32 : public IFunctionCustomWeek<Transform>
|
2022-08-25 08:11:21 +00:00
|
|
|
{
|
2022-09-29 11:04:13 +00:00
|
|
|
private:
|
2022-09-16 09:54:24 +00:00
|
|
|
const bool enable_extended_results_for_datetime_functions = false;
|
2022-08-25 08:11:21 +00:00
|
|
|
|
2022-09-29 11:04:13 +00:00
|
|
|
public:
|
2022-08-25 08:11:21 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionCustomWeekToDateOrDate32>(context_);
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:37:30 +00:00
|
|
|
explicit FunctionCustomWeekToDateOrDate32(ContextPtr context_)
|
2022-09-29 11:04:13 +00:00
|
|
|
: enable_extended_results_for_datetime_functions(context_->getSettingsRef().enable_extended_results_for_datetime_functions)
|
2022-08-25 08:11:21 +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 08:11:21 +00:00
|
|
|
|
|
|
|
const IDataType * from_type = arguments[0].type.get();
|
|
|
|
WhichDataType which(from_type);
|
2022-09-15 12:48:09 +00:00
|
|
|
if ((which.isDate32() || which.isDateTime64()) && enable_extended_results_for_datetime_functions)
|
2022-08-25 08:11:21 +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 CustomWeekTransformImpl<DataTypeDate, DataTypeDate>::execute(
|
|
|
|
arguments, result_type, input_rows_count, Transform{});
|
|
|
|
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 CustomWeekTransformImpl<DataTypeDate32, DataTypeDate32, /*is_extended_result*/ 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{});
|
2022-09-29 11:04:13 +00:00
|
|
|
}
|
2022-08-25 08:11:21 +00:00
|
|
|
else if (which.isDateTime())
|
|
|
|
return CustomWeekTransformImpl<DataTypeDateTime, DataTypeDate>::execute(
|
|
|
|
arguments, result_type, input_rows_count, Transform{});
|
|
|
|
else if (which.isDateTime64())
|
|
|
|
{
|
2022-09-15 12:48:09 +00:00
|
|
|
if (enable_extended_results_for_datetime_functions)
|
|
|
|
return CustomWeekTransformImpl<DataTypeDateTime64, DataTypeDate32, /*is_extended_result*/ 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
|
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-25 08:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|