Support SQL user defined functions for clickhouse-local

This commit is contained in:
Maksim Kita 2021-10-14 16:15:39 +03:00
parent a163230f37
commit fbaa9e7be4
5 changed files with 28 additions and 4 deletions

View File

@ -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<String, String> prompt_substitutions{{"display_name", server_display_name}};

View File

@ -36,7 +36,7 @@ BlockIO InterpreterCreateFunctionQuery::execute()
UserDefinedSQLFunctionFactory::instance().registerFunction(function_name, query_ptr);
if (!is_internal)
if (!persist_function)
{
try
{

View File

@ -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;
};
}

View File

@ -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_;
}
}

View File

@ -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;
};
}