SQLUserDefinedFunctions added DROP IF EXISTS support

This commit is contained in:
Maksim Kita 2021-10-20 13:05:57 +03:00
parent 68b29dfe3b
commit ad409d9b47
8 changed files with 33 additions and 0 deletions

View File

@ -18,6 +18,11 @@ BlockIO InterpreterDropFunctionQuery::execute()
FunctionNameNormalizer().visit(query_ptr.get());
auto & drop_function_query = query_ptr->as<ASTDropFunctionQuery &>();
auto & user_defined_functions_factory = UserDefinedSQLFunctionFactory::instance();
if (drop_function_query.if_exists && !user_defined_functions_factory.has(drop_function_query.function_name))
return {};
UserDefinedSQLFunctionFactory::instance().unregisterFunction(drop_function_query.function_name);
UserDefinedSQLObjectsLoader::instance().removeObject(current_context, UserDefinedSQLObjectType::Function, drop_function_query.function_name);

View File

@ -77,6 +77,11 @@ ASTPtr UserDefinedSQLFunctionFactory::tryGet(const std::string & function_name)
return it->second;
}
bool UserDefinedSQLFunctionFactory::has(const String & function_name) const
{
return tryGet(function_name) != nullptr;
}
std::vector<std::string> UserDefinedSQLFunctionFactory::getAllRegisteredNames() const
{
std::vector<std::string> registered_names;

View File

@ -23,6 +23,8 @@ public:
ASTPtr tryGet(const String & function_name) const;
bool has(const String & function_name) const;
std::vector<String> getAllRegisteredNames() const override;
private:

View File

@ -14,6 +14,8 @@ void ASTDropFunctionQuery::formatImpl(const IAST::FormatSettings & settings, IAS
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "DROP FUNCTION " << (settings.hilite ? hilite_none : "");
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(function_name) << (settings.hilite ? hilite_none : "");
if (if_exists)
settings.ostr << "IF EXISTS";
}
}

View File

@ -10,6 +10,8 @@ class ASTDropFunctionQuery : public IAST
public:
String function_name;
bool if_exists = false;
String getID(char) const override { return "DropFunctionQuery"; }
ASTPtr clone() const override;

View File

@ -11,7 +11,10 @@ bool ParserDropFunctionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expec
{
ParserKeyword s_drop("DROP");
ParserKeyword s_function("FUNCTION");
ParserKeyword s_if_exists("IF EXISTS");
ParserIdentifier function_name_p;
bool if_exists = false;
ASTPtr function_name;
@ -21,10 +24,14 @@ bool ParserDropFunctionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expec
if (!s_function.ignore(pos, expected))
return false;
if (s_if_exists.ignore(pos, expected))
if_exists = true;
if (!function_name_p.parse(pos, function_name, expected))
return false;
auto drop_function_query = std::make_shared<ASTDropFunctionQuery>();
drop_function_query->if_exists = if_exists;
node = drop_function_query;
drop_function_query->function_name = function_name->as<ASTIdentifier &>().name();

View File

@ -0,0 +1,9 @@
-- Tags: no-parallel
CREATE FUNCTION 02101_test_function AS x -> x + 1;
SELECT 02101_test_function(1);
DROP FUNCTION 02101_test_function;
DROP FUNCTION 02101_test_function; --{serverError 46}
DROP FUNCTION IF EXISTS 02101_test_function;