Skip to content

Commit f0c7e56

Browse files
authored
Merge pull request #4962 from vitlibar/resolve-dictionary-depends-on-dictionary
Resolve correctly when dictionary depends on dictionary
2 parents f1cc83c + 39c7107 commit f0c7e56

File tree

15 files changed

+365
-170
lines changed

15 files changed

+365
-170
lines changed

dbms/src/Databases/DatabaseDictionary.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,12 @@ StoragePtr DatabaseDictionary::tryGetTable(
6565
const Context & context,
6666
const String & table_name) const
6767
{
68-
auto objects_map = context.getExternalDictionaries().getObjectsMap();
69-
const auto & dictionaries = objects_map.get();
70-
68+
auto dict_ptr = context.getExternalDictionaries().tryGetDictionary(table_name);
69+
if (dict_ptr)
7170
{
72-
auto it = dictionaries.find(table_name);
73-
if (it != dictionaries.end())
74-
{
75-
const auto & dict_ptr = std::static_pointer_cast<IDictionaryBase>(it->second.loadable);
76-
if (dict_ptr)
77-
{
78-
const DictionaryStructure & dictionary_structure = dict_ptr->getStructure();
79-
auto columns = StorageDictionary::getNamesAndTypes(dictionary_structure);
80-
return StorageDictionary::create(table_name, ColumnsDescription{columns}, context, true, table_name);
81-
}
82-
}
71+
const DictionaryStructure & dictionary_structure = dict_ptr->getStructure();
72+
auto columns = StorageDictionary::getNamesAndTypes(dictionary_structure);
73+
return StorageDictionary::create(table_name, ColumnsDescription{columns}, context, true, table_name);
8374
}
8475

8576
return {};

dbms/src/Interpreters/Context.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct ContextShared
104104
mutable std::recursive_mutex mutex;
105105
/// Separate mutex for access of dictionaries. Separate mutex to avoid locks when server doing request to itself.
106106
mutable std::mutex embedded_dictionaries_mutex;
107-
mutable std::mutex external_dictionaries_mutex;
107+
mutable std::recursive_mutex external_dictionaries_mutex;
108108
mutable std::mutex external_models_mutex;
109109
/// Separate mutex for re-initialization of zookeer session. This operation could take a long time and must not interfere with another operations.
110110
mutable std::mutex zookeeper_mutex;
@@ -1240,44 +1240,38 @@ EmbeddedDictionaries & Context::getEmbeddedDictionariesImpl(const bool throw_on_
12401240

12411241
ExternalDictionaries & Context::getExternalDictionariesImpl(const bool throw_on_error) const
12421242
{
1243-
const auto & config = getConfigRef();
1243+
{
1244+
std::lock_guard lock(shared->external_dictionaries_mutex);
1245+
if (shared->external_dictionaries)
1246+
return *shared->external_dictionaries;
1247+
}
12441248

1249+
const auto & config = getConfigRef();
12451250
std::lock_guard lock(shared->external_dictionaries_mutex);
1246-
12471251
if (!shared->external_dictionaries)
12481252
{
12491253
if (!this->global_context)
12501254
throw Exception("Logical error: there is no global context", ErrorCodes::LOGICAL_ERROR);
12511255

12521256
auto config_repository = shared->runtime_components_factory->createExternalDictionariesConfigRepository();
1253-
1254-
shared->external_dictionaries.emplace(
1255-
std::move(config_repository),
1256-
config,
1257-
*this->global_context,
1258-
throw_on_error);
1257+
shared->external_dictionaries.emplace(std::move(config_repository), config, *this->global_context);
1258+
shared->external_dictionaries->init(throw_on_error);
12591259
}
1260-
12611260
return *shared->external_dictionaries;
12621261
}
12631262

12641263
ExternalModels & Context::getExternalModelsImpl(bool throw_on_error) const
12651264
{
12661265
std::lock_guard lock(shared->external_models_mutex);
1267-
12681266
if (!shared->external_models)
12691267
{
12701268
if (!this->global_context)
12711269
throw Exception("Logical error: there is no global context", ErrorCodes::LOGICAL_ERROR);
12721270

12731271
auto config_repository = shared->runtime_components_factory->createExternalModelsConfigRepository();
1274-
1275-
shared->external_models.emplace(
1276-
std::move(config_repository),
1277-
*this->global_context,
1278-
throw_on_error);
1272+
shared->external_models.emplace(std::move(config_repository), *this->global_context);
1273+
shared->external_models->init(throw_on_error);
12791274
}
1280-
12811275
return *shared->external_models;
12821276
}
12831277

dbms/src/Interpreters/ExternalDictionaries.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ namespace
3030
ExternalDictionaries::ExternalDictionaries(
3131
std::unique_ptr<IExternalLoaderConfigRepository> config_repository,
3232
const Poco::Util::AbstractConfiguration & config,
33-
Context & context,
34-
bool throw_on_error)
33+
Context & context)
3534
: ExternalLoader(config,
3635
externalDictionariesUpdateSettings,
3736
getExternalDictionariesConfigSettings(),
@@ -40,11 +39,11 @@ ExternalDictionaries::ExternalDictionaries(
4039
"external dictionary"),
4140
context(context)
4241
{
43-
init(throw_on_error);
4442
}
4543

44+
4645
std::unique_ptr<IExternalLoadable> ExternalDictionaries::create(
47-
const std::string & name, const Configuration & config, const std::string & config_prefix)
46+
const std::string & name, const Configuration & config, const std::string & config_prefix) const
4847
{
4948
return DictionaryFactory::instance().create(name, config, config_prefix, context);
5049
}

dbms/src/Interpreters/ExternalDictionaries.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class ExternalDictionaries : public ExternalLoader
2121
ExternalDictionaries(
2222
std::unique_ptr<IExternalLoaderConfigRepository> config_repository,
2323
const Poco::Util::AbstractConfiguration & config,
24-
Context & context,
25-
bool throw_on_error);
24+
Context & context);
2625

2726
/// Forcibly reloads specified dictionary.
2827
void reloadDictionary(const std::string & name) { reload(name); }
@@ -40,7 +39,7 @@ class ExternalDictionaries : public ExternalLoader
4039
protected:
4140

4241
std::unique_ptr<IExternalLoadable> create(const std::string & name, const Configuration & config,
43-
const std::string & config_prefix) override;
42+
const std::string & config_prefix) const override;
4443

4544
using ExternalLoader::getObjectsMap;
4645

0 commit comments

Comments
 (0)