ClickHouse/dbms/Parsers/ParserDictionaryAttributeDeclaration.cpp

114 lines
3.2 KiB
C++
Raw Normal View History

2019-10-07 16:23:16 +00:00
#include <Parsers/ParserDictionaryAttributeDeclaration.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
namespace DB
{
bool ParserDictionaryAttributeDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
ParserIdentifier name_parser;
ParserIdentifierWithOptionalParameters type_parser;
ParserKeyword s_default{"DEFAULT"};
ParserKeyword s_expression{"EXPRESSION"};
ParserKeyword s_hierarchical{"HIERARCHICAL"};
ParserKeyword s_injective{"INJECTIVE"};
ParserKeyword s_is_object_id{"IS_OBJECT_ID"};
ParserLiteral default_parser;
ParserTernaryOperatorExpression expression_parser;
2019-10-08 13:26:15 +00:00
/// mandatory attribute name
2019-10-07 16:23:16 +00:00
ASTPtr name;
if (!name_parser.parse(pos, name, expected))
return false;
ASTPtr type;
ASTPtr default_value;
ASTPtr expression;
bool hierarchical = false;
bool injective = false;
bool is_object_id = false;
2019-10-08 13:26:15 +00:00
/// attribute name should be followed by type name if it
2019-10-08 10:50:57 +00:00
if (!type_parser.parse(pos, type, expected))
return false;
2019-10-08 13:26:15 +00:00
/// loop to avoid strict order of attribute properties
2019-10-08 12:35:17 +00:00
while (true)
2019-10-07 16:23:16 +00:00
{
2019-10-08 10:50:57 +00:00
if (!default_value && s_default.ignore(pos, expected))
{
if (!default_parser.parse(pos, default_value, expected))
return false;
continue;
}
2019-10-07 16:23:16 +00:00
2019-10-08 10:50:57 +00:00
if (!expression && s_expression.ignore(pos, expected))
{
if (!expression_parser.parse(pos, expression, expected))
return false;
continue;
}
2019-10-07 16:23:16 +00:00
2019-10-08 13:26:15 +00:00
/// just single keyword, we don't use "true" or "1" for value
2019-10-08 10:50:57 +00:00
if (!hierarchical && s_hierarchical.ignore(pos, expected))
{
hierarchical = true;
continue;
}
2019-10-07 16:23:16 +00:00
2019-10-08 10:50:57 +00:00
if (!injective && s_injective.ignore(pos, expected))
{
injective = true;
continue;
}
2019-10-07 16:23:16 +00:00
2019-10-08 10:50:57 +00:00
if (!is_object_id && s_is_object_id.ignore(pos, expected))
{
is_object_id = true;
continue;
}
2019-10-07 16:23:16 +00:00
2019-10-08 10:50:57 +00:00
break;
}
2019-10-07 16:23:16 +00:00
auto attribute_declaration = std::make_shared<ASTDictionaryAttributeDeclaration>();
node = attribute_declaration;
tryGetIdentifierNameInto(name, attribute_declaration->name);
if (type)
{
attribute_declaration->type = type;
attribute_declaration->children.push_back(std::move(type));
}
if (default_value)
{
attribute_declaration->default_value = default_value;
attribute_declaration->children.push_back(std::move(default_value));
}
2019-10-07 16:46:35 +00:00
if (expression)
{
attribute_declaration->expression = expression;
attribute_declaration->children.push_back(std::move(expression));
}
2019-10-08 13:26:15 +00:00
attribute_declaration->hierarchical = hierarchical;
attribute_declaration->injective = injective;
attribute_declaration->is_object_id = is_object_id;
2019-10-07 16:23:16 +00:00
return true;
}
bool ParserDictionaryAttributeDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
2019-10-08 13:26:15 +00:00
return ParserList(std::make_unique<ParserDictionaryAttributeDeclaration>(),
std::make_unique<ParserToken>(TokenType::Comma), false)
2019-10-07 16:23:16 +00:00
.parse(pos, node, expected);
}
}