2017-09-06 20:34:26 +00:00
|
|
|
#include <Parsers/ParserPartition.h>
|
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ASTPartition.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2022-03-31 09:50:07 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2017-09-06 20:34:26 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool ParserPartition::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserKeyword s_id("ID");
|
2022-03-31 09:50:07 +00:00
|
|
|
ParserKeyword s_all("ALL");
|
2017-09-06 20:34:26 +00:00
|
|
|
ParserStringLiteral parser_string_literal;
|
|
|
|
ParserExpression parser_expr;
|
|
|
|
|
|
|
|
Pos begin = pos;
|
|
|
|
|
|
|
|
auto partition = std::make_shared<ASTPartition>();
|
|
|
|
|
|
|
|
if (s_id.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
ASTPtr partition_id;
|
|
|
|
if (!parser_string_literal.parse(pos, partition_id, expected))
|
|
|
|
return false;
|
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
partition->id = partition_id->as<ASTLiteral &>().value.get<String>();
|
2017-09-06 20:34:26 +00:00
|
|
|
}
|
2022-03-31 09:50:07 +00:00
|
|
|
else if (s_all.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
partition->all = true;
|
|
|
|
}
|
2017-09-06 20:34:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ASTPtr value;
|
|
|
|
if (!parser_expr.parse(pos, value, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t fields_count;
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * tuple_ast = value->as<ASTFunction>();
|
2020-03-19 01:15:01 +00:00
|
|
|
bool surrounded_by_parens = false;
|
2017-09-06 20:34:26 +00:00
|
|
|
if (tuple_ast && tuple_ast->name == "tuple")
|
|
|
|
{
|
2020-03-19 01:15:01 +00:00
|
|
|
surrounded_by_parens = true;
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * arguments_ast = tuple_ast->arguments->as<ASTExpressionList>();
|
2017-09-06 20:34:26 +00:00
|
|
|
if (arguments_ast)
|
|
|
|
fields_count = arguments_ast->children.size();
|
|
|
|
else
|
|
|
|
fields_count = 0;
|
2020-03-19 01:15:01 +00:00
|
|
|
}
|
2020-04-22 05:39:31 +00:00
|
|
|
else if (const auto * literal = value->as<ASTLiteral>())
|
2020-03-19 01:15:01 +00:00
|
|
|
{
|
|
|
|
if (literal->value.getType() == Field::Types::Tuple)
|
|
|
|
{
|
|
|
|
surrounded_by_parens = true;
|
2020-04-22 05:39:31 +00:00
|
|
|
fields_count = literal->value.get<const Tuple &>().size();
|
2020-03-19 01:15:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fields_count = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2017-09-06 20:34:26 +00:00
|
|
|
|
2020-03-19 01:15:01 +00:00
|
|
|
if (surrounded_by_parens)
|
|
|
|
{
|
2017-09-06 20:34:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|