feat: support trivial count optimization for Merge

This commit is contained in:
skyoct 2023-12-14 14:08:24 +00:00
parent efc3323fe2
commit d700eb2501
2 changed files with 62 additions and 0 deletions

View File

@ -1153,6 +1153,63 @@ std::tuple<bool /* is_regexp */, ASTPtr> StorageMerge::evaluateDatabaseName(cons
return {false, ast}; 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<UInt64> 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<UInt64>(total_rows);
}
std::optional<UInt64> 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<UInt64>(total_bytes);
}
void registerStorageMerge(StorageFactory & factory) void registerStorageMerge(StorageFactory & factory)
{ {

View File

@ -77,6 +77,11 @@ public:
/// Evaluate database name or regexp for StorageMerge and TableFunction merge /// Evaluate database name or regexp for StorageMerge and TableFunction merge
static std::tuple<bool /* is_regexp */, ASTPtr> evaluateDatabaseName(const ASTPtr & node, ContextPtr context); static std::tuple<bool /* is_regexp */, ASTPtr> evaluateDatabaseName(const ASTPtr & node, ContextPtr context);
bool supportsTrivialCountOptimization() const override;
std::optional<UInt64> totalRows(const Settings &) const override;
std::optional<UInt64> totalBytes(const Settings &) const override;
private: private:
std::optional<OptimizedRegularExpression> source_database_regexp; std::optional<OptimizedRegularExpression> source_database_regexp;
std::optional<OptimizedRegularExpression> source_table_regexp; std::optional<OptimizedRegularExpression> source_table_regexp;