1 | #include <Interpreters/InterpreterShowRowPoliciesQuery.h> |
2 | #include <Parsers/ASTShowRowPoliciesQuery.h> |
3 | #include <Parsers/formatAST.h> |
4 | #include <Interpreters/executeQuery.h> |
5 | #include <Common/quoteString.h> |
6 | #include <Interpreters/Context.h> |
7 | #include <ext/range.h> |
8 | |
9 | |
10 | namespace DB |
11 | { |
12 | InterpreterShowRowPoliciesQuery::InterpreterShowRowPoliciesQuery(const ASTPtr & query_ptr_, Context & context_) |
13 | : query_ptr(query_ptr_), context(context_) |
14 | { |
15 | } |
16 | |
17 | |
18 | BlockIO InterpreterShowRowPoliciesQuery::execute() |
19 | { |
20 | return executeQuery(getRewrittenQuery(), context, true); |
21 | } |
22 | |
23 | |
24 | String InterpreterShowRowPoliciesQuery::getRewrittenQuery() const |
25 | { |
26 | const auto & query = query_ptr->as<ASTShowRowPoliciesQuery &>(); |
27 | |
28 | const String & table_name = query.table_name; |
29 | String database; |
30 | if (!table_name.empty()) |
31 | { |
32 | database = query.database; |
33 | if (database.empty()) |
34 | database = context.getCurrentDatabase(); |
35 | } |
36 | |
37 | String filter; |
38 | if (query.current) |
39 | { |
40 | if (table_name.empty()) |
41 | filter = "has(currentRowPolicyIDs(), id)" ; |
42 | else |
43 | filter = "has(currentRowPolicyIDs(" + quoteString(database) + ", " + quoteString(table_name) + "), id)" ; |
44 | } |
45 | else |
46 | { |
47 | if (!table_name.empty()) |
48 | filter = "database = " + quoteString(database) + " AND table = " + quoteString(table_name); |
49 | } |
50 | |
51 | String expr = table_name.empty() ? "full_name" : "name" ; |
52 | |
53 | return "SELECT " + expr + " AS " + backQuote(getResultDescription()) + " from system.row_policies" |
54 | + (filter.empty() ? "" : " WHERE " + filter) + " ORDER BY " + expr; |
55 | } |
56 | |
57 | |
58 | String InterpreterShowRowPoliciesQuery::getResultDescription() const |
59 | { |
60 | std::stringstream ss; |
61 | formatAST(*query_ptr, ss, false, true); |
62 | String desc = ss.str(); |
63 | String prefix = "SHOW " ; |
64 | if (startsWith(desc, prefix)) |
65 | desc = desc.substr(prefix.length()); /// `desc` always starts with "SHOW ", so we can trim this prefix. |
66 | return desc; |
67 | } |
68 | } |
69 | |