Skip to content

Commit 2e59d67

Browse files
committed
Split UserDefinedSQLObjectsLoader to interface and implementation.
1 parent 9cb2052 commit 2e59d67

36 files changed

+995
-667
lines changed

programs/local/LocalServer.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <Interpreters/DatabaseCatalog.h>
1414
#include <base/getFQDNOrHostName.h>
1515
#include <Common/scope_guard_safe.h>
16-
#include <Interpreters/UserDefinedSQLObjectsLoader.h>
1716
#include <Interpreters/Session.h>
1817
#include <Access/AccessControl.h>
1918
#include <Common/Exception.h>
@@ -32,6 +31,7 @@
3231
#include <Parsers/IAST.h>
3332
#include <Parsers/ASTInsertQuery.h>
3433
#include <Common/ErrorHandlers.h>
34+
#include <Functions/UserDefined/IUserDefinedSQLObjectsLoader.h>
3535
#include <Functions/registerFunctions.h>
3636
#include <AggregateFunctions/registerAggregateFunctions.h>
3737
#include <TableFunctions/registerTableFunctions.h>
@@ -602,21 +602,13 @@ void LocalServer::processConfig()
602602
global_context->setCurrentDatabase(default_database);
603603
applyCmdOptions(global_context);
604604

605-
bool enable_objects_loader = false;
606-
607605
if (config().has("path"))
608606
{
609607
String path = global_context->getPath();
610608

611609
/// Lock path directory before read
612610
status.emplace(fs::path(path) / "status", StatusFile::write_full_info);
613611

614-
LOG_DEBUG(log, "Loading user defined objects from {}", path);
615-
Poco::File(path + "user_defined/").createDirectories();
616-
UserDefinedSQLObjectsLoader::instance().loadObjects(global_context);
617-
enable_objects_loader = true;
618-
LOG_DEBUG(log, "Loaded user defined objects.");
619-
620612
LOG_DEBUG(log, "Loading metadata from {}", path);
621613
loadMetadataSystem(global_context);
622614
attachSystemTablesLocal(global_context, *createMemoryDatabaseIfNotExists(global_context, DatabaseCatalog::SYSTEM_DATABASE));
@@ -630,6 +622,9 @@ void LocalServer::processConfig()
630622
DatabaseCatalog::instance().loadDatabases();
631623
}
632624

625+
/// For ClickHouse local if path is not set the loader will be disabled.
626+
global_context->getUserDefinedSQLObjectsLoader().loadObjects();
627+
633628
LOG_DEBUG(log, "Loaded metadata.");
634629
}
635630
else if (!config().has("no-system-tables"))
@@ -639,9 +634,6 @@ void LocalServer::processConfig()
639634
attachInformationSchema(global_context, *createMemoryDatabaseIfNotExists(global_context, DatabaseCatalog::INFORMATION_SCHEMA_UPPERCASE));
640635
}
641636

642-
/// Persist SQL user defined objects only if user_defined folder was created
643-
UserDefinedSQLObjectsLoader::instance().enable(enable_objects_loader);
644-
645637
server_display_name = config().getString("display_name", getFQDNOrHostName());
646638
prompt_by_server_display_name = config().getRawString("prompt_by_server_display_name.default", "{display_name} :) ");
647639
std::map<String, String> prompt_substitutions{{"display_name", server_display_name}};

programs/server/Server.cpp

+3-19
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#include <Interpreters/ExternalDictionariesLoader.h>
5454
#include <Interpreters/ProcessList.h>
5555
#include <Interpreters/loadMetadata.h>
56-
#include <Interpreters/UserDefinedSQLObjectsLoader.h>
5756
#include <Interpreters/JIT/CompiledExpressionCache.h>
5857
#include <Access/AccessControl.h>
5958
#include <Storages/StorageReplicatedMergeTree.h>
@@ -62,6 +61,7 @@
6261
#include <Storages/Cache/ExternalDataSourceCache.h>
6362
#include <Storages/Cache/registerRemoteFileMetadatas.h>
6463
#include <AggregateFunctions/registerAggregateFunctions.h>
64+
#include <Functions/UserDefined/IUserDefinedSQLObjectsLoader.h>
6565
#include <Functions/registerFunctions.h>
6666
#include <TableFunctions/registerTableFunctions.h>
6767
#include <Formats/registerFormats.h>
@@ -1007,12 +1007,6 @@ int Server::main(const std::vector<std::string> & /*args*/)
10071007
fs::create_directories(user_scripts_path);
10081008
}
10091009

