ClickHouse/src/Functions/addressToSymbol.cpp

97 lines
2.9 KiB
C++
Raw Normal View History

#if defined(__ELF__) && !defined(__FreeBSD__)
2019-08-21 00:48:34 +00:00
2019-07-29 01:08:52 +00:00
#include <Common/SymbolIndex.h>
2019-02-10 03:10:07 +00:00
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
2019-02-10 03:10:07 +00:00
#include <DataTypes/DataTypeString.h>
#include <Functions/IFunctionImpl.h>
2019-07-23 12:55:13 +00:00
#include <Functions/FunctionFactory.h>
#include <Access/AccessFlags.h>
#include <Interpreters/Context.h>
2019-02-10 03:10:07 +00:00
#include <IO/WriteHelpers.h>
2019-02-10 03:10:07 +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;
}
class FunctionAddressToSymbol : public IFunction
2019-02-10 03:10:07 +00:00
{
public:
static constexpr auto name = "addressToSymbol";
static FunctionPtr create(const Context & context)
2019-02-10 03:10:07 +00:00
{
2020-01-24 16:20:36 +00:00
context.checkAccess(AccessType::addressToSymbol);
return std::make_shared<FunctionAddressToSymbol>();
2019-02-10 03:10:07 +00:00
}
String getName() const override
{
return name;
}
size_t getNumberOfArguments() const override
{
return 1;
}
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);
2019-02-10 03:10:07 +00:00
const auto & type = arguments[0].type;
2019-02-10 03:10:07 +00:00
if (!WhichDataType(type.get()).isUInt64())
throw Exception("The only argument for function " + getName() + " must be UInt64. Found "
+ type->getName() + " instead.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2019-02-10 03:10:07 +00:00
return std::make_shared<DataTypeString>();
}
bool useDefaultImplementationForConstants() const override
{
return true;
}
2020-07-21 13:58:07 +00:00
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
2019-02-10 03:10:07 +00:00
{
2019-07-29 22:26:44 +00:00
const SymbolIndex & symbol_index = SymbolIndex::instance();
2019-07-29 01:08:52 +00:00
const ColumnPtr & column = block.getByPosition(arguments[0]).column;
const ColumnUInt64 * column_concrete = checkAndGetColumn<ColumnUInt64>(column.get());
2019-02-10 03:10:07 +00:00
if (!column_concrete)
throw Exception("Illegal column " + column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN);
2019-02-10 03:10:07 +00:00
const typename ColumnVector<UInt64>::Container & data = column_concrete->getData();
2019-02-10 03:10:07 +00:00
auto result_column = ColumnString::create();
for (size_t i = 0; i < input_rows_count; ++i)
2019-02-10 03:10:07 +00:00
{
2019-07-29 18:38:04 +00:00
if (const auto * symbol = symbol_index.findSymbol(reinterpret_cast<const void *>(data[i])))
2019-07-29 22:26:44 +00:00
result_column->insertDataWithTerminatingZero(symbol->name, strlen(symbol->name) + 1);
2019-07-29 01:08:52 +00:00
else
result_column->insertDefault();
2019-02-10 03:10:07 +00:00
}
block.getByPosition(result).column = std::move(result_column);
}
};
void registerFunctionAddressToSymbol(FunctionFactory & factory)
2019-07-23 12:55:13 +00:00
{
factory.registerFunction<FunctionAddressToSymbol>();
2019-07-23 12:55:13 +00:00
}
2019-02-10 03:10:07 +00:00
}
2019-08-21 00:48:34 +00:00
#endif