ClickHouse/src/Functions/monthName.cpp

78 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
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
class FunctionMonthNameImpl : public IFunction
{
public:
static constexpr auto name = "monthName";
static constexpr auto month_str = "month";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionMonthNameImpl>(context); }
explicit FunctionMonthNameImpl(ContextPtr context_) : context(context_) {}
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]
};
auto dateName = FunctionFactory::instance().get("dateName", context)->build(temporary_columns);
return dateName->execute(temporary_columns, result_type, input_rows_count);
}
private:
ContextPtr context;
};
void registerFunctionMonthName(FunctionFactory & factory)
{
factory.registerFunction<FunctionMonthNameImpl>(FunctionFactory::CaseInsensitive);
}
}