2015-09-24 03:50:09 +00:00
|
|
|
|
#include <DB/Storages/System/StorageSystemColumns.h>
|
2015-07-26 11:34:41 +00:00
|
|
|
|
#include <DB/Storages/MergeTree/MergeTreeData.h>
|
|
|
|
|
#include <DB/Storages/StorageMergeTree.h>
|
|
|
|
|
#include <DB/Storages/StorageReplicatedMergeTree.h>
|
2015-04-24 12:26:23 +00:00
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
2015-07-26 11:34:41 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2015-04-24 12:26:23 +00:00
|
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
2015-04-24 15:49:30 +00:00
|
|
|
|
#include <DB/Common/VirtualColumnUtils.h>
|
2016-03-19 01:18:49 +00:00
|
|
|
|
#include <DB/Databases/IDatabase.h>
|
|
|
|
|
|
2015-04-24 12:26:23 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
StorageSystemColumns::StorageSystemColumns(const std::string & name_)
|
|
|
|
|
: name(name_)
|
|
|
|
|
, columns{
|
2016-05-28 07:48:40 +00:00
|
|
|
|
{ "database", std::make_shared<DataTypeString>() },
|
|
|
|
|
{ "table", std::make_shared<DataTypeString>() },
|
|
|
|
|
{ "name", std::make_shared<DataTypeString>() },
|
|
|
|
|
{ "type", std::make_shared<DataTypeString>() },
|
|
|
|
|
{ "default_type", std::make_shared<DataTypeString>() },
|
|
|
|
|
{ "default_expression", std::make_shared<DataTypeString>() },
|
|
|
|
|
{ "bytes", std::make_shared<DataTypeUInt64>() },
|
2015-04-24 12:26:23 +00:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StoragePtr StorageSystemColumns::create(const std::string & name_)
|
|
|
|
|
{
|
|
|
|
|
return (new StorageSystemColumns{name_})->thisPtr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlockInputStreams StorageSystemColumns::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 threads)
|
|
|
|
|
{
|
2015-04-24 15:49:30 +00:00
|
|
|
|
check(column_names);
|
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
|
|
|
|
|
2015-04-24 23:23:25 +00:00
|
|
|
|
Block block;
|
2015-04-24 12:26:23 +00:00
|
|
|
|
|
2015-04-24 16:49:32 +00:00
|
|
|
|
std::map<std::pair<std::string, std::string>, StoragePtr> storages;
|
|
|
|
|
|
2015-04-24 12:26:23 +00:00
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
|
Databases databases = context.getDatabases();
|
2015-04-24 16:49:32 +00:00
|
|
|
|
|
2015-04-27 09:43:23 +00:00
|
|
|
|
/// Добавляем столбец database.
|
2016-05-28 05:31:36 +00:00
|
|
|
|
ColumnPtr database_column = std::make_shared<ColumnString>();
|
2015-04-24 15:49:30 +00:00
|
|
|
|
for (const auto & database : databases)
|
2015-04-24 23:23:25 +00:00
|
|
|
|
database_column->insert(database.first);
|
2016-05-28 07:48:40 +00:00
|
|
|
|
block.insert(ColumnWithTypeAndName(database_column, std::make_shared<DataTypeString>(), "database"));
|
2015-04-24 23:23:25 +00:00
|
|
|
|
|
|
|
|
|
/// Отфильтруем блок со столбцом database.
|
|
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, block, context);
|
|
|
|
|
|
|
|
|
|
if (!block.rows())
|
|
|
|
|
return BlockInputStreams();
|
|
|
|
|
|
|
|
|
|
database_column = block.getByName("database").column;
|
|
|
|
|
size_t rows = database_column->size();
|
|
|
|
|
|
2015-04-27 09:43:23 +00:00
|
|
|
|
/// Добавляем столбец table.
|
2016-05-28 05:31:36 +00:00
|
|
|
|
ColumnPtr table_column = std::make_shared<ColumnString>();
|
2015-04-27 09:43:23 +00:00
|
|
|
|
IColumn::Offsets_t offsets(rows);
|
2015-04-24 23:23:25 +00:00
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
2015-04-24 15:49:30 +00:00
|
|
|
|
{
|
2015-04-27 09:43:23 +00:00
|
|
|
|
const std::string database_name = (*database_column)[i].get<std::string>();
|
2016-03-19 01:18:49 +00:00
|
|
|
|
const DatabasePtr database = databases.at(database_name);
|
2015-04-24 23:23:25 +00:00
|
|
|
|
offsets[i] = i ? offsets[i - 1] : 0;
|
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
for (auto iterator = database->getIterator(); iterator->isValid(); iterator->next())
|
2015-04-24 12:26:23 +00:00
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
|
const String & table_name = iterator->name();
|
|
|
|
|
storages.emplace(std::piecewise_construct,
|
|
|
|
|
std::forward_as_tuple(database_name, table_name),
|
|
|
|
|
std::forward_as_tuple(iterator->table()));
|
|
|
|
|
table_column->insert(table_name);
|
2015-04-24 23:23:25 +00:00
|
|
|
|
offsets[i] += 1;
|
2015-04-24 12:26:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 23:23:25 +00:00
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
|
{
|
|
|
|
|
ColumnPtr & column = block.getByPosition(i).column;
|
|
|
|
|
column = column->replicate(offsets);
|
|
|
|
|
}
|
2015-04-24 16:49:32 +00:00
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
block.insert(ColumnWithTypeAndName(table_column, std::make_shared<DataTypeString>(), "table"));
|
2015-04-24 16:49:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 23:23:25 +00:00
|
|
|
|
/// Отфильтруем блок со столбцами database и table.
|
|
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, block, context);
|
|
|
|
|
|
|
|
|
|
if (!block.rows())
|
|
|
|
|
return BlockInputStreams();
|
|
|
|
|
|
|
|
|
|
ColumnPtr filtered_database_column = block.getByName("database").column;
|
|
|
|
|
ColumnPtr filtered_table_column = block.getByName("table").column;
|
|
|
|
|
|
2015-04-27 09:43:23 +00:00
|
|
|
|
/// Составляем результат.
|
2016-05-28 05:31:36 +00:00
|
|
|
|
ColumnPtr database_column = std::make_shared<ColumnString>();
|
|
|
|
|
ColumnPtr table_column = std::make_shared<ColumnString>();
|
|
|
|
|
ColumnPtr name_column = std::make_shared<ColumnString>();
|
|
|
|
|
ColumnPtr type_column = std::make_shared<ColumnString>();
|
|
|
|
|
ColumnPtr default_type_column = std::make_shared<ColumnString>();
|
|
|
|
|
ColumnPtr default_expression_column = std::make_shared<ColumnString>();
|
|
|
|
|
ColumnPtr bytes_column = std::make_shared<ColumnUInt64>();
|
2015-04-24 23:23:25 +00:00
|
|
|
|
|
|
|
|
|
size_t rows = filtered_database_column->size();
|
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
2015-04-24 16:49:32 +00:00
|
|
|
|
{
|
2015-04-27 09:43:23 +00:00
|
|
|
|
const std::string database_name = (*filtered_database_column)[i].get<std::string>();
|
|
|
|
|
const std::string table_name = (*filtered_table_column)[i].get<std::string>();
|
2015-04-24 16:49:32 +00:00
|
|
|
|
|
|
|
|
|
NamesAndTypesList columns;
|
|
|
|
|
ColumnDefaults column_defaults;
|
2015-07-26 11:34:41 +00:00
|
|
|
|
std::unordered_map<String, size_t> column_sizes;
|
2015-04-24 16:49:32 +00:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
StoragePtr storage = storages.at(std::make_pair(database_name, table_name));
|
|
|
|
|
auto table_lock = storage->lockStructure(false);
|
|
|
|
|
|
|
|
|
|
columns = storage->getColumnsList();
|
|
|
|
|
columns.insert(std::end(columns), std::begin(storage->alias_columns), std::end(storage->alias_columns));
|
|
|
|
|
column_defaults = storage->column_defaults;
|
2015-07-26 11:34:41 +00:00
|
|
|
|
|
|
|
|
|
/** Данные о размерах столбцов для таблиц семейства MergeTree.
|
|
|
|
|
* NOTE: В дальнейшем можно сделать интерфейс, позволяющий получить размеры столбцов у IStorage.
|
|
|
|
|
*/
|
|
|
|
|
if (auto storage_concrete = dynamic_cast<StorageMergeTree *>(storage.get()))
|
|
|
|
|
{
|
|
|
|
|
column_sizes = storage_concrete->getData().getColumnSizes();
|
|
|
|
|
}
|
|
|
|
|
else if (auto storage_concrete = dynamic_cast<StorageReplicatedMergeTree *>(storage.get()))
|
|
|
|
|
{
|
|
|
|
|
column_sizes = storage_concrete->getData().getColumnSizes();
|
|
|
|
|
|
|
|
|
|
auto unreplicated_data = storage_concrete->getUnreplicatedData();
|
|
|
|
|
if (unreplicated_data)
|
|
|
|
|
{
|
|
|
|
|
auto unreplicated_column_sizes = unreplicated_data->getColumnSizes();
|
|
|
|
|
for (const auto & name_size : unreplicated_column_sizes)
|
|
|
|
|
column_sizes[name_size.first] += name_size.second;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-24 16:49:32 +00:00
|
|
|
|
}
|
2015-04-24 15:49:30 +00:00
|
|
|
|
|
2015-04-24 16:49:32 +00:00
|
|
|
|
for (const auto & column : columns)
|
|
|
|
|
{
|
2015-04-24 23:23:25 +00:00
|
|
|
|
database_column->insert(database_name);
|
|
|
|
|
table_column->insert(table_name);
|
|
|
|
|
name_column->insert(column.name);
|
|
|
|
|
type_column->insert(column.type->getName());
|
2015-04-24 16:49:32 +00:00
|
|
|
|
|
|
|
|
|
{
|
2015-07-26 11:34:41 +00:00
|
|
|
|
const auto it = column_defaults.find(column.name);
|
|
|
|
|
if (it == std::end(column_defaults))
|
|
|
|
|
{
|
|
|
|
|
default_type_column->insertDefault();
|
|
|
|
|
default_expression_column->insertDefault();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
default_type_column->insert(toString(it->second.type));
|
|
|
|
|
default_expression_column->insert(queryToString(it->second.expression));
|
|
|
|
|
}
|
2015-04-24 16:49:32 +00:00
|
|
|
|
}
|
2015-07-26 11:34:41 +00:00
|
|
|
|
|
2015-04-24 16:49:32 +00:00
|
|
|
|
{
|
2015-07-26 11:34:41 +00:00
|
|
|
|
const auto it = column_sizes.find(column.name);
|
|
|
|
|
if (it == std::end(column_sizes))
|
|
|
|
|
bytes_column->insertDefault();
|
|
|
|
|
else
|
|
|
|
|
bytes_column->insert(it->second);
|
2015-04-24 16:49:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-24 15:49:30 +00:00
|
|
|
|
|
2015-04-24 23:23:25 +00:00
|
|
|
|
block.clear();
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
block.insert(ColumnWithTypeAndName(database_column, std::make_shared<DataTypeString>(), "database"));
|
|
|
|
|
block.insert(ColumnWithTypeAndName(table_column, std::make_shared<DataTypeString>(), "table"));
|
|
|
|
|
block.insert(ColumnWithTypeAndName(name_column, std::make_shared<DataTypeString>(), "name"));
|
|
|
|
|
block.insert(ColumnWithTypeAndName(type_column, std::make_shared<DataTypeString>(), "type"));
|
|
|
|
|
block.insert(ColumnWithTypeAndName(default_type_column, std::make_shared<DataTypeString>(), "default_type"));
|
|
|
|
|
block.insert(ColumnWithTypeAndName(default_expression_column, std::make_shared<DataTypeString>(), "default_expression"));
|
|
|
|
|
block.insert(ColumnWithTypeAndName(bytes_column, std::make_shared<DataTypeUInt64>(), "bytes"));
|
2015-04-24 23:23:25 +00:00
|
|
|
|
|
|
|
|
|
return BlockInputStreams{ 1, new OneBlockInputStream(block) };
|
2015-04-24 12:26:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|