Merge pull request #15803 from ucasFL/tid-function

Add `tid` and `logTrace` function
This commit is contained in:
alexey-milovidov 2020-10-10 19:37:04 +03:00 committed by GitHub
commit 1f6c2c8efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 1 deletions

View File

@ -0,0 +1,62 @@
#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>();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
{
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);
static auto * log = &Poco::Logger::get("FunctionLogTrace");
LOG_TRACE(log, message);
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(input_rows_count, 0);
}
};
}
void registerFunctionLogTrace(FunctionFactory & factory)
{
factory.registerFunction<FunctionLogTrace>();
}
}

View File

@ -3,7 +3,6 @@
namespace DB
{
void registerFunctionsArithmetic(FunctionFactory &);
void registerFunctionsArray(FunctionFactory &);
void registerFunctionsTuple(FunctionFactory &);
@ -41,6 +40,8 @@ void registerFunctionsUnixTimestamp64(FunctionFactory & factory);
#if !defined(ARCADIA_BUILD)
void registerFunctionBayesAB(FunctionFactory &);
#endif
void registerFunctionTid(FunctionFactory & factory);
void registerFunctionLogTrace(FunctionFactory & factory);
void registerFunctions()
@ -86,6 +87,8 @@ void registerFunctions()
#if !defined(ARCADIA_BUILD)
registerFunctionBayesAB(factory);
#endif
registerFunctionTid(factory);
registerFunctionLogTrace(factory);
}
}

38
src/Functions/tid.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunctionImpl.h>
#include <common/getThreadId.h>
namespace DB
{
namespace
{
class FunctionTid : public IFunction
{
public:
static constexpr auto name = "tid";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionTid>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 0; }
DataTypePtr getReturnTypeImpl(const DataTypes &) const override { return std::make_shared<DataTypeUInt64>(); }
void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) const override
{
auto current_tid = getThreadId();
block.getByPosition(result).column = DataTypeUInt64().createColumnConst(input_rows_count, current_tid);
}
};
}
void registerFunctionTid(FunctionFactory & factory)
{
factory.registerFunction<FunctionTid>();
}
}

View File

@ -267,6 +267,7 @@ SRCS(
log10.cpp
log2.cpp
log.cpp
logTrace.cpp
lowCardinalityIndices.cpp
lowCardinalityKeys.cpp
lower.cpp
@ -388,6 +389,7 @@ SRCS(
TargetSpecific.cpp
tgamma.cpp
throwIf.cpp
tid.cpp
timeSlot.cpp
timeSlots.cpp
timezone.cpp

View File

@ -0,0 +1 @@
SELECT tid() FORMAT Null

View File

@ -0,0 +1 @@
OK

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "$CURDIR"/../shell_config.sh
CLICKHOUSE_CLIENT=$(echo ${CLICKHOUSE_CLIENT} | sed 's/'"--send_logs_level=${CLICKHOUSE_CLIENT_SERVER_LOGS_LEVEL}"'/--send_logs_level=debug/g')
${CLICKHOUSE_CLIENT} --query="SELECT logTrace('logTrace Function Test');" 2>&1 | grep -q "logTrace Function Test" && echo "OK" || echo "FAIL"