ClickHouse/dbms/src/Storages/StorageDictionary.cpp

126 lines
5.0 KiB
C++
Raw Normal View History

2017-04-25 09:10:27 +00:00
#include <sstream>
#include <Parsers/ASTCreateQuery.h>
2017-04-28 18:33:31 +00:00
#include <DataTypes/DataTypesNumber.h>
2017-04-25 09:10:27 +00:00
#include <Dictionaries/IDictionarySource.h>
#include <Dictionaries/DictionaryStructure.h>
2017-04-27 17:16:24 +00:00
#include <Dictionaries/CacheDictionary.h>
2017-04-25 09:10:27 +00:00
#include <Storages/StorageDictionary.h>
#include <Interpreters/Context.h>
2017-06-22 15:44:19 +00:00
#include <Interpreters/ExternalDictionaries.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
StoragePtr StorageDictionary::create(
2017-06-22 15:44:19 +00:00
const String & table_name,
Context & context,
const ASTCreateQuery & query,
2017-06-22 15:44:19 +00:00
NamesAndTypesListPtr columns,
const NamesAndTypesList & materialized_columns,
const NamesAndTypesList & alias_columns,
const ColumnDefaults & column_defaults)
{
const ASTFunction & engine = *query.storage->engine;
2017-06-22 15:44:19 +00:00
String dictionary_name;
if (engine.arguments)
{
2017-06-22 15:44:19 +00:00
std::stringstream iss;
engine.arguments->format(IAST::FormatSettings(iss, false, false));
2017-06-22 15:44:19 +00:00
dictionary_name = iss.str();
}
const auto & dictionary = context.getExternalDictionaries().getDictionary(dictionary_name);
const DictionaryStructure & dictionary_structure = dictionary->getStructure();
return ext::shared_ptr_helper<StorageDictionary>::create(
table_name, columns, materialized_columns, alias_columns,
column_defaults, dictionary_structure, dictionary_name);
2017-06-22 15:44:19 +00:00
}
StoragePtr StorageDictionary::create(
const String & table_name,
NamesAndTypesListPtr columns,
const NamesAndTypesList & materialized_columns,
const NamesAndTypesList & alias_columns,
const ColumnDefaults & column_defaults,
const DictionaryStructure & dictionary_structure,
const String & dictionary_name)
2017-04-25 09:10:27 +00:00
{
return ext::shared_ptr_helper<StorageDictionary>::create(
table_name, columns, materialized_columns, alias_columns,
column_defaults, dictionary_structure, dictionary_name);
2017-04-25 09:10:27 +00:00
}
StorageDictionary::StorageDictionary(
const String & table_name_,
NamesAndTypesListPtr columns_,
const NamesAndTypesList & materialized_columns_,
const NamesAndTypesList & alias_columns_,
const ColumnDefaults & column_defaults_,
2017-06-22 15:44:19 +00:00
const DictionaryStructure & dictionary_structure_,
const String & dictionary_name_)
2017-04-25 09:10:27 +00:00
: IStorage{materialized_columns_, alias_columns_, column_defaults_}, table_name(table_name_),
columns(columns_), 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
}
2017-06-22 15:44:19 +00:00
NamesAndTypesListPtr StorageDictionary::getNamesAndTypes(const DictionaryStructure & dictionaryStructure)
2017-04-25 09:10:27 +00:00
{
2017-06-22 15:44:19 +00:00
NamesAndTypesListPtr dictionaryNamesAndTypes = std::make_shared<NamesAndTypesList>();
2017-04-28 18:33:31 +00:00
if (dictionaryStructure.id)
2017-06-22 15:44:19 +00:00
dictionaryNamesAndTypes->push_back(NameAndTypePair(dictionaryStructure.id->name,
2017-04-28 18:33:31 +00:00
std::make_shared<DataTypeUInt64>()));
if (dictionaryStructure.range_min)
2017-06-22 15:44:19 +00:00
dictionaryNamesAndTypes->push_back(NameAndTypePair(dictionaryStructure.range_min->name,
2017-04-28 18:33:31 +00:00
std::make_shared<DataTypeUInt16>()));
if (dictionaryStructure.range_max)
2017-06-22 15:44:19 +00:00
dictionaryNamesAndTypes->push_back(NameAndTypePair(dictionaryStructure.range_max->name,
2017-04-28 18:33:31 +00:00
std::make_shared<DataTypeUInt16>()));
if (dictionaryStructure.key)
for (const auto & attribute : *dictionaryStructure.key)
2017-06-22 15:44:19 +00:00
dictionaryNamesAndTypes->push_back(NameAndTypePair(attribute.name, attribute.type));
2017-04-28 18:33:31 +00:00
for (const auto & attribute : dictionaryStructure.attributes)
2017-06-22 15:44:19 +00:00
dictionaryNamesAndTypes->push_back(NameAndTypePair(attribute.name, attribute.type));
2017-04-28 18:33:31 +00:00
return dictionaryNamesAndTypes;
}
2017-06-22 15:44:19 +00:00
void StorageDictionary::checkNamesAndTypesCompatibleWithDictionary(const DictionaryStructure & dictionaryStructure) const
2017-04-28 18:33:31 +00:00
{
2017-06-22 15:44:19 +00:00
auto dictionaryNamesAndTypes = getNamesAndTypes(dictionaryStructure);
std::set<NameAndTypePair> namesAndTypesSet(dictionaryNamesAndTypes->begin(), dictionaryNamesAndTypes->end());
2017-04-28 18:33:31 +00:00
for (auto & column : *columns)
{
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 ";
2017-06-22 15:44:19 +00:00
message += generateNamesAndTypesDescription(dictionaryNamesAndTypes->begin(), dictionaryNamesAndTypes->end());
2017-04-25 09:10:27 +00:00
throw Exception(message);
}
}
}
}