2021-09-07 23:55:17 +00:00
|
|
|
#include "UserDefinedSQLFunctionFactory.h"
|
2021-08-18 21:54:55 +00:00
|
|
|
|
2021-10-27 15:49:18 +00:00
|
|
|
#include <Common/quoteString.h>
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
2021-10-27 15:49:18 +00:00
|
|
|
#include <Interpreters/UserDefinedSQLObjectsLoader.h>
|
|
|
|
#include <Interpreters/UserDefinedExecutableFunctionFactory.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int FUNCTION_ALREADY_EXISTS;
|
|
|
|
extern const int UNKNOWN_FUNCTION;
|
2021-10-27 15:49:18 +00:00
|
|
|
extern const int CANNOT_DROP_FUNCTION;
|
2021-08-18 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
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-10-27 15:49:18 +00:00
|
|
|
void UserDefinedSQLFunctionFactory::registerFunction(ContextPtr context, const String & function_name, ASTPtr create_function_query, bool replace, bool if_not_exists, bool persist)
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
|
|
|
if (FunctionFactory::instance().hasNameOrAlias(function_name))
|
2021-10-27 15:49:18 +00:00
|
|
|
{
|
|
|
|
if (if_not_exists)
|
|
|
|
return;
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "The function '{}' already exists", function_name);
|
2021-10-27 15:49:18 +00:00
|
|
|
}
|
2021-08-18 21:54:55 +00:00
|
|
|
|
|
|
|
if (AggregateFunctionFactory::instance().hasNameOrAlias(function_name))
|
2021-10-27 15:49:18 +00:00
|
|
|
{
|
|
|
|
if (if_not_exists)
|
|
|
|
return;
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "The aggregate function '{}' already exists", function_name);
|
2021-10-27 15:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UserDefinedExecutableFunctionFactory::instance().has(function_name, context))
|
|
|
|
{
|
|
|
|
if (if_not_exists)
|
|
|
|
return;
|
|
|
|
|
2022-04-28 20:09:29 +00:00
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS, "User defined executable function '{}' already exists", function_name);
|
2021-10-27 15:49:18 +00:00
|
|
|
}
|
2021-08-18 21:54:55 +00:00
|
|
|
|
2021-08-26 21:39:11 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2021-10-20 13:04:02 +00:00
|
|
|
auto [it, inserted] = function_name_to_create_query.emplace(function_name, create_function_query);
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
if (!inserted)
|
2021-10-20 13:04:02 +00:00
|
|
|
{
|
2021-10-27 15:49:18 +00:00
|
|
|
if (if_not_exists)
|
|
|
|
return;
|
|
|
|
|
2021-10-20 13:04:02 +00:00
|
|
|
if (replace)
|
2021-10-27 15:49:18 +00:00
|
|
|
it->second = create_function_query;
|
2021-10-20 13:04:02 +00:00
|
|
|
else
|
|
|
|
throw Exception(ErrorCodes::FUNCTION_ALREADY_EXISTS,
|
|
|
|
"The function name '{}' is not unique",
|
|
|
|
function_name);
|
|
|
|
}
|
2021-10-27 15:49:18 +00:00
|
|
|
|
|
|
|
if (persist)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
UserDefinedSQLObjectsLoader::instance().storeObject(context, UserDefinedSQLObjectType::Function, function_name, *create_function_query, replace);
|
|
|
|
}
|
|
|
|
catch (Exception & exception)
|
|
|
|
{
|
|
|
|
function_name_to_create_query.erase(it);
|
|
|
|
exception.addMessage(fmt::format("while storing user defined function {} on disk", backQuote(function_name)));
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2021-08-18 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 15:49:18 +00:00
|
|
|
void UserDefinedSQLFunctionFactory::unregisterFunction(ContextPtr context, const String & function_name, bool if_exists)
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
|
|
|
if (FunctionFactory::instance().hasNameOrAlias(function_name) ||
|
|
|
|
AggregateFunctionFactory::instance().hasNameOrAlias(function_name))
|
2021-10-27 15:49:18 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_DROP_FUNCTION, "Cannot drop system function '{}'", function_name);
|
|
|
|
|
|
|
|
if (UserDefinedExecutableFunctionFactory::instance().has(function_name, context))
|
|
|
|
throw Exception(ErrorCodes::CANNOT_DROP_FUNCTION, "Cannot drop user defined executable function '{}'", function_name);
|
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())
|
2021-10-27 15:49:18 +00:00
|
|
|
{
|
|
|
|
if (if_exists)
|
|
|
|
return;
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_FUNCTION,
|
|
|
|
"The function name '{}' is not registered",
|
|
|
|
function_name);
|
2021-10-27 15:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
UserDefinedSQLObjectsLoader::instance().removeObject(context, UserDefinedSQLObjectType::Function, function_name);
|
|
|
|
}
|
|
|
|
catch (Exception & exception)
|
|
|
|
{
|
|
|
|
exception.addMessage(fmt::format("while removing user defined function {} from disk", backQuote(function_name)));
|
|
|
|
throw;
|
|
|
|
}
|
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-10-20 10:05:57 +00:00
|
|
|
bool UserDefinedSQLFunctionFactory::has(const String & function_name) const
|
|
|
|
{
|
|
|
|
return tryGet(function_name) != nullptr;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|