aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/Access/ParserRowPolicyName.cpp
blob: efdff3c24bf57c0ac04578731af7b0e285632aa4 (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
#include <Parsers/Access/ParserRowPolicyName.h>
#include <Parsers/Access/ASTRowPolicyName.h>
#include <Parsers/ASTQueryWithOnCluster.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Parsers/parseDatabaseAndTableName.h>
#include <base/insertAtEnd.h>


namespace DB
{
namespace
{
    bool parseOnCluster(IParserBase::Pos & pos, Expected & expected, String & cluster)
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
            return ParserKeyword{"ON"}.ignore(pos, expected) && ASTQueryWithOnCluster::parse(pos, cluster, expected);
        });
    }


    bool parseDBAndTableName(IParser::Pos & pos, Expected & expected, String & database, String & table_name)
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
            String res_database, res_table_name;
            bool is_any_database = false;
            bool is_any_table = false;

            if (!parseDatabaseAndTableNameOrAsterisks(pos, expected, res_database, is_any_database, res_table_name, is_any_table)
                || is_any_database)
            {
                return false;
            }
            else if (is_any_table)
            {
                res_table_name = RowPolicyName::ANY_TABLE_MARK;
            }

            /// If table is specified without DB it cannot be followed by "ON"
            /// (but can be followed by "ON CLUSTER").
            /// The following code is necessary to figure out while parsing something like
            /// policy1 ON table1, policy2 ON table2
            /// that policy2 is another policy, not another table.
            auto end_pos = pos;
            if (res_database.empty() && ParserKeyword{"ON"}.ignore(pos, expected))
            {
                String unused;
                if (ASTQueryWithOnCluster::parse(pos, unused, expected))
                    pos = end_pos;
                else
                    return false;
            }

            database = std::move(res_database);
            table_name = std::move(res_table_name);
            return true;
        });
    }


    bool parseOnDBAndTableName(IParser::Pos & pos, Expected & expected, String & database, String & table_name)
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
            return ParserKeyword{"ON"}.ignore(pos, expected) && parseDBAndTableName(pos, expected, database, table_name);
        });
    }


    bool parseOnDBAndTableNames(IParser::Pos & pos, Expected & expected, std::vector<std::pair<String, String>> & database_and_table_names)
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
            if (!ParserKeyword{"ON"}.ignore(pos, expected))
                return false;

            std::vector<std::pair<String, String>> res;

            auto parse_db_and_table_name = [&]
            {
                String database, table_name;
                if (!parseDBAndTableName(pos, expected, database, table_name))
                    return false;
                res.emplace_back(std::move(database), std::move(table_name));
                return true;
            };

            if (!ParserList::parseUtil(pos, expected, parse_db_and_table_name, false))
                return false;

            database_and_table_names = std::move(res);
            return true;
         });
    }


    bool parseRowPolicyNamesAroundON(IParser::Pos & pos, Expected & expected,
                                     bool allow_multiple_short_names,
                                     bool allow_multiple_tables,
                                     bool allow_on_cluster,
                                     std::vector<RowPolicyName> & full_names,
                                     String & cluster)
    {
        return IParserBase::wrapParseImpl(pos, [&]
        {
            std::vector<String> short_names;
            if (allow_multiple_short_names)
            {
                if (!parseIdentifiersOrStringLiterals(pos, expected, short_names))
                    return false;
            }
            else
            {
                if (!parseIdentifierOrStringLiteral(pos, expected, short_names.emplace_back()))
                    return false;
            }

            String res_cluster;
            if (allow_on_cluster)
                parseOnCluster(pos, expected, res_cluster);

            std::vector<std::pair<String, String>> database_and_table_names;
            if (allow_multiple_tables && (short_names.size() == 1))
            {
                if (!parseOnDBAndTableNames(pos, expected, database_and_table_names))
                    return false;
            }
            else
            {
                String database, table_name;
                if (!parseOnDBAndTableName(pos, expected, database, table_name))
                    return false;
                database_and_table_names.emplace_back(std::move(database), std::move(table_name));
            }


            if (allow_on_cluster && res_cluster.empty())
                parseOnCluster(pos, expected, res_cluster);

            assert(!short_names.empty());
            assert(!database_and_table_names.empty());
            full_names.clear();
            for (const String & short_name : short_names)
                for (const auto & [database, table_name] : database_and_table_names)
                    full_names.push_back({short_name, database, table_name});

            cluster = std::move(res_cluster);
            return true;
        });
    }
}


bool ParserRowPolicyName::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    std::vector<RowPolicyName> full_names;
    String cluster;
    if (!parseRowPolicyNamesAroundON(pos, expected, false, false, allow_on_cluster, full_names, cluster))
        return false;

    assert(full_names.size() == 1);
    auto result = std::make_shared<ASTRowPolicyName>();
    result->full_name = std::move(full_names.front());
    result->cluster = std::move(cluster);
    node = result;
    return true;
}


bool ParserRowPolicyNames::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    std::vector<RowPolicyName> full_names;
    size_t num_added_names_last_time = 0;
    String cluster;

    auto parse_around_on = [&]
    {
        if (!full_names.empty())
        {
            if ((num_added_names_last_time != 1) || !cluster.empty())
                return false;
        }

        std::vector<RowPolicyName> new_full_names;
        if (!parseRowPolicyNamesAroundON(pos, expected, full_names.empty(), full_names.empty(), allow_on_cluster, new_full_names, cluster))
            return false;

        num_added_names_last_time = new_full_names.size();
        insertAtEnd(full_names, std::move(new_full_names));
        return true;
    };

    if (!ParserList::parseUtil(pos, expected, parse_around_on, false))
        return false;

    auto result = std::make_shared<ASTRowPolicyNames>();
    result->full_names = std::move(full_names);
    result->cluster = std::move(cluster);
    node = result;
    return true;
}

}