2020-10-12 18:22:09 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2020-10-29 06:42:08 +00:00
|
|
|
#include <DataTypes/DataTypeLowCardinality.h>
|
2020-10-12 18:22:09 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2020-10-29 07:07:42 +00:00
|
|
|
#include <Common/ErrorCodes.h>
|
2020-10-12 18:22:09 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** errorCodeToName() - returns the variable name for the error code.
|
|
|
|
*/
|
|
|
|
class FunctionErrorCodeToName : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "errorCodeToName";
|
|
|
|
static FunctionPtr create(const Context &)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionErrorCodeToName>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & types) const override
|
|
|
|
{
|
|
|
|
if (!isNumber(types.at(0)))
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "The argument of function {} must have simple numeric type, possibly Nullable", name);
|
|
|
|
|
2020-10-29 06:42:08 +00:00
|
|
|
return std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>());
|
2020-10-12 18:22:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & res_type, size_t input_rows_count) const override
|
2020-10-12 18:22:09 +00:00
|
|
|
{
|
2020-10-29 07:35:16 +00:00
|
|
|
const auto & input_column = *arguments[0].column;
|
2020-10-29 06:42:08 +00:00
|
|
|
auto col_res = res_type->createColumn();
|
2020-10-12 18:22:09 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
const Int64 error_code = input_column.getInt(i);
|
2020-10-29 07:07:42 +00:00
|
|
|
std::string_view error_name = ErrorCodes::getName(error_code);
|
2020-10-12 18:22:09 +00:00
|
|
|
col_res->insertData(error_name.data(), error_name.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
return col_res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionErrorCodeToName(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionErrorCodeToName>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|