ClickHouse/src/Functions/logTrace.cpp

63 lines
2.0 KiB
C++
Raw Normal View History

2020-10-10 02:48:15 +00:00
#include <Columns/ColumnConst.h>
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunctionImpl.h>
#include <common/logger_useful.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
class FunctionLogTrace : public IFunction
{
public:
static constexpr auto name = "logTrace";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionLogTrace>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isString(arguments[0]))
throw Exception(
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeUInt8>();
}
2020-10-10 08:02:04 +00:00
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
2020-10-10 02:48:15 +00:00
{
String message;
if (const ColumnConst * col = checkAndGetColumnConst<ColumnString>(block.getByPosition(arguments[0]).column.get()))
message = col->getDataAt(0).data;
else
throw Exception(
"First argument for function " + getName() + " must be Constant string", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2020-10-10 08:02:04 +00:00
static auto * log = &Poco::Logger::get("FunctionLogTrace");
2020-10-10 02:48:15 +00:00
LOG_TRACE(log, message);
2020-10-10 08:02:04 +00:00
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(input_rows_count, 0);
2020-10-10 02:48:15 +00:00
}
};
}
void registerFunctionLogTrace(FunctionFactory & factory)
{
factory.registerFunction<FunctionLogTrace>();
}
}