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
|
#include <Parsers/Access/ASTCreateRowPolicyQuery.h>
#include <Parsers/Access/ASTRolesOrUsersSet.h>
#include <Parsers/Access/ASTRowPolicyName.h>
#include <Parsers/formatAST.h>
#include <Common/quoteString.h>
#include <IO/Operators.h>
#include <base/range.h>
#include <boost/container/flat_set.hpp>
#include <boost/range/algorithm/transform.hpp>
namespace DB
{
namespace
{
void formatRenameTo(const String & new_short_name, const IAST::FormatSettings & settings)
{
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " RENAME TO " << (settings.hilite ? IAST::hilite_none : "")
<< backQuote(new_short_name);
}
void formatAsRestrictiveOrPermissive(bool is_restrictive, const IAST::FormatSettings & settings)
{
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " AS " << (settings.hilite ? IAST::hilite_none : "")
<< (is_restrictive ? "restrictive" : "permissive");
}
void formatFilterExpression(const ASTPtr & expr, const IAST::FormatSettings & settings)
{
settings.ostr << " ";
if (expr)
expr->format(settings);
else
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << "NONE" << (settings.hilite ? IAST::hilite_none : "");
}
void formatForClause(const boost::container::flat_set<std::string_view> & commands, const String & filter, const String & check, bool alter, const IAST::FormatSettings & settings)
{
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " FOR " << (settings.hilite ? IAST::hilite_none : "");
bool need_comma = false;
for (const auto & command : commands)
{
if (std::exchange(need_comma, true))
settings.ostr << ", ";
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << command << (settings.hilite ? IAST::hilite_none : "");
}
if (!filter.empty())
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " USING" << (settings.hilite ? IAST::hilite_none : "") << filter;
if (!check.empty() && (alter || (check != filter)))
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " WITH CHECK" << (settings.hilite ? IAST::hilite_none : "") << check;
}
void formatForClauses(const std::vector<std::pair<RowPolicyFilterType, ASTPtr>> & filters, bool alter, const IAST::FormatSettings & settings)
{
std::vector<std::pair<RowPolicyFilterType, String>> filters_as_strings;
WriteBufferFromOwnString temp_buf;
IAST::FormatSettings temp_settings(temp_buf, settings);
for (const auto & [filter_type, filter] : filters)
{
formatFilterExpression(filter, temp_settings);
filters_as_strings.emplace_back(filter_type, temp_buf.str());
temp_buf.restart();
}
boost::container::flat_set<std::string_view> commands;
String filter, check;
do
{
commands.clear();
filter.clear();
check.clear();
/// Collect commands using the same filter and check conditions.
for (auto & [filter_type, str] : filters_as_strings)
{
if (str.empty())
continue;
const auto & type_info = RowPolicyFilterTypeInfo::get(filter_type);
if (type_info.is_check)
{
if (check.empty())
check = str;
else if (check != str)
continue;
}
else
{
if (filter.empty())
filter = str;
else if (filter != str)
continue;
}
commands.emplace(type_info.command);
str.clear(); /// Skip this condition on the next iteration.
}
if (!filter.empty() || !check.empty())
formatForClause(commands, filter, check, alter, settings);
}
while (!filter.empty() || !check.empty());
}
void formatToRoles(const ASTRolesOrUsersSet & roles, const IAST::FormatSettings & settings)
{
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "") << " TO " << (settings.hilite ? IAST::hilite_none : "");
roles.format(settings);
}
}
String ASTCreateRowPolicyQuery::getID(char) const
{
return "CREATE ROW POLICY or ALTER ROW POLICY query";
}
ASTPtr ASTCreateRowPolicyQuery::clone() const
{
auto res = std::make_shared<ASTCreateRowPolicyQuery>(*this);
if (names)
res->names = std::static_pointer_cast<ASTRowPolicyNames>(names->clone());
if (roles)
res->roles = std::static_pointer_cast<ASTRolesOrUsersSet>(roles->clone());
/// `res->filters` is already initialized by the copy constructor of ASTCreateRowPolicyQuery (see the first line of this function).
/// But the copy constructor just copied the pointers inside `filters` instead of cloning.
/// We need to make a deep copy and not a shallow copy, so we have to manually clone each pointer in `res->filters`.
chassert(res->filters.size() == filters.size());
for (auto & [_, res_filter] : res->filters)
{
if (res_filter)
res_filter = res_filter->clone();
}
return res;
}
void ASTCreateRowPolicyQuery::formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const
{
if (attach)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "ATTACH ROW POLICY";
}
else
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << (alter ? "ALTER ROW POLICY" : "CREATE ROW POLICY")
<< (settings.hilite ? hilite_none : "");
}
if (if_exists)
settings.ostr << (settings.hilite ? hilite_keyword : "") << " IF EXISTS" << (settings.hilite ? hilite_none : "");
else if (if_not_exists)
settings.ostr << (settings.hilite ? hilite_keyword : "") << " IF NOT EXISTS" << (settings.hilite ? hilite_none : "");
else if (or_replace)
settings.ostr << (settings.hilite ? hilite_keyword : "") << " OR REPLACE" << (settings.hilite ? hilite_none : "");
settings.ostr << " ";
names->format(settings);
if (!storage_name.empty())
settings.ostr << (settings.hilite ? IAST::hilite_keyword : "")
<< " IN " << (settings.hilite ? IAST::hilite_none : "")
<< backQuoteIfNeed(storage_name);
formatOnCluster(settings);
assert(names->cluster.empty());
if (!new_short_name.empty())
formatRenameTo(new_short_name, settings);
if (is_restrictive)
formatAsRestrictiveOrPermissive(*is_restrictive, settings);
formatForClauses(filters, alter, settings);
if (roles && (!roles->empty() || alter))
formatToRoles(*roles, settings);
}
void ASTCreateRowPolicyQuery::replaceCurrentUserTag(const String & current_user_name) const
{
if (roles)
roles->replaceCurrentUserTag(current_user_name);
}
void ASTCreateRowPolicyQuery::replaceEmptyDatabase(const String & current_database) const
{
if (names)
names->replaceEmptyDatabase(current_database);
}
}
|