ClickHouse/src/Functions/serverConstants.cpp

192 lines
6.8 KiB
C++
Raw Normal View History

2021-09-15 21:17:20 +00:00
#include <Functions/FunctionConstantBase.h>
#include <base/getFQDNOrHostName.h>
#include <Poco/Util/AbstractConfiguration.h>
2021-09-15 21:17:20 +00:00
#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
2022-07-31 07:36:40 +00:00
#include <Poco/Environment.h>
2021-10-09 08:49:49 +00:00
#include "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
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
};
class FunctionDisplayName : public FunctionConstantBase<FunctionDisplayName, String, DataTypeString>
{
public:
static constexpr auto name = "displayName";
static FunctionPtr create(ContextPtr context) {return std::make_shared<FunctionDisplayName>(context); }
explicit FunctionDisplayName(ContextPtr context) : FunctionConstantBase(context->getConfigRef().getString("display_name", getFQDNOrHostName()), context->isDistributed()) {}
};
2021-09-15 21:17:20 +00:00
}
#if defined(__ELF__) && !defined(OS_FREEBSD)
REGISTER_FUNCTION(BuildId)
{
2021-09-15 21:17:20 +00:00
factory.registerFunction<FunctionBuildId>();
}
#endif
2021-09-15 21:17:20 +00:00
REGISTER_FUNCTION(HostName)
2021-09-15 21:17:20 +00:00
{
factory.registerFunction<FunctionHostName>();
factory.registerAlias("hostname", "hostName");
}
REGISTER_FUNCTION(ServerUUID)
2021-09-15 21:17:20 +00:00
{
factory.registerFunction<FunctionServerUUID>();
}
REGISTER_FUNCTION(TcpPort)
2021-09-15 21:17:20 +00:00
{
factory.registerFunction<FunctionTcpPort>();
}
REGISTER_FUNCTION(Timezone)
2021-09-15 21:17:20 +00:00
{
factory.registerFunction<FunctionTimezone>();
factory.registerAlias("timeZone", "timezone");
}
REGISTER_FUNCTION(Uptime)
2021-09-15 21:17:20 +00:00
{
factory.registerFunction<FunctionUptime>();
}
REGISTER_FUNCTION(Version)
2021-09-15 21:17:20 +00:00
{
2022-08-27 20:06:03 +00:00
factory.registerFunction<FunctionVersion>({}, FunctionFactory::CaseInsensitive);
2021-09-15 21:17:20 +00:00
}
REGISTER_FUNCTION(Revision)
{
2022-08-27 20:06:03 +00:00
factory.registerFunction<FunctionRevision>({}, FunctionFactory::CaseInsensitive);
}
REGISTER_FUNCTION(ZooKeeperSessionUptime)
2021-09-15 21:17:20 +00:00
{
factory.registerFunction<FunctionZooKeeperSessionUptime>();
}
2021-10-09 08:49:49 +00:00
REGISTER_FUNCTION(GetOSKernelVersion)
{
2021-10-09 11:53:21 +00:00
factory.registerFunction<FunctionGetOSKernelVersion>();
2021-10-09 08:49:49 +00:00
}
REGISTER_FUNCTION(DisplayName)
{
factory.registerFunction<FunctionDisplayName>();
}
2021-09-15 21:17:20 +00:00
}