dbms: filtered tables in 'show tables from' query [#METR-13667]

This commit is contained in:
Pavel Kartavyy 2014-11-13 15:27:42 +03:00
parent ed6b36d33f
commit 14c231c055
4 changed files with 42 additions and 3 deletions

View File

@ -38,6 +38,8 @@ private:
NamesAndTypesList columns; NamesAndTypesList columns;
StorageSystemTables(const std::string & name_, const Context & context_); StorageSystemTables(const std::string & name_, const Context & context_);
ColumnWithNameAndType getFilteredDatabases(ASTPtr query);
}; };
} }

View File

@ -2,6 +2,7 @@
#include <DB/DataTypes/DataTypeString.h> #include <DB/DataTypes/DataTypeString.h>
#include <DB/DataStreams/OneBlockInputStream.h> #include <DB/DataStreams/OneBlockInputStream.h>
#include <DB/Storages/StorageSystemTables.h> #include <DB/Storages/StorageSystemTables.h>
#include <DB/Common/VirtualColumnUtils.h>
namespace DB namespace DB
@ -51,11 +52,19 @@ BlockInputStreams StorageSystemTables::read(
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex()); Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
for (Databases::const_iterator it = context.getDatabases().begin(); it != context.getDatabases().end(); ++it) ColumnWithNameAndType filtered_databases_column = getFilteredDatabases(query);
for (size_t row_number = 0; row_number < filtered_databases_column.column->size(); ++row_number)
{ {
for (Tables::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt) std::string database_name = filtered_databases_column.column->getDataAt(row_number).toString();
auto database_it = context.getDatabases().find(database_name);
if (database_it == context.getDatabases().end())
throw DB::Exception(std::string("Fail to find database " + database_name), DB::ErrorCodes::LOGICAL_ERROR);
for (Tables::const_iterator jt = database_it->second.begin(); jt != database_it->second.end(); ++jt)
{ {
col_db.column->insert(it->first); col_db.column->insert(database_name);
col_name.column->insert(jt->first); col_name.column->insert(jt->first);
col_engine.column->insert(jt->second->getName()); col_engine.column->insert(jt->second->getName());
} }
@ -64,5 +73,22 @@ BlockInputStreams StorageSystemTables::read(
return BlockInputStreams(1, new OneBlockInputStream(block)); return BlockInputStreams(1, new OneBlockInputStream(block));
} }
ColumnWithNameAndType StorageSystemTables::getFilteredDatabases(ASTPtr query)
{
ColumnWithNameAndType filtered_databases_column;
filtered_databases_column.name = "database";
filtered_databases_column.type = new DataTypeString;
filtered_databases_column.column = new ColumnString;
Block filtered_databases_block;
filtered_databases_block.insert(filtered_databases_column);
for (auto database_it = context.getDatabases().begin(); database_it != context.getDatabases().end(); ++database_it)
{
filtered_databases_column.column->insert(database_it->first);
}
VirtualColumnUtils::filterBlockWithQuery(query, filtered_databases_block, context);
return filtered_databases_column;
}
} }

View File

@ -0,0 +1,2 @@
A
B

View File

@ -0,0 +1,9 @@
DROP DATABASE IF EXISTS test_show_tables;
CREATE DATABASE test_show_tables;
CREATE TABLE test_show_tables.A (A UInt8) ENGINE = TinyLog;
CREATE TABLE test_show_tables.B (A UInt8) ENGINE = TinyLog;
SHOW TABLES from test_show_tables;