aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/MySQL/ASTDeclareIndex.cpp
blob: c5b4686e976eeeb62e501100f9435485c392c143 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/MySQL/ASTDeclareIndex.h>
#include <Parsers/MySQL/ASTDeclareOption.h>
#include <Parsers/MySQL/ASTDeclareReference.h>

namespace DB
{

namespace MySQLParser
{

struct ParserIndexColumn : public IParserBase
{
protected:
    const char * getName() const override { return "index column"; }

    bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
    {
        ParserExpression p_expression;

        if (!p_expression.parse(pos, node, expected))
            return false;

        ParserKeyword("ASC").ignore(pos, expected);
        ParserKeyword("DESC").ignore(pos, expected);
        return true;
    }
};

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

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


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


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

    return res;
}

static inline bool parseDeclareOrdinaryIndex(IParser::Pos & pos, String & index_name, String & index_type, Expected & expected)
{
    ASTPtr temp_node;
    ParserKeyword k_key("KEY");
    ParserKeyword k_index("INDEX");

    ParserIdentifier p_identifier;

    if (ParserKeyword("SPATIAL").ignore(pos, expected))
    {
        if (!k_key.ignore(pos, expected))
            k_index.ignore(pos, expected);

        index_type = "SPATIAL";
        if (p_identifier.parse(pos, temp_node, expected))
            index_name = temp_node->as<ASTIdentifier>()->name();
    }
    else if (ParserKeyword("FULLTEXT").ignore(pos, expected))
    {
        if (!k_key.ignore(pos, expected))
            k_index.ignore(pos, expected);

        index_type = "FULLTEXT";
        if (p_identifier.parse(pos, temp_node, expected))
            index_name = temp_node->as<ASTIdentifier>()->name();
    }
    else
    {
        if (!k_key.ignore(pos, expected))
        {
            if (!k_index.ignore(pos, expected))
                return false;
        }

        index_type = "KEY_BTREE";   /// default index type
        if (p_identifier.parse(pos, temp_node, expected))
            index_name = temp_node->as<ASTIdentifier>()->name();

        if (ParserKeyword("USING").ignore(pos, expected))
        {
            if (!p_identifier.parse(pos, temp_node, expected))
                return false;

            index_type = "KEY_" + temp_node->as<ASTIdentifier>()->name();
        }
    }

    return true;
}

static inline bool parseDeclareConstraintIndex(IParser::Pos & pos, String & index_name, String & index_type, Expected & expected)
{
    ASTPtr temp_node;
    ParserIdentifier p_identifier;

    if (ParserKeyword("CONSTRAINT").ignore(pos, expected))
    {

        if (!ParserKeyword("PRIMARY").checkWithoutMoving(pos, expected) && !ParserKeyword("UNIQUE").checkWithoutMoving(pos, expected)
            && !ParserKeyword("FOREIGN").checkWithoutMoving(pos, expected))
        {
            if (!p_identifier.parse(pos, temp_node, expected))
                return false;

            index_name = temp_node->as<ASTIdentifier>()->name();
        }
    }

    if (ParserKeyword("UNIQUE").ignore(pos, expected))
    {
        if (!ParserKeyword("KEY").ignore(pos, expected))
            ParserKeyword("INDEX").ignore(pos, expected);

        if (p_identifier.parse(pos, temp_node, expected))
            index_name = temp_node->as<ASTIdentifier>()->name();  /// reset index_name

        index_type = "UNIQUE_BTREE"; /// default btree index_type
        if (ParserKeyword("USING").ignore(pos, expected))
        {
            if (!p_identifier.parse(pos, temp_node, expected))
                return false;

            index_type = "UNIQUE_" + temp_node->as<ASTIdentifier>()->name();
        }
    }
    else if (ParserKeyword("PRIMARY KEY").ignore(pos, expected))
    {
        index_type = "PRIMARY_KEY_BTREE"; /// default btree index_type
        if (ParserKeyword("USING").ignore(pos, expected))
        {
            if (!p_identifier.parse(pos, temp_node, expected))
                return false;

            index_type = "PRIMARY_KEY_" + temp_node->as<ASTIdentifier>()->name();
        }
    }
    else if (ParserKeyword("FOREIGN KEY").ignore(pos, expected))
    {
        index_type = "FOREIGN";
        if (p_identifier.parse(pos, temp_node, expected))
            index_name = temp_node->as<ASTIdentifier>()->name();  /// reset index_name
    }

    return true;
}

bool ParserDeclareIndex::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
    String index_name;
    String index_type;
    ASTPtr index_columns;
    ASTPtr index_options;
    ASTPtr declare_reference;

    ParserDeclareOptions p_index_options{
        {
            OptionDescribe("KEY_BLOCK_SIZE", "key_block_size", std::make_unique<ParserLiteral>()),
            OptionDescribe("USING", "index_type", std::make_unique<ParserIdentifier>()),
            OptionDescribe("WITH PARSER", "index_parser", std::make_unique<ParserIdentifier>()),
            OptionDescribe("COMMENT", "comment", std::make_unique<ParserStringLiteral>()),
            OptionDescribe("VISIBLE", "visible", std::make_unique<ParserAlwaysTrue>()),
            OptionDescribe("INVISIBLE", "visible", std::make_unique<ParserAlwaysFalse>()),
        }
    };

    if (!parseDeclareOrdinaryIndex(pos, index_name, index_type, expected))
    {
        if (!parseDeclareConstraintIndex(pos, index_name, index_type, expected))
            return false;
    }

    ParserToken s_opening_round(TokenType::OpeningRoundBracket);
    ParserToken s_closing_round(TokenType::ClosingRoundBracket);

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

    ParserList p_index_columns(std::make_unique<ParserIndexColumn>(), std::make_unique<ParserToken>(TokenType::Comma));

    if (!p_index_columns.parse(pos, index_columns, expected))
        return false;

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

    if (index_type != "FOREIGN")
        p_index_options.parse(pos, index_options, expected);
    else
    {
        if (!ParserDeclareReference().parse(pos, declare_reference, expected))
            return false;
    }

    auto declare_index = std::make_shared<ASTDeclareIndex>();
    declare_index->index_name = index_name;
    declare_index->index_type = index_type;
    declare_index->index_columns = index_columns;
    declare_index->index_options = index_options;
    declare_index->reference_definition = declare_reference;

    if (declare_index->index_columns)
        declare_index->children.emplace_back(declare_index->index_columns);

    if (declare_index->index_options)
        declare_index->children.emplace_back(declare_index->index_options);

    if (declare_index->reference_definition)
        declare_index->children.emplace_back(declare_index->reference_definition);

    node = declare_index;
    return true;
}

}

}