mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-13 09:52:38 +00:00
142 lines
6.6 KiB
C++
142 lines
6.6 KiB
C++
|
#pragma once
|
||
|
#include <Functions/IFunctionCustomWeek.h>
|
||
|
|
||
|
namespace DB
|
||
|
{
|
||
|
|
||
|
namespace ErrorCodes
|
||
|
{
|
||
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
||
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
||
|
}
|
||
|
|
||
|
template <typename Transform>
|
||
|
class FunctionCustomWeekToDateOrDate32 : public IFunctionCustomWeek<Transform>, WithContext
|
||
|
{
|
||
|
public:
|
||
|
static constexpr auto name = Transform::name;
|
||
|
String getName() const override { return name; }
|
||
|
|
||
|
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
|
||
|
{
|
||
|
if (arguments.size() == 1)
|
||
|
{
|
||
|
if (!isDate(arguments[0].type) && !isDate32(arguments[0].type) && !isDateTime(arguments[0].type)
|
||
|
&& !isDateTime64(arguments[0].type))
|
||
|
throw Exception(
|
||
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName()
|
||
|
+ ". Should be a date or a date with time",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
}
|
||
|
else if (arguments.size() == 2)
|
||
|
{
|
||
|
if (!isDate(arguments[0].type) && !isDate32(arguments[0].type) && !isDateTime(arguments[0].type)
|
||
|
&& !isDateTime64(arguments[0].type))
|
||
|
throw Exception(
|
||
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName()
|
||
|
+ ". Should be a date or a date with time",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
if (!isUInt8(arguments[1].type))
|
||
|
throw Exception(
|
||
|
"Function " + getName()
|
||
|
+ " supports 1 or 2 or 3 arguments. The 1st argument "
|
||
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
||
|
"a constant UInt8 with week mode. The 3rd argument (optional) must be "
|
||
|
"a constant string with timezone name",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
}
|
||
|
else if (arguments.size() == 3)
|
||
|
{
|
||
|
if (!isDate(arguments[0].type) && !isDate32(arguments[0].type) && !isDateTime(arguments[0].type)
|
||
|
&& !isDateTime64(arguments[0].type))
|
||
|
throw Exception(
|
||
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName()
|
||
|
+ ". Should be a date or a date with time",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
if (!isUInt8(arguments[1].type))
|
||
|
throw Exception(
|
||
|
"Function " + getName()
|
||
|
+ " supports 1 or 2 or 3 arguments. The 1st argument "
|
||
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
||
|
"a constant UInt8 with week mode. The 3rd argument (optional) must be "
|
||
|
"a constant string with timezone name",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
if (!isString(arguments[2].type))
|
||
|
throw Exception(
|
||
|
"Function " + getName()
|
||
|
+ " supports 1 or 2 or 3 arguments. The 1st argument "
|
||
|
"must be of type Date or DateTime. The 2nd argument (optional) must be "
|
||
|
"a constant UInt8 with week mode. The 3rd argument (optional) must be "
|
||
|
"a constant string with timezone name",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
if (isDate(arguments[0].type))
|
||
|
throw Exception(
|
||
|
"The timezone argument of function " + getName() + " is allowed only when the 1st argument has the type DateTime",
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
}
|
||
|
else
|
||
|
throw Exception(
|
||
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
||
|
+ ", should be 1 or 2 or 3",
|
||
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
||
|
|
||
|
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)
|
||
|
return CustomWeekTransformImpl<DataTypeDate32, DataTypeDate32>::execute(
|
||
|
arguments, result_type, input_rows_count, Transform{});
|
||
|
else
|
||
|
return CustomWeekTransformImpl<DataTypeDate, DataTypeDate32>::execute(
|
||
|
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)
|
||
|
return CustomWeekTransformImpl<DataTypeDateTime64, DataTypeDate32>::execute(
|
||
|
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(
|
||
|
"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName(),
|
||
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
}
|