ClickHouse/dbms/src/Functions/FunctionFactory.cpp

77 lines
2.0 KiB
C++
Raw Normal View History

#include <Functions/FunctionFactory.h>
#include <Interpreters/Context.h>
#include <Common/Exception.h>
#include <Poco/String.h>
#include <IO/WriteHelpers.h>
namespace DB
{
2016-01-12 02:21:15 +00:00
namespace ErrorCodes
{
extern const int UNKNOWN_FUNCTION;
2017-08-18 16:36:02 +00:00
extern const int LOGICAL_ERROR;
2016-01-12 02:21:15 +00:00
}
2017-08-14 04:23:38 +00:00
void FunctionFactory::registerFunction(const
std::string & name,
Creator creator,
CaseSensitiveness case_sensitiveness)
{
2017-08-14 04:23:38 +00:00
if (!functions.emplace(name, creator).second)
throw Exception("FunctionFactory: the function name '" + name + "' is not unique",
ErrorCodes::LOGICAL_ERROR);
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
&& !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",
ErrorCodes::LOGICAL_ERROR);
}
2018-02-02 08:33:36 +00:00
FunctionBuilderPtr FunctionFactory::get(
const std::string & name,
const Context & context) const
{
auto res = tryGet(name, context);
if (!res)
{
auto hints = this->getHints(name);
if (!hints.empty())
throw Exception("Unknown function " + name + ". Maybe you meant: " + toString(hints), ErrorCodes::UNKNOWN_FUNCTION);
else
throw Exception("Unknown function " + name, ErrorCodes::UNKNOWN_FUNCTION);
}
return res;
}
2018-02-02 08:33:36 +00:00
FunctionBuilderPtr FunctionFactory::tryGet(
const std::string & name_param,
const Context & context) const
{
String name = getAliasToOrName(name_param);
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 {};
}
}