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>
|
2016-11-15 19:51:06 +00:00
|
|
|
#include <DB/Dictionaries/ExecutableDictionarySource.h>
|
|
|
|
#include <DB/Dictionaries/HTTPDictionarySource.h>
|
2016-05-22 00:54:26 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_MONGODB
|
2015-10-13 10:20:48 +00:00
|
|
|
#include <DB/Dictionaries/MongoDBDictionarySource.h>
|
2016-05-22 00:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-10 04:00:00 +00:00
|
|
|
#include <DB/Dictionaries/ODBCDictionarySource.h>
|
2015-01-26 15:27:51 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2016-04-10 10:24:07 +00:00
|
|
|
#include <DB/Core/FieldVisitors.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/singleton.h>
|
2015-02-10 21:10:58 +00:00
|
|
|
#include <memory>
|
2016-04-10 10:24:07 +00:00
|
|
|
#include <Poco/Data/ODBC/Connector.h>
|
2015-10-13 15:38:08 +00:00
|
|
|
|
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;
|
2016-05-22 00:54:26 +00:00
|
|
|
extern const int SUPPORT_IS_DISABLED;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
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-05-28 07:48:40 +00:00
|
|
|
std::make_shared<ColumnUInt64>(1), std::make_shared<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-05-28 07:48:40 +00:00
|
|
|
std::make_shared<ColumnUInt16>(1), std::make_shared<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:
|
2016-04-10 10:24:07 +00:00
|
|
|
DictionarySourceFactory()
|
|
|
|
{
|
|
|
|
Poco::Data::ODBC::Connector::registerConnector();
|
|
|
|
}
|
|
|
|
|
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",
|
2016-05-22 00:54:26 +00:00
|
|
|
ErrorCodes::LOGICAL_ERROR};
|
2015-09-07 17:22:54 +00:00
|
|
|
|
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
|
|
|
{
|
2016-05-22 00:54:26 +00:00
|
|
|
#ifndef DISABLE_MONGODB
|
2016-04-10 04:00:00 +00:00
|
|
|
return std::make_unique<MongoDBDictionarySource>(dict_struct, config, config_prefix + ".mongodb", sample_block);
|
2016-05-22 00:54:26 +00:00
|
|
|
#else
|
|
|
|
throw Exception{
|
|
|
|
"MongoDB dictionary source was disabled at build time", ErrorCodes::SUPPORT_IS_DISABLED};
|
|
|
|
#endif
|
2016-04-10 04:00:00 +00:00
|
|
|
}
|
|
|
|
else if ("odbc" == source_type)
|
|
|
|
{
|
|
|
|
return std::make_unique<ODBCDictionarySource>(dict_struct, config, config_prefix + ".odbc", sample_block);
|
2015-10-13 10:20:48 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2016-11-15 19:51:06 +00:00
|
|
|
else if ("executable" == source_type)
|
|
|
|
{
|
|
|
|
if (dict_struct.has_expressions)
|
|
|
|
throw Exception{
|
|
|
|
"Dictionary source of type `executable` does not support attribute expressions",
|
|
|
|
ErrorCodes::LOGICAL_ERROR};
|
|
|
|
|
|
|
|
const auto name = config.getString(config_prefix + ".executable.name");
|
|
|
|
const auto format = config.getString(config_prefix + ".executable.format");
|
|
|
|
return std::make_unique<ExecutableDictionarySource>(name, format, sample_block, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ("http" == source_type)
|
|
|
|
{
|
|
|
|
if (dict_struct.has_expressions)
|
|
|
|
throw Exception{
|
2016-11-15 22:05:49 +00:00
|
|
|
"Dictionary source of type `http` does not support attribute expressions",
|
2016-11-15 19:51:06 +00:00
|
|
|
ErrorCodes::LOGICAL_ERROR};
|
|
|
|
|
2016-11-15 22:05:49 +00:00
|
|
|
return std::make_unique<HTTPDictionarySource>(config, config_prefix + ".http", sample_block, context);
|
2016-11-15 19:51:06 +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,
|
2016-04-10 04:00:00 +00:00
|
|
|
ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG};
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|