2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2021-05-09 09:47:29 +00:00
|
|
|
#include <Functions/UserDefinedFunction.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>
|
2021-01-22 09:02:58 +00:00
|
|
|
#include <Common/CurrentThread.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>
|
|
|
|
|
2020-12-16 21:42:14 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
|
2021-01-22 09:02:58 +00:00
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-05-10 22:35:22 +00:00
|
|
|
class UserDefinedFunction;
|
|
|
|
|
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;
|
2021-05-13 21:41:35 +00:00
|
|
|
extern const int FUNCTION_ALREADY_EXISTS;
|
2021-05-14 22:43:04 +00:00
|
|
|
extern const int CANNOT_DROP_SYSTEM_FUNCTION;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 11:09:36 +00:00
|
|
|
const String & getFunctionCanonicalNameIfAny(const String & name)
|
|
|
|
{
|
|
|
|
return FunctionFactory::instance().getCanonicalNameIfAny(name);
|
|
|
|
}
|
2017-08-14 04:23:38 +00:00
|
|
|
|
2021-07-03 21:47:43 +00:00
|
|
|
void FunctionFactory::registerFunction(
|
|
|
|
const std::string & name,
|
2020-06-14 07:44:02 +00:00
|
|
|
Value creator,
|
2019-12-12 14:16:59 +00:00
|
|
|
CaseSensitiveness case_sensitiveness)
|
2014-08-22 00:57:20 +00:00
|
|
|
{
|
2021-07-03 21:47:43 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
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);
|
|
|
|
|
2021-02-14 11:09:36 +00:00
|
|
|
if (case_sensitiveness == CaseInsensitive)
|
|
|
|
{
|
|
|
|
if (!case_insensitive_functions.emplace(function_name_lowercase, creator).second)
|
|
|
|
throw Exception("FunctionFactory: the case insensitive function name '" + name + "' is not unique",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
case_insensitive_name_mapping[function_name_lowercase] = name;
|
|
|
|
}
|
2014-08-22 00:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionOverloadResolverPtr FunctionFactory::getImpl(
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & name,
|
2021-07-03 20:17:02 +00:00
|
|
|
ContextConstPtr 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
|
|
|
{
|
2020-12-16 21:42:14 +00:00
|
|
|
String extra_info;
|
|
|
|
if (AggregateFunctionFactory::instance().hasNameOrAlias(name))
|
|
|
|
extra_info = ". There is an aggregate function with the same name, but ordinary function is expected here";
|
|
|
|
|
2019-02-02 14:54:50 +00:00
|
|
|
auto hints = this->getHints(name);
|
|
|
|
if (!hints.empty())
|
2020-12-16 21:42:14 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}{}. Maybe you meant: {}", name, extra_info, toString(hints));
|
2019-02-02 14:54:50 +00:00
|
|
|
else
|
2020-12-16 21:42:14 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}{}", name, extra_info);
|
2019-02-02 14:54:50 +00:00
|
|
|
}
|
2021-01-20 10:54:11 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-11-29 08:06:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:21:40 +00:00
|
|
|
std::vector<std::string> FunctionFactory::getAllNames() const
|
|
|
|
{
|
2021-07-03 21:47:43 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2020-05-11 21:18:50 +00:00
|
|
|
std::vector<std::string> res;
|
|
|
|
res.reserve(functions.size());
|
2020-05-11 21:21:40 +00:00
|
|
|
for (const auto & func : functions)
|
2020-05-11 21:18:50 +00:00
|
|
|
res.emplace_back(func.first);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionOverloadResolverPtr FunctionFactory::get(
|
|
|
|
const std::string & name,
|
2021-07-03 20:17:02 +00:00
|
|
|
ContextConstPtr context) const
|
2019-12-09 14:41:55 +00:00
|
|
|
{
|
2021-05-15 17:33:15 +00:00
|
|
|
return getImpl(name, context);
|
2019-12-09 14:41:55 +00:00
|
|
|
}
|
2015-11-29 08:06:29 +00:00
|
|
|
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionOverloadResolverPtr FunctionFactory::tryGetImpl(
|
2018-07-25 16:08:23 +00:00
|
|
|
const std::string & name_param,
|
2021-07-03 20:17:02 +00:00
|
|
|
ContextConstPtr context) const
|
2012-12-18 20:06:36 +00:00
|
|
|
{
|
2021-07-03 21:47:43 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2018-07-25 16:08:23 +00:00
|
|
|
String name = getAliasToOrName(name_param);
|
2021-05-15 17:33:15 +00:00
|
|
|
FunctionOverloadResolverPtr res;
|
2021-01-23 10:18:40 +00:00
|
|
|
|
|
|
|
auto it = functions.find(name);
|
|
|
|
if (functions.end() != it)
|
|
|
|
res = it->second(context);
|
|
|
|
else
|
|
|
|
{
|
2021-01-28 09:22:01 +00:00
|
|
|
name = Poco::toLower(name);
|
|
|
|
it = case_insensitive_functions.find(name);
|
2021-01-23 10:18:40 +00:00
|
|
|
if (case_insensitive_functions.end() != it)
|
|
|
|
res = it->second(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
return nullptr;
|
2018-07-25 16:08:23 +00:00
|
|
|
|
2021-01-22 11:58:29 +00:00
|
|
|
if (CurrentThread::isInitialized())
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
auto query_context = CurrentThread::get().getQueryContext();
|
2021-01-22 11:58:29 +00:00
|
|
|
if (query_context && query_context->getSettingsRef().log_queries)
|
|
|
|
query_context->addQueryFactoriesInfo(Context::QueryLogFactories::Function, name);
|
|
|
|
}
|
|
|
|
|
2021-01-23 10:18:40 +00:00
|
|
|
return res;
|
2012-12-18 20:06:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionOverloadResolverPtr FunctionFactory::tryGet(
|
|
|
|
const std::string & name,
|
2021-07-03 20:17:02 +00:00
|
|
|
ContextConstPtr context) const
|
2019-12-09 14:41:55 +00:00
|
|
|
{
|
|
|
|
auto impl = tryGetImpl(name, context);
|
2021-05-15 17:33:15 +00:00
|
|
|
return impl ? std::move(impl) : nullptr;
|
2019-12-09 14:41:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
FunctionFactory & FunctionFactory::instance()
|
|
|
|
{
|
|
|
|
static FunctionFactory ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-05-14 22:43:04 +00:00
|
|
|
void FunctionFactory::registerUserDefinedFunction(const ASTCreateFunctionQuery & create_function_query)
|
2021-05-09 09:47:29 +00:00
|
|
|
{
|
2021-05-13 21:41:35 +00:00
|
|
|
if (hasNameOrAlias(create_function_query.function_name))
|
2021-05-14 22:43:04 +00:00
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "The function {} already exists", create_function_query.function_name);
|
2021-05-13 21:41:35 +00:00
|
|
|
|
2021-07-03 20:17:02 +00:00
|
|
|
if (AggregateFunctionFactory::instance().isAggregateFunctionName(create_function_query.function_name))
|
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "The aggregate function {} already exists", create_function_query.function_name);
|
|
|
|
|
2021-07-03 21:47:43 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
|
|
|
user_defined_functions.insert(create_function_query.function_name);
|
|
|
|
}
|
2021-05-09 09:47:29 +00:00
|
|
|
registerFunction(create_function_query.function_name, [create_function_query](ContextPtr context)
|
|
|
|
{
|
|
|
|
auto function = UserDefinedFunction::create(context);
|
|
|
|
function->setName(create_function_query.function_name);
|
|
|
|
function->setFunctionCore(create_function_query.function_core);
|
|
|
|
|
2021-06-14 23:07:20 +00:00
|
|
|
FunctionOverloadResolverPtr res = std::make_unique<FunctionToOverloadResolverAdaptor>(function);
|
2021-05-09 09:47:29 +00:00
|
|
|
return res;
|
2021-05-14 22:43:04 +00:00
|
|
|
}, CaseSensitiveness::CaseSensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionFactory::unregisterUserDefinedFunction(const String & name)
|
|
|
|
{
|
2021-07-03 21:47:43 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2021-05-14 22:43:04 +00:00
|
|
|
if (functions.contains(name))
|
|
|
|
{
|
|
|
|
if (user_defined_functions.contains(name))
|
|
|
|
{
|
|
|
|
functions.erase(name);
|
|
|
|
user_defined_functions.erase(name);
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
throw Exception("System functions cannot be dropped", ErrorCodes::CANNOT_DROP_SYSTEM_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hints = this->getHints(name);
|
|
|
|
if (!hints.empty())
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}. Maybe you meant: {}", name, toString(hints));
|
|
|
|
else
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION, "Unknown function {}", name);
|
2021-05-09 09:47:29 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 20:06:36 +00:00
|
|
|
}
|