2017-04-25 09:10:27 +00:00
|
|
|
#include <sstream>
|
2017-04-28 18:33:31 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2017-12-25 18:29:04 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
2017-04-25 09:10:27 +00:00
|
|
|
#include <Dictionaries/IDictionarySource.h>
|
|
|
|
#include <Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <Storages/StorageDictionary.h>
|
2017-12-28 21:36:27 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
2017-04-25 09:10:27 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2018-01-12 18:56:13 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2017-06-22 15:44:19 +00:00
|
|
|
#include <Interpreters/ExternalDictionaries.h>
|
2017-12-28 21:36:27 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2017-04-27 17:16:24 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-25 09:10:27 +00:00
|
|
|
|
2017-12-28 21:36:27 +00:00
|
|
|
|
2017-06-23 15:55:45 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-25 09:10:27 +00:00
|
|
|
|
2017-12-28 21:36:27 +00:00
|
|
|
namespace ErrorCodes
|
2017-06-22 15:44:19 +00:00
|
|
|
{
|
2017-12-28 21:36:27 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2018-11-22 21:19:58 +00:00
|
|
|
extern const int THERE_IS_NO_COLUMN;
|
2017-06-22 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 09:10:27 +00:00
|
|
|
|
|
|
|
StorageDictionary::StorageDictionary(
|
2019-07-09 15:40:21 +00:00
|
|
|
const String & database_name_,
|
2017-04-25 09:10:27 +00:00
|
|
|
const String & table_name_,
|
2018-03-06 20:18:34 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2019-02-04 19:45:22 +00:00
|
|
|
const Context & context,
|
|
|
|
bool attach,
|
2017-06-22 15:44:19 +00:00
|
|
|
const String & dictionary_name_)
|
2019-08-24 21:20:20 +00:00
|
|
|
: table_name(table_name_),
|
2019-07-09 15:40:21 +00:00
|
|
|
database_name(database_name_),
|
2018-01-25 14:42:39 +00:00
|
|
|
dictionary_name(dictionary_name_),
|
2017-06-22 15:44:19 +00:00
|
|
|
logger(&Poco::Logger::get("StorageDictionary"))
|
2017-04-25 09:10:27 +00:00
|
|
|
{
|
2019-08-24 21:20:20 +00:00
|
|
|
setColumns(columns_);
|
|
|
|
|
2019-02-04 19:45:22 +00:00
|
|
|
if (!attach)
|
|
|
|
{
|
|
|
|
const auto & dictionary = context.getExternalDictionaries().getDictionary(dictionary_name);
|
|
|
|
const DictionaryStructure & dictionary_structure = dictionary->getStructure();
|
|
|
|
checkNamesAndTypesCompatibleWithDictionary(dictionary_structure);
|
|
|
|
}
|
2017-04-25 09:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreams StorageDictionary::read(
|
|
|
|
const Names & column_names,
|
2018-04-19 15:18:26 +00:00
|
|
|
const SelectQueryInfo & /*query_info*/,
|
2017-04-25 09:10:27 +00:00
|
|
|
const Context & context,
|
2018-09-08 11:29:23 +00:00
|
|
|
QueryProcessingStage::Enum /*processed_stage*/,
|
2019-02-18 23:38:44 +00:00
|
|
|
const size_t max_block_size,
|
2017-12-01 21:13:25 +00:00
|
|
|
const unsigned /*threads*/)
|
2017-04-25 09:10:27 +00:00
|
|
|
{
|
2017-06-22 15:44:19 +00:00
|
|
|
auto dictionary = context.getExternalDictionaries().getDictionary(dictionary_name);
|
2017-05-29 17:26:45 +00:00
|
|
|
return BlockInputStreams{dictionary->getBlockInputStream(column_names, max_block_size)};
|
2017-04-25 09:10:27 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList StorageDictionary::getNamesAndTypes(const DictionaryStructure & dictionary_structure)
|
2017-04-25 09:10:27 +00:00
|
|
|
{
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList dictionary_names_and_types;
|
2017-04-28 18:33:31 +00:00
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
if (dictionary_structure.id)
|
|
|
|
dictionary_names_and_types.emplace_back(dictionary_structure.id->name, std::make_shared<DataTypeUInt64>());
|
|
|
|
if (dictionary_structure.range_min)
|
2018-09-13 13:33:44 +00:00
|
|
|
dictionary_names_and_types.emplace_back(dictionary_structure.range_min->name, dictionary_structure.range_min->type);
|
2017-12-25 21:10:46 +00:00
|
|
|
if (dictionary_structure.range_max)
|
2018-09-13 13:33:44 +00:00
|
|
|
dictionary_names_and_types.emplace_back(dictionary_structure.range_max->name, dictionary_structure.range_max->type);
|
2017-12-25 21:10:46 +00:00
|
|
|
if (dictionary_structure.key)
|
|
|
|
for (const auto & attribute : *dictionary_structure.key)
|
|
|
|
dictionary_names_and_types.emplace_back(attribute.name, attribute.type);
|
2017-04-28 18:33:31 +00:00
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
for (const auto & attribute : dictionary_structure.attributes)
|
|
|
|
dictionary_names_and_types.emplace_back(attribute.name, attribute.type);
|
2017-04-28 18:33:31 +00:00
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
return dictionary_names_and_types;
|
2017-04-28 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
void StorageDictionary::checkNamesAndTypesCompatibleWithDictionary(const DictionaryStructure & dictionary_structure) const
|
2017-04-28 18:33:31 +00:00
|
|
|
{
|
2017-12-25 21:10:46 +00:00
|
|
|
auto dictionary_names_and_types = getNamesAndTypes(dictionary_structure);
|
2019-02-04 19:45:22 +00:00
|
|
|
std::set<NameAndTypePair> names_and_types_set(dictionary_names_and_types.begin(), dictionary_names_and_types.end());
|
2017-04-28 18:33:31 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
for (const auto & column : getColumns().getOrdinary())
|
2017-06-23 15:55:45 +00:00
|
|
|
{
|
2019-02-04 19:45:22 +00:00
|
|
|
if (names_and_types_set.find(column) == names_and_types_set.end())
|
2017-06-23 15:55:45 +00:00
|
|
|
{
|
2017-04-25 09:10:27 +00:00
|
|
|
std::string message = "Not found column ";
|
|
|
|
message += column.name + " " + column.type->getName();
|
|
|
|
message += " in dictionary " + dictionary_name + ". ";
|
|
|
|
message += "There are only columns ";
|
2017-12-25 21:10:46 +00:00
|
|
|
message += generateNamesAndTypesDescription(dictionary_names_and_types.begin(), dictionary_names_and_types.end());
|
2018-11-22 21:19:58 +00:00
|
|
|
throw Exception(message, ErrorCodes::THERE_IS_NO_COLUMN);
|
2017-04-25 09:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 21:36:27 +00:00
|
|
|
void registerStorageDictionary(StorageFactory & factory)
|
|
|
|
{
|
2017-12-30 00:36:06 +00:00
|
|
|
factory.registerStorage("Dictionary", [](const StorageFactory::Arguments & args)
|
2017-12-28 21:36:27 +00:00
|
|
|
{
|
2017-12-30 00:36:06 +00:00
|
|
|
if (args.engine_args.size() != 1)
|
2017-12-28 21:36:27 +00:00
|
|
|
throw Exception("Storage Dictionary requires single parameter: name of dictionary",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2018-01-12 18:56:13 +00:00
|
|
|
args.engine_args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(args.engine_args[0], args.local_context);
|
2019-03-15 17:09:14 +00:00
|
|
|
String dictionary_name = args.engine_args[0]->as<ASTLiteral &>().value.safeGet<String>();
|
2017-12-28 21:36:27 +00:00
|
|
|
|
|
|
|
return StorageDictionary::create(
|
2019-07-09 15:40:21 +00:00
|
|
|
args.database_name, args.table_name, args.columns, args.context, args.attach, dictionary_name);
|
2017-12-28 21:36:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-15 13:58:40 +00:00
|
|
|
}
|