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/InterpreterCreateFunctionQuery.h>
|
|
|
|
#include <Interpreters/FunctionNameNormalizer.h>
|
2021-07-20 20:20:23 +00:00
|
|
|
#include <Interpreters/UserDefinedObjectsOnDisk.h>
|
2021-05-09 09:47:29 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2021-07-03 20:17:02 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
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-05-10 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 09:47:29 +00:00
|
|
|
BlockIO InterpreterCreateFunctionQuery::execute()
|
|
|
|
{
|
2021-07-19 23:34:04 +00:00
|
|
|
getContext()->checkAccess(AccessType::CREATE_FUNCTION);
|
2021-05-09 09:47:29 +00:00
|
|
|
FunctionNameNormalizer().visit(query_ptr.get());
|
|
|
|
auto & create_function_query = query_ptr->as<ASTCreateFunctionQuery &>();
|
2021-07-03 20:17:02 +00:00
|
|
|
validateFunction(create_function_query.function_core, create_function_query.function_name);
|
2021-05-09 09:47:29 +00:00
|
|
|
FunctionFactory::instance().registerUserDefinedFunction(create_function_query);
|
2021-07-20 20:20:23 +00:00
|
|
|
if (!internal)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
UserDefinedObjectsOnDisk::instance().storeUserDefinedFunction(getContext(), create_function_query);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
FunctionFactory::instance().unregisterUserDefinedFunction(create_function_query.function_name);
|
|
|
|
e.addMessage(fmt::format("while storing user defined function {} on disk", backQuote(create_function_query.function_name)));
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
arguments.insert(argument->as<ASTIdentifier>()->name());
|
|
|
|
|
2021-07-03 20:17:02 +00:00
|
|
|
std::set<String> identifiers_in_body;
|
2021-05-10 22:35:22 +00:00
|
|
|
ASTPtr function_body = function->as<ASTFunction>()->children.at(0)->children.at(1);
|
|
|
|
getIdentifiers(function_body, identifiers_in_body);
|
|
|
|
|
|
|
|
for (const auto & identifier : identifiers_in_body)
|
|
|
|
{
|
|
|
|
if (!arguments.contains(identifier))
|
|
|
|
{
|
2021-05-10 23:20:43 +00:00
|
|
|
WriteBufferFromOwnString s;
|
2021-05-10 22:35:22 +00:00
|
|
|
s << "Identifier '" << identifier << "' does not exist in arguments";
|
|
|
|
throw Exception(s.str(), ErrorCodes::UNKNOWN_IDENTIFIER);
|
|
|
|
}
|
|
|
|
}
|
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::getIdentifiers(ASTPtr node, std::set<String> & identifiers)
|
2021-05-10 22:35:22 +00:00
|
|
|
{
|
|
|
|
for (const auto & child : node->children)
|
|
|
|
{
|
|
|
|
auto identifier_name_opt = tryGetIdentifierName(child);
|
|
|
|
if (identifier_name_opt)
|
2021-07-03 20:17:02 +00:00
|
|
|
identifiers.insert(identifier_name_opt.value());
|
2021-05-10 22:35:22 +00:00
|
|
|
|
|
|
|
getIdentifiers(child, identifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
throw Exception("You cannot create recursive function", ErrorCodes::CANNOT_CREATE_RECURSIVE_FUNCTION);
|
|
|
|
|
|
|
|
validateFunctionRecursiveness(child, function_to_create);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:20:23 +00:00
|
|
|
void InterpreterCreateFunctionQuery::setInternal(bool internal_)
|
|
|
|
{
|
|
|
|
internal = internal_;
|
|
|
|
}
|
2021-07-03 20:17:02 +00:00
|
|
|
|
2021-05-09 09:47:29 +00:00
|
|
|
}
|