ClickHouse/dbms/Interpreters/PredicateExpressionsOptimizer.h

54 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
2019-04-09 16:49:52 +00:00
#include <Parsers/ASTSelectQuery.h>
2019-12-18 03:56:03 +00:00
#include <Interpreters/DatabaseAndTableWithAlias.h>
namespace DB
{
class Context;
struct Settings;
/** Predicate optimization based on rewriting ast rules
2019-09-23 16:18:19 +00:00
* For more details : https://github.com/ClickHouse/ClickHouse/pull/2015#issuecomment-374283452
* The optimizer does two different optimizations
* - Move predicates from having to where
* - Push the predicate down from the current query to the having of the subquery
*/
class PredicateExpressionsOptimizer
{
2019-12-18 03:56:03 +00:00
public:
PredicateExpressionsOptimizer(const Context & context_, const TablesWithColumnNames & tables_with_columns_, const Settings & settings_);
bool optimize(ASTSelectQuery & select_query);
2019-12-18 03:56:03 +00:00
private:
/// Extracts settings, mostly to show which are used and which are not.
struct ExtractedSettings
{
2018-10-19 15:42:47 +00:00
const bool enable_optimize_predicate_expression;
2019-07-25 01:05:16 +00:00
const bool enable_optimize_predicate_expression_to_final_subquery;
2018-10-19 15:42:47 +00:00
template<typename T>
2019-08-03 11:02:40 +00:00
ExtractedSettings(const T & settings_)
2019-12-18 03:56:03 +00:00
: enable_optimize_predicate_expression(settings_.enable_optimize_predicate_expression),
enable_optimize_predicate_expression_to_final_subquery(settings_.enable_optimize_predicate_expression_to_final_subquery)
{}
};
const Context & context;
2019-12-18 03:56:03 +00:00
const std::vector<TableWithColumnNames> & tables_with_columns;
2019-12-18 03:56:03 +00:00
const ExtractedSettings settings;
2019-12-18 03:56:03 +00:00
std::vector<ASTs> extractTablesPredicates(const ASTPtr & where, const ASTPtr & prewhere);
2019-12-18 03:56:03 +00:00
bool tryRewritePredicatesToTables(ASTs & tables_element, const std::vector<ASTs> & tables_predicates);
2019-12-18 03:56:03 +00:00
bool tryRewritePredicatesToTable(ASTPtr & table_element, const ASTs & table_predicates, const Names & table_column) const;
bool tryMovePredicatesFromHavingToWhere(ASTSelectQuery & select_query);
};
2018-08-14 21:48:39 +00:00
}