From fbaa9e7be450f3363f543b41b9a5c8a06d770be3 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Thu, 14 Oct 2021 16:15:39 +0300 Subject: [PATCH] Support SQL user defined functions for clickhouse-local --- programs/local/LocalServer.cpp | 6 ++++++ .../InterpreterCreateFunctionQuery.cpp | 2 +- src/Interpreters/InterpreterCreateFunctionQuery.h | 6 +++--- src/Interpreters/UserDefinedSQLObjectsLoader.cpp | 14 ++++++++++++++ src/Interpreters/UserDefinedSQLObjectsLoader.h | 4 ++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/programs/local/LocalServer.cpp b/programs/local/LocalServer.cpp index 8066650006a..92bc51a48da 100644 --- a/programs/local/LocalServer.cpp +++ b/programs/local/LocalServer.cpp @@ -565,6 +565,8 @@ void LocalServer::processConfig() global_context->setCurrentDatabase(default_database); applyCmdOptions(global_context); + bool enable_objects_loader = false; + if (config().has("path")) { String path = global_context->getPath(); @@ -576,6 +578,7 @@ void LocalServer::processConfig() LOG_DEBUG(log, "Loading user defined objects from {}", path); Poco::File(path + "user_defined/").createDirectories(); UserDefinedSQLObjectsLoader::instance().loadObjects(global_context); + enable_objects_loader = true; LOG_DEBUG(log, "Loaded user defined objects."); LOG_DEBUG(log, "Loading metadata from {}", path); @@ -599,6 +602,9 @@ void LocalServer::processConfig() attachInformationSchema(global_context, *createMemoryDatabaseIfNotExists(global_context, DatabaseCatalog::INFORMATION_SCHEMA_UPPERCASE)); } + /// Persist SQL user defined objects only if user_defined folder was created + UserDefinedSQLObjectsLoader::instance().enable(enable_objects_loader); + server_display_name = config().getString("display_name", getFQDNOrHostName()); prompt_by_server_display_name = config().getRawString("prompt_by_server_display_name.default", "{display_name} :) "); std::map prompt_substitutions{{"display_name", server_display_name}}; diff --git a/src/Interpreters/InterpreterCreateFunctionQuery.cpp b/src/Interpreters/InterpreterCreateFunctionQuery.cpp index b7ed3a2fe75..ccb5f4040ec 100644 --- a/src/Interpreters/InterpreterCreateFunctionQuery.cpp +++ b/src/Interpreters/InterpreterCreateFunctionQuery.cpp @@ -36,7 +36,7 @@ BlockIO InterpreterCreateFunctionQuery::execute() UserDefinedSQLFunctionFactory::instance().registerFunction(function_name, query_ptr); - if (!is_internal) + if (!persist_function) { try { diff --git a/src/Interpreters/InterpreterCreateFunctionQuery.h b/src/Interpreters/InterpreterCreateFunctionQuery.h index b10760c5e9d..fdc03b379db 100644 --- a/src/Interpreters/InterpreterCreateFunctionQuery.h +++ b/src/Interpreters/InterpreterCreateFunctionQuery.h @@ -11,10 +11,10 @@ class Context; class InterpreterCreateFunctionQuery : public IInterpreter, WithContext { public: - InterpreterCreateFunctionQuery(const ASTPtr & query_ptr_, ContextPtr context_, bool is_internal_) + InterpreterCreateFunctionQuery(const ASTPtr & query_ptr_, ContextPtr context_, bool persist_function_) : WithContext(context_) , query_ptr(query_ptr_) - , is_internal(is_internal_) {} + , persist_function(persist_function_) {} BlockIO execute() override; @@ -26,7 +26,7 @@ private: static void validateFunctionRecursiveness(ASTPtr node, const String & function_to_create); ASTPtr query_ptr; - bool is_internal; + bool persist_function; }; } diff --git a/src/Interpreters/UserDefinedSQLObjectsLoader.cpp b/src/Interpreters/UserDefinedSQLObjectsLoader.cpp index 455c73bf685..e4eb97f3002 100644 --- a/src/Interpreters/UserDefinedSQLObjectsLoader.cpp +++ b/src/Interpreters/UserDefinedSQLObjectsLoader.cpp @@ -83,6 +83,9 @@ void UserDefinedSQLObjectsLoader::loadUserDefinedObject(ContextPtr context, User void UserDefinedSQLObjectsLoader::loadObjects(ContextPtr context) { + if (unlikely(!enable_persistence)) + return; + LOG_DEBUG(log, "loading user defined objects"); String dir_path = context->getPath() + "user_defined/"; @@ -110,6 +113,9 @@ void UserDefinedSQLObjectsLoader::loadObjects(ContextPtr context) void UserDefinedSQLObjectsLoader::storeObject(ContextPtr context, UserDefinedSQLObjectType object_type, const String & object_name, const IAST & ast) { + if (unlikely(!enable_persistence)) + return; + String dir_path = context->getPath() + "user_defined/"; String file_path; @@ -143,6 +149,9 @@ void UserDefinedSQLObjectsLoader::storeObject(ContextPtr context, UserDefinedSQL void UserDefinedSQLObjectsLoader::removeObject(ContextPtr context, UserDefinedSQLObjectType object_type, const String & object_name) { + if (unlikely(!enable_persistence)) + return; + String dir_path = context->getPath() + "user_defined/"; LOG_DEBUG(log, "Removing file for user defined object {} from {}", backQuote(object_name), dir_path); @@ -162,4 +171,9 @@ void UserDefinedSQLObjectsLoader::removeObject(ContextPtr context, UserDefinedSQ std::filesystem::remove(file_path); } +void UserDefinedSQLObjectsLoader::enable(bool enable_persistence_) +{ + enable_persistence = enable_persistence_; +} + } diff --git a/src/Interpreters/UserDefinedSQLObjectsLoader.h b/src/Interpreters/UserDefinedSQLObjectsLoader.h index 6daf3e99833..17493933f21 100644 --- a/src/Interpreters/UserDefinedSQLObjectsLoader.h +++ b/src/Interpreters/UserDefinedSQLObjectsLoader.h @@ -24,10 +24,14 @@ public: void storeObject(ContextPtr context, UserDefinedSQLObjectType object_type, const String & object_name, const IAST & ast); void removeObject(ContextPtr context, UserDefinedSQLObjectType object_type, const String & object_name); + /// For ClickHouse local if path is not set we can disable loader. + void enable(bool enable_persistence); + private: void loadUserDefinedObject(ContextPtr context, UserDefinedSQLObjectType object_type, const std::string_view & object_name, const String & file_path); Poco::Logger * log; + bool enable_persistence = true; }; }