2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <Storages/System/StorageSystemTables.h>
|
|
|
|
#include <Common/VirtualColumnUtils.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2012-05-08 11:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
StorageSystemTables::StorageSystemTables(const std::string & name_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: name(name_),
|
|
|
|
columns
|
|
|
|
{
|
|
|
|
{"database", std::make_shared<DataTypeString>()},
|
|
|
|
{"name", std::make_shared<DataTypeString>()},
|
|
|
|
{"engine", std::make_shared<DataTypeString>()},
|
|
|
|
{"metadata_modification_time", std::make_shared<DataTypeDateTime>()}
|
|
|
|
}
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-21 04:30:33 +00:00
|
|
|
|
2017-05-25 01:12:41 +00:00
|
|
|
static ColumnWithTypeAndName getFilteredDatabases(const ASTPtr & query, const Context & context)
|
2015-01-21 04:30:33 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnWithTypeAndName column;
|
|
|
|
column.name = "database";
|
|
|
|
column.type = std::make_shared<DataTypeString>();
|
|
|
|
column.column = std::make_shared<ColumnString>();
|
2015-01-21 04:30:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & db : context.getDatabases())
|
|
|
|
column.column->insert(db.first);
|
2015-03-26 23:32:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Block block;
|
|
|
|
block.insert(std::move(column));
|
2016-08-04 23:35:07 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
VirtualColumnUtils::filterBlockWithQuery(query, block, context);
|
2015-01-21 04:30:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return block.safeGetByPosition(0);
|
2013-02-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 11:19:00 +00:00
|
|
|
|
|
|
|
BlockInputStreams StorageSystemTables::read(
|
2017-04-01 07:20:54 +00:00
|
|
|
const Names & column_names,
|
2017-07-15 03:48:36 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context,
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
const size_t max_block_size,
|
2017-06-02 15:54:39 +00:00
|
|
|
const unsigned num_streams)
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
check(column_names);
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
|
|
|
|
|
|
|
Block block;
|
|
|
|
|
|
|
|
ColumnWithTypeAndName col_db;
|
|
|
|
col_db.name = "database";
|
|
|
|
col_db.type = std::make_shared<DataTypeString>();
|
|
|
|
col_db.column = std::make_shared<ColumnString>();
|
|
|
|
block.insert(col_db);
|
|
|
|
|
|
|
|
ColumnWithTypeAndName col_name;
|
|
|
|
col_name.name = "name";
|
|
|
|
col_name.type = std::make_shared<DataTypeString>();
|
|
|
|
col_name.column = std::make_shared<ColumnString>();
|
|
|
|
block.insert(col_name);
|
|
|
|
|
|
|
|
ColumnWithTypeAndName col_engine;
|
|
|
|
col_engine.name = "engine";
|
|
|
|
col_engine.type = std::make_shared<DataTypeString>();
|
|
|
|
col_engine.column = std::make_shared<ColumnString>();
|
|
|
|
block.insert(col_engine);
|
|
|
|
|
|
|
|
ColumnWithTypeAndName col_meta_mod_time;
|
|
|
|
col_meta_mod_time.name = "metadata_modification_time";
|
|
|
|
col_meta_mod_time.type = std::make_shared<DataTypeDateTime>();
|
|
|
|
col_meta_mod_time.column = std::make_shared<ColumnUInt32>();
|
|
|
|
block.insert(col_meta_mod_time);
|
|
|
|
|
2017-07-15 03:48:36 +00:00
|
|
|
ColumnWithTypeAndName filtered_databases_column = getFilteredDatabases(query_info.query, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t row_number = 0; row_number < filtered_databases_column.column->size(); ++row_number)
|
|
|
|
{
|
|
|
|
std::string database_name = filtered_databases_column.column->getDataAt(row_number).toString();
|
|
|
|
auto database = context.tryGetDatabase(database_name);
|
|
|
|
|
|
|
|
if (!database)
|
|
|
|
{
|
|
|
|
/// Database was deleted just now.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto iterator = database->getIterator(); iterator->isValid(); iterator->next())
|
|
|
|
{
|
|
|
|
auto table_name = iterator->name();
|
|
|
|
col_db.column->insert(database_name);
|
|
|
|
col_name.column->insert(table_name);
|
|
|
|
col_engine.column->insert(iterator->table()->getName());
|
|
|
|
col_meta_mod_time.column->insert(static_cast<UInt64>(database->getTableMetadataModificationTime(table_name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BlockInputStreams(1, std::make_shared<OneBlockInputStream>(block));
|
2012-05-08 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|