2021-09-15 21:17:20 +00:00
|
|
|
#include <Functions/FunctionConstantBase.h>
|
2022-05-31 09:07:27 +00:00
|
|
|
#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>
|
2021-12-21 13:41:53 +00:00
|
|
|
#include <Common/DateLUT.h>
|
2022-06-30 10:58:26 +00:00
|
|
|
#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
|
|
|
|
2022-09-28 08:55:05 +00:00
|
|
|
#include "config_version.h"
|
2021-09-15 21:17:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#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); }
|
2023-07-09 07:20:03 +00:00
|
|
|
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); }
|
2021-10-09 03:47:08 +00:00
|
|
|
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); }
|
2021-10-09 03:47:08 +00:00
|
|
|
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); }
|
2021-10-09 03:47:08 +00:00
|
|
|
explicit FunctionTcpPort(ContextPtr context) : FunctionConstantBase(context->getTCPPort(), context->isDistributed()) {}
|
2021-09-15 21:17:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-05-21 23:00:40 +00:00
|
|
|
/// Returns timezone for current session.
|
2021-09-15 21:17:20 +00:00
|
|
|
class FunctionTimezone : public FunctionConstantBase<FunctionTimezone, String, DataTypeString>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "timezone";
|
|
|
|
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTimezone>(context); }
|
2023-06-16 00:16:04 +00:00
|
|
|
explicit FunctionTimezone(ContextPtr context) : FunctionConstantBase(DateLUT::instance().getTimeZone(), context->isDistributed()) {}
|
2023-02-23 12:38:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns the server time zone (timezone in which server runs).
|
|
|
|
class FunctionServerTimezone : public FunctionConstantBase<FunctionServerTimezone, String, DataTypeString>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "serverTimezone";
|
|
|
|
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionServerTimezone>(context); }
|
2023-06-16 00:16:04 +00:00
|
|
|
explicit FunctionServerTimezone(ContextPtr context) : FunctionConstantBase(DateLUT::serverTimezoneInstance().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); }
|
2021-10-09 03:47:08 +00:00
|
|
|
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); }
|
2021-10-09 03:47:08 +00:00
|
|
|
explicit FunctionVersion(ContextPtr context) : FunctionConstantBase(VERSION_STRING, context->isDistributed()) {}
|
2021-09-15 21:17:20 +00:00
|
|
|
};
|
|
|
|
|
2022-06-30 10:58:26 +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";
|
2021-10-09 03:47:08 +00:00
|
|
|
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";
|
2021-10-10 00:40:51 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-05-31 09:07:27 +00:00
|
|
|
class FunctionDisplayName : public FunctionConstantBase<FunctionDisplayName, String, DataTypeString>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "displayName";
|
|
|
|
explicit FunctionDisplayName(ContextPtr context) : FunctionConstantBase(context->getConfigRef().getString("display_name", getFQDNOrHostName()), context->isDistributed()) {}
|
2022-09-19 08:21:35 +00:00
|
|
|
static FunctionPtr create(ContextPtr context) {return std::make_shared<FunctionDisplayName>(context); }
|
2022-05-31 09:07:27 +00:00
|
|
|
};
|
2021-09-15 21:17:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(OS_FREEBSD)
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(BuildId)
|
|
|
|
{
|
2021-09-15 21:17:20 +00:00
|
|
|
factory.registerFunction<FunctionBuildId>();
|
|
|
|
}
|
2022-07-04 07:01:39 +00:00
|
|
|
#endif
|
2021-09-15 21:17:20 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(HostName)
|
2021-09-15 21:17:20 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionHostName>();
|
|
|
|
factory.registerAlias("hostname", "hostName");
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ServerUUID)
|
2021-09-15 21:17:20 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionServerUUID>();
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(TcpPort)
|
2021-09-15 21:17:20 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionTcpPort>();
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Timezone)
|
2021-09-15 21:17:20 +00:00
|
|
|
{
|
2023-05-20 14:38:45 +00:00
|
|
|
factory.registerFunction<FunctionTimezone>(
|
|
|
|
FunctionDocumentation{
|
|
|
|
.description=R"(
|
2023-02-23 18:14:49 +00:00
|
|
|
Returns the default timezone for current session.
|
|
|
|
Used as default timezone for parsing DateTime|DateTime64 without explicitly specified timezone.
|
|
|
|
Can be changed with SET timezone = 'New/Tz'
|
|
|
|
|
|
|
|
[example:timezone]
|
2023-05-20 14:38:45 +00:00
|
|
|
)",
|
|
|
|
.examples{{"timezone", "SELECT timezone();", ""}},
|
|
|
|
.categories{"Constant", "Miscellaneous"}
|
|
|
|
});
|
|
|
|
factory.registerAlias("timeZone", "timezone");
|
2021-09-15 21:17:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 12:38:13 +00:00
|
|
|
REGISTER_FUNCTION(ServerTimezone)
|
|
|
|
{
|
2023-05-20 14:38:45 +00:00
|
|
|
factory.registerFunction<FunctionServerTimezone>(
|
|
|
|
FunctionDocumentation{
|
|
|
|
.description=R"(
|
2023-02-23 18:14:49 +00:00
|
|
|
Returns the timezone name in which server operates.
|
|
|
|
|
|
|
|
[example:serverTimezone]
|
2023-05-20 14:38:45 +00:00
|
|
|
)",
|
|
|
|
.examples{{"serverTimezone", "SELECT serverTimezone();", ""}},
|
|
|
|
.categories{"Constant", "Miscellaneous"}
|
|
|
|
});
|
2023-06-16 00:16:04 +00:00
|
|
|
factory.registerAlias("serverTimeZone", "serverTimezone");
|
2023-02-23 12:38:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Uptime)
|
2021-09-15 21:17:20 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionUptime>();
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Revision)
|
2022-06-30 10:58:26 +00:00
|
|
|
{
|
2022-08-27 20:06:03 +00:00
|
|
|
factory.registerFunction<FunctionRevision>({}, FunctionFactory::CaseInsensitive);
|
2022-06-30 10:58:26 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ZooKeeperSessionUptime)
|
2021-09-15 21:17:20 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionZooKeeperSessionUptime>();
|
|
|
|
}
|
|
|
|
|
2021-10-09 08:49:49 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(GetOSKernelVersion)
|
|
|
|
{
|
2021-10-09 11:53:21 +00:00
|
|
|
factory.registerFunction<FunctionGetOSKernelVersion>();
|
2021-10-09 08:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-31 09:07:27 +00:00
|
|
|
REGISTER_FUNCTION(DisplayName)
|
|
|
|
{
|
2023-04-28 10:10:42 +00:00
|
|
|
factory.registerFunction<FunctionDisplayName>(FunctionDocumentation
|
2022-09-19 08:21:35 +00:00
|
|
|
{
|
2023-04-28 10:10:42 +00:00
|
|
|
.description=R"(
|
2022-09-19 08:21:35 +00:00
|
|
|
Returns the value of `display_name` from config or server FQDN if not set.
|
|
|
|
|
|
|
|
[example:displayName]
|
|
|
|
)",
|
2023-04-28 10:10:42 +00:00
|
|
|
.examples{{"displayName", "SELECT displayName();", ""}},
|
|
|
|
.categories{"Constant", "Miscellaneous"}
|
2022-09-19 08:21:35 +00:00
|
|
|
},
|
|
|
|
FunctionFactory::CaseSensitive);
|
2022-05-31 09:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-15 21:17:20 +00:00
|
|
|
}
|