2018-03-04 16:15:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2019-12-18 03:56:03 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2019-02-11 19:53:55 +00:00
|
|
|
|
2018-03-04 16:15:31 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-11-26 17:35:24 +00:00
|
|
|
class ASTSelectQuery;
|
2020-01-04 04:31:45 +00:00
|
|
|
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
|
2020-01-04 04:31:45 +00:00
|
|
|
* 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
|
2018-03-04 16:15:31 +00:00
|
|
|
*/
|
2021-06-01 12:20:52 +00:00
|
|
|
class PredicateExpressionsOptimizer : WithContext
|
2018-03-04 16:15:31 +00:00
|
|
|
{
|
2019-12-18 03:56:03 +00:00
|
|
|
public:
|
2021-06-01 12:20:52 +00:00
|
|
|
PredicateExpressionsOptimizer(ContextPtr context_, const TablesWithColumns & tables_with_columns_, const Settings & settings_);
|
2019-12-18 03:56:03 +00:00
|
|
|
|
|
|
|
bool optimize(ASTSelectQuery & select_query);
|
2019-01-25 15:42:24 +00:00
|
|
|
|
2019-12-18 03:56:03 +00:00
|
|
|
private:
|
2020-06-05 21:17:00 +00:00
|
|
|
const bool enable_optimize_predicate_expression;
|
|
|
|
const bool enable_optimize_predicate_expression_to_final_subquery;
|
2020-07-24 09:00:18 +00:00
|
|
|
const bool allow_push_predicate_when_subquery_contains_with;
|
2020-06-05 21:17:00 +00:00
|
|
|
const TablesWithColumns & tables_with_columns;
|
2018-03-04 16:15:31 +00:00
|
|
|
|
2019-12-18 03:56:03 +00:00
|
|
|
std::vector<ASTs> extractTablesPredicates(const ASTPtr & where, const ASTPtr & prewhere);
|
2018-08-22 06:42:37 +00:00
|
|
|
|
2019-12-18 03:56:03 +00:00
|
|
|
bool tryRewritePredicatesToTables(ASTs & tables_element, const std::vector<ASTs> & tables_predicates);
|
2018-09-27 05:27:45 +00:00
|
|
|
|
2021-03-24 12:48:29 +00:00
|
|
|
bool tryRewritePredicatesToTable(
|
|
|
|
ASTPtr & table_element, const ASTs & table_predicates, const TableWithColumnNamesAndTypes & table_columns) const;
|
2020-01-04 04:31:45 +00:00
|
|
|
|
|
|
|
bool tryMovePredicatesFromHavingToWhere(ASTSelectQuery & select_query);
|
2018-03-04 16:15:31 +00:00
|
|
|
};
|
|
|
|
|
2018-08-14 21:48:39 +00:00
|
|
|
}
|