added support of custom settings to FileDictionarySource

This commit is contained in:
Артем Стрельцов 2020-04-03 01:35:22 +03:00
parent 0ac44d6c21
commit e50ebd5ee1
4 changed files with 22 additions and 40 deletions

View File

@ -84,10 +84,11 @@ DictionarySourcePtr DictionarySourceFactory::create(
{
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(config_prefix, keys);
if (keys.size() != 1)
throw Exception{name + ": element dictionary.source should have exactly one child element",
if (keys.empty() || keys.size() > 2)
throw Exception{name + ": element dictionary.source should have one or two child elements",
ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG};
const auto & source_type = keys.front();
const auto found = registered_sources.find(source_type);

View File

@ -5,7 +5,6 @@
#include <Formats/FormatSettings.h>
#include <IO/WriteHelpers.h>
#include <Common/StringUtils/StringUtils.h>
#include <Core/Settings.h>
#include <numeric>
#include <unordered_map>
@ -196,9 +195,6 @@ DictionaryStructure::DictionaryStructure(const Poco::Util::AbstractConfiguration
attributes = getAttributes(config, config_prefix);
settings = Settings();
getSettings(config, config_prefix, settings);
if (attributes.empty())
throw Exception{"Dictionary has no attributes defined", ErrorCodes::BAD_ARGUMENTS};
}
@ -360,30 +356,4 @@ std::vector<DictionaryAttribute> DictionaryStructure::getAttributes(
return res_attributes;
}
void DictionaryStructure::getSettings(
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
Settings & dict_settings)
{
Poco::Util::AbstractConfiguration::Keys config_elems;
config.keys(config_prefix, config_elems);
for (const auto & config_elem : config_elems)
{
if (startsWith(config_elem, "settings"))
{
/* i won't do break after this if in case there can be multiple settings sections */
const auto prefix = config_prefix + '.' + config_elem;
Poco::Util::AbstractConfiguration::Keys setting_keys;
config.keys(prefix, setting_keys);
dict_settings.loadSettingsFromConfig(prefix, config);
}
}
}
}
}

View File

@ -21,7 +21,7 @@ namespace ErrorCodes
FileDictionarySource::FileDictionarySource(
const std::string & filepath_, const std::string & format_,
Block & sample_block_, const Context & context_, bool check_config)
Block & sample_block_, Context & context_, bool check_config)
: filepath{filepath_}
, format{format_}
, sample_block{sample_block_}
@ -83,7 +83,18 @@ void registerDictionarySourceFile(DictionarySourceFactory & factory)
const auto filepath = config.getString(config_prefix + ".file.path");
const auto format = config.getString(config_prefix + ".file.format");
return std::make_unique<FileDictionarySource>(filepath, format, sample_block, context, check_config);
Context context_local_copy(context);
if (config.has(config_prefix + ".settings"))
{
const auto prefix = config_prefix + ".settings";
Settings settings;
settings.loadSettingsFromConfig(prefix, config);
// const_cast<Context&>(context).setSettings(settings);
context_local_copy.setSettings(settings);
}
return std::make_unique<FileDictionarySource>(filepath, format, sample_block, context_local_copy, check_config);
};
factory.registerSource("file", create_table_source);

View File

@ -3,7 +3,7 @@
#include <Poco/Timestamp.h>
#include "IDictionarySource.h"
#include <Core/Block.h>
#include <Interpreters/Context.h>
namespace DB
{
@ -11,14 +11,14 @@ namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
class Context;
// class Context;
/// Allows loading dictionaries from a file with given format, does not support "random access"
class FileDictionarySource final : public IDictionarySource
{
public:
FileDictionarySource(const std::string & filepath_, const std::string & format_,
Block & sample_block_, const Context & context_, bool check_config);
Block & sample_block_, Context & context_, bool check_config);
FileDictionarySource(const FileDictionarySource & other);
@ -62,7 +62,7 @@ private:
const std::string filepath;
const std::string format;
Block sample_block;
const Context & context;
const Context context;
Poco::Timestamp last_modification;
};