2021-07-19 23:34:04 +00:00
|
|
|
#include <Access/ContextAccess.h>
|
2021-08-23 14:31:58 +00:00
|
|
|
#include <Parsers/ASTCreateFunctionQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2021-07-19 23:34:04 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-05-09 09:47:29 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/InterpreterCreateFunctionQuery.h>
|
|
|
|
#include <Interpreters/FunctionNameNormalizer.h>
|
2021-09-07 23:55:17 +00:00
|
|
|
#include <Interpreters/UserDefinedSQLObjectsLoader.h>
|
|
|
|
#include <Interpreters/UserDefinedSQLFunctionFactory.h>
|
2021-09-16 17:40:42 +00:00
|
|
|
#include <stack>
|
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
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_IDENTIFIER;
|
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()
|
|
|
|
{
|
2021-08-24 16:13:38 +00:00
|
|
|
auto current_context = getContext();
|
|
|
|
current_context->checkAccess(AccessType::CREATE_FUNCTION);
|
2021-08-23 14:31:58 +00:00
|
|
|
|
2021-05-09 09:47:29 +00:00
|
|
|
FunctionNameNormalizer().visit(query_ptr.get());
|
2021-08-18 09:29:52 +00:00
|
|
|
auto * create_function_query = query_ptr->as<ASTCreateFunctionQuery>();
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
if (!create_function_query)
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "Expected CREATE FUNCTION query");
|
2021-08-18 09:29:52 +00:00
|
|
|
|
|
|
|
auto & function_name = create_function_query->function_name;
|
|
|
|
validateFunction(create_function_query->function_core, function_name);
|
|
|
|
|
2021-09-07 23:55:17 +00:00
|
|
|
UserDefinedSQLFunctionFactory::instance().registerFunction(function_name, query_ptr);
|
2021-08-18 21:54:55 +00:00
|
|
|
|
2021-10-14 13:15:39 +00:00
|
|
|
if (!persist_function)
|
2021-07-20 20:20:23 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-09-07 23:55:17 +00:00
|
|
|
UserDefinedSQLObjectsLoader::instance().storeObject(current_context, UserDefinedSQLObjectType::Function, function_name, *query_ptr);
|
2021-07-20 20:20:23 +00:00
|
|
|
}
|
2021-08-23 14:31:58 +00:00
|
|
|
catch (Exception & exception)
|
2021-07-20 20:20:23 +00:00
|
|
|
{
|
2021-09-07 23:55:17 +00:00
|
|
|
UserDefinedSQLFunctionFactory::instance().unregisterFunction(function_name);
|
2021-08-23 14:31:58 +00:00
|
|
|
exception.addMessage(fmt::format("while storing user defined function {} on disk", backQuote(function_name)));
|
2021-07-20 20:20:23 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
const auto * args_tuple = function->as<ASTFunction>()->arguments->children.at(0)->as<ASTFunction>();
|
|
|
|
std::unordered_set<String> arguments;
|
|
|
|
for (const auto & argument : args_tuple->arguments->children)
|
2021-08-18 21:54:55 +00:00
|
|
|
{
|
|
|
|
const auto & argument_name = argument->as<ASTIdentifier>()->name();
|
|
|
|
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
|
|
|
|
|
|
|
ASTPtr function_body = function->as<ASTFunction>()->children.at(0)->children.at(1);
|
2021-08-18 21:54:55 +00:00
|
|
|
std::unordered_set<String> identifiers_in_body = getIdentifiers(function_body);
|
2021-05-10 22:35:22 +00:00
|
|
|
|
|
|
|
for (const auto & identifier : identifiers_in_body)
|
|
|
|
{
|
|
|
|
if (!arguments.contains(identifier))
|
2021-08-18 09:29:52 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_IDENTIFIER, "Identifier {} does not exist in arguments", backQuote(identifier));
|
2021-05-10 22:35:22 +00:00
|
|
|
}
|
2021-07-03 20:17:02 +00:00
|
|
|
|
|
|
|
validateFunctionRecursiveness(function_body, name);
|
2021-05-10 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
std::unordered_set<String> InterpreterCreateFunctionQuery::getIdentifiers(ASTPtr node)
|
2021-05-10 22:35:22 +00:00
|
|
|
{
|
2021-08-18 21:54:55 +00:00
|
|
|
std::unordered_set<String> identifiers;
|
|
|
|
|
|
|
|
std::stack<ASTPtr> ast_nodes_to_process;
|
|
|
|
ast_nodes_to_process.push(node);
|
|
|
|
|
|
|
|
while (!ast_nodes_to_process.empty())
|
2021-05-10 22:35:22 +00:00
|
|
|
{
|
2021-08-18 21:54:55 +00:00
|
|
|
auto ast_node_to_process = ast_nodes_to_process.top();
|
|
|
|
ast_nodes_to_process.pop();
|
|
|
|
|
|
|
|
for (const auto & child : ast_node_to_process->children)
|
|
|
|
{
|
|
|
|
auto identifier_name_opt = tryGetIdentifierName(child);
|
|
|
|
if (identifier_name_opt)
|
|
|
|
identifiers.insert(identifier_name_opt.value());
|
2021-05-10 22:35:22 +00:00
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
ast_nodes_to_process.push(child);
|
|
|
|
}
|
2021-05-10 22:35:22 +00:00
|
|
|
}
|
2021-08-18 21:54:55 +00:00
|
|
|
|
|
|
|
return identifiers;
|
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
|
|
|
}
|