better trivial count optimization for storage Merge

This commit is contained in:
Anton Popov 2023-12-18 15:13:43 +00:00
parent bd17ee769e
commit 7ebb7d2955
2 changed files with 24 additions and 24 deletions

View File

@ -1295,38 +1295,35 @@ std::tuple<bool /* is_regexp */, ASTPtr> StorageMerge::evaluateDatabaseName(cons
bool StorageMerge::supportsTrivialCountOptimization() const bool StorageMerge::supportsTrivialCountOptimization() const
{ {
bool supported = true; return getFirstTable([&](const auto & table) { return !table->supportsTrivialCountOptimization(); }) == nullptr;
forEachTable([&](const auto & table)
{
supported &= table->supportsTrivialCountOptimization();
});
return supported;
} }
std::optional<UInt64> StorageMerge::totalRows(const Settings & settings) const std::optional<UInt64> StorageMerge::totalRows(const Settings & settings) const
{ {
UInt64 total_rows = 0; return totalRowsOrBytes([&](const auto & table) { return table->totalRows(settings); });
forEachTable([&](const auto & table)
{
std::optional<UInt64> rows = table->totalRows(settings);
if (rows)
total_rows += *rows;
});
return {total_rows};
} }
std::optional<UInt64> StorageMerge::totalBytes(const Settings & settings) const std::optional<UInt64> StorageMerge::totalBytes(const Settings & settings) const
{ {
UInt64 total_bytes = 0; return totalRowsOrBytes([&](const auto & table) { return table->totalBytes(settings); });
forEachTable([&](const auto & table)
{
std::optional<UInt64> bytes = table->totalBytes(settings);
if (bytes)
total_bytes += *bytes;
});
return {total_bytes};
} }
template <typename F>
std::optional<UInt64> StorageMerge::totalRowsOrBytes(F && func) const
{
UInt64 total_rows_or_bytes = 0;
auto first_table = getFirstTable([&](const auto & table)
{
if (auto rows_or_bytes = func(table))
{
total_rows_or_bytes += *rows_or_bytes;
return false;
}
return true;
});
return first_table ? std::nullopt : std::make_optional(total_rows_or_bytes);
}
void registerStorageMerge(StorageFactory & factory) void registerStorageMerge(StorageFactory & factory)
{ {

View File

@ -79,8 +79,8 @@ public:
bool supportsTrivialCountOptimization() const override; bool supportsTrivialCountOptimization() const override;
std::optional<UInt64> totalRows(const Settings &) const override; std::optional<UInt64> totalRows(const Settings & settings) const override;
std::optional<UInt64> totalBytes(const Settings &) const override; std::optional<UInt64> totalBytes(const Settings & settings) const override;
private: private:
std::optional<OptimizedRegularExpression> source_database_regexp; std::optional<OptimizedRegularExpression> source_database_regexp;
@ -118,6 +118,9 @@ private:
bool tableSupportsPrewhere() const; bool tableSupportsPrewhere() const;
template <typename F>
std::optional<UInt64> totalRowsOrBytes(F && func) const;
friend class ReadFromMerge; friend class ReadFromMerge;
}; };