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-10-13 15:38:08 +00:00
|
|
|
#include <DB/Core/FieldVisitors.h>
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
|
|
|
|
extern const int EXCESSIVE_ELEMENT_IN_CONFIG;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
Block createSampleBlock(const DictionaryStructure & dict_struct)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-11-12 14:28:23 +00:00
|
|
|
Block block;
|
|
|
|
|
|
|
|
if (dict_struct.id)
|
|
|
|
block.insert(ColumnWithTypeAndName{
|
2016-03-07 06:00:25 +00:00
|
|
|
new ColumnUInt64{1}, new DataTypeUInt64, dict_struct.id.value().name
|
2015-11-12 14:28:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (dict_struct.key)
|
|
|
|
{
|
|
|
|
for (const auto & attribute : *dict_struct.key)
|
|
|
|
{
|
|
|
|
auto column = attribute.type->createColumn();
|
2015-11-13 01:44:41 +00:00
|
|
|
column->insertDefault();
|
2015-11-12 14:28:23 +00:00
|
|
|
|
2016-03-07 06:25:36 +00:00
|
|
|
block.insert(ColumnWithTypeAndName{column, attribute.type, attribute.name});
|
2015-11-12 14:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
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 })
|
2015-11-12 14:28:23 +00:00
|
|
|
block.insert(ColumnWithTypeAndName{
|
2016-03-07 06:24:36 +00:00
|
|
|
new ColumnUInt16{1}, new DataTypeDate, attribute.value().name});
|
2015-09-07 17:22:54 +00:00
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
for (const auto & attribute : dict_struct.attributes)
|
2015-10-13 15:38:08 +00:00
|
|
|
{
|
|
|
|
auto column = attribute.type->createColumn();
|
|
|
|
column->insert(attribute.null_value);
|
|
|
|
|
|
|
|
block.insert(ColumnWithTypeAndName{column, 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|