ClickHouse/dbms/src/Storages/StorageDictionary.cpp

109 lines
4.2 KiB
C++
Raw Normal View History

2017-04-25 09:10:27 +00:00
#include <sstream>
2017-04-28 18:33:31 +00:00
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeDate.h>
2017-04-25 09:10:27 +00:00
#include <Dictionaries/IDictionarySource.h>
#include <Dictionaries/DictionaryStructure.h>
#include <Storages/StorageDictionary.h>
#include <Storages/StorageFactory.h>
2017-04-25 09:10:27 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
2017-06-22 15:44:19 +00:00
#include <Interpreters/ExternalDictionaries.h>
#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
namespace DB
{
2017-04-25 09:10:27 +00:00
namespace ErrorCodes
2017-06-22 15:44:19 +00:00
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
2017-06-22 15:44:19 +00:00
}
2017-04-25 09:10:27 +00:00
StorageDictionary::StorageDictionary(
const String & table_name_,
const ColumnsDescription & columns_,
2017-06-22 15:44:19 +00:00
const DictionaryStructure & dictionary_structure_,
const String & dictionary_name_)
: IStorage{columns_}, table_name(table_name_),
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
{
2017-06-22 15:44:19 +00:00
checkNamesAndTypesCompatibleWithDictionary(dictionary_structure_);
2017-04-25 09:10:27 +00:00
}
BlockInputStreams StorageDictionary::read(
const Names & column_names,
2017-12-01 21:13:25 +00:00
const SelectQueryInfo & /*query_info*/,
2017-04-25 09:10:27 +00:00
const Context & context,
QueryProcessingStage::Enum & processed_stage,
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
{
processed_stage = QueryProcessingStage::FetchColumns;
2017-06-22 15:44:19 +00:00
auto dictionary = context.getExternalDictionaries().getDictionary(dictionary_name);
return BlockInputStreams{dictionary->getBlockInputStream(column_names, max_block_size)};
2017-04-25 09:10:27 +00:00
}
NamesAndTypesList StorageDictionary::getNamesAndTypes(const DictionaryStructure & dictionary_structure)
2017-04-25 09:10:27 +00:00
{
NamesAndTypesList dictionary_names_and_types;
2017-04-28 18:33:31 +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)
2017-12-26 10:54:39 +00:00
dictionary_names_and_types.emplace_back(dictionary_structure.range_min->name, std::make_shared<DataTypeDate>());
if (dictionary_structure.range_max)
2017-12-26 10:54:39 +00:00
dictionary_names_and_types.emplace_back(dictionary_structure.range_max->name, std::make_shared<DataTypeDate>());
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
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
return dictionary_names_and_types;
2017-04-28 18:33:31 +00:00
}
void StorageDictionary::checkNamesAndTypesCompatibleWithDictionary(const DictionaryStructure & dictionary_structure) const
2017-04-28 18:33:31 +00:00
{
auto dictionary_names_and_types = getNamesAndTypes(dictionary_structure);
std::set<NameAndTypePair> namesAndTypesSet(dictionary_names_and_types.begin(), dictionary_names_and_types.end());
2017-04-28 18:33:31 +00:00
for (auto & column : getColumns().ordinary)
{
if (namesAndTypesSet.find(column) == namesAndTypesSet.end())
{
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 ";
message += generateNamesAndTypesDescription(dictionary_names_and_types.begin(), dictionary_names_and_types.end());
2017-04-25 09:10:27 +00:00
throw Exception(message);
}
}
}
void registerStorageDictionary(StorageFactory & factory)
{
factory.registerStorage("Dictionary", [](const StorageFactory::Arguments & args)
{
if (args.engine_args.size() != 1)
throw Exception("Storage Dictionary requires single parameter: name of dictionary",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
args.engine_args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(args.engine_args[0], args.local_context);
String dictionary_name = typeid_cast<const ASTLiteral &>(*args.engine_args[0]).value.safeGet<String>();
const auto & dictionary = args.context.getExternalDictionaries().getDictionary(dictionary_name);
const DictionaryStructure & dictionary_structure = dictionary->getStructure();
return StorageDictionary::create(
args.table_name, args.columns, dictionary_structure, dictionary_name);
});
}
}