1#include <Parsers/ParserPartition.h>
2#include <Parsers/CommonParsers.h>
3#include <Parsers/ExpressionElementParsers.h>
4#include <Parsers/ExpressionListParsers.h>
5#include <Parsers/ASTPartition.h>
6#include <Parsers/ASTLiteral.h>
7#include <Parsers/ASTFunction.h>
8#include <Common/typeid_cast.h>
9
10namespace DB
11{
12
13bool ParserPartition::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
14{
15 ParserKeyword s_id("ID");
16 ParserStringLiteral parser_string_literal;
17 ParserExpression parser_expr;
18
19 Pos begin = pos;
20
21 auto partition = std::make_shared<ASTPartition>();
22
23 if (s_id.ignore(pos, expected))
24 {
25 ASTPtr partition_id;
26 if (!parser_string_literal.parse(pos, partition_id, expected))
27 return false;
28
29 partition->id = partition_id->as<ASTLiteral &>().value.get<String>();
30 }
31 else
32 {
33 ASTPtr value;
34 if (!parser_expr.parse(pos, value, expected))
35 return false;
36
37 size_t fields_count;
38 String fields_str;
39
40 const auto * tuple_ast = value->as<ASTFunction>();
41 if (tuple_ast && tuple_ast->name == "tuple")
42 {
43 const auto * arguments_ast = tuple_ast->arguments->as<ASTExpressionList>();
44 if (arguments_ast)
45 fields_count = arguments_ast->children.size();
46 else
47 fields_count = 0;
48
49 Pos left_paren = begin;
50 Pos right_paren = pos;
51
52 while (left_paren != right_paren && left_paren->type != TokenType::OpeningRoundBracket)
53 ++left_paren;
54 if (left_paren->type != TokenType::OpeningRoundBracket)
55 return false;
56
57 while (right_paren != left_paren && right_paren->type != TokenType::ClosingRoundBracket)
58 --right_paren;
59 if (right_paren->type != TokenType::ClosingRoundBracket)
60 return false;
61
62 fields_str = String(left_paren->end, right_paren->begin - left_paren->end);
63 }
64 else
65 {
66 fields_count = 1;
67 fields_str = String(begin->begin, pos->begin - begin->begin);
68 }
69
70 partition->value = value;
71 partition->children.push_back(value);
72 partition->fields_str = std::move(fields_str);
73 partition->fields_count = fields_count;
74 }
75
76 node = partition;
77 return true;
78}
79
80}
81