aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/OptimizeIfChains.cpp
blob: 9a5f9bcb2e10a647b3142193e2d21c8c41a8261b (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
#include <Common/typeid_cast.h>
#include <Common/checkStackSize.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTExpressionList.h>
#include <Interpreters/OptimizeIfChains.h>
#include <IO/WriteHelpers.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
    extern const int UNEXPECTED_AST_STRUCTURE;
}

void OptimizeIfChainsVisitor::visit(ASTPtr & current_ast)
{
    if (!current_ast)
        return;

    checkStackSize();

    for (ASTPtr & child : current_ast->children)
    {
        /// Fallthrough cases

        const auto * function_node = child->as<ASTFunction>();
        if (!function_node || function_node->name != "if" || !function_node->arguments)
        {
            visit(child);
            continue;
        }

        const auto * function_args = function_node->arguments->as<ASTExpressionList>();
        if (!function_args || function_args->children.size() != 3 || !function_args->children[2])
        {
            visit(child);
            continue;
        }

        const auto * else_arg = function_args->children[2]->as<ASTFunction>();
        if (!else_arg || else_arg->name != "if")
        {
            visit(child);
            continue;
        }

        /// The case of:
        /// if(cond, a, if(...))

        auto chain = ifChain(child);
        std::reverse(chain.begin(), chain.end());
        child->as<ASTFunction>()->name = "multiIf";
        child->as<ASTFunction>()->arguments->children = std::move(chain);
    }
}

ASTs OptimizeIfChainsVisitor::ifChain(const ASTPtr & child)
{
    const auto * function_node = child->as<ASTFunction>();
    if (!function_node || !function_node->arguments)
        throw Exception(ErrorCodes::UNEXPECTED_AST_STRUCTURE, "Unexpected AST for function 'if'");

    const auto * function_args = function_node->arguments->as<ASTExpressionList>();
    chassert(function_args);

    if (!function_args || function_args->children.size() != 3)
        throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
                        "Wrong number of arguments for function 'if' ({} instead of 3)",
                        function_args->children.size());

    const auto * else_arg = function_args->children[2]->as<ASTFunction>();

    /// Recursively collect arguments from the innermost if ("head-recursion").
    /// Arguments will be returned in reverse order.

    if (else_arg && else_arg->name == "if")
    {
        auto cur = ifChain(function_node->arguments->children[2]);
        cur.push_back(function_node->arguments->children[1]);
        cur.push_back(function_node->arguments->children[0]);
        return cur;
    }
    else
    {
        ASTs end;
        end.reserve(3);
        end.push_back(function_node->arguments->children[2]);
        end.push_back(function_node->arguments->children[1]);
        end.push_back(function_node->arguments->children[0]);
        return end;
    }
}

}