ClickHouse

Форк
0
67 строк · 2.7 Кб
1
#include <Processors/QueryPlan/Optimizations/Optimizations.h>
2
#include <Processors/QueryPlan/FilterStep.h>
3
#include <Processors/QueryPlan/ExpressionStep.h>
4
#include <Interpreters/ActionsDAG.h>
5

6
namespace DB::QueryPlanOptimizations
7
{
8

9
size_t tryMergeExpressions(QueryPlan::Node * parent_node, QueryPlan::Nodes &)
10
{
11
    if (parent_node->children.size() != 1)
12
        return false;
13

14
    QueryPlan::Node * child_node = parent_node->children.front();
15

16
    auto & parent = parent_node->step;
17
    auto & child = child_node->step;
18

19
    auto * parent_expr = typeid_cast<ExpressionStep *>(parent.get());
20
    auto * parent_filter = typeid_cast<FilterStep *>(parent.get());
21
    auto * child_expr = typeid_cast<ExpressionStep *>(child.get());
22

23
    if (parent_expr && child_expr)
24
    {
25
        const auto & child_actions = child_expr->getExpression();
26
        const auto & parent_actions = parent_expr->getExpression();
27

28
        /// We cannot combine actions with arrayJoin and stateful function because we not always can reorder them.
29
        /// Example: select rowNumberInBlock() from (select arrayJoin([1, 2]))
30
        /// Such a query will return two zeroes if we combine actions together.
31
        if (child_actions->hasArrayJoin() && parent_actions->hasStatefulFunctions())
32
            return 0;
33

34
        auto merged = ActionsDAG::merge(std::move(*child_actions), std::move(*parent_actions));
35

36
        auto expr = std::make_unique<ExpressionStep>(child_expr->getInputStreams().front(), merged);
37
        expr->setStepDescription("(" + parent_expr->getStepDescription() + " + " + child_expr->getStepDescription() + ")");
38

39
        parent_node->step = std::move(expr);
40
        parent_node->children.swap(child_node->children);
41
        return 1;
42
    }
43
    else if (parent_filter && child_expr)
44
    {
45
        const auto & child_actions = child_expr->getExpression();
46
        const auto & parent_actions = parent_filter->getExpression();
47

48
        if (child_actions->hasArrayJoin() && parent_actions->hasStatefulFunctions())
49
            return 0;
50

51
        auto merged = ActionsDAG::merge(std::move(*child_actions), std::move(*parent_actions));
52

53
        auto filter = std::make_unique<FilterStep>(child_expr->getInputStreams().front(),
54
                                                   merged,
55
                                                   parent_filter->getFilterColumnName(),
56
                                                   parent_filter->removesFilterColumn());
57
        filter->setStepDescription("(" + parent_filter->getStepDescription() + " + " + child_expr->getStepDescription() + ")");
58

59
        parent_node->step = std::move(filter);
60
        parent_node->children.swap(child_node->children);
61
        return 1;
62
    }
63

64
    return 0;
65
}
66

67
}
68

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.