2015-09-24 03:50:09 +00:00
|
|
|
#include <DB/Storages/System/StorageSystemDictionaries.h>
|
2015-03-24 09:46:07 +00:00
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2015-03-24 13:59:19 +00:00
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
2015-03-24 17:01:49 +00:00
|
|
|
#include <DB/DataTypes/DataTypeDateTime.h>
|
2015-03-24 09:46:07 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-03-24 13:59:19 +00:00
|
|
|
#include <DB/Columns/ColumnArray.h>
|
2015-03-24 09:46:07 +00:00
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DB/Interpreters/Context.h>
|
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
2015-03-25 10:09:33 +00:00
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-03-24 13:59:19 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <DB/Interpreters/ExternalDictionaries.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/map.hpp>
|
2015-03-24 09:46:07 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
StorageSystemDictionaries::StorageSystemDictionaries(const std::string & name)
|
|
|
|
: name{name},
|
|
|
|
columns{
|
2016-05-28 07:48:40 +00:00
|
|
|
{ "name", std::make_shared<DataTypeString>() },
|
|
|
|
{ "origin", std::make_shared<DataTypeString>() },
|
|
|
|
{ "type", std::make_shared<DataTypeString>() },
|
|
|
|
{ "key", std::make_shared<DataTypeString>() },
|
|
|
|
{ "attribute.names", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()) },
|
|
|
|
{ "attribute.types", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()) },
|
|
|
|
{ "bytes_allocated", std::make_shared<DataTypeUInt64>() },
|
|
|
|
{ "query_count", std::make_shared<DataTypeUInt64>() },
|
|
|
|
{ "hit_rate", std::make_shared<DataTypeFloat64>() },
|
|
|
|
{ "element_count", std::make_shared<DataTypeUInt64>() },
|
|
|
|
{ "load_factor", std::make_shared<DataTypeFloat64>() },
|
|
|
|
{ "creation_time", std::make_shared<DataTypeDateTime>() },
|
|
|
|
{ "last_exception", std::make_shared<DataTypeString>() },
|
|
|
|
{ "source", std::make_shared<DataTypeString>() }
|
2015-03-24 09:46:07 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr StorageSystemDictionaries::create(const std::string & name)
|
|
|
|
{
|
|
|
|
return (new StorageSystemDictionaries{name})->thisPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreams StorageSystemDictionaries::read(
|
|
|
|
const Names & column_names,
|
|
|
|
ASTPtr query,
|
|
|
|
const Context & context,
|
|
|
|
const Settings & settings,
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
const size_t max_block_size,
|
|
|
|
const unsigned)
|
|
|
|
{
|
|
|
|
check(column_names);
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
ColumnWithTypeAndName col_name{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "name"};
|
|
|
|
ColumnWithTypeAndName col_origin{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "origin"};
|
|
|
|
ColumnWithTypeAndName col_type{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "type"};
|
|
|
|
ColumnWithTypeAndName col_key{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "key"};
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName col_attribute_names{
|
2016-05-28 05:31:36 +00:00
|
|
|
std::make_shared<ColumnArray>(std::make_shared<ColumnString>()),
|
2016-05-28 07:48:40 +00:00
|
|
|
std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()),
|
2015-03-24 17:01:49 +00:00
|
|
|
"attribute.names"
|
2015-03-24 13:59:19 +00:00
|
|
|
};
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName col_attribute_types{
|
2016-05-28 05:31:36 +00:00
|
|
|
std::make_shared<ColumnArray>(std::make_shared<ColumnString>()),
|
2016-05-28 07:48:40 +00:00
|
|
|
std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>()),
|
2015-03-24 17:01:49 +00:00
|
|
|
"attribute.types"
|
2015-03-24 13:59:19 +00:00
|
|
|
};
|
2016-05-28 07:48:40 +00:00
|
|
|
ColumnWithTypeAndName col_has_hierarchy{std::make_shared<ColumnUInt8>(), std::make_shared<DataTypeUInt8>(), "has_hierarchy"};
|
|
|
|
ColumnWithTypeAndName col_bytes_allocated{std::make_shared<ColumnUInt64>(), std::make_shared<DataTypeUInt64>(), "bytes_allocated"};
|
|
|
|
ColumnWithTypeAndName col_query_count{std::make_shared<ColumnUInt64>(), std::make_shared<DataTypeUInt64>(), "query_count"};
|
|
|
|
ColumnWithTypeAndName col_hit_rate{std::make_shared<ColumnFloat64>(), std::make_shared<DataTypeFloat64>(), "hit_rate"};
|
|
|
|
ColumnWithTypeAndName col_element_count{std::make_shared<ColumnUInt64>(), std::make_shared<DataTypeUInt64>(), "element_count"};
|
|
|
|
ColumnWithTypeAndName col_load_factor{std::make_shared<ColumnFloat64>(), std::make_shared<DataTypeFloat64>(), "load_factor"};
|
|
|
|
ColumnWithTypeAndName col_creation_time{std::make_shared<ColumnUInt32>(), std::make_shared<DataTypeDateTime>(), "creation_time"};
|
|
|
|
ColumnWithTypeAndName col_last_exception{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "last_exception"};
|
|
|
|
ColumnWithTypeAndName col_source{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "source"};
|
2015-03-24 09:46:07 +00:00
|
|
|
|
|
|
|
const auto & external_dictionaries = context.getExternalDictionaries();
|
|
|
|
const std::lock_guard<std::mutex> lock{external_dictionaries.dictionaries_mutex};
|
|
|
|
|
|
|
|
for (const auto & dict_info : external_dictionaries.dictionaries)
|
|
|
|
{
|
2015-05-26 11:53:58 +00:00
|
|
|
col_name.column->insert(dict_info.first);
|
|
|
|
col_origin.column->insert(dict_info.second.origin);
|
2015-03-24 13:59:19 +00:00
|
|
|
|
2015-05-26 11:53:58 +00:00
|
|
|
if (dict_info.second.dict)
|
|
|
|
{
|
|
|
|
const auto dict_ptr = dict_info.second.dict->get();
|
2015-03-24 13:59:19 +00:00
|
|
|
|
2015-05-26 11:53:58 +00:00
|
|
|
col_type.column->insert(dict_ptr->getTypeName());
|
2015-03-24 17:01:49 +00:00
|
|
|
|
2015-05-26 11:53:58 +00:00
|
|
|
const auto & dict_struct = dict_ptr->getStructure();
|
2015-11-20 16:20:54 +00:00
|
|
|
col_key.column->insert(dict_struct.getKeyDescription());
|
|
|
|
|
2015-05-26 11:53:58 +00:00
|
|
|
col_attribute_names.column->insert(ext::map<Array>(dict_struct.attributes, [] (auto & attr) -> decltype(auto) {
|
|
|
|
return attr.name;
|
|
|
|
}));
|
|
|
|
col_attribute_types.column->insert(ext::map<Array>(dict_struct.attributes, [] (auto & attr) -> decltype(auto) {
|
|
|
|
return attr.type->getName();
|
|
|
|
}));
|
|
|
|
col_bytes_allocated.column->insert(dict_ptr->getBytesAllocated());
|
|
|
|
col_query_count.column->insert(dict_ptr->getQueryCount());
|
|
|
|
col_hit_rate.column->insert(dict_ptr->getHitRate());
|
|
|
|
col_element_count.column->insert(dict_ptr->getElementCount());
|
|
|
|
col_load_factor.column->insert(dict_ptr->getLoadFactor());
|
|
|
|
col_creation_time.column->insert(std::chrono::system_clock::to_time_t(dict_ptr->getCreationTime()));
|
|
|
|
col_source.column->insert(dict_ptr->getSource()->toString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
col_type.column->insertDefault();
|
2015-11-20 16:20:54 +00:00
|
|
|
col_key.column->insertDefault();
|
2015-05-26 11:53:58 +00:00
|
|
|
col_attribute_names.column->insertDefault();
|
|
|
|
col_attribute_types.column->insertDefault();
|
|
|
|
col_bytes_allocated.column->insertDefault();
|
|
|
|
col_query_count.column->insertDefault();
|
|
|
|
col_hit_rate.column->insertDefault();
|
|
|
|
col_element_count.column->insertDefault();
|
|
|
|
col_load_factor.column->insertDefault();
|
|
|
|
col_creation_time.column->insertDefault();
|
|
|
|
col_source.column->insertDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dict_info.second.exception)
|
2015-03-24 17:01:49 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-05-26 11:53:58 +00:00
|
|
|
std::rethrow_exception(dict_info.second.exception);
|
2015-03-24 17:01:49 +00:00
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
2015-08-19 13:02:00 +00:00
|
|
|
col_last_exception.column->insert("DB::Exception. Code " + toString(e.code()) + ". " +
|
|
|
|
std::string{e.displayText()});
|
2015-03-24 17:01:49 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
2015-08-19 13:02:00 +00:00
|
|
|
col_last_exception.column->insert("Poco::Exception. " + std::string{e.displayText()});
|
2015-03-24 17:01:49 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
col_last_exception.column->insert("std::exception. " + std::string{e.what()});
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
col_last_exception.column->insert(std::string{"<unknown exception type>"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-05-26 11:53:58 +00:00
|
|
|
col_last_exception.column->insertDefault();
|
2015-03-24 09:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block block{
|
|
|
|
col_name,
|
2015-03-24 12:12:48 +00:00
|
|
|
col_origin,
|
2015-05-26 11:53:58 +00:00
|
|
|
col_type,
|
2015-11-20 16:20:54 +00:00
|
|
|
col_key,
|
2015-03-24 13:59:19 +00:00
|
|
|
col_attribute_names,
|
|
|
|
col_attribute_types,
|
2015-03-24 17:01:49 +00:00
|
|
|
col_bytes_allocated,
|
2015-05-08 12:31:00 +00:00
|
|
|
col_query_count,
|
2015-03-24 17:01:49 +00:00
|
|
|
col_hit_rate,
|
|
|
|
col_element_count,
|
|
|
|
col_load_factor,
|
|
|
|
col_creation_time,
|
2015-03-25 10:09:33 +00:00
|
|
|
col_last_exception,
|
|
|
|
col_source
|
2015-03-24 09:46:07 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 12:22:22 +00:00
|
|
|
return BlockInputStreams{1, std::make_shared<OneBlockInputStream>(block)};
|
2015-03-24 09:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|