aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/Passes/OptimizeRedundantFunctionsInOrderByPass.cpp
blob: 875d0c8b5fbcd141785ac7f487ce4e716c6ae636 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <Analyzer/Passes/OptimizeRedundantFunctionsInOrderByPass.h>

#include <Functions/IFunction.h>

#include <Analyzer/ColumnNode.h>
#include <Analyzer/FunctionNode.h>
#include <Analyzer/HashUtils.h>
#include <Analyzer/InDepthQueryTreeVisitor.h>
#include <Analyzer/QueryNode.h>
#include <Analyzer/SortNode.h>

namespace DB
{

namespace
{

class OptimizeRedundantFunctionsInOrderByVisitor : public InDepthQueryTreeVisitorWithContext<OptimizeRedundantFunctionsInOrderByVisitor>
{
public:
    using Base = InDepthQueryTreeVisitorWithContext<OptimizeRedundantFunctionsInOrderByVisitor>;
    using Base::Base;

    static bool needChildVisit(QueryTreeNodePtr & node, QueryTreeNodePtr & /*parent*/)
    {
        if (node->as<FunctionNode>())
            return false;
        return true;
    }

    void enterImpl(QueryTreeNodePtr & node)
    {
        if (!getSettings().optimize_redundant_functions_in_order_by)
            return;

        auto * query = node->as<QueryNode>();
        if (!query)
            return;

        if (!query->hasOrderBy())
            return;

        auto & order_by = query->getOrderBy();
        for (auto & elem : order_by.getNodes())
        {
            auto * order_by_elem = elem->as<SortNode>();
            if (order_by_elem->withFill())
                return;
        }

        QueryTreeNodes new_order_by_nodes;
        new_order_by_nodes.reserve(order_by.getNodes().size());

        for (auto & elem : order_by.getNodes())
        {
            auto & order_by_expr = elem->as<SortNode>()->getExpression();
            switch (order_by_expr->getNodeType())
            {
                case QueryTreeNodeType::FUNCTION:
                {
                    if (isRedundantExpression(order_by_expr))
                        continue;
                    break;
                }
                case QueryTreeNodeType::COLUMN:
                {
                    existing_keys.insert(order_by_expr);
                    break;
                }
                default:
                    break;
            }

            new_order_by_nodes.push_back(elem);
        }
        existing_keys.clear();

        if (new_order_by_nodes.size() < order_by.getNodes().size())
            order_by.getNodes() = std::move(new_order_by_nodes);
    }

private:
    QueryTreeNodePtrWithHashSet existing_keys;

    bool isRedundantExpression(QueryTreeNodePtr function)
    {
        QueryTreeNodes nodes_to_process{ function };
        while (!nodes_to_process.empty())
        {
            auto node = nodes_to_process.back();
            nodes_to_process.pop_back();

            // TODO: handle constants here
            switch (node->getNodeType())
            {
                case QueryTreeNodeType::FUNCTION:
                {
                    auto * function_node = node->as<FunctionNode>();
                    const auto & function_arguments = function_node->getArguments().getNodes();
                    if (function_arguments.empty())
                        return false;
                    const auto & function_base = function_node->getFunction();
                    if (!function_base || !function_base->isDeterministicInScopeOfQuery())
                        return false;

                    // Process arguments in order
                    for (auto it = function_arguments.rbegin(); it != function_arguments.rend(); ++it)
                        nodes_to_process.push_back(*it);
                    break;
                }
                case QueryTreeNodeType::COLUMN:
                {
                    if (!existing_keys.contains(node))
                        return false;
                    break;
                }
                default:
                    return false;
            }
        }
        return true;
    }
};

}

void OptimizeRedundantFunctionsInOrderByPass::run(QueryTreeNodePtr query_tree_node, ContextPtr context)
{
    OptimizeRedundantFunctionsInOrderByVisitor visitor(std::move(context));
    visitor.visit(query_tree_node);
}

}