2021-10-20 09:43:03 +00:00
|
|
|
#include <Interpreters/InterpreterCreateFunctionQuery.h>
|
|
|
|
|
2021-07-19 23:34:04 +00:00
|
|
|
#include <Access/ContextAccess.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2021-05-09 09:47:29 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/FunctionNameNormalizer.h>
|
2021-09-07 23:55:17 +00:00
|
|
|
#include <Interpreters/UserDefinedSQLFunctionFactory.h>
|
2021-11-26 17:21:54 +00:00
|
|
|
#include <Interpreters/UserDefinedSQLObjectsLoader.h>
|
2021-10-27 08:28:21 +00:00
|
|
|
#include <Interpreters/executeDDLQueryOnCluster.h>
|
2021-11-26 17:21:54 +00:00
|
|
|
#include <Parsers/ASTCreateFunctionQuery.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2021-10-20 09:43:03 +00:00
|
|
|
|
2021-08-23 14:31:58 +00:00
|
|
|
|
2021-05-09 09:47:29 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-05-10 22:35:22 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2021-07-03 20:17:02 +00:00
|
|
|
extern const int CANNOT_CREATE_RECURSIVE_FUNCTION;
|
2021-08-18 21:54:55 +00:00
|
|
|
extern const int UNSUPPORTED_METHOD;
|
2021-05-10 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 09:47:29 +00:00
|
|
|
BlockIO InterpreterCreateFunctionQuery::execute()
|
|
|
|
{
|
|
|
|
FunctionNameNormalizer().visit(query_ptr.get());
|
2021-10-27 15:49:18 +00:00
|
|
|
ASTCreateFunctionQuery & create_function_query = query_ptr->as<ASTCreateFunctionQuery &>();
|
2021-08-18 09:29:52 +00:00
|
|
|
|
2021-10-27 08:28:21 +00:00
|
|
|
AccessRightsElements access_rights_elements;
|
|
|
|
access_rights_elements.emplace_back(AccessType::CREATE_FUNCTION);
|
|
|
|
|
2021-10-27 15:49:18 +00:00
|
|
|
if (create_function_query.or_replace)
|
|
|
|
access_rights_elements.emplace_back(AccessType::DROP_FUNCTION);
|
|
|
|
|
|
|
|
if (!create_function_query.cluster.empty())
|
2021-10-27 08:28:21 +00:00
|
|
|
return executeDDLQueryOnCluster(query_ptr, getContext(), access_rights_elements);
|
|
|
|
|
|
|
|
auto current_context = getContext();
|
|
|
|
current_context->checkAccess(access_rights_elements);
|
|
|
|
|
2021-10-20 13:04:02 +00:00
|
|
|
auto & user_defined_function_factory = UserDefinedSQLFunctionFactory::instance();
|
|
|
|
|
2022-01-20 08:23:52 +00:00
|
|
|
auto function_name = create_function_query.getFunctionName();
|
2021-10-20 13:04:02 +00:00
|
|
|
|
2021-10-27 15:49:18 +00:00
|
|
|
bool if_not_exists = create_function_query.if_not_exists;
|
|
|
|
bool replace = create_function_query.or_replace;
|
2021-08-18 09:29:52 +00:00
|
|
|
|
2021-10-27 15:49:18 +00:00
|
|
|
create_function_query.if_not_exists = false;
|
|
|
|
create_function_query.or_replace = false;
|
2021-08-18 21:54:55 +00:00
|
|
|
|
2021-10-27 15:49:18 +00:00
|
|
|
validateFunction(create_function_query.function_core, function_name);
|
|
|
|
user_defined_function_factory.registerFunction(current_context, function_name, query_ptr, replace, if_not_exists, persist_function);
|
2021-08-18 09:29:52 +00:00
|
|
|
|
2021-05-09 09:47:29 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-07-03 20:17:02 +00:00
|
|
|
void InterpreterCreateFunctionQuery::validateFunction(ASTPtr function, const String & name)
|
2021-05-10 22:35:22 +00:00
|
|
|
{
|
2021-12-14 15:46:21 +00:00
|
|
|
auto & lambda_function = function->as<ASTFunction &>();
|
|
|
|
auto & lambda_function_expression_list = lambda_function.arguments->children;
|
|
|
|
|
|
|
|
const auto & tuple_function_arguments = lambda_function_expression_list.at(0)->as<ASTFunction &>();
|
|
|
|
|
2021-05-10 22:35:22 +00:00
|
|
|
std::unordered_set<String> arguments;
|
2021-12-14 15:46:21 +00:00
|
|
|
|
|
|
|
for (const auto & argument : tuple_function_arguments.arguments->children)
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
2022-01-21 14:33:57 +00:00
|
|
|
const auto * argument_identifier = argument->as<ASTIdentifier>();
|
|
|
|
|
|
|
|
if (!argument_identifier)
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Lambda argument must be identifier");
|
|
|
|
|
|
|
|
const auto & argument_name = argument_identifier->name();
|
2021-08-18 21:54:55 +00:00
|
|
|
auto [_, inserted] = arguments.insert(argument_name);
|
|
|
|
if (!inserted)
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Identifier {} already used as function parameter", argument_name);
|
|
|
|
}
|
2021-05-10 22:35:22 +00:00
|
|
|
|
2021-12-14 15:46:21 +00:00
|
|
|
ASTPtr function_body = lambda_function_expression_list.at(1);
|
2021-07-03 20:17:02 +00:00
|
|
|
validateFunctionRecursiveness(function_body, name);
|
2021-05-10 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 20:17:02 +00:00
|
|
|
void InterpreterCreateFunctionQuery::validateFunctionRecursiveness(ASTPtr node, const String & function_to_create)
|
|
|
|
{
|
|
|
|
for (const auto & child : node->children)
|
|
|
|
{
|
|
|
|
auto function_name_opt = tryGetFunctionName(child);
|
|
|
|
if (function_name_opt && function_name_opt.value() == function_to_create)
|
2021-08-18 09:29:52 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_CREATE_RECURSIVE_FUNCTION, "You cannot create recursive function");
|
2021-07-03 20:17:02 +00:00
|
|
|
|
|
|
|
validateFunctionRecursiveness(child, function_to_create);
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 09:47:29 +00:00
|
|
|
}
|