1010-
{
1011-
std::string user_defined_path = config().getString("user_defined_path", path / "user_defined/");
1012-
global_context->setUserDefinedPath(user_defined_path);
1013-
fs::create_directories(user_defined_path);
1014-
}
1015-
10161010
/// top_level_domains_lists
10171011
{
10181012
const std::string & top_level_domains_path = config().getString("top_level_domains_path", path / "top_level_domains/");
@@ -1551,18 +1545,6 @@ int Server::main(const std::vector<std::string> & /*args*/)
15511545
/// system logs may copy global context.
15521546
global_context->setCurrentDatabaseNameInGlobalContext(default_database);
15531547

1554-
LOG_INFO(log, "Loading user defined objects from {}", path_str);
1555-
try
1556-
{
1557-
UserDefinedSQLObjectsLoader::instance().loadObjects(global_context);
1558-
}
1559-
catch (...)
1560-
{
1561-
tryLogCurrentException(log, "Caught exception while loading user defined objects");
1562-
throw;
1563-
}
1564-
LOG_DEBUG(log, "Loaded user defined objects");
1565-
15661548
LOG_INFO(log, "Loading metadata from {}", path_str);
15671549

15681550
try
@@ -1590,6 +1572,8 @@ int Server::main(const std::vector<std::string> & /*args*/)
15901572
database_catalog.loadDatabases();
15911573
/// After loading validate that default database exists
15921574
database_catalog.assertDatabaseExists(default_database);
1575+
/// Load user-defined SQL functions.
1576+
global_context->getUserDefinedSQLObjectsLoader().loadObjects();
15931577
}
15941578
catch (...)
15951579
{

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ add_object_library(clickhouse_processors_ttl Processors/TTL)
284284
add_object_library(clickhouse_processors_merges_algorithms Processors/Merges/Algorithms)
285285
add_object_library(clickhouse_processors_queryplan Processors/QueryPlan)
286286
add_object_library(clickhouse_processors_queryplan_optimizations Processors/QueryPlan/Optimizations)
287+
add_object_library(clickhouse_user_defined_functions Functions/UserDefined)
287288

288289
if (TARGET ch_contrib::nuraft)
289290
add_object_library(clickhouse_coordination Coordination)

src/Interpreters/ExternalUserDefinedExecutableFunctionsLoader.cpp src/Functions/UserDefined/ExternalUserDefinedExecutableFunctionsLoader.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
#include <DataTypes/DataTypeFactory.h>
77

8-
#include <Interpreters/UserDefinedExecutableFunction.h>
9-
#include <Interpreters/UserDefinedExecutableFunctionFactory.h>
8+
#include <Functions/UserDefined/UserDefinedExecutableFunction.h>
9+
#include <Functions/UserDefined/UserDefinedExecutableFunctionFactory.h>
1010
#include <Functions/FunctionFactory.h>
1111
#include <AggregateFunctions/AggregateFunctionFactory.h>
1212

src/Interpreters/ExternalUserDefinedExecutableFunctionsLoader.h src/Functions/UserDefined/ExternalUserDefinedExecutableFunctionsLoader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <Interpreters/Context_fwd.h>
66
#include <Interpreters/ExternalLoader.h>
7-
#include <Interpreters/UserDefinedExecutableFunction.h>
7+
#include <Functions/UserDefined/UserDefinedExecutableFunction.h>
88

99
namespace DB
1010
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <base/types.h>
4+
5+
6+
namespace DB
7+
{
8+
class IAST;
9+
struct Settings;
10+
enum class UserDefinedSQLObjectType;
11+
12+
/// Interface for a loader of user-defined SQL objects.
13+
/// Implementations: UserDefinedSQLLoaderFromDisk, UserDefinedSQLLoaderFromZooKeeper
14+
class IUserDefinedSQLObjectsLoader
15+
{
16+
public:
17+
virtual ~IUserDefinedSQLObjectsLoader() = default;
18+
19+
/// Whether this loader can replicate SQL objects to another node.
20+
virtual bool isReplicated() const { return false; }
21+
virtual String getReplicationID() const { return ""; }
22+
23+
/// Loads all objects. Can be called once - if objects are already loaded the function does nothing.
24+
virtual void loadObjects() = 0;
25+
26+
/// Stops watching.
27+
virtual void stopWatching() {}
28+
29+
/// Immediately reloads all objects, throws an exception if failed.
30+
virtual void reloadObjects() = 0;
31+
32+
/// Immediately reloads a specified object only.
33+
virtual void reloadObject(UserDefinedSQLObjectType object_type, const String & object_name) = 0;
34+
35+
/// Stores an object (must be called only by UserDefinedSQLFunctionFactory::registerFunction).
36+
virtual bool storeObject(
37+
UserDefinedSQLObjectType object_type,
38+
const String & object_name,
39+
const IAST & create_object_query,
40+
bool throw_if_exists,
41+
bool replace_if_exists,
42+
const Settings & settings) = 0;
43+
44+
/// Removes an object (must be called only by UserDefinedSQLFunctionFactory::unregisterFunction).
45+
virtual bool removeObject(UserDefinedSQLObjectType object_type, const String & object_name, bool throw_if_not_exists) = 0;
46+
};
47+
}

src/Interpreters/UserDefinedExecutableFunctionFactory.cpp src/Functions/UserDefined/UserDefinedExecutableFunctionFactory.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
#include <Functions/FunctionFactory.h>
1414
#include <Functions/FunctionHelpers.h>
15+
#include <Functions/UserDefined/ExternalUserDefinedExecutableFunctionsLoader.h>
1516
#include <AggregateFunctions/AggregateFunctionFactory.h>
1617
#include <Interpreters/convertFieldToType.h>
17-
#include <Interpreters/ExternalUserDefinedExecutableFunctionsLoader.h>
1818
#include <Interpreters/Context.h>
1919
#include <Interpreters/castColumn.h>
2020

0 commit comments

Comments
 (0)