mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-15 19:02:04 +00:00
83 lines
2.7 KiB
C++
83 lines
2.7 KiB
C++
|
#include "UserDefinedFunctionFactory.h"
|
||
|
|
||
|
#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;
|
||
|
}
|
||
|
|
||
|
UserDefinedFunctionFactory & UserDefinedFunctionFactory::instance()
|
||
|
{
|
||
|
static UserDefinedFunctionFactory result;
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
void UserDefinedFunctionFactory::registerFunction(const String & function_name, ASTPtr create_function_query)
|
||
|
{
|
||
|
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);
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
void UserDefinedFunctionFactory::unregisterFunction(const String & function_name)
|
||
|
{
|
||
|
if (FunctionFactory::instance().hasNameOrAlias(function_name) ||
|
||
|
AggregateFunctionFactory::instance().hasNameOrAlias(function_name))
|
||
|
throw Exception(ErrorCodes::CANNOT_DROP_SYSTEM_FUNCTION, "Cannot drop system function '{}'", function_name);
|
||
|
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
ASTPtr UserDefinedFunctionFactory::get(const String & function_name) const
|
||
|
{
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
ASTPtr UserDefinedFunctionFactory::tryGet(const std::string & function_name) const
|
||
|
{
|
||
|
auto it = function_name_to_create_query.find(function_name);
|
||
|
if (it == function_name_to_create_query.end())
|
||
|
return nullptr;
|
||
|
|
||
|
return it->second;
|
||
|
}
|
||
|
|
||
|
std::vector<std::string> UserDefinedFunctionFactory::getAllRegisteredNames() const
|
||
|
{
|
||
|
std::vector<std::string> registered_names;
|
||
|
registered_names.reserve(function_name_to_create_query.size());
|
||
|
|
||
|
for (const auto & [name, _] : function_name_to_create_query)
|
||
|
registered_names.emplace_back(name);
|
||
|
|
||
|
return registered_names;
|
||
|
}
|
||
|
|
||
|
}
|