ClickHouse/dbms/include/DB/Storages/MergeTree/MergeTreeWhereOptimizer.h
Alexey Milovidov 58e5dad1a1 Squashed commit of the following:
commit e712f469a5
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:59:13 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 2a00282308
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:58:30 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 9e06f407c8
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:55:14 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 9581620f1e
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:54:22 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 2a8564c68c
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:47:34 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit cf60632d78
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:40:09 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit ee3d1dc6e0
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:22:49 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 65592ef711
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:18:17 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 37972c2573
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:17:06 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit dd909d1499
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:16:28 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 3cf43266ca
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:15:42 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 6731a3df96
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:13:35 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 1b5727e0d5
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:11:18 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit bbcf726a55
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:09:04 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit c03b477d5e
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:06:30 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 2986e2fb04
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:05:44 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit 5d6cdef13d
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:04:53 2017 +0300

    Less dependencies [#CLICKHOUSE-2]

commit f2b819b25c
Author: Alexey Milovidov <milovidov@yandex-team.ru>
Date:   Sat Jan 14 11:01:47 2017 +0300

    Less dependencies [#CLICKHOUSE-2]
2017-01-14 12:00:19 +03:00

89 lines
2.7 KiB
C++

#pragma once
#include <memory>
#include <unordered_map>
#include <set>
#include <DB/Core/Block.h>
namespace Poco { class Logger; }
namespace DB
{
class IAST;
using ASTPtr = std::shared_ptr<IAST>;
class ASTSelectQuery;
class ASTFunction;
class MergeTreeData;
using IdentifierNameSet = std::set<std::string>;
/** 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`.
*/
class MergeTreeWhereOptimizer
{
public:
MergeTreeWhereOptimizer(const MergeTreeWhereOptimizer&) = delete;
MergeTreeWhereOptimizer& operator=(const MergeTreeWhereOptimizer&) = delete;
MergeTreeWhereOptimizer(
ASTPtr & query, const Context & context, const MergeTreeData & data, const Names & column_names,
Poco::Logger * log);
private:
void optimize(ASTSelectQuery & select) const;
void calculateColumnSizes(const MergeTreeData & data, const Names & column_names);
void optimizeConjunction(ASTSelectQuery & select, ASTFunction * const fun) const;
void optimizeArbitrary(ASTSelectQuery & select) const;
std::size_t getIdentifiersColumnSize(const IdentifierNameSet & identifiers) const;
bool isConditionGood(const IAST * condition) const;
static void collectIdentifiersNoSubqueries(const IAST * const ast, IdentifierNameSet & set);
bool hasPrimaryKeyAtoms(const IAST * ast) const;
bool isPrimaryKeyAtom(const IAST * const ast) const;
bool isConstant(const ASTPtr & expr) const;
bool isSubsetOfTableColumns(const IdentifierNameSet & identifiers) const;
/** ARRAY JOIN'ed columns as well as arrayJoin() result cannot be used in PREWHERE, therefore expressions
* 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.
*/
bool cannotBeMoved(const IAST * ptr) const;
void determineArrayJoinedNames(ASTSelectQuery & select);
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;
Poco::Logger * log;
std::unordered_map<std::string, std::size_t> column_sizes{};
std::size_t total_column_size{};
NameSet array_joined_names;
};
}