2014-09-17 13:05:18 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-02-10 21:10:58 +00:00
|
|
|
#include <memory>
|
2014-09-17 13:05:18 +00:00
|
|
|
#include <unordered_map>
|
2017-01-14 09:00:19 +00:00
|
|
|
#include <set>
|
|
|
|
#include <DB/Core/Block.h>
|
2014-09-17 13:05:18 +00:00
|
|
|
|
2015-06-05 17:30:24 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
namespace Poco { class Logger; }
|
|
|
|
|
2014-09-17 13:05:18 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
class IAST;
|
|
|
|
using ASTPtr = std::shared_ptr<IAST>;
|
|
|
|
|
|
|
|
class ASTSelectQuery;
|
|
|
|
class ASTFunction;
|
|
|
|
class MergeTreeData;
|
|
|
|
|
|
|
|
using IdentifierNameSet = std::set<std::string>;
|
|
|
|
|
2014-09-17 13:05:18 +00:00
|
|
|
|
2014-09-23 11:35:27 +00:00
|
|
|
/** Identifies WHERE expressions that can be placed in PREWHERE by calculating respective
|
|
|
|
* sizes of columns used in particular expression and identifying "good" conditions of
|
|
|
|
* form "column_name = constant", where "constant" is outside some `threshold` specified in advance.
|
|
|
|
*
|
|
|
|
* If there are "good" conditions present in WHERE, the one with minimal summary column size is
|
|
|
|
* transferred to PREWHERE.
|
|
|
|
* Otherwise any condition with minimal summary column size can be transferred to PREWHERE, if only
|
|
|
|
* its relative size (summary column size divided by query column size) is less than `max_columns_relative_size`.
|
|
|
|
*/
|
2014-09-17 13:05:18 +00:00
|
|
|
class MergeTreeWhereOptimizer
|
|
|
|
{
|
|
|
|
public:
|
2014-09-19 11:44:29 +00:00
|
|
|
MergeTreeWhereOptimizer(const MergeTreeWhereOptimizer&) = delete;
|
|
|
|
MergeTreeWhereOptimizer& operator=(const MergeTreeWhereOptimizer&) = delete;
|
|
|
|
|
2014-10-01 16:38:04 +00:00
|
|
|
MergeTreeWhereOptimizer(
|
2015-09-21 12:13:05 +00:00
|
|
|
ASTPtr & query, const Context & context, const MergeTreeData & data, const Names & column_names,
|
2017-01-14 09:00:19 +00:00
|
|
|
Poco::Logger * log);
|
2014-09-17 13:05:18 +00:00
|
|
|
|
2014-09-19 11:44:29 +00:00
|
|
|
private:
|
2017-01-14 09:00:19 +00:00
|
|
|
void optimize(ASTSelectQuery & select) const;
|
2014-09-18 16:06:56 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
void calculateColumnSizes(const MergeTreeData & data, const Names & column_names);
|
2014-09-18 16:06:56 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
void optimizeConjunction(ASTSelectQuery & select, ASTFunction * const fun) const;
|
2014-09-18 12:58:01 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
void optimizeArbitrary(ASTSelectQuery & select) const;
|
2014-09-18 16:06:56 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
std::size_t getIdentifiersColumnSize(const IdentifierNameSet & identifiers) const;
|
2014-09-17 13:05:18 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
bool isConditionGood(const IAST * condition) const;
|
2015-10-09 14:52:45 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
static void collectIdentifiersNoSubqueries(const IAST * const ast, IdentifierNameSet & set);
|
2014-09-18 16:06:56 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
bool hasPrimaryKeyAtoms(const IAST * ast) const;
|
2014-09-18 16:06:56 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
bool isPrimaryKeyAtom(const IAST * const ast) const;
|
2014-09-17 13:05:18 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
bool isConstant(const ASTPtr & expr) const;
|
2014-09-17 13:05:18 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
bool isSubsetOfTableColumns(const IdentifierNameSet & identifiers) const;
|
2014-10-01 16:38:04 +00:00
|
|
|
|
2015-06-08 16:05:44 +00:00
|
|
|
/** ARRAY JOIN'ed columns as well as arrayJoin() result cannot be used in PREWHERE, therefore expressions
|
2015-07-15 05:07:00 +00:00
|
|
|
* containing said columns should not be moved to PREWHERE at all.
|
|
|
|
* We assume all AS aliases have been expanded prior to using this class
|
|
|
|
*
|
|
|
|
* Also, disallow moving expressions with GLOBAL [NOT] IN.
|
|
|
|
*/
|
2017-01-14 09:00:19 +00:00
|
|
|
bool cannotBeMoved(const IAST * ptr) const;
|
2015-06-08 16:05:44 +00:00
|
|
|
|
2017-01-14 09:00:19 +00:00
|
|
|
void determineArrayJoinedNames(ASTSelectQuery & select);
|
2015-06-08 16:05:44 +00:00
|
|
|
|
2015-09-21 12:13:05 +00:00
|
|
|
using string_set_t = std::unordered_set<std::string>;
|
|
|
|
|
|
|
|
const string_set_t primary_key_columns;
|
|
|
|
const string_set_t table_columns;
|
|
|
|
const Block block_with_constants;
|
2017-01-14 09:00:19 +00:00
|
|
|
Poco::Logger * log;
|
2014-09-17 13:05:18 +00:00
|
|
|
std::unordered_map<std::string, std::size_t> column_sizes{};
|
2014-09-18 16:06:56 +00:00
|
|
|
std::size_t total_column_size{};
|
2015-06-08 16:05:44 +00:00
|
|
|
NameSet array_joined_names;
|
2014-09-17 13:05:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|