Merge pull request #57996 from CurtizJ/better-trivial-count-merge

Better trivial count optimization for storage `Merge`
This commit is contained in:
Anton Popov 2023-12-19 02:25:58 +01:00 committed by GitHub
commit bfd403cc8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 supported = true;
forEachTable([&](const auto & table)
{
supported &= table->supportsTrivialCountOptimization();
});
return supported;
return getFirstTable([&](const auto & table) { return !table->supportsTrivialCountOptimization(); }) == nullptr;
}
std::optional<UInt64> StorageMerge::totalRows(const Settings & settings) const
{
UInt64 total_rows = 0;
forEachTable([&](const auto & table)
{
std::optional<UInt64> rows = table->totalRows(settings);
if (rows)
total_rows += *rows;
});
return {total_rows};
return totalRowsOrBytes([&](const auto & table) { return table->totalRows(settings); });
}
std::optional<UInt64> StorageMerge::totalBytes(const Settings & settings) const
{
UInt64 total_bytes = 0;
forEachTable([&](const auto & table)
{
std::optional<UInt64> bytes = table->totalBytes(settings);
if (bytes)
total_bytes += *bytes;
});
return {total_bytes};
return totalRowsOrBytes([&](const auto & table) { return table->totalBytes(settings); });
}
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)
{

View File

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