2021-10-02 07:13:14 +00:00
|
|
|
#include <base/demangle.h>
|
2019-07-29 22:27:02 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Access/Common/AccessFlags.h>
|
2019-07-29 23:54:49 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2019-07-29 22:27:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-07-29 22:27:02 +00:00
|
|
|
class FunctionDemangle : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "demangle";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2019-07-29 22:27:02 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
context->checkAccess(AccessType::demangle);
|
2019-07-29 22:27:02 +00:00
|
|
|
return std::make_shared<FunctionDemangle>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override
|
2021-04-29 14:48:26 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:27:02 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
throw Exception("Function " + getName() + " needs exactly one argument; passed "
|
|
|
|
+ toString(arguments.size()) + ".", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
const auto & type = arguments[0].type;
|
|
|
|
|
|
|
|
if (!WhichDataType(type.get()).isString())
|
|
|
|
throw Exception("The only argument for function " + getName() + " must be String. Found "
|
|
|
|
+ type->getName() + " instead.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2019-07-29 22:27:02 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
const ColumnPtr & column = arguments[0].column;
|
2019-07-29 22:27:02 +00:00
|
|
|
const ColumnString * column_concrete = checkAndGetColumn<ColumnString>(column.get());
|
|
|
|
|
|
|
|
if (!column_concrete)
|
|
|
|
throw Exception("Illegal column " + column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
auto result_column = ColumnString::create();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
2022-09-17 03:34:18 +00:00
|
|
|
StringRef source = column_concrete->getDataAt(i);
|
2020-02-04 13:31:15 +00:00
|
|
|
auto demangled = tryDemangle(source.data);
|
2020-02-05 10:24:24 +00:00
|
|
|
if (demangled)
|
2020-02-03 19:50:08 +00:00
|
|
|
{
|
2022-09-17 03:34:18 +00:00
|
|
|
result_column->insertData(demangled.get(), strlen(demangled.get()));
|
2020-02-03 19:50:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-17 03:34:18 +00:00
|
|
|
result_column->insertData(source.data, source.size);
|
2020-02-03 19:50:08 +00:00
|
|
|
}
|
2019-07-29 22:27:02 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:41:50 +00:00
|
|
|
return result_column;
|
2019-07-29 22:27:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Demangle)
|
2019-07-29 22:27:02 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionDemangle>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|