2015-01-26 15:27:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Core/Block.h>
|
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <DB/Dictionaries/FileDictionarySource.h>
|
2015-02-10 14:50:43 +00:00
|
|
|
#include <DB/Dictionaries/MySQLDictionarySource.h>
|
|
|
|
#include <DB/Dictionaries/ClickHouseDictionarySource.h>
|
2015-10-13 10:20:48 +00:00
|
|
|
#include <DB/Dictionaries/MongoDBDictionarySource.h>
|
2015-01-26 15:27:51 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/singleton.h>
|
2015-02-10 21:10:58 +00:00
|
|
|
#include <memory>
|
2015-01-26 15:27:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
Block createSampleBlock(const DictionaryStructure & dict_struct)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
|
|
|
Block block{
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName{
|
2015-01-26 15:27:51 +00:00
|
|
|
new ColumnUInt64,
|
|
|
|
new DataTypeUInt64,
|
2015-09-07 17:22:54 +00:00
|
|
|
dict_struct.id.name
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-07 17:22:54 +00:00
|
|
|
if (dict_struct.range_min)
|
|
|
|
for (const auto & attribute : { dict_struct.range_min, dict_struct.range_max })
|
|
|
|
block.insert(ColumnWithTypeAndName{
|
|
|
|
new ColumnUInt16,
|
|
|
|
new DataTypeDate,
|
|
|
|
attribute->name
|
|
|
|
});
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
for (const auto & attribute : dict_struct.attributes)
|
2015-07-17 01:27:35 +00:00
|
|
|
block.insert(ColumnWithTypeAndName{
|
2015-03-20 15:21:29 +00:00
|
|
|
attribute.type->createColumn(), attribute.type, attribute.name
|
2015-01-26 15:27:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// creates IDictionarySource instance from config and DictionaryStructure
|
2015-01-26 15:27:51 +00:00
|
|
|
class DictionarySourceFactory : public Singleton<DictionarySourceFactory>
|
|
|
|
{
|
|
|
|
public:
|
2015-08-19 13:06:21 +00:00
|
|
|
DictionarySourcePtr create(
|
|
|
|
const std::string & name, Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
|
|
|
|
const DictionaryStructure & dict_struct, Context & context) const
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
if (keys.size() != 1)
|
|
|
|
throw Exception{
|
2015-08-19 13:06:21 +00:00
|
|
|
name +": element dictionary.source should have exactly one child element",
|
2015-02-10 14:50:43 +00:00
|
|
|
ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG
|
|
|
|
};
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
auto sample_block = createSampleBlock(dict_struct);
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto & source_type = keys.front();
|
|
|
|
|
|
|
|
if ("file" == source_type)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-09-07 17:22:54 +00:00
|
|
|
if (dict_struct.has_expressions)
|
|
|
|
throw Exception{
|
|
|
|
"Dictionary source of type `file` does not support attribute expressions",
|
|
|
|
ErrorCodes::LOGICAL_ERROR
|
|
|
|
};
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto filename = config.getString(config_prefix + ".file.path");
|
|
|
|
const auto format = config.getString(config_prefix + ".file.format");
|
2015-02-10 21:10:58 +00:00
|
|
|
return std::make_unique<FileDictionarySource>(filename, format, sample_block, context);
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-02-10 14:50:43 +00:00
|
|
|
else if ("mysql" == source_type)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-05-26 15:09:53 +00:00
|
|
|
return std::make_unique<MySQLDictionarySource>(dict_struct, config, config_prefix + ".mysql", sample_block);
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-02-10 14:50:43 +00:00
|
|
|
else if ("clickhouse" == source_type)
|
2015-01-29 11:51:52 +00:00
|
|
|
{
|
2015-05-26 15:09:53 +00:00
|
|
|
return std::make_unique<ClickHouseDictionarySource>(dict_struct, config, config_prefix + ".clickhouse",
|
2015-02-04 13:06:56 +00:00
|
|
|
sample_block, context);
|
2015-01-29 11:51:52 +00:00
|
|
|
}
|
2015-10-13 10:20:48 +00:00
|
|
|
else if ("mongodb" == source_type)
|
2015-10-09 14:51:31 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<MongoDBDictionarySource>(dict_struct, config, config_prefix + ".mongodb",
|
|
|
|
sample_block, context);
|
2015-10-13 10:20:48 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
throw Exception{
|
2015-08-19 13:06:21 +00:00
|
|
|
name + ": unknown dictionary source type: " + source_type,
|
2015-02-10 14:50:43 +00:00
|
|
|
ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG
|
|
|
|
};
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|