1 | #include <Parsers/ParserDictionaryAttributeDeclaration.h> |
2 | |
3 | #include <Parsers/ExpressionElementParsers.h> |
4 | #include <Parsers/ExpressionListParsers.h> |
5 | |
6 | namespace DB |
7 | { |
8 | |
9 | bool ParserDictionaryAttributeDeclaration::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
10 | { |
11 | ParserIdentifier name_parser; |
12 | ParserIdentifierWithOptionalParameters type_parser; |
13 | ParserKeyword s_default{"DEFAULT" }; |
14 | ParserKeyword s_expression{"EXPRESSION" }; |
15 | ParserKeyword s_hierarchical{"HIERARCHICAL" }; |
16 | ParserKeyword s_injective{"INJECTIVE" }; |
17 | ParserKeyword s_is_object_id{"IS_OBJECT_ID" }; |
18 | ParserLiteral default_parser; |
19 | ParserTernaryOperatorExpression expression_parser; |
20 | |
21 | /// mandatory attribute name |
22 | ASTPtr name; |
23 | if (!name_parser.parse(pos, name, expected)) |
24 | return false; |
25 | |
26 | ASTPtr type; |
27 | ASTPtr default_value; |
28 | ASTPtr expression; |
29 | bool hierarchical = false; |
30 | bool injective = false; |
31 | bool is_object_id = false; |
32 | |
33 | /// attribute name should be followed by type name if it |
34 | if (!type_parser.parse(pos, type, expected)) |
35 | return false; |
36 | |
37 | /// loop to avoid strict order of attribute properties |
38 | while (true) |
39 | { |
40 | if (!default_value && s_default.ignore(pos, expected)) |
41 | { |
42 | if (!default_parser.parse(pos, default_value, expected)) |
43 | return false; |
44 | continue; |
45 | } |
46 | |
47 | if (!expression && s_expression.ignore(pos, expected)) |
48 | { |
49 | if (!expression_parser.parse(pos, expression, expected)) |
50 | return false; |
51 | continue; |
52 | } |
53 | |
54 | /// just single keyword, we don't use "true" or "1" for value |
55 | if (!hierarchical && s_hierarchical.ignore(pos, expected)) |
56 | { |
57 | hierarchical = true; |
58 | continue; |
59 | } |
60 | |
61 | if (!injective && s_injective.ignore(pos, expected)) |
62 | { |
63 | injective = true; |
64 | continue; |
65 | } |
66 | |
67 | if (!is_object_id && s_is_object_id.ignore(pos, expected)) |
68 | { |
69 | is_object_id = true; |
70 | continue; |
71 | } |
72 | |
73 | break; |
74 | } |
75 | |
76 | auto attribute_declaration = std::make_shared<ASTDictionaryAttributeDeclaration>(); |
77 | node = attribute_declaration; |
78 | tryGetIdentifierNameInto(name, attribute_declaration->name); |
79 | |
80 | if (type) |
81 | { |
82 | attribute_declaration->type = type; |
83 | attribute_declaration->children.push_back(std::move(type)); |
84 | } |
85 | |
86 | if (default_value) |
87 | { |
88 | attribute_declaration->default_value = default_value; |
89 | attribute_declaration->children.push_back(std::move(default_value)); |
90 | } |
91 | |
92 | if (expression) |
93 | { |
94 | attribute_declaration->expression = expression; |
95 | attribute_declaration->children.push_back(std::move(expression)); |
96 | } |
97 | |
98 | attribute_declaration->hierarchical = hierarchical; |
99 | attribute_declaration->injective = injective; |
100 | attribute_declaration->is_object_id = is_object_id; |
101 | |
102 | return true; |
103 | } |
104 | |
105 | |
106 | bool ParserDictionaryAttributeDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
107 | { |
108 | return ParserList(std::make_unique<ParserDictionaryAttributeDeclaration>(), |
109 | std::make_unique<ParserToken>(TokenType::Comma), false) |
110 | .parse(pos, node, expected); |
111 | } |
112 | |
113 | } |
114 | |