2023-09-07 11:15:49 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionDateOrDateTimeAddInterval.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2023-09-12 16:45:02 +00:00
|
|
|
template <typename Op>
|
|
|
|
class FunctionOpDate : public IFunction
|
2023-09-07 11:15:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-09-12 16:45:02 +00:00
|
|
|
static constexpr auto name = Op::name;
|
|
|
|
|
|
|
|
explicit FunctionOpDate(ContextPtr context_) : context(context_) {}
|
2023-09-07 11:15:49 +00:00
|
|
|
|
|
|
|
|
2023-09-12 16:45:02 +00:00
|
|
|
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionOpDate<Op>>(context); }
|
2023-09-07 11:15:49 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isDateOrDate32(arguments[0].type) && !isDateTime(arguments[0].type) && !isDateTime64(arguments[0].type))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of 1st argument of function {}. Should be a date or a date with time",
|
|
|
|
arguments[0].type->getName(),
|
|
|
|
getName());
|
|
|
|
|
|
|
|
if (!isInterval(arguments[1].type))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
2023-09-12 16:45:02 +00:00
|
|
|
"Illegal type {} of 2nd argument of function {}. Should be an interval",
|
|
|
|
arguments[1].type->getName(),
|
2023-09-07 11:15:49 +00:00
|
|
|
getName());
|
|
|
|
|
2023-09-12 16:45:02 +00:00
|
|
|
auto op = FunctionFactory::instance().get(Op::internal_name, context);
|
|
|
|
auto op_build = op->build(arguments);
|
2023-09-07 11:15:49 +00:00
|
|
|
|
2023-09-12 16:45:02 +00:00
|
|
|
return op_build->getResultType();
|
2023-09-07 11:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 2}; }
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
if (!isDateOrDate32(arguments[0].type) && !isDateTime(arguments[0].type) && !isDateTime64(arguments[0].type))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of 1st argument of function {}. Should be a date or a date with time",
|
|
|
|
arguments[0].type->getName(),
|
|
|
|
getName());
|
|
|
|
|
|
|
|
if (!isInterval(arguments[1].type))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
2023-09-12 16:45:02 +00:00
|
|
|
"Illegal type {} of 2nd argument of function {}. Should be an interval",
|
|
|
|
arguments[1].type->getName(),
|
2023-09-07 11:15:49 +00:00
|
|
|
getName());
|
|
|
|
|
2023-09-12 16:45:02 +00:00
|
|
|
auto op = FunctionFactory::instance().get(Op::internal_name, context);
|
|
|
|
auto op_build = op->build(arguments);
|
2023-09-07 11:15:49 +00:00
|
|
|
|
2023-09-12 16:45:02 +00:00
|
|
|
auto res_type = op_build->getResultType();
|
|
|
|
return op_build->execute(arguments, res_type, input_rows_count);
|
2023-09-07 11:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ContextPtr context;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-12 16:45:02 +00:00
|
|
|
struct AddDate
|
|
|
|
{
|
|
|
|
static constexpr auto name = "addDate";
|
|
|
|
static constexpr auto internal_name = "plus";
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SubDate
|
|
|
|
{
|
|
|
|
static constexpr auto name = "subDate";
|
|
|
|
static constexpr auto internal_name = "minus";
|
|
|
|
};
|
|
|
|
|
|
|
|
using FunctionAddDate = FunctionOpDate<AddDate>;
|
|
|
|
using FunctionSubDate = FunctionOpDate<SubDate>;
|
2023-09-07 11:15:49 +00:00
|
|
|
|
|
|
|
REGISTER_FUNCTION(AddInterval)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionAddDate>();
|
2023-09-12 16:45:02 +00:00
|
|
|
factory.registerFunction<FunctionSubDate>();
|
2023-09-07 11:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|