ClickHouse/src/Functions/serverConstants.cpp

185 lines
6.6 KiB
C++
Raw Normal View History

2021-09-15 21:17:20 +00:00
#include <Functions/FunctionConstantBase.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeUUID.h>
#include <Core/ServerUUID.h>
#include <Common/SymbolIndex.h>
#include <Common/DNSResolver.h>
#include <Common/DateLUT.h>
#include <Common/ClickHouseRevision.h>
2021-09-15 21:17:20 +00:00
2021-10-09 08:49:49 +00:00
#if defined(OS_LINUX)
# include <Poco/Environment.h>
#endif
2021-10-27 23:10:39 +00:00
#include <Common/config_version.h>
2021-09-15 21:17:20 +00:00
namespace DB
{
namespace
{
#if defined(__ELF__) && !defined(OS_FREEBSD)
2021-09-15 21:17:20 +00:00
/// buildId() - returns the compiler build id of the running binary.
class FunctionBuildId : public FunctionConstantBase<FunctionBuildId, String, DataTypeString>
{
public:
static constexpr auto name = "buildId";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionBuildId>(context); }
explicit FunctionBuildId(ContextPtr context) : FunctionConstantBase(SymbolIndex::instance()->getBuildIDHex(), context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
#endif
/// Get the host name. Is is constant on single server, but is not constant in distributed queries.
class FunctionHostName : public FunctionConstantBase<FunctionHostName, String, DataTypeString>
{
public:
static constexpr auto name = "hostName";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionHostName>(context); }
explicit FunctionHostName(ContextPtr context) : FunctionConstantBase(DNSResolver::instance().getHostName(), context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
class FunctionServerUUID : public FunctionConstantBase<FunctionServerUUID, UUID, DataTypeUUID>
{
public:
static constexpr auto name = "serverUUID";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionServerUUID>(context); }
explicit FunctionServerUUID(ContextPtr context) : FunctionConstantBase(ServerUUID::get(), context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
class FunctionTcpPort : public FunctionConstantBase<FunctionTcpPort, UInt16, DataTypeUInt16>
{
public:
static constexpr auto name = "tcpPort";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTcpPort>(context); }
explicit FunctionTcpPort(ContextPtr context) : FunctionConstantBase(context->getTCPPort(), context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
/// Returns the server time zone.
class FunctionTimezone : public FunctionConstantBase<FunctionTimezone, String, DataTypeString>
{
public:
static constexpr auto name = "timezone";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTimezone>(context); }
explicit FunctionTimezone(ContextPtr context) : FunctionConstantBase(String{DateLUT::instance().getTimeZone()}, context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
/// Returns server uptime in seconds.
class FunctionUptime : public FunctionConstantBase<FunctionUptime, UInt32, DataTypeUInt32>
{
public:
static constexpr auto name = "uptime";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionUptime>(context); }
explicit FunctionUptime(ContextPtr context) : FunctionConstantBase(context->getUptimeSeconds(), context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
/// version() - returns the current version as a string.
class FunctionVersion : public FunctionConstantBase<FunctionVersion, String, DataTypeString>
{
public:
static constexpr auto name = "version";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionVersion>(context); }
explicit FunctionVersion(ContextPtr context) : FunctionConstantBase(VERSION_STRING, context->isDistributed()) {}
2021-09-15 21:17:20 +00:00
};
/// revision() - returns the current revision.
class FunctionRevision : public FunctionConstantBase<FunctionRevision, UInt32, DataTypeUInt32>
{
public:
static constexpr auto name = "revision";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionRevision>(context); }
explicit FunctionRevision(ContextPtr context) : FunctionConstantBase(ClickHouseRevision::getVersionRevision(), context->isDistributed()) {}
};
2021-09-15 21:17:20 +00:00
class FunctionZooKeeperSessionUptime : public FunctionConstantBase<FunctionZooKeeperSessionUptime, UInt32, DataTypeUInt32>
{
public:
static constexpr auto name = "zookeeperSessionUptime";
explicit FunctionZooKeeperSessionUptime(ContextPtr context)
: FunctionConstantBase(context->getZooKeeperSessionUptime(), context->isDistributed())
{
}
2021-09-15 21:17:20 +00:00
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionZooKeeperSessionUptime>(context); }
};
2021-10-09 08:49:49 +00:00
#if defined(OS_LINUX)
2021-10-09 11:53:21 +00:00
class FunctionGetOSKernelVersion : public FunctionConstantBase<FunctionGetOSKernelVersion, String, DataTypeString>
2021-10-09 08:49:49 +00:00
{
public:
2021-10-09 11:53:21 +00:00
static constexpr auto name = "getOSKernelVersion";
explicit FunctionGetOSKernelVersion(ContextPtr context) : FunctionConstantBase(Poco::Environment::osName() + " " + Poco::Environment::osVersion(), context->isDistributed()) {}
2021-10-09 11:53:21 +00:00
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionGetOSKernelVersion>(context); }
2021-10-09 08:49:49 +00:00
};
#endif
2021-09-15 21:17:20 +00:00
}
2021-09-16 09:15:22 +00:00
void registerFunctionBuildId([[maybe_unused]] FunctionFactory & factory)
2021-09-15 21:17:20 +00:00
{
#if defined(__ELF__) && !defined(OS_FREEBSD)
2021-09-15 21:17:20 +00:00
factory.registerFunction<FunctionBuildId>();
#endif
}
void registerFunctionHostName(FunctionFactory & factory)
{
factory.registerFunction<FunctionHostName>();
factory.registerAlias("hostname", "hostName");
}
void registerFunctionServerUUID(FunctionFactory & factory)
{
factory.registerFunction<FunctionServerUUID>();
}
void registerFunctionTcpPort(FunctionFactory & factory)
{
factory.registerFunction<FunctionTcpPort>();
}
void registerFunctionTimezone(FunctionFactory & factory)
{
factory.registerFunction<FunctionTimezone>();
factory.registerAlias("timeZone", "timezone");
}
void registerFunctionUptime(FunctionFactory & factory)
{
factory.registerFunction<FunctionUptime>();
}
void registerFunctionVersion(FunctionFactory & factory)
{
factory.registerFunction<FunctionVersion>(FunctionFactory::CaseInsensitive);
}
void registerFunctionRevision(FunctionFactory & factory)
{
factory.registerFunction<FunctionRevision>(FunctionFactory::CaseInsensitive);
}
2021-09-15 21:17:20 +00:00
void registerFunctionZooKeeperSessionUptime(FunctionFactory & factory)
{
factory.registerFunction<FunctionZooKeeperSessionUptime>();
}
2021-10-09 08:49:49 +00:00
2021-10-09 11:53:21 +00:00
void registerFunctionGetOSKernelVersion([[maybe_unused]] FunctionFactory & factory)
2021-10-09 08:49:49 +00:00
{
#if defined(OS_LINUX)
2021-10-09 11:53:21 +00:00
factory.registerFunction<FunctionGetOSKernelVersion>();
2021-10-09 08:49:49 +00:00
#endif
}
2021-09-15 21:17:20 +00:00
}