Merge pull request #59901 from ClickHouse/fix-partition-expression-parsing-with-parens

Fix parsing of partition expressions surrounded by parens
This commit is contained in:
alesapin 2024-02-13 12:52:24 +01:00 committed by GitHub
commit f48c5973cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 17 deletions

View File

@ -18,8 +18,6 @@ bool ParserPartition::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
ParserKeyword s_all("ALL"); ParserKeyword s_all("ALL");
ParserStringLiteral parser_string_literal; ParserStringLiteral parser_string_literal;
ParserSubstitution parser_substitution; ParserSubstitution parser_substitution;
ParserLiteral literal_parser;
ParserTupleOfLiterals tuple_of_literals;
ParserExpression parser_expr; ParserExpression parser_expr;
auto partition = std::make_shared<ASTPartition>(); auto partition = std::make_shared<ASTPartition>();
@ -45,34 +43,35 @@ bool ParserPartition::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{ {
ASTPtr value; ASTPtr value;
std::optional<size_t> fields_count; std::optional<size_t> fields_count;
if (literal_parser.parse(pos, value, expected) || tuple_of_literals.parse(pos, value, expected)) if (parser_substitution.parse(pos, value, expected))
{
auto * literal = value->as<ASTLiteral>();
if (literal->value.getType() == Field::Types::Tuple)
{
fields_count = literal->value.get<const Tuple &>().size();
}
else
{
fields_count = 1;
}
}
else if (parser_substitution.parse(pos, value, expected))
{ {
/// It can be tuple substitution /// It can be tuple substitution
fields_count = std::nullopt; fields_count = std::nullopt;
} }
else if (parser_expr.parse(pos, value, expected)) else if (parser_expr.parse(pos, value, expected))
{ {
const auto * tuple_ast = value->as<ASTFunction>(); if (const auto * tuple_ast = value->as<ASTFunction>(); tuple_ast)
if (tuple_ast && tuple_ast->name == "tuple")
{ {
if (tuple_ast->name != "tuple")
return false;
const auto * arguments_ast = tuple_ast->arguments->as<ASTExpressionList>(); const auto * arguments_ast = tuple_ast->arguments->as<ASTExpressionList>();
if (arguments_ast) if (arguments_ast)
fields_count = arguments_ast->children.size(); fields_count = arguments_ast->children.size();
else else
fields_count = 0; fields_count = 0;
} }
else if (const auto* literal_ast = value->as<ASTLiteral>(); literal_ast)
{
if (literal_ast->value.getType() == Field::Types::Tuple)
{
fields_count = literal_ast->value.get<const Tuple &>().size();
}
else
{
fields_count = 1;
}
}
else else
return false; return false;
} }

View File

@ -7,3 +7,8 @@
0 0
0 0
0 0
0
0
0
0
0

View File

@ -10,6 +10,24 @@ PARTITION BY toMonday(EventDate);
INSERT INTO test VALUES(toDate('2023-10-09')); INSERT INTO test VALUES(toDate('2023-10-09'));
ALTER TABLE test DROP PARTITION ('2023-10-09');
SELECT count() FROM test;
INSERT INTO test VALUES(toDate('2023-10-09'));
ALTER TABLE test DROP PARTITION (('2023-10-09'));
SELECT count() FROM test;
INSERT INTO test VALUES(toDate('2023-10-09'));
ALTER TABLE test DROP PARTITION '2023-10-09';
SELECT count() FROM test;
INSERT INTO test VALUES(toDate('2023-10-09'));
SET param_partition='2023-10-09'; SET param_partition='2023-10-09';
ALTER TABLE test DROP PARTITION {partition:String}; ALTER TABLE test DROP PARTITION {partition:String};
@ -51,6 +69,17 @@ ENGINE = MergeTree
ORDER BY tuple() ORDER BY tuple()
PARTITION BY (a * b, b * b); PARTITION BY (a * b, b * b);
INSERT INTO test2 VALUES(1, 2);
ALTER TABLE test2 DROP PARTITION tuple(2, 4);
SELECT count() FROM test2;
INSERT INTO test2 VALUES(1, 2);
ALTER TABLE test2 DROP PARTITION (2, 4);
SELECT count() FROM test2;
INSERT INTO test2 VALUES(1, 2); INSERT INTO test2 VALUES(1, 2);