2021-09-07 23:55:17 +00:00
|
|
|
#include "UserDefinedSQLFunctionFactory.h"
|
2021-08-18 21:54:55 +00:00
|
|
|
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int FUNCTION_ALREADY_EXISTS;
|
|
|
|
extern const int UNKNOWN_FUNCTION;
|
|
|
|
extern const int CANNOT_DROP_SYSTEM_FUNCTION;
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
UserDefinedSQLFunctionFactory & UserDefinedSQLFunctionFactory::instance()
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
2021-09-07 23:55:17 +00:00
|
|
|
static UserDefinedSQLFunctionFactory result;
|
2021-08-18 21:54:55 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
void UserDefinedSQLFunctionFactory::registerFunction(const String & function_name, ASTPtr create_function_query)
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
|
|
|
if (FunctionFactory::instance().hasNameOrAlias(function_name))
|
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "The function '{}' already exists", function_name);
|
|
|
|
|
|
|
|
if (AggregateFunctionFactory::instance().hasNameOrAlias(function_name))
|
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "The aggregate function '{}' already exists", function_name);
|
|
|
|
|
2021-08-26 21:39:11 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
auto [_, inserted] = function_name_to_create_query.emplace(function_name, std::move(create_function_query));
|
|
|
|
if (!inserted)
|
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS,
|
|
|
|
"The function name '{}' is not unique",
|
|
|
|
function_name);
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
void UserDefinedSQLFunctionFactory::unregisterFunction(const String & function_name)
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
|
|
|
if (FunctionFactory::instance().hasNameOrAlias(function_name) ||
|
|
|
|
AggregateFunctionFactory::instance().hasNameOrAlias(function_name))
|
|
|
|
throw Exception(ErrorCodes::CANNOT_DROP_SYSTEM_FUNCTION, "Cannot drop system function '{}'", function_name);
|
|
|
|
|
2021-08-26 21:39:11 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
auto it = function_name_to_create_query.find(function_name);
|
|
|
|
if (it == function_name_to_create_query.end())
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION,
|
|
|
|
"The function name '{}' is not registered",
|
|
|
|
function_name);
|
2021-08-23 14:31:58 +00:00
|
|
|
|
|
|
|
function_name_to_create_query.erase(it);
|
2021-08-18 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
ASTPtr UserDefinedSQLFunctionFactory::get(const String & function_name) const
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
2021-08-26 21:39:11 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
auto it = function_name_to_create_query.find(function_name);
|
|
|
|
if (it == function_name_to_create_query.end())
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION,
|
|
|
|
"The function name '{}' is not registered",
|
|
|
|
function_name);
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
ASTPtr UserDefinedSQLFunctionFactory::tryGet(const std::string & function_name) const
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
2021-08-26 21:39:11 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
auto it = function_name_to_create_query.find(function_name);
|
|
|
|
if (it == function_name_to_create_query.end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
std::vector<std::string> UserDefinedSQLFunctionFactory::getAllRegisteredNames() const
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> registered_names;
|
|
|
|
|
2021-08-26 21:39:11 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2021-09-01 07:38:46 +00:00
|
|
|
registered_names.reserve(function_name_to_create_query.size());
|
2021-08-26 21:39:11 +00:00
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
for (const auto & [name, _] : function_name_to_create_query)
|
|
|
|
registered_names.emplace_back(name);
|
|
|
|
|
|
|
|
return registered_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|