#include #include #include #include #include #include #include #include #include namespace DB { bool ParserPartition::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { ParserKeyword s_id("ID"); ParserKeyword s_all("ALL"); ParserStringLiteral parser_string_literal; ParserExpression parser_expr; Pos begin = pos; auto partition = std::make_shared(); if (s_id.ignore(pos, expected)) { ASTPtr partition_id; if (!parser_string_literal.parse(pos, partition_id, expected)) return false; partition->id = partition_id->as().value.get(); } else if (s_all.ignore(pos, expected)) { partition->all = true; } else { ASTPtr value; if (!parser_expr.parse(pos, value, expected)) return false; size_t fields_count; const auto * tuple_ast = value->as(); bool surrounded_by_parens = false; if (tuple_ast && tuple_ast->name == "tuple") { surrounded_by_parens = true; const auto * arguments_ast = tuple_ast->arguments->as(); if (arguments_ast) fields_count = arguments_ast->children.size(); else fields_count = 0; } else if (const auto * literal = value->as()) { if (literal->value.getType() == Field::Types::Tuple) { surrounded_by_parens = true; fields_count = literal->value.get().size(); } else { fields_count = 1; } } else return false; if (surrounded_by_parens) { Pos left_paren = begin; Pos right_paren = pos; while (left_paren != right_paren && left_paren->type != TokenType::OpeningRoundBracket) ++left_paren; if (left_paren->type != TokenType::OpeningRoundBracket) return false; while (right_paren != left_paren && right_paren->type != TokenType::ClosingRoundBracket) --right_paren; if (right_paren->type != TokenType::ClosingRoundBracket) return false; } partition->value = value; partition->children.push_back(value); partition->fields_count = fields_count; } node = partition; return true; } }