aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/MySQL/ASTDeclarePartitionOptions.cpp
blob: 199a5e2dd26f452077195bca6bc05f7696ac8169 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <Parsers/MySQL/ASTDeclarePartitionOptions.h>

#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/MySQL/ASTDeclarePartition.h>

namespace DB
{

namespace MySQLParser
{

ASTPtr ASTDeclarePartitionOptions::clone() const
{
    auto res = std::make_shared<ASTDeclarePartitionOptions>(*this);
    res->children.clear();

    if (partition_numbers)
    {
        res->partition_numbers = partition_numbers->clone();
        res->children.emplace_back(res->partition_numbers);
    }

    if (partition_expression)
    {
        res->partition_expression = partition_expression->clone();
        res->children.emplace_back(res->partition_expression);
    }

    if (subpartition_numbers)
    {
        res->subpartition_numbers = subpartition_numbers->clone();
        res->children.emplace_back(res->subpartition_numbers);
    }

    if (subpartition_expression)
    {
        res->subpartition_expression = subpartition_expression->clone();
        res->children.emplace_back(res->subpartition_expression);
    }

    return res;
}

static inline bool parsePartitionExpression(IParser::Pos & pos, std::string & type, ASTPtr & node, Expected & expected, bool subpartition = false)
{
    ASTPtr expression;
    ParserExpression p_expression;
    if (!subpartition && ParserKeyword("LIST").ignore(pos, expected))
    {
        type = "list";
        ParserKeyword("COLUMNS").ignore(pos, expected);
        if (!p_expression.parse(pos, expression, expected))
            return false;
    }
    else if (!subpartition && ParserKeyword("RANGE").ignore(pos, expected))
    {
        type = "range";
        ParserKeyword("COLUMNS").ignore(pos, expected);
        if (!p_expression.parse(pos, expression, expected))
            return false;
    }
    else
    {
        if (ParserKeyword("LINEAR").ignore(pos, expected))
            type = "linear_";

        if (ParserKeyword("KEY").ignore(pos, expected))
        {
            type += "key";

            if (ParserKeyword("ALGORITHM").ignore(pos, expected))
            {
                if (!ParserToken(TokenType::Equals).ignore(pos, expected))
                    return false;

                ASTPtr algorithm;
                ParserLiteral p_literal;
                if (!p_literal.parse(pos, algorithm, expected) || !algorithm->as<ASTLiteral>())
                    return false;

                UInt64 algorithm_type = algorithm->as<ASTLiteral>()->value.safeGet<UInt64>();

                if (algorithm_type != 1 && algorithm_type != 2)
                    return false;

                type += "_" + toString(algorithm_type);
            }

            if (!p_expression.parse(pos, expression, expected))
                return false;
        }
        else if (ParserKeyword("HASH").ignore(pos, expected))
        {
            type += "hash";
            if (!p_expression.parse(pos, expression, expected))
                return false;
        }
        else
            return false;
    }

    node = expression;
    return true;
}

bool ParserDeclarePartitionOptions::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    String partition_type;
    ASTPtr partition_numbers;
    ASTPtr partition_expression;
    String subpartition_type;
    ASTPtr subpartition_numbers;
    ASTPtr subpartition_expression;
    ASTPtr declare_partitions;

    if (!ParserKeyword("PARTITION BY").ignore(pos, expected))
        return false;

    if (!parsePartitionExpression(pos, partition_type, partition_expression, expected))
        return false;

    if (ParserKeyword("PARTITIONS").ignore(pos, expected))
    {
        ParserLiteral p_literal;
        if (!p_literal.parse(pos, partition_numbers, expected))
            return false;
    }

    if (ParserKeyword("SUBPARTITION BY").ignore(pos, expected))
    {
        if (!parsePartitionExpression(pos, subpartition_type, subpartition_expression, expected, true))
            return false;

        if (ParserKeyword("SUBPARTITIONS").ignore(pos, expected))
        {
            ParserLiteral p_literal;
            if (!p_literal.parse(pos, subpartition_numbers, expected))
                return false;
        }
    }

    if (ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected))
    {
        if (!ParserList(std::make_unique<ParserDeclarePartition>(), std::make_unique<ParserToken>(TokenType::Comma))
                 .parse(pos, declare_partitions, expected))
            return false;

        if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
            return false;
    }

    auto declare_partition_options = std::make_shared<ASTDeclarePartitionOptions>();
    declare_partition_options->partition_type = partition_type;
    declare_partition_options->partition_numbers = partition_numbers;
    declare_partition_options->partition_expression = partition_expression;
    declare_partition_options->subpartition_type = subpartition_type;
    declare_partition_options->subpartition_numbers = subpartition_numbers;
    declare_partition_options->subpartition_expression = subpartition_expression;
    declare_partition_options->declare_partitions = declare_partitions;

    if (declare_partition_options->partition_numbers)
        declare_partition_options->children.emplace_back(declare_partition_options->partition_numbers);

    if (declare_partition_options->partition_expression)
        declare_partition_options->children.emplace_back(declare_partition_options->partition_expression);

    if (declare_partition_options->subpartition_numbers)
        declare_partition_options->children.emplace_back(declare_partition_options->subpartition_numbers);

    if (declare_partition_options->subpartition_expression)
        declare_partition_options->children.emplace_back(declare_partition_options->subpartition_expression);

    if (declare_partition_options->declare_partitions)
        declare_partition_options->children.emplace_back(declare_partition_options->declare_partitions);

    node = declare_partition_options;
    return true;
}
}

}