mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-03 13:02:00 +00:00
SQLUserDefinedFunctions added DROP IF EXISTS support
This commit is contained in:
parent
68b29dfe3b
commit
ad409d9b47
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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:
|
||||
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -0,0 +1 @@
|
||||
2
|
@ -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;
|
Loading…
Reference in New Issue
Block a user