ClickHouse/dbms/Dictionaries/DictionarySourceFactory.cpp

112 lines
3.4 KiB
C++
Raw Normal View History

#include "DictionarySourceFactory.h"
#include <Columns/ColumnsNumber.h>
#include <Core/Block.h>
#include <Core/ColumnWithTypeAndName.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypesNumber.h>
#include <Poco/Logger.h>
#include <common/logger_useful.h>
#include "DictionaryStructure.h"
2016-12-08 02:49:04 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
extern const int EXCESSIVE_ELEMENT_IN_CONFIG;
extern const int LOGICAL_ERROR;
2016-12-08 02:49:04 +00:00
}
namespace
{
Block createSampleBlock(const DictionaryStructure & dict_struct)
{
Block block;
2016-12-08 02:49:04 +00:00
if (dict_struct.id)
block.insert(ColumnWithTypeAndName{ColumnUInt64::create(1, 0), std::make_shared<DataTypeUInt64>(), dict_struct.id->name});
2016-12-08 02:49:04 +00:00
if (dict_struct.key)
{
for (const auto & attribute : *dict_struct.key)
{
auto column = attribute.type->createColumn();
column->insertDefault();
2016-12-08 02:49:04 +00:00
block.insert(ColumnWithTypeAndName{std::move(column), attribute.type, attribute.name});
}
}
2016-12-08 02:49:04 +00:00
if (dict_struct.range_min)
{
for (const auto & attribute : {dict_struct.range_min, dict_struct.range_max})
{
const auto & type = std::make_shared<DataTypeNullable>(attribute->type);
auto column = type->createColumn();
column->insertDefault();
block.insert(ColumnWithTypeAndName{std::move(column), type, attribute->name});
}
}
2016-12-08 02:49:04 +00:00
for (const auto & attribute : dict_struct.attributes)
{
auto column = attribute.type->createColumn();
column->insert(attribute.null_value);
2016-12-08 02:49:04 +00:00
block.insert(ColumnWithTypeAndName{std::move(column), attribute.type, attribute.name});
}
2016-12-08 02:49:04 +00:00
return block;
}
2016-12-08 02:49:04 +00:00
}
DictionarySourceFactory::DictionarySourceFactory() : log(&Poco::Logger::get("DictionarySourceFactory"))
2016-12-08 02:49:04 +00:00
{
}
2017-11-27 19:05:45 +00:00
void DictionarySourceFactory::registerSource(const std::string & source_type, Creator create_source)
{
2017-11-27 19:05:45 +00:00
if (!registered_sources.emplace(source_type, std::move(create_source)).second)
throw Exception("DictionarySourceFactory: the source name '" + source_type + "' is not unique", ErrorCodes::LOGICAL_ERROR);
}
2016-12-08 02:49:04 +00:00
DictionarySourcePtr DictionarySourceFactory::create(
const std::string & name,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
const DictionaryStructure & dict_struct,
const Context & context,
bool check_config) const
2016-12-08 02:49:04 +00:00
{
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(config_prefix, keys);
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);
if (found != registered_sources.end())
{
const auto & create_source = found->second;
auto sample_block = createSampleBlock(dict_struct);
return create_source(dict_struct, config, config_prefix, sample_block, context, check_config);
}
2016-12-08 02:49:04 +00:00
throw Exception{name + ": unknown dictionary source type: " + source_type, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG};
2016-12-08 02:49:04 +00:00
}
DictionarySourceFactory & DictionarySourceFactory::instance()
{
static DictionarySourceFactory instance;
return instance;
}
2016-12-08 02:49:04 +00:00
}