Skip to content

Commit cf3e774

Browse files
committed
Lazy init for StorageObjectStorageCluster
1 parent fe0a2ce commit cf3e774

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

src/Databases/DataLake/DatabaseDataLake.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ StoragePtr DatabaseDataLake::tryGetTableImpl(const String & name, ContextPtr con
445445
context_copy,
446446
/* comment */"",
447447
getFormatSettings(context_copy),
448-
LoadingStrictnessLevel::CREATE);
448+
LoadingStrictnessLevel::CREATE,
449+
/* lazy_init */true);
449450
}
450451

451452
DatabaseTablesIteratorPtr DatabaseDataLake::getTablesIterator(

src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,52 @@ StorageObjectStorageCluster::StorageObjectStorageCluster(
8787
ContextPtr context_,
8888
const String & comment_,
8989
std::optional<FormatSettings> format_settings_,
90-
LoadingStrictnessLevel mode_)
90+
LoadingStrictnessLevel mode_,
91+
bool lazy_init)
9192
: IStorageCluster(
9293
cluster_name_, table_id_, getLogger(fmt::format("{}({})", configuration_->getEngineName(), table_id_.table_name)))
9394
, configuration{configuration_}
9495
, object_storage(object_storage_)
9596
, cluster_name_in_settings(false)
9697
{
9798
configuration->initPartitionStrategy(partition_by, columns_in_table_or_function_definition, context_);
98-
/// We allow exceptions to be thrown on update(),
99-
/// because Cluster engine can only be used as table function,
100-
/// so no lazy initialization is allowed.
101-
configuration->update(
102-
object_storage,
103-
context_,
104-
/* if_not_updated_before */false,
105-
/* check_consistent_with_previous_metadata */true);
99+
100+
const bool need_resolve_columns_or_format = columns_in_table_or_function_definition.empty() || (configuration->getFormat() == "auto");
101+
const bool do_lazy_init = lazy_init && !need_resolve_columns_or_format;
102+
103+
auto log_ = getLogger("StorageObjectStorageCluster");
104+
105+
try
106+
{
107+
if (!do_lazy_init)
108+
{
109+
/// We allow exceptions to be thrown on update(),
110+
/// because Cluster engine can only be used as table function,
111+
/// so no lazy initialization is allowed.
112+
configuration->update(
113+
object_storage,
114+
context_,
115+
/* if_not_updated_before */false,
116+
/* check_consistent_with_previous_metadata */true);
117+
}
118+
}
119+
catch (...)
120+
{
121+
// If we don't have format or schema yet, we can't ignore failed configuration update,
122+
// because relevant configuration is crucial for format and schema inference
123+
if (mode_ <= LoadingStrictnessLevel::CREATE || need_resolve_columns_or_format)
124+
{
125+
throw;
126+
}
127+
tryLogCurrentException(log_);
128+
}
106129

107130
ColumnsDescription columns{columns_in_table_or_function_definition};
108131
std::string sample_path;
109-
resolveSchemaAndFormat(columns, object_storage, configuration, {}, sample_path, context_);
132+
if (need_resolve_columns_or_format)
133+
resolveSchemaAndFormat(columns, object_storage, configuration, {}, sample_path, context_);
134+
else
135+
validateSupportedColumns(columns, *configuration);
110136
configuration->check(context_);
111137

112138
if (sample_path.empty() && context_->getSettingsRef()[Setting::use_hive_partitioning] && !configuration->isDataLakeConfiguration() && !configuration->getPartitionStrategy())
@@ -162,7 +188,9 @@ StorageObjectStorageCluster::StorageObjectStorageCluster(
162188
format_settings_,
163189
mode_,
164190
/* distributed_processing */false,
165-
partition_by);
191+
partition_by,
192+
/* is_table_function */false,
193+
/* lazy_init */lazy_init);
166194

167195
auto virtuals_ = getVirtualsPtr();
168196
if (virtuals_)

src/Storages/ObjectStorage/StorageObjectStorageCluster.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class StorageObjectStorageCluster : public IStorageCluster
2323
ContextPtr context_,
2424
const String & comment_,
2525
std::optional<FormatSettings> format_settings_,
26-
LoadingStrictnessLevel mode_);
26+
LoadingStrictnessLevel mode_,
27+
bool lazy_init = false);
2728

2829
std::string getName() const override;
2930

src/TableFunctions/TableFunctionObjectStorageCluster.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ StoragePtr TableFunctionObjectStorageCluster<Definition, Configuration, is_data_
4747
/* mode */ LoadingStrictnessLevel::CREATE,
4848
/* distributed_processing */ true,
4949
/* partition_by_ */Base::partition_by,
50-
/* is_table_function */true);
50+
/* is_table_function */true,
51+
/* lazy_init */ true);
5152
}
5253
else
5354
{
@@ -62,7 +63,8 @@ StoragePtr TableFunctionObjectStorageCluster<Definition, Configuration, is_data_
6263
context,
6364
/* comment */ String{},
6465
/* format_settings */ std::nullopt, /// No format_settings
65-
/* mode */ LoadingStrictnessLevel::CREATE);
66+
/* mode */ LoadingStrictnessLevel::CREATE,
67+
/* lazy_init */ true);
6668
}
6769

6870
storage->startup();

0 commit comments

Comments
 (0)