From 0cd36019280bc243d9b5bb926cfa2ec08ccaf623 Mon Sep 17 00:00:00 2001 From: Maxim Akhmedov Date: Sat, 6 Feb 2021 18:08:42 +0300 Subject: [PATCH] Allow using MergeTreeWhereOptimizer not only with MergeTree-based storages --- src/Interpreters/InterpreterSelectQuery.cpp | 9 +++++++-- .../MergeTree/MergeTreeWhereOptimizer.cpp | 18 +++++------------- .../MergeTree/MergeTreeWhereOptimizer.h | 4 +--- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 2ee1b3956e4..6122719d94e 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -390,13 +390,18 @@ InterpreterSelectQuery::InterpreterSelectQuery( if (try_move_to_prewhere && storage && !row_policy_filter && query.where() && !query.prewhere() && !query.final()) { /// PREWHERE optimization: transfer some condition from WHERE to PREWHERE if enabled and viable - if (const auto * merge_tree = dynamic_cast(storage.get())) + if (const auto & column_sizes = storage->getColumnSizes(); !column_sizes.empty()) { + /// Extract column compressed sizes. + std::unordered_map column_compressed_sizes; + for (const auto & [name, sizes] : column_sizes) + column_compressed_sizes[name] = sizes.data_compressed; + SelectQueryInfo current_info; current_info.query = query_ptr; current_info.syntax_analyzer_result = syntax_analyzer_result; - MergeTreeWhereOptimizer{current_info, *context, *merge_tree, metadata_snapshot, syntax_analyzer_result->requiredSourceColumns(), log}; + MergeTreeWhereOptimizer{current_info, *context, std::move(column_compressed_sizes), metadata_snapshot, syntax_analyzer_result->requiredSourceColumns(), log}; } } diff --git a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp index 5d6b74cabe9..34cac56d74c 100644 --- a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp +++ b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp @@ -30,7 +30,7 @@ static constexpr auto threshold = 2; MergeTreeWhereOptimizer::MergeTreeWhereOptimizer( SelectQueryInfo & query_info, const Context & context, - const MergeTreeData & data, + std::unordered_map column_sizes_, const StorageMetadataPtr & metadata_snapshot, const Names & queried_columns_, Poco::Logger * log_) @@ -39,28 +39,20 @@ MergeTreeWhereOptimizer::MergeTreeWhereOptimizer( , queried_columns{queried_columns_} , block_with_constants{KeyCondition::getBlockWithConstants(query_info.query, query_info.syntax_analyzer_result, context)} , log{log_} + , column_sizes{std::move(column_sizes_)} { const auto & primary_key = metadata_snapshot->getPrimaryKey(); if (!primary_key.column_names.empty()) first_primary_key_column = primary_key.column_names[0]; - calculateColumnSizes(data, queried_columns); + for (const auto & [_, size] : column_sizes) + total_size_of_queried_columns += size; + determineArrayJoinedNames(query_info.query->as()); optimize(query_info.query->as()); } -void MergeTreeWhereOptimizer::calculateColumnSizes(const MergeTreeData & data, const Names & column_names) -{ - for (const auto & column_name : column_names) - { - UInt64 size = data.getColumnCompressedSize(column_name); - column_sizes[column_name] = size; - total_size_of_queried_columns += size; - } -} - - static void collectIdentifiersNoSubqueries(const ASTPtr & ast, NameSet & set) { if (auto opt_name = tryGetIdentifierName(ast)) diff --git a/src/Storages/MergeTree/MergeTreeWhereOptimizer.h b/src/Storages/MergeTree/MergeTreeWhereOptimizer.h index 939c265b3e5..cad77fb9eed 100644 --- a/src/Storages/MergeTree/MergeTreeWhereOptimizer.h +++ b/src/Storages/MergeTree/MergeTreeWhereOptimizer.h @@ -33,7 +33,7 @@ public: MergeTreeWhereOptimizer( SelectQueryInfo & query_info, const Context & context, - const MergeTreeData & data, + std::unordered_map column_sizes_, const StorageMetadataPtr & metadata_snapshot, const Names & queried_columns_, Poco::Logger * log_); @@ -75,8 +75,6 @@ private: /// Transform Conditions list to WHERE or PREWHERE expression. static ASTPtr reconstruct(const Conditions & conditions); - void calculateColumnSizes(const MergeTreeData & data, const Names & column_names); - void optimizeConjunction(ASTSelectQuery & select, ASTFunction * const fun) const; void optimizeArbitrary(ASTSelectQuery & select) const;