2020-03-18 14:11:44 +00:00
|
|
|
#include <Parsers/ParserSettingsProfileElement.h>
|
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ASTSettingsProfileElement.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/parseIdentifierOrStringLiteral.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool parseProfileNameOrID(IParserBase::Pos & pos, Expected & expected, bool parse_id, String & res)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!parse_id)
|
|
|
|
return parseIdentifierOrStringLiteral(pos, expected, res);
|
|
|
|
|
|
|
|
if (!ParserKeyword{"ID"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
if (!ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
if (!ParserStringLiteral{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
String id = ast->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
res = std::move(id);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool parseValue(IParserBase::Pos & pos, Expected & expected, Field & res)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserToken{TokenType::Equals}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserLiteral{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
res = ast->as<ASTLiteral &>().value;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool parseMinMaxValue(IParserBase::Pos & pos, Expected & expected, Field & min_value, Field & max_value)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
bool is_min_value = ParserKeyword{"MIN"}.ignore(pos, expected);
|
|
|
|
bool is_max_value = !is_min_value && ParserKeyword{"MAX"}.ignore(pos, expected);
|
|
|
|
if (!is_min_value && !is_max_value)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ParserToken{TokenType::Equals}.ignore(pos, expected);
|
|
|
|
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserLiteral{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto min_or_max_value = ast->as<ASTLiteral &>().value;
|
|
|
|
|
|
|
|
if (is_min_value)
|
|
|
|
min_value = min_or_max_value;
|
|
|
|
else
|
|
|
|
max_value = min_or_max_value;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool parseReadonlyOrWritableKeyword(IParserBase::Pos & pos, Expected & expected, std::optional<bool> & readonly)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (ParserKeyword{"READONLY"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
readonly = true;
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-05 20:44:10 +00:00
|
|
|
else if (ParserKeyword{"WRITABLE"}.ignore(pos, expected))
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
|
|
|
readonly = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserSettingsProfileElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
String parent_profile;
|
2020-04-21 10:21:03 +00:00
|
|
|
String setting_name;
|
2020-03-18 14:11:44 +00:00
|
|
|
Field value;
|
|
|
|
Field min_value;
|
|
|
|
Field max_value;
|
|
|
|
std::optional<bool> readonly;
|
|
|
|
|
2020-04-07 23:57:14 +00:00
|
|
|
if (ParserKeyword{"PROFILE"}.ignore(pos, expected) ||
|
|
|
|
(enable_inherit_keyword && ParserKeyword{"INHERIT"}.ignore(pos, expected)))
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
|
|
|
if (!parseProfileNameOrID(pos, expected, id_mode, parent_profile))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASTPtr name_ast;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, name_ast, expected))
|
|
|
|
return false;
|
2020-04-21 10:21:03 +00:00
|
|
|
setting_name = getIdentifierName(name_ast);
|
2020-03-18 14:11:44 +00:00
|
|
|
|
2020-04-07 23:57:14 +00:00
|
|
|
bool has_value_or_constraint = false;
|
2020-03-18 14:11:44 +00:00
|
|
|
while (parseValue(pos, expected, value) || parseMinMaxValue(pos, expected, min_value, max_value)
|
|
|
|
|| parseReadonlyOrWritableKeyword(pos, expected, readonly))
|
2020-04-07 23:57:14 +00:00
|
|
|
{
|
|
|
|
has_value_or_constraint = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_value_or_constraint)
|
|
|
|
return false;
|
2020-03-18 14:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = std::make_shared<ASTSettingsProfileElement>();
|
|
|
|
result->parent_profile = std::move(parent_profile);
|
2020-04-21 10:21:03 +00:00
|
|
|
result->setting_name = std::move(setting_name);
|
2020-03-18 14:11:44 +00:00
|
|
|
result->value = std::move(value);
|
|
|
|
result->min_value = std::move(min_value);
|
|
|
|
result->max_value = std::move(max_value);
|
|
|
|
result->readonly = readonly;
|
|
|
|
result->id_mode = id_mode;
|
2020-04-07 23:57:14 +00:00
|
|
|
result->use_inherit_keyword = enable_inherit_keyword;
|
2020-03-18 14:11:44 +00:00
|
|
|
node = result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserSettingsProfileElements::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<ASTSettingsProfileElement>> elements;
|
|
|
|
|
2020-04-07 23:57:14 +00:00
|
|
|
if (ParserKeyword{"NONE"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
2020-03-18 14:11:44 +00:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ASTPtr ast;
|
2020-04-07 23:57:14 +00:00
|
|
|
if (!ParserSettingsProfileElement{}.useIDMode(id_mode).enableInheritKeyword(enable_inherit_keyword).parse(pos, ast, expected))
|
2020-03-18 14:11:44 +00:00
|
|
|
return false;
|
|
|
|
auto element = typeid_cast<std::shared_ptr<ASTSettingsProfileElement>>(ast);
|
|
|
|
elements.push_back(std::move(element));
|
|
|
|
}
|
|
|
|
while (ParserToken{TokenType::Comma}.ignore(pos, expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = std::make_shared<ASTSettingsProfileElements>();
|
|
|
|
result->elements = std::move(elements);
|
|
|
|
node = result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|