diff --git a/src/Storages/StorageMerge.cpp b/src/Storages/StorageMerge.cpp index 7958cdf9f50..5faadffc7a2 100644 --- a/src/Storages/StorageMerge.cpp +++ b/src/Storages/StorageMerge.cpp @@ -1153,6 +1153,63 @@ std::tuple StorageMerge::evaluateDatabaseName(cons return {false, ast}; } +bool StorageMerge::supportsTrivialCountOptimization() const +{ + bool is_support = true; + + auto database_table_iterators = getDatabaseIterators(getContext()); + for (auto & iterator : database_table_iterators) + { + while (iterator->isValid()) + { + const auto & table = iterator->table(); + is_support = table->supportsTrivialCountOptimization(); + iterator->next(); + } + } + return is_support; +} + +std::optional StorageMerge::totalRows(const Settings &) const +{ + UInt64 total_rows = 0; + + auto database_table_iterators = getDatabaseIterators(getContext()); + for (auto & iterator : database_table_iterators) + { + while (iterator->isValid()) + { + const auto & table = iterator->table(); + auto table_rows = table->totalRows(getContext()->getSettingsRef()); + if (table_rows){ + total_rows += *table_rows; + } + iterator->next(); + } + } + return std::make_optional(total_rows); +} + +std::optional StorageMerge::totalBytes(const Settings & ) const +{ + UInt64 total_bytes = 0; + + auto database_table_iterators = getDatabaseIterators(getContext()); + for (auto & iterator : database_table_iterators) + { + while (iterator->isValid()) + { + const auto & table = iterator->table(); + auto table_bytes = table->totalBytes(getContext()->getSettingsRef()); + if (total_bytes){ + total_bytes += *table_bytes; + } + iterator->next(); + } + } + return std::make_optional(total_bytes); +} + void registerStorageMerge(StorageFactory & factory) { diff --git a/src/Storages/StorageMerge.h b/src/Storages/StorageMerge.h index 80a5fa335f7..8b6d15910db 100644 --- a/src/Storages/StorageMerge.h +++ b/src/Storages/StorageMerge.h @@ -77,6 +77,11 @@ public: /// Evaluate database name or regexp for StorageMerge and TableFunction merge static std::tuple evaluateDatabaseName(const ASTPtr & node, ContextPtr context); + bool supportsTrivialCountOptimization() const override; + + std::optional totalRows(const Settings &) const override; + std::optional totalBytes(const Settings &) const override; + private: std::optional source_database_regexp; std::optional source_table_regexp;