aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/ParserCase.cpp
blob: 521eec212da36a32c6d2c7af5afddbcdb8397324 (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
#include <Parsers/ParserCase.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
#include <Core/Field.h>

namespace DB
{

bool ParserCase::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    ParserKeyword s_case{"CASE"};
    ParserKeyword s_when{"WHEN"};
    ParserKeyword s_then{"THEN"};
    ParserKeyword s_else{"ELSE"};
    ParserKeyword s_end{ "END"};
    ParserExpressionWithOptionalAlias p_expr{false};

    if (!s_case.ignore(pos, expected))
        return false;

    auto old_pos = pos;
    bool has_case_expr = !s_when.ignore(pos, expected);
    pos = old_pos;

    ASTs args;

    auto parse_branches = [&]()
    {
        bool has_branch = false;
        while (s_when.ignore(pos, expected))
        {
            has_branch = true;

            ASTPtr expr_when;
            if (!p_expr.parse(pos, expr_when, expected))
                return false;
            args.push_back(expr_when);

            if (!s_then.ignore(pos, expected))
                return false;

            ASTPtr expr_then;
            if (!p_expr.parse(pos, expr_then, expected))
                return false;
            args.push_back(expr_then);
        }

        if (!has_branch)
            return false;

        ASTPtr expr_else;
        if (s_else.ignore(pos, expected))
        {
            if (!p_expr.parse(pos, expr_else, expected))
                return false;
        }
        else
        {
            Field field_with_null;
            ASTLiteral null_literal(field_with_null);
            expr_else = std::make_shared<ASTLiteral>(null_literal);
        }
        args.push_back(expr_else);

        return s_end.ignore(pos, expected);
    };

    if (has_case_expr)
    {
        ASTPtr case_expr;
        if (!p_expr.parse(pos, case_expr, expected))
            return false;
        args.push_back(case_expr);

        if (!parse_branches())
            return false;

        auto function_args = std::make_shared<ASTExpressionList>();
        function_args->children = std::move(args);

        auto function = std::make_shared<ASTFunction>();
        function->name = "caseWithExpression";
        function->arguments = function_args;
        function->children.push_back(function->arguments);

        node = function;
    }
    else
    {
        if (!parse_branches())
            return false;

        auto function_args = std::make_shared<ASTExpressionList>();
        function_args->children = std::move(args);

        auto function = std::make_shared<ASTFunction>();
        function->name = "multiIf";
        function->arguments = function_args;
        function->children.push_back(function->arguments);

        node = function;
    }

    return true;
}

}