aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/ParserAlterNamedCollectionQuery.cpp
blob: 8fb84f86246fbc27bf92bdf4d86dc2f67b1e04b2 (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
#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ParserAlterNamedCollectionQuery.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTAlterNamedCollectionQuery.h>
#include <Parsers/ASTSetQuery.h>

namespace DB
{

bool ParserAlterNamedCollectionQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected & expected)
{
    ParserKeyword s_alter("ALTER");
    ParserKeyword s_collection("NAMED COLLECTION");
    ParserKeyword s_if_exists("IF EXISTS");
    ParserKeyword s_on("ON");
    ParserKeyword s_delete("DELETE");
    ParserIdentifier name_p;
    ParserSetQuery set_p;
    ParserToken s_comma(TokenType::Comma);

    String cluster_str;
    bool if_exists = false;

    ASTPtr collection_name;
    ASTPtr set;
    std::vector<std::string> delete_keys;

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

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

    if (s_if_exists.ignore(pos, expected))
        if_exists = true;

    if (!name_p.parse(pos, collection_name, expected))
        return false;

    if (s_on.ignore(pos, expected))
    {
        if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
            return false;
    }

    bool parsed_delete = false;
    if (!set_p.parse(pos, set, expected))
    {
        if (!s_delete.ignore(pos, expected))
            return false;

        parsed_delete = true;
    }
    else if (s_delete.ignore(pos, expected))
    {
        parsed_delete = true;
    }

    if (parsed_delete)
    {
        while (true)
        {
            if (!delete_keys.empty() && !s_comma.ignore(pos))
                break;

            ASTPtr key;
            if (!name_p.parse(pos, key, expected))
                return false;

            delete_keys.push_back(getIdentifierName(key));
        }
    }

    auto query = std::make_shared<ASTAlterNamedCollectionQuery>();

    query->collection_name = getIdentifierName(collection_name);
    query->if_exists = if_exists;
    query->cluster = std::move(cluster_str);
    if (set)
        query->changes = set->as<ASTSetQuery>()->changes;
    query->delete_keys = delete_keys;

    node = query;
    return true;
}

}