From 14c231c055bc37b4c6fc95e55f6c7c1ab69a34f8 Mon Sep 17 00:00:00 2001 From: Pavel Kartavyy Date: Thu, 13 Nov 2014 15:27:42 +0300 Subject: [PATCH] dbms: filtered tables in 'show tables from' query [#METR-13667] --- .../include/DB/Storages/StorageSystemTables.h | 2 ++ dbms/src/Storages/StorageSystemTables.cpp | 32 +++++++++++++++++-- .../0_stateless/00080_show_tables.reference | 2 ++ .../queries/0_stateless/00080_show_tables.sql | 9 ++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00080_show_tables.reference create mode 100644 dbms/tests/queries/0_stateless/00080_show_tables.sql diff --git a/dbms/include/DB/Storages/StorageSystemTables.h b/dbms/include/DB/Storages/StorageSystemTables.h index 67dca8aaf2b..d4b1dee4881 100644 --- a/dbms/include/DB/Storages/StorageSystemTables.h +++ b/dbms/include/DB/Storages/StorageSystemTables.h @@ -38,6 +38,8 @@ private: NamesAndTypesList columns; StorageSystemTables(const std::string & name_, const Context & context_); + + ColumnWithNameAndType getFilteredDatabases(ASTPtr query); }; } diff --git a/dbms/src/Storages/StorageSystemTables.cpp b/dbms/src/Storages/StorageSystemTables.cpp index 68acc504df7..2866d2453e5 100644 --- a/dbms/src/Storages/StorageSystemTables.cpp +++ b/dbms/src/Storages/StorageSystemTables.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace DB @@ -51,11 +52,19 @@ BlockInputStreams StorageSystemTables::read( Poco::ScopedLock 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_engine.column->insert(jt->second->getName()); } @@ -64,5 +73,22 @@ BlockInputStreams StorageSystemTables::read( 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; +} } diff --git a/dbms/tests/queries/0_stateless/00080_show_tables.reference b/dbms/tests/queries/0_stateless/00080_show_tables.reference new file mode 100644 index 00000000000..35d242ba79a --- /dev/null +++ b/dbms/tests/queries/0_stateless/00080_show_tables.reference @@ -0,0 +1,2 @@ +A +B diff --git a/dbms/tests/queries/0_stateless/00080_show_tables.sql b/dbms/tests/queries/0_stateless/00080_show_tables.sql new file mode 100644 index 00000000000..872b8e7e224 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00080_show_tables.sql @@ -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; +