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>
|
2023-10-13 14:22:18 +00:00
|
|
|
#include <Parsers/ASTQueryParameter.h>
|
2017-09-06 20:34:26 +00:00
|
|
|
|
|
|
|
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;
|
2023-10-13 14:22:18 +00:00
|
|
|
ParserSubstitution parser_substitution;
|
|
|
|
ParserLiteral literal_parser;
|
2023-10-13 15:36:38 +00:00
|
|
|
ParserTupleOfLiterals tuple_of_literals;
|
2023-10-13 16:16:51 +00:00
|
|
|
ParserExpression parser_expr;
|
2023-10-13 14:22:18 +00:00
|
|
|
|
2017-09-06 20:34:26 +00:00
|
|
|
auto partition = std::make_shared<ASTPartition>();
|
|
|
|
|
|
|
|
if (s_id.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
ASTPtr partition_id;
|
2023-10-13 14:22:18 +00:00
|
|
|
if (!parser_string_literal.parse(pos, partition_id, expected) && !parser_substitution.parse(pos, partition_id, expected))
|
2017-09-06 20:34:26 +00:00
|
|
|
return false;
|
|
|
|
|
2023-10-13 14:22:18 +00:00
|
|
|
if (auto * partition_id_literal = partition_id->as<ASTLiteral>(); partition_id_literal != nullptr)
|
|
|
|
partition->setPartitionID(partition_id);
|
|
|
|
else if (auto * partition_id_query_parameter = partition_id->as<ASTQueryParameter>(); partition_id_query_parameter != nullptr)
|
|
|
|
partition->setPartitionID(partition_id);
|
|
|
|
else
|
|
|
|
return false;
|
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;
|
2023-10-13 16:51:32 +00:00
|
|
|
std::optional<size_t> fields_count;
|
2023-10-13 15:36:38 +00:00
|
|
|
if (literal_parser.parse(pos, value, expected) || tuple_of_literals.parse(pos, value, expected))
|
2020-03-19 01:15:01 +00:00
|
|
|
{
|
2023-10-13 14:22:18 +00:00
|
|
|
auto * literal = value->as<ASTLiteral>();
|
2020-03-19 01:15:01 +00:00
|
|
|
if (literal->value.getType() == Field::Types::Tuple)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 16:16:51 +00:00
|
|
|
else if (parser_substitution.parse(pos, value, expected))
|
|
|
|
{
|
2023-10-13 16:51:32 +00:00
|
|
|
/// It can be tuple substitution
|
|
|
|
fields_count = std::nullopt;
|
2023-10-13 16:16:51 +00:00
|
|
|
}
|
|
|
|
else if (parser_expr.parse(pos, value, expected))
|
2023-10-13 14:22:18 +00:00
|
|
|
{
|
|
|
|
const auto * tuple_ast = value->as<ASTFunction>();
|
2023-10-13 14:52:04 +00:00
|
|
|
if (tuple_ast && tuple_ast->name == "tuple")
|
2023-10-13 14:22:18 +00:00
|
|
|
{
|
2023-10-13 14:52:04 +00:00
|
|
|
const auto * arguments_ast = tuple_ast->arguments->as<ASTExpressionList>();
|
|
|
|
if (arguments_ast)
|
|
|
|
fields_count = arguments_ast->children.size();
|
|
|
|
else
|
|
|
|
fields_count = 0;
|
2023-10-13 14:22:18 +00:00
|
|
|
}
|
2023-10-13 14:52:04 +00:00
|
|
|
else
|
|
|
|
return false;
|
2023-10-13 14:22:18 +00:00
|
|
|
}
|
2023-10-13 14:52:04 +00:00
|
|
|
else
|
2023-10-13 14:22:18 +00:00
|
|
|
{
|
2020-03-19 01:15:01 +00:00
|
|
|
return false;
|
2023-10-13 14:22:18 +00:00
|
|
|
}
|
2017-09-06 20:34:26 +00:00
|
|
|
|
2023-10-13 14:22:18 +00:00
|
|
|
partition->setPartitionValue(value);
|
2017-09-06 20:34:26 +00:00
|
|
|
partition->fields_count = fields_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = partition;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|