diff --git a/docs/en/operations/system-tables/dropped_tables_parts.md b/docs/en/operations/system-tables/dropped_tables_parts.md deleted file mode 100644 index 095f35287fe..00000000000 --- a/docs/en/operations/system-tables/dropped_tables_parts.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -slug: /en/operations/system-tables/dropped_tables_parts ---- -# dropped_tables_parts {#system_tables-dropped_tables_parts} - -Contains information about parts of [MergeTree](../../engines/table-engines/mergetree-family/mergetree.md) dropped tables from [system.dropped_tables](./dropped_tables.md) - -The schema of this table is the same as [system.parts](./parts.md) - -**See Also** - -- [MergeTree family](../../engines/table-engines/mergetree-family/mergetree.md) -- [system.parts](./parts.md) -- [system.dropped_tables](./dropped_tables.md) diff --git a/src/Storages/System/StorageSystemDroppedTablesParts.cpp b/src/Storages/System/StorageSystemDroppedTablesParts.cpp deleted file mode 100644 index 9253cc99d72..00000000000 --- a/src/Storages/System/StorageSystemDroppedTablesParts.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#include -#include -#include - - -namespace DB -{ - - -StoragesDroppedInfoStream::StoragesDroppedInfoStream(const SelectQueryInfo & query_info, ContextPtr context) - : StoragesInfoStreamBase(context) -{ - needsLock = false; - - /// 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. - - Block block_to_filter; - - MutableColumnPtr database_column_mut = ColumnString::create(); - MutableColumnPtr table_column_mut = ColumnString::create(); - MutableColumnPtr engine_column_mut = ColumnString::create(); - MutableColumnPtr active_column_mut = ColumnUInt8::create(); - MutableColumnPtr storage_uuid_column_mut = ColumnUUID::create(); - - const auto access = context->getAccess(); - const bool check_access_for_tables = !access->isGranted(AccessType::SHOW_TABLES); - - auto tables_mark_dropped = DatabaseCatalog::instance().getTablesMarkedDropped(); - for (const auto & dropped_table : tables_mark_dropped) - { - StoragePtr storage = dropped_table.table; - if (!storage) - continue; - - UUID storage_uuid = storage->getStorageID().uuid; - String database_name = storage->getStorageID().getDatabaseName(); - String table_name = storage->getStorageID().getTableName(); - String engine_name = storage->getName(); -#if USE_MYSQL - if (auto * proxy = dynamic_cast(storage.get())) - { - auto nested = proxy->getNested(); - storage.swap(nested); - } -#endif - if (!dynamic_cast(storage.get())) - continue; - - if (check_access_for_tables && !access->isGranted(AccessType::SHOW_TABLES, database_name, table_name)) - continue; - - storages[storage_uuid] = storage; - - /// Add all combinations of flag 'active'. - for (UInt64 active : {0, 1}) - { - database_column_mut->insert(database_name); - table_column_mut->insert(table_name); - engine_column_mut->insert(engine_name); - active_column_mut->insert(active); - storage_uuid_column_mut->insert(storage_uuid); - } - } - - block_to_filter.insert(ColumnWithTypeAndName(std::move(database_column_mut), std::make_shared(), "database")); - block_to_filter.insert(ColumnWithTypeAndName(std::move(table_column_mut), std::make_shared(), "table")); - block_to_filter.insert(ColumnWithTypeAndName(std::move(engine_column_mut), std::make_shared(), "engine")); - block_to_filter.insert(ColumnWithTypeAndName(std::move(active_column_mut), std::make_shared(), "active")); - block_to_filter.insert(ColumnWithTypeAndName(std::move(storage_uuid_column_mut), std::make_shared(), "uuid")); - - if (block_to_filter.rows()) - { - /// Filter block_to_filter with columns 'database', 'table', 'engine', 'active'. - VirtualColumnUtils::filterBlockWithQuery(query_info.query, block_to_filter, context); - rows = block_to_filter.rows(); - } - - database_column = block_to_filter.getByName("database").column; - table_column = block_to_filter.getByName("table").column; - active_column = block_to_filter.getByName("active").column; - storage_uuid_column = block_to_filter.getByName("uuid").column; -} - - -} diff --git a/src/Storages/System/StorageSystemDroppedTablesParts.h b/src/Storages/System/StorageSystemDroppedTablesParts.h deleted file mode 100644 index 1a8a27e0b7c..00000000000 --- a/src/Storages/System/StorageSystemDroppedTablesParts.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include - - -namespace DB -{ - -class StoragesDroppedInfoStream : public StoragesInfoStreamBase -{ -public: - StoragesDroppedInfoStream(const SelectQueryInfo & query_info, ContextPtr context); -}; - -class Context; - - -/** Implements system table 'dropped_tables_parts' which allows to get information about data parts for dropped but not yet removed tables. - */ -class StorageSystemDroppedTablesParts final : public StorageSystemParts -{ -public: - explicit StorageSystemDroppedTablesParts(const StorageID & table_id) : StorageSystemParts(table_id) {} - - std::string getName() const override { return "SystemDroppedTablesParts"; } -protected: - std::unique_ptr getStoragesInfoStream(const SelectQueryInfo & query_info, ContextPtr context) override - { - return std::make_unique(query_info, context); - } -}; - -} diff --git a/src/Storages/System/StorageSystemParts.h b/src/Storages/System/StorageSystemParts.h index e0082e40e7d..c7a46cfda54 100644 --- a/src/Storages/System/StorageSystemParts.h +++ b/src/Storages/System/StorageSystemParts.h @@ -11,7 +11,7 @@ class Context; /** Implements system table 'parts' which allows to get information about data parts for tables of MergeTree family. */ -class StorageSystemParts : public StorageSystemPartsBase +class StorageSystemParts final : public StorageSystemPartsBase { public: explicit StorageSystemParts(const StorageID & table_id_); diff --git a/src/Storages/System/StorageSystemPartsBase.cpp b/src/Storages/System/StorageSystemPartsBase.cpp index 7b2e9fe18a7..8d2e2900722 100644 --- a/src/Storages/System/StorageSystemPartsBase.cpp +++ b/src/Storages/System/StorageSystemPartsBase.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -23,6 +22,11 @@ namespace DB { +namespace ErrorCodes +{ + extern const int LOGICAL_ERROR; +} + bool StorageSystemPartsBase::hasStateColumn(const Names & column_names, const StorageSnapshotPtr & storage_snapshot) { bool has_state_column = false; @@ -79,7 +83,7 @@ StoragesInfo::getProjectionParts(MergeTreeData::DataPartStateVector & state, boo } StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, ContextPtr context) - : StoragesInfoStreamBase(context) + : query_id(context->getCurrentQueryId()), settings(context->getSettingsRef()) { /// 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. @@ -89,7 +93,6 @@ StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, Conte MutableColumnPtr table_column_mut = ColumnString::create(); MutableColumnPtr engine_column_mut = ColumnString::create(); MutableColumnPtr active_column_mut = ColumnUInt8::create(); - MutableColumnPtr storage_uuid_column_mut = ColumnUUID::create(); const auto access = context->getAccess(); const bool check_access_for_tables = !access->isGranted(AccessType::SHOW_TABLES); @@ -136,7 +139,6 @@ StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, Conte continue; String engine_name = storage->getName(); - UUID storage_uuid = storage->getStorageID().uuid; #if USE_MYSQL if (auto * proxy = dynamic_cast(storage.get())) @@ -151,7 +153,7 @@ StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, Conte if (check_access_for_tables && !access->isGranted(AccessType::SHOW_TABLES, database_name, table_name)) continue; - storages[storage_uuid] = storage; + storages[std::make_pair(database_name, iterator->name())] = storage; /// Add all combinations of flag 'active'. for (UInt64 active : {0, 1}) @@ -159,7 +161,6 @@ StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, Conte table_column_mut->insert(table_name); engine_column_mut->insert(engine_name); active_column_mut->insert(active); - storage_uuid_column_mut->insert(storage_uuid); } offsets[i] += 2; @@ -177,7 +178,6 @@ StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, Conte block_to_filter.insert(ColumnWithTypeAndName(std::move(table_column_mut), std::make_shared(), "table")); block_to_filter.insert(ColumnWithTypeAndName(std::move(engine_column_mut), std::make_shared(), "engine")); block_to_filter.insert(ColumnWithTypeAndName(std::move(active_column_mut), std::make_shared(), "active")); - block_to_filter.insert(ColumnWithTypeAndName(std::move(storage_uuid_column_mut), std::make_shared(), "uuid")); if (rows) { @@ -189,9 +189,57 @@ StoragesInfoStream::StoragesInfoStream(const SelectQueryInfo & query_info, Conte database_column = block_to_filter.getByName("database").column; table_column = block_to_filter.getByName("table").column; active_column = block_to_filter.getByName("active").column; - storage_uuid_column = block_to_filter.getByName("uuid").column; + + next_row = 0; } +StoragesInfo StoragesInfoStream::next() +{ + while (next_row < rows) + { + StoragesInfo info; + + info.database = (*database_column)[next_row].get(); + info.table = (*table_column)[next_row].get(); + + auto is_same_table = [&info, this] (size_t row) -> bool + { + return (*database_column)[row].get() == info.database && + (*table_column)[row].get() == info.table; + }; + + /// We may have two rows per table which differ in 'active' value. + /// If rows with 'active = 0' were not filtered out, this means we + /// must collect the inactive parts. Remember this fact in StoragesInfo. + for (; next_row < rows && is_same_table(next_row); ++next_row) + { + const auto active = (*active_column)[next_row].get(); + if (active == 0) + info.need_inactive_parts = true; + } + + info.storage = storages.at(std::make_pair(info.database, info.table)); + + /// For table not to be dropped and set of columns to remain constant. + info.table_lock = info.storage->tryLockForShare(query_id, settings.lock_acquire_timeout); + + if (info.table_lock == nullptr) + { + // Table was dropped while acquiring the lock, skipping table + continue; + } + + info.engine = info.storage->getName(); + + info.data = dynamic_cast(info.storage.get()); + if (!info.data) + throw Exception(ErrorCodes::LOGICAL_ERROR, "Unknown engine {}", info.engine); + + return info; + } + + return {}; +} Pipe StorageSystemPartsBase::read( const Names & column_names, @@ -204,7 +252,7 @@ Pipe StorageSystemPartsBase::read( { bool has_state_column = hasStateColumn(column_names, storage_snapshot); - auto stream = getStoragesInfoStream(query_info, context); + StoragesInfoStream stream(query_info, context); /// Create the result. Block sample = storage_snapshot->metadata->getSampleBlock(); @@ -215,7 +263,7 @@ Pipe StorageSystemPartsBase::read( if (has_state_column) res_columns.push_back(ColumnString::create()); - while (StoragesInfo info = stream->next()) + while (StoragesInfo info = stream.next()) { processNextStorage(context, res_columns, columns_mask, info, has_state_column); } diff --git a/src/Storages/System/StorageSystemPartsBase.h b/src/Storages/System/StorageSystemPartsBase.h index fd20b0756b2..c3d2e64b303 100644 --- a/src/Storages/System/StorageSystemPartsBase.h +++ b/src/Storages/System/StorageSystemPartsBase.h @@ -8,11 +8,6 @@ namespace DB { -namespace ErrorCodes -{ - extern const int LOGICAL_ERROR; -} - class Context; struct StoragesInfo @@ -34,64 +29,13 @@ struct StoragesInfo }; /** A helper class that enumerates the storages that match given query. */ -class StoragesInfoStreamBase +class StoragesInfoStream { public: - StoragesInfoStreamBase(ContextPtr context) - : query_id(context->getCurrentQueryId()), settings(context->getSettingsRef()), next_row(0), rows(0) - {} + StoragesInfoStream(const SelectQueryInfo & query_info, ContextPtr context); + StoragesInfo next(); - StoragesInfo next() - { - while (next_row < rows) - { - StoragesInfo info; - - info.database = (*database_column)[next_row].get(); - info.table = (*table_column)[next_row].get(); - UUID storage_uuid = (*storage_uuid_column)[next_row].get(); - - auto is_same_table = [&storage_uuid, this] (size_t row) -> bool - { - return (*storage_uuid_column)[row].get() == storage_uuid; - }; - - /// We may have two rows per table which differ in 'active' value. - /// If rows with 'active = 0' were not filtered out, this means we - /// must collect the inactive parts. Remember this fact in StoragesInfo. - for (; next_row < rows && is_same_table(next_row); ++next_row) - { - const auto active = (*active_column)[next_row].get(); - if (active == 0) - info.need_inactive_parts = true; - } - - info.storage = storages.at(storage_uuid); - - if (needsLock) - { - /// For table not to be dropped and set of columns to remain constant. - info.table_lock = info.storage->tryLockForShare(query_id, settings.lock_acquire_timeout); - if (info.table_lock == nullptr) - { - // Table was dropped while acquiring the lock, skipping table - continue; - } - } - - info.engine = info.storage->getName(); - - info.data = dynamic_cast(info.storage.get()); - if (!info.data) - throw Exception(ErrorCodes::LOGICAL_ERROR, "Unknown engine {}", info.engine); - - return info; - } - - return {}; - } - -protected: +private: String query_id; Settings settings; @@ -99,22 +43,12 @@ protected: ColumnPtr database_column; ColumnPtr table_column; ColumnPtr active_column; - ColumnPtr storage_uuid_column; size_t next_row; size_t rows; - using StoragesMap = std::unordered_map; + using StoragesMap = std::map, StoragePtr>; StoragesMap storages; - - bool needsLock = true; -}; - - -class StoragesInfoStream : public StoragesInfoStreamBase -{ -public: - StoragesInfoStream(const SelectQueryInfo & query_info, ContextPtr context); }; /** Implements system table 'parts' which allows to get information about data parts for tables of MergeTree family. @@ -143,11 +77,6 @@ protected: StorageSystemPartsBase(const StorageID & table_id_, NamesAndTypesList && columns_); - virtual std::unique_ptr getStoragesInfoStream(const SelectQueryInfo & query_info, ContextPtr context) - { - return std::make_unique(query_info, context); - } - virtual void processNextStorage(ContextPtr context, MutableColumns & columns, std::vector & columns_mask, const StoragesInfo & info, bool has_state_column) = 0; }; diff --git a/src/Storages/System/attachSystemTables.cpp b/src/Storages/System/attachSystemTables.cpp index 57f37c8899e..ffe74f1c94a 100644 --- a/src/Storages/System/attachSystemTables.cpp +++ b/src/Storages/System/attachSystemTables.cpp @@ -82,7 +82,6 @@ #include #include #include -#include #include #include #include @@ -157,7 +156,6 @@ void attachSystemTablesLocal(ContextPtr context, IDatabase & system_database) attach(context, system_database, "backups"); attach(context, system_database, "schema_inference_cache"); attach(context, system_database, "dropped_tables"); - attach(context, system_database, "dropped_tables_parts"); attach(context, system_database, "scheduler"); #if defined(__ELF__) && !defined(OS_FREEBSD) attach(context, system_database, "symbols");