2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2017-08-18 17:06:22 +00:00
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2012-12-18 20:06:36 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
#include <Poco/String.h>
|
|
|
|
|
2019-02-02 14:54:50 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2019-12-12 14:16:59 +00:00
|
|
|
extern const int UNKNOWN_FUNCTION;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
|
2017-08-18 17:06:22 +00:00
|
|
|
void FunctionFactory::registerFunction(const
|
2019-12-12 14:16:59 +00:00
|
|
|
std::string & name,
|
|
|
|
Creator creator,
|
|
|
|
CaseSensitiveness case_sensitiveness)
|
2014-08-22 00:57:20 +00:00
|
|
|
{
|
2017-08-14 04:23:38 +00:00
|
|
|
if (!functions.emplace(name, creator).second)
|
|
|
|
throw Exception("FunctionFactory: the function name '" + name + "' is not unique",
|
2019-12-09 14:41:55 +00:00
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
2017-08-14 04:23:38 +00:00
|
|
|
|
2018-07-25 16:08:23 +00:00
|
|
|
String function_name_lowercase = Poco::toLower(name);
|
|
|
|
if (isAlias(name) || isAlias(function_name_lowercase))
|
|
|
|
throw Exception("FunctionFactory: the function name '" + name + "' is already registered as alias",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2017-08-14 04:23:38 +00:00
|
|
|
if (case_sensitiveness == CaseInsensitive
|
2018-07-25 16:08:23 +00:00
|
|
|
&& !case_insensitive_functions.emplace(function_name_lowercase, creator).second)
|
2017-08-14 04:23:38 +00:00
|
|
|
throw Exception("FunctionFactory: the case insensitive function name '" + name + "' is not unique",
|
2019-12-09 14:41:55 +00:00
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
2014-08-22 00:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionOverloadResolverImplPtr FunctionFactory::getImpl(
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & name,
|
|
|
|
const Context & context) const
|
2015-11-29 08:06:29 +00:00
|
|
|
{
|
2019-12-09 14:41:55 +00:00
|
|
|
auto res = tryGetImpl(name, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
2019-02-02 14:54:50 +00:00
|
|
|
{
|
|
|
|
auto hints = this->getHints(name);
|
|
|
|
if (!hints.empty())
|
2019-12-09 14:41:55 +00:00
|
|
|
throw Exception("Unknown function " + name + ". Maybe you meant: " + toString(hints),
|
|
|
|
ErrorCodes::UNKNOWN_FUNCTION);
|
2019-02-02 14:54:50 +00:00
|
|
|
else
|
|
|
|
throw Exception("Unknown function " + name, ErrorCodes::UNKNOWN_FUNCTION);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-11-29 08:06:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:18:50 +00:00
|
|
|
std::vector<std::string> FunctionFactory::getAllNames() const {
|
|
|
|
std::vector<std::string> res;
|
|
|
|
res.reserve(functions.size());
|
|
|
|
for (const auto& func : functions) {
|
|
|
|
res.emplace_back(func.first);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionOverloadResolverPtr FunctionFactory::get(
|
|
|
|
const std::string & name,
|
|
|
|
const Context & context) const
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionOverloadResolverAdaptor>(getImpl(name, context));
|
|
|
|
}
|
2015-11-29 08:06:29 +00:00
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionOverloadResolverImplPtr FunctionFactory::tryGetImpl(
|
2018-07-25 16:08:23 +00:00
|
|
|
const std::string & name_param,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context) const
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2018-07-25 16:08:23 +00:00
|
|
|
String name = getAliasToOrName(name_param);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto it = functions.find(name);
|
|
|
|
if (functions.end() != it)
|
|
|
|
return it->second(context);
|
2017-08-14 04:23:38 +00:00
|
|
|
|
|
|
|
it = case_insensitive_functions.find(Poco::toLower(name));
|
|
|
|
if (case_insensitive_functions.end() != it)
|
|
|
|
return it->second(context);
|
|
|
|
|
|
|
|
return {};
|
2012-12-18 20:06:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionOverloadResolverPtr FunctionFactory::tryGet(
|
|
|
|
const std::string & name,
|
|
|
|
const Context & context) const
|
|
|
|
{
|
|
|
|
auto impl = tryGetImpl(name, context);
|
|
|
|
return impl ? std::make_shared<FunctionOverloadResolverAdaptor>(std::move(impl))
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
FunctionFactory & FunctionFactory::instance()
|
|
|
|
{
|
|
|
|
static FunctionFactory ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
}
|