2019-11-29 17:22:56 +00:00
|
|
|
#include <Parsers/ParserCreateRowPolicyQuery.h>
|
|
|
|
#include <Parsers/ASTCreateRowPolicyQuery.h>
|
|
|
|
#include <Access/RowPolicy.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Parsers/ParserExtendedRoleSet.h>
|
|
|
|
#include <Parsers/ASTExtendedRoleSet.h>
|
2019-11-29 17:22:56 +00:00
|
|
|
#include <Parsers/parseIdentifierOrStringLiteral.h>
|
|
|
|
#include <Parsers/parseDatabaseAndTableName.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
2020-02-21 19:27:12 +00:00
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
2019-11-29 17:22:56 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
using ConditionType = RowPolicy::ConditionType;
|
2019-11-29 17:22:56 +00:00
|
|
|
|
2020-02-10 15:24:33 +00:00
|
|
|
bool parseRenameTo(IParserBase::Pos & pos, Expected & expected, String & new_policy_name)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"RENAME TO"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return parseIdentifierOrStringLiteral(pos, expected, new_policy_name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:24:33 +00:00
|
|
|
bool parseAsRestrictiveOrPermissive(IParserBase::Pos & pos, Expected & expected, std::optional<bool> & is_restrictive)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"AS"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ParserKeyword{"RESTRICTIVE"}.ignore(pos, expected))
|
2020-02-10 15:24:33 +00:00
|
|
|
{
|
2019-11-29 17:22:56 +00:00
|
|
|
is_restrictive = true;
|
2020-02-10 15:24:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ParserKeyword{"PERMISSIVE"}.ignore(pos, expected))
|
2019-11-29 17:22:56 +00:00
|
|
|
return false;
|
|
|
|
|
2020-02-10 15:24:33 +00:00
|
|
|
is_restrictive = false;
|
2019-11-29 17:22:56 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseConditionalExpression(IParserBase::Pos & pos, Expected & expected, std::optional<ASTPtr> & expr)
|
|
|
|
{
|
2020-02-10 15:24:33 +00:00
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
2020-02-10 15:24:33 +00:00
|
|
|
if (ParserKeyword("NONE").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
expr = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParserExpression parser;
|
|
|
|
ASTPtr x;
|
|
|
|
if (!parser.parse(pos, x, expected))
|
|
|
|
return false;
|
|
|
|
|
2019-11-29 17:22:56 +00:00
|
|
|
expr = x;
|
|
|
|
return true;
|
2020-02-10 15:24:33 +00:00
|
|
|
});
|
2019-11-29 17:22:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool parseConditions(IParserBase::Pos & pos, Expected & expected, bool alter, std::vector<std::pair<ConditionType, ASTPtr>> & conditions)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
static constexpr char select_op[] = "SELECT";
|
|
|
|
static constexpr char insert_op[] = "INSERT";
|
|
|
|
static constexpr char update_op[] = "UPDATE";
|
|
|
|
static constexpr char delete_op[] = "DELETE";
|
|
|
|
std::vector<const char *> ops;
|
|
|
|
|
|
|
|
bool keyword_for = false;
|
|
|
|
if (ParserKeyword{"FOR"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
keyword_for = true;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"SELECT"}.ignore(pos, expected))
|
|
|
|
ops.push_back(select_op);
|
|
|
|
else if (ParserKeyword{"INSERT"}.ignore(pos, expected))
|
|
|
|
ops.push_back(insert_op);
|
|
|
|
else if (ParserKeyword{"UPDATE"}.ignore(pos, expected))
|
|
|
|
ops.push_back(update_op);
|
|
|
|
else if (ParserKeyword{"DELETE"}.ignore(pos, expected))
|
|
|
|
ops.push_back(delete_op);
|
|
|
|
else if (ParserKeyword{"ALL"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (ParserToken{TokenType::Comma}.ignore(pos, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ops.empty())
|
|
|
|
{
|
|
|
|
ops.push_back(select_op);
|
|
|
|
ops.push_back(insert_op);
|
|
|
|
ops.push_back(update_op);
|
|
|
|
ops.push_back(delete_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ASTPtr> filter;
|
|
|
|
std::optional<ASTPtr> check;
|
|
|
|
bool keyword_using = false, keyword_with_check = false;
|
|
|
|
if (ParserKeyword{"USING"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
keyword_using = true;
|
|
|
|
if (!parseConditionalExpression(pos, expected, filter))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ParserKeyword{"WITH CHECK"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
keyword_with_check = true;
|
|
|
|
if (!parseConditionalExpression(pos, expected, check))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keyword_for && !keyword_using && !keyword_with_check)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (filter && !check && !alter)
|
|
|
|
check = filter;
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
auto set_condition = [&](ConditionType index, const ASTPtr & condition)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
auto it = std::find_if(conditions.begin(), conditions.end(), [index](const std::pair<ConditionType, ASTPtr> & element)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
|
|
|
return element.first == index;
|
|
|
|
});
|
|
|
|
if (it == conditions.end())
|
2020-03-07 17:37:38 +00:00
|
|
|
it = conditions.insert(conditions.end(), std::pair<ConditionType, ASTPtr>{index, nullptr});
|
2019-11-29 17:22:56 +00:00
|
|
|
it->second = condition;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto & op : ops)
|
|
|
|
{
|
|
|
|
if ((op == select_op) && filter)
|
|
|
|
set_condition(RowPolicy::SELECT_FILTER, *filter);
|
|
|
|
else if ((op == insert_op) && check)
|
|
|
|
set_condition(RowPolicy::INSERT_CHECK, *check);
|
|
|
|
else if (op == update_op)
|
|
|
|
{
|
|
|
|
if (filter)
|
|
|
|
set_condition(RowPolicy::UPDATE_FILTER, *filter);
|
|
|
|
if (check)
|
|
|
|
set_condition(RowPolicy::UPDATE_CHECK, *check);
|
|
|
|
}
|
|
|
|
else if ((op == delete_op) && filter)
|
|
|
|
set_condition(RowPolicy::DELETE_FILTER, *filter);
|
|
|
|
else
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool parseMultipleConditions(IParserBase::Pos & pos, Expected & expected, bool alter, std::vector<std::pair<ConditionType, ASTPtr>> & conditions)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
std::vector<std::pair<ConditionType, ASTPtr>> res_conditions;
|
2019-11-29 17:22:56 +00:00
|
|
|
do
|
|
|
|
{
|
2020-02-10 15:24:33 +00:00
|
|
|
if (!parseConditions(pos, expected, alter, res_conditions))
|
2019-11-29 17:22:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (ParserToken{TokenType::Comma}.ignore(pos, expected));
|
2020-02-10 15:24:33 +00:00
|
|
|
|
|
|
|
conditions = std::move(res_conditions);
|
2019-11-29 17:22:56 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool parseToRoles(IParserBase::Pos & pos, Expected & expected, bool id_mode, std::shared_ptr<ASTExtendedRoleSet> & roles)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
2020-02-10 15:24:33 +00:00
|
|
|
ASTPtr ast;
|
2020-02-21 19:27:12 +00:00
|
|
|
if (roles || !ParserKeyword{"TO"}.ignore(pos, expected)
|
2020-03-07 17:37:38 +00:00
|
|
|
|| !ParserExtendedRoleSet{}.useIDMode(id_mode).parse(pos, ast, expected))
|
2019-11-29 17:22:56 +00:00
|
|
|
return false;
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
roles = std::static_pointer_cast<ASTExtendedRoleSet>(ast);
|
2019-11-29 17:22:56 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserCreateRowPolicyQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
bool alter = false;
|
|
|
|
if (attach_mode)
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"ATTACH POLICY"}.ignore(pos, expected) && !ParserKeyword{"ATTACH ROW POLICY"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-29 17:22:56 +00:00
|
|
|
else
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
|
|
|
if (ParserKeyword{"ALTER POLICY"}.ignore(pos, expected) || ParserKeyword{"ALTER ROW POLICY"}.ignore(pos, expected))
|
|
|
|
alter = true;
|
|
|
|
else if (!ParserKeyword{"CREATE POLICY"}.ignore(pos, expected) && !ParserKeyword{"CREATE ROW POLICY"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-29 17:22:56 +00:00
|
|
|
|
|
|
|
bool if_exists = false;
|
|
|
|
bool if_not_exists = false;
|
|
|
|
bool or_replace = false;
|
|
|
|
if (alter)
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"IF EXISTS"}.ignore(pos, expected))
|
|
|
|
if_exists = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"IF NOT EXISTS"}.ignore(pos, expected))
|
|
|
|
if_not_exists = true;
|
|
|
|
else if (ParserKeyword{"OR REPLACE"}.ignore(pos, expected))
|
|
|
|
or_replace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RowPolicy::FullNameParts name_parts;
|
|
|
|
String & database = name_parts.database;
|
|
|
|
String & table_name = name_parts.table_name;
|
|
|
|
String & policy_name = name_parts.policy_name;
|
|
|
|
if (!parseIdentifierOrStringLiteral(pos, expected, policy_name) || !ParserKeyword{"ON"}.ignore(pos, expected)
|
|
|
|
|| !parseDatabaseAndTableName(pos, expected, database, table_name))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String new_policy_name;
|
|
|
|
std::optional<bool> is_restrictive;
|
2020-03-07 17:37:38 +00:00
|
|
|
std::vector<std::pair<ConditionType, ASTPtr>> conditions;
|
2019-11-29 17:22:56 +00:00
|
|
|
|
2020-02-10 15:24:33 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (alter && new_policy_name.empty() && parseRenameTo(pos, expected, new_policy_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!is_restrictive && parseAsRestrictiveOrPermissive(pos, expected, is_restrictive))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (parseMultipleConditions(pos, expected, alter, conditions))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2019-11-29 17:22:56 +00:00
|
|
|
|
2020-03-18 14:11:44 +00:00
|
|
|
std::shared_ptr<ASTExtendedRoleSet> roles;
|
|
|
|
parseToRoles(pos, expected, attach_mode, roles);
|
|
|
|
|
2019-11-29 17:22:56 +00:00
|
|
|
auto query = std::make_shared<ASTCreateRowPolicyQuery>();
|
|
|
|
node = query;
|
|
|
|
|
|
|
|
query->alter = alter;
|
2020-03-18 14:11:44 +00:00
|
|
|
query->attach = attach_mode;
|
2019-11-29 17:22:56 +00:00
|
|
|
query->if_exists = if_exists;
|
|
|
|
query->if_not_exists = if_not_exists;
|
|
|
|
query->or_replace = or_replace;
|
|
|
|
query->name_parts = std::move(name_parts);
|
|
|
|
query->new_policy_name = std::move(new_policy_name);
|
|
|
|
query->is_restrictive = is_restrictive;
|
|
|
|
query->conditions = std::move(conditions);
|
|
|
|
query->roles = std::move(roles);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|