2014-07-29 14:05:15 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
#include <DB/DataTypes/DataTypeDateTime.h>
|
2015-07-13 11:45:59 +00:00
|
|
|
#include <DB/DataTypes/DataTypeDate.h>
|
2014-07-29 14:05:15 +00:00
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
2015-09-24 03:50:09 +00:00
|
|
|
#include <DB/Storages/System/StorageSystemParts.h>
|
2014-07-29 15:21:03 +00:00
|
|
|
#include <DB/Storages/StorageMergeTree.h>
|
|
|
|
#include <DB/Storages/StorageReplicatedMergeTree.h>
|
2014-07-29 14:05:15 +00:00
|
|
|
#include <DB/Common/VirtualColumnUtils.h>
|
2016-03-19 01:18:49 +00:00
|
|
|
#include <DB/Databases/IDatabase.h>
|
2014-07-29 14:05:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2015-01-21 03:56:28 +00:00
|
|
|
StorageSystemParts::StorageSystemParts(const std::string & name_)
|
2015-03-26 23:32:16 +00:00
|
|
|
: name(name_),
|
|
|
|
columns
|
|
|
|
{
|
2016-05-28 07:48:40 +00:00
|
|
|
{"partition", std::make_shared<DataTypeString>()},
|
|
|
|
{"name", std::make_shared<DataTypeString>()},
|
|
|
|
{"replicated", std::make_shared<DataTypeUInt8>()},
|
|
|
|
{"active", std::make_shared<DataTypeUInt8>()},
|
|
|
|
{"marks", std::make_shared<DataTypeUInt64>()},
|
|
|
|
{"bytes", std::make_shared<DataTypeUInt64>()},
|
|
|
|
{"modification_time", std::make_shared<DataTypeDateTime>()},
|
|
|
|
{"remove_time", std::make_shared<DataTypeDateTime>()},
|
|
|
|
{"refcount", std::make_shared<DataTypeUInt32>()},
|
|
|
|
{"min_date", std::make_shared<DataTypeDate>()},
|
|
|
|
{"max_date", std::make_shared<DataTypeDate>()},
|
|
|
|
{"min_block_number", std::make_shared<DataTypeInt64>()},
|
|
|
|
{"max_block_number", std::make_shared<DataTypeInt64>()},
|
|
|
|
{"level", std::make_shared<DataTypeUInt32>()},
|
2017-01-17 20:49:16 +00:00
|
|
|
{"primary_key_bytes_in_memory", std::make_shared<DataTypeUInt64>()},
|
2017-01-17 20:54:32 +00:00
|
|
|
{"primary_key_bytes_in_memory_allocated", std::make_shared<DataTypeUInt64>()},
|
2016-05-28 07:48:40 +00:00
|
|
|
|
|
|
|
{"database", std::make_shared<DataTypeString>()},
|
|
|
|
{"table", std::make_shared<DataTypeString>()},
|
|
|
|
{"engine", std::make_shared<DataTypeString>()},
|
2015-03-26 23:32:16 +00:00
|
|
|
}
|
2014-07-29 14:05:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-21 03:56:28 +00:00
|
|
|
StoragePtr StorageSystemParts::create(const std::string & name_)
|
2014-07-29 14:05:15 +00:00
|
|
|
{
|
2016-08-26 21:25:05 +00:00
|
|
|
return make_shared(name_);
|
2014-07-29 14:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreams StorageSystemParts::read(
|
2014-12-17 11:53:17 +00:00
|
|
|
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)
|
2014-07-29 14:05:15 +00:00
|
|
|
{
|
|
|
|
check(column_names);
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// Will apply WHERE to subset of columns and then add more columns.
|
|
|
|
/// This is kind of complicated, but we use WHERE to do less work.
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
Block block_to_filter;
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2014-07-29 15:21:03 +00:00
|
|
|
std::map<std::pair<String, String>, StoragePtr> storages;
|
|
|
|
|
2014-07-29 14:05:15 +00:00
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
Databases databases = context.getDatabases();
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// Add column 'database'.
|
2016-05-28 05:31:36 +00:00
|
|
|
ColumnPtr database_column = std::make_shared<ColumnString>();
|
2014-07-29 14:05:15 +00:00
|
|
|
for (const auto & database : databases)
|
|
|
|
database_column->insert(database.first);
|
2017-01-17 20:49:16 +00:00
|
|
|
block_to_filter.insert(ColumnWithTypeAndName(database_column, std::make_shared<DataTypeString>(), "database"));
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// Filter block_to_filter with column 'database'.
|
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, block_to_filter, context);
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
if (!block_to_filter.rows())
|
2014-07-29 15:21:03 +00:00
|
|
|
return BlockInputStreams();
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// Add columns 'table', 'engine', 'active', 'replicated'.
|
|
|
|
database_column = block_to_filter.getByName("database").column;
|
2014-07-29 15:21:03 +00:00
|
|
|
size_t rows = database_column->size();
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2014-07-29 15:21:03 +00:00
|
|
|
IColumn::Offsets_t offsets(rows);
|
2016-05-28 05:31:36 +00:00
|
|
|
ColumnPtr table_column = std::make_shared<ColumnString>();
|
|
|
|
ColumnPtr engine_column = std::make_shared<ColumnString>();
|
|
|
|
ColumnPtr replicated_column = std::make_shared<ColumnUInt8>();
|
|
|
|
ColumnPtr active_column = std::make_shared<ColumnUInt8>();
|
2014-07-29 15:21:03 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
String database_name = (*database_column)[i].get<String>();
|
|
|
|
const DatabasePtr database = databases.at(database_name);
|
|
|
|
|
2014-07-29 15:21:03 +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())
|
2014-07-29 15:21:03 +00:00
|
|
|
{
|
2016-03-19 01:18:49 +00:00
|
|
|
String table_name = iterator->name();
|
|
|
|
StoragePtr storage = iterator->table();
|
|
|
|
String engine_name = storage->getName();
|
|
|
|
|
2014-07-29 15:21:03 +00:00
|
|
|
if (!dynamic_cast<StorageMergeTree *>(&*storage) &&
|
|
|
|
!dynamic_cast<StorageReplicatedMergeTree *>(&*storage))
|
|
|
|
continue;
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
storages[std::make_pair(database_name, iterator->name())] = storage;
|
2014-07-29 15:21:03 +00:00
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// Add all four combinations of flags 'replicated' and 'active'.
|
2016-03-19 01:18:49 +00:00
|
|
|
for (UInt64 replicated : {0, 1})
|
|
|
|
{
|
|
|
|
for (UInt64 active : {0, 1})
|
|
|
|
{
|
|
|
|
table_column->insert(table_name);
|
|
|
|
engine_column->insert(engine_name);
|
|
|
|
replicated_column->insert(replicated);
|
|
|
|
active_column->insert(active);
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 15:21:03 +00:00
|
|
|
|
|
|
|
offsets[i] += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
for (size_t i = 0; i < block_to_filter.columns(); ++i)
|
2014-07-29 15:21:03 +00:00
|
|
|
{
|
2017-01-17 20:49:16 +00:00
|
|
|
ColumnPtr & column = block_to_filter.safeGetByPosition(i).column;
|
2014-07-29 15:21:03 +00:00
|
|
|
column = column->replicate(offsets);
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
block_to_filter.insert(ColumnWithTypeAndName(table_column, std::make_shared<DataTypeString>(), "table"));
|
|
|
|
block_to_filter.insert(ColumnWithTypeAndName(engine_column, std::make_shared<DataTypeString>(), "engine"));
|
|
|
|
block_to_filter.insert(ColumnWithTypeAndName(replicated_column, std::make_shared<DataTypeUInt8>(), "replicated"));
|
|
|
|
block_to_filter.insert(ColumnWithTypeAndName(active_column, std::make_shared<DataTypeUInt8>(), "active"));
|
2014-07-29 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// Filter block_to_filter with columns 'database', 'table', 'engine', 'replicated', 'active'.
|
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, block_to_filter, context);
|
|
|
|
|
|
|
|
/// If all was filtered out.
|
|
|
|
if (!block_to_filter.rows())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ColumnPtr filtered_database_column = block_to_filter.getByName("database").column;
|
|
|
|
ColumnPtr filtered_table_column = block_to_filter.getByName("table").column;
|
|
|
|
ColumnPtr filtered_replicated_column = block_to_filter.getByName("replicated").column;
|
|
|
|
ColumnPtr filtered_active_column = block_to_filter.getByName("active").column;
|
|
|
|
|
|
|
|
/// Finally, create the result.
|
|
|
|
|
|
|
|
Block block = getSampleBlock();
|
2014-07-29 14:05:15 +00:00
|
|
|
|
2014-07-29 15:21:03 +00:00
|
|
|
for (size_t i = 0; i < filtered_database_column->size();)
|
2014-07-29 14:05:15 +00:00
|
|
|
{
|
2014-07-29 15:21:03 +00:00
|
|
|
String database = (*filtered_database_column)[i].get<String>();
|
|
|
|
String table = (*filtered_table_column)[i].get<String>();
|
|
|
|
|
2017-01-17 20:49:16 +00:00
|
|
|
/// What combinations of 'replicated' and 'active' values we need.
|
2014-07-29 15:21:03 +00:00
|
|
|
bool need[2][2]{}; /// [replicated][active]
|
|
|
|
for (; i < filtered_database_column->size() &&
|
|
|
|
(*filtered_database_column)[i].get<String>() == database &&
|
|
|
|
(*filtered_table_column)[i].get<String>() == table; ++i)
|
2014-07-29 14:05:15 +00:00
|
|
|
{
|
2014-07-29 15:21:03 +00:00
|
|
|
bool replicated = !!(*filtered_replicated_column)[i].get<UInt64>();
|
|
|
|
bool active = !!(*filtered_active_column)[i].get<UInt64>();
|
|
|
|
need[replicated][active] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr storage = storages.at(std::make_pair(database, table));
|
2016-07-26 17:22:55 +00:00
|
|
|
IStorage::TableStructureReadLockPtr table_lock;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
table_lock = storage->lockStructure(false); /// For table not to be dropped.
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
/** There are case when IStorage::drop was called,
|
2017-01-17 20:49:16 +00:00
|
|
|
* but we still own the object.
|
|
|
|
* Then table will throw exception at attempt to lock it.
|
|
|
|
* Just skip the table.
|
|
|
|
*/
|
2016-07-26 17:22:55 +00:00
|
|
|
if (e.code() == ErrorCodes::TABLE_IS_DROPPED)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2014-07-29 15:21:03 +00:00
|
|
|
|
|
|
|
String engine = storage->getName();
|
|
|
|
|
|
|
|
MergeTreeData * data[2]{}; /// [0] - unreplicated, [1] - replicated.
|
|
|
|
|
|
|
|
if (StorageMergeTree * merge_tree = dynamic_cast<StorageMergeTree *>(&*storage))
|
|
|
|
{
|
|
|
|
data[0] = &merge_tree->getData();
|
|
|
|
}
|
|
|
|
else if (StorageReplicatedMergeTree * replicated_merge_tree = dynamic_cast<StorageReplicatedMergeTree *>(&*storage))
|
|
|
|
{
|
|
|
|
data[0] = replicated_merge_tree->getUnreplicatedData();
|
|
|
|
data[1] = &replicated_merge_tree->getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (UInt64 replicated = 0; replicated <= 1; ++replicated)
|
|
|
|
{
|
|
|
|
if (!need[replicated][0] && !need[replicated][1])
|
|
|
|
continue;
|
|
|
|
if (!data[replicated])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MergeTreeData::DataParts active_parts = data[replicated]->getDataParts();
|
|
|
|
MergeTreeData::DataParts all_parts;
|
|
|
|
if (need[replicated][0])
|
|
|
|
all_parts = data[replicated]->getAllDataParts();
|
|
|
|
else
|
|
|
|
all_parts = active_parts;
|
|
|
|
|
|
|
|
/// Наконец пройдем по списку кусочков.
|
|
|
|
for (const MergeTreeData::DataPartPtr & part : all_parts)
|
|
|
|
{
|
2016-02-03 01:17:58 +00:00
|
|
|
LocalDate partition_date {part->month};
|
2014-10-09 22:54:17 +00:00
|
|
|
String partition = toString(partition_date.year()) + (partition_date.month() < 10 ? "0" : "") + toString(partition_date.month());
|
2017-01-17 20:49:16 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
block.getByPosition(i++).column->insert(partition);
|
|
|
|
block.getByPosition(i++).column->insert(part->name);
|
|
|
|
block.getByPosition(i++).column->insert(replicated);
|
|
|
|
block.getByPosition(i++).column->insert(static_cast<UInt64>(!need[replicated][0] || active_parts.count(part)));
|
|
|
|
block.getByPosition(i++).column->insert(part->size);
|
|
|
|
block.getByPosition(i++).column->insert(static_cast<size_t>(part->size_in_bytes));
|
|
|
|
block.getByPosition(i++).column->insert(part->modification_time);
|
|
|
|
block.getByPosition(i++).column->insert(part->remove_time);
|
|
|
|
|
|
|
|
/// For convenience, in returned refcount, don't add references that was due to local variables in this method: all_parts, active_parts.
|
|
|
|
block.getByPosition(i++).column->insert(part.use_count() - (active_parts.count(part) ? 2 : 1));
|
|
|
|
|
|
|
|
block.getByPosition(i++).column->insert(static_cast<UInt64>(part->left_date));
|
|
|
|
block.getByPosition(i++).column->insert(static_cast<UInt64>(part->right_date));
|
|
|
|
block.getByPosition(i++).column->insert(part->left);
|
|
|
|
block.getByPosition(i++).column->insert(part->right);
|
|
|
|
block.getByPosition(i++).column->insert(static_cast<UInt64>(part->level));
|
|
|
|
block.getByPosition(i++).column->insert(part->getIndexSizeInBytes());
|
2017-01-17 20:54:32 +00:00
|
|
|
block.getByPosition(i++).column->insert(part->getIndexSizeInAllocatedBytes());
|
2017-01-17 20:49:16 +00:00
|
|
|
|
|
|
|
block.getByPosition(i++).column->insert(database);
|
|
|
|
block.getByPosition(i++).column->insert(table);
|
|
|
|
block.getByPosition(i++).column->insert(engine);
|
2014-07-29 15:21:03 +00:00
|
|
|
}
|
2014-07-29 14:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 12:22:22 +00:00
|
|
|
return BlockInputStreams(1, std::make_shared<OneBlockInputStream>(block));
|
2014-07-29 14:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|