aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/Passes/OrderByTupleEliminationPass.cpp
blob: f70ec27ba5dbf1452a802bc393d0643478c3a877 (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
#include <Analyzer/Passes/OrderByTupleEliminationPass.h>

#include <Analyzer/InDepthQueryTreeVisitor.h>
#include <Analyzer/FunctionNode.h>
#include <Analyzer/QueryNode.h>
#include <Analyzer/SortNode.h>
#include <Functions/FunctionFactory.h>

namespace DB
{

namespace
{

class OrderByTupleEliminationVisitor : public InDepthQueryTreeVisitor<OrderByTupleEliminationVisitor>
{
public:
    static void visitImpl(QueryTreeNodePtr & node)
    {
        auto * query_node = node->as<QueryNode>();
        if (!query_node || !query_node->hasOrderBy())
            return;

        QueryTreeNodes result_nodes;

        for (auto & sort_node : query_node->getOrderBy().getNodes())
        {
            auto & sort_node_typed = sort_node->as<SortNode &>();
            auto * function_expression = sort_node_typed.getExpression()->as<FunctionNode>();
            if (sort_node_typed.withFill() || !function_expression || function_expression->getFunctionName() != "tuple")
            {
                result_nodes.push_back(sort_node);
                continue;
            }

            auto & tuple_arguments_nodes = function_expression->getArguments().getNodes();
            for (auto & argument_node : tuple_arguments_nodes)
            {
                auto result_sort_node = std::make_shared<SortNode>(argument_node,
                    sort_node_typed.getSortDirection(),
                    sort_node_typed.getNullsSortDirection(),
                    sort_node_typed.getCollator());
                result_nodes.push_back(std::move(result_sort_node));
            }
        }

        query_node->getOrderBy().getNodes() = std::move(result_nodes);
    }
};

}

void OrderByTupleEliminationPass::run(QueryTreeNodePtr query_tree_node, ContextPtr)
{
    OrderByTupleEliminationVisitor visitor;
    visitor.visit(query_tree_node);
}

}