ClickHouse/src/Functions/monthName.cpp

81 lines
2.6 KiB
C++
Raw Normal View History

2022-01-07 05:06:47 +00:00
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <IO/WriteHelpers.h>
#include <Functions/FunctionFactory.h>
namespace DB
{
2022-01-08 08:04:22 +00:00
2022-01-07 05:06:47 +00:00
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
2022-01-08 08:04:22 +00:00
class FunctionMonthName : public IFunction
2022-01-07 05:06:47 +00:00
{
public:
static constexpr auto name = "monthName";
static constexpr auto month_str = "month";
2022-01-08 08:04:22 +00:00
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionMonthName>(context); }
2022-01-07 05:06:47 +00:00
2022-01-08 08:04:22 +00:00
explicit FunctionMonthName(ContextPtr context_)
: function_resolver(FunctionFactory::instance().get("dateName", std::move(context_)))
{}
2022-01-07 05:06:47 +00:00
String getName() const override { return name; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
size_t getNumberOfArguments() const override { return 1; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if (arguments.size() != 1)
throw Exception(
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Number of arguments for function {} doesn't match: passed {}, should be 1",
getName(),
toString(arguments.size()));
WhichDataType argument_type(arguments[0].type);
if (!argument_type.isDate() && !argument_type.isDateTime() && !argument_type.isDateTime64())
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type of argument of function {}, should be Date, DateTime or DateTime64",
getName());
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(
const ColumnsWithTypeAndName & arguments,
const DataTypePtr & result_type,
2022-01-07 05:23:22 +00:00
size_t input_rows_count) const override
2022-01-07 05:06:47 +00:00
{
auto month_column = DataTypeString().createColumnConst(arguments[0].column->size(), month_str);
ColumnsWithTypeAndName temporary_columns
{
ColumnWithTypeAndName(month_column, std::make_shared<DataTypeString>(), ""),
arguments[0]
};
2022-01-08 08:04:22 +00:00
auto date_name_func = function_resolver->build(temporary_columns);
2022-01-07 15:47:21 +00:00
return date_name_func->execute(temporary_columns, result_type, input_rows_count);
2022-01-07 05:06:47 +00:00
}
private:
2022-01-08 08:04:22 +00:00
FunctionOverloadResolverPtr function_resolver;
2022-01-07 05:06:47 +00:00
};
void registerFunctionMonthName(FunctionFactory & factory)
{
2022-01-08 08:04:22 +00:00
factory.registerFunction<FunctionMonthName>(FunctionFactory::CaseInsensitive);
2022-01-07 05:06:47 +00:00
}
}