Skip to content

Commit fd36c8f

Browse files
committed
clickhouse: support ALTER TABLE DROP PARTITION without parentheses
Allow DROP PARTITION expr without parentheses by detecting missing ( and falling back to parse_expr() for bare partition values.
1 parent dd98566 commit fd36c8f

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/parser/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6628,12 +6628,18 @@ impl<'a> Parser<'a> {
66286628
partition: Partition::Part(self.parse_expr()?),
66296629
}
66306630
} else if self.parse_keyword(Keyword::PARTITION) {
6631-
self.expect_token(&Token::LParen)?;
6632-
let partitions = self.parse_comma_separated(Parser::parse_expr)?;
6633-
self.expect_token(&Token::RParen)?;
6634-
AlterTableOperation::DropPartitions {
6635-
partitions,
6636-
if_exists: false,
6631+
if self.peek_token().token == Token::LParen {
6632+
self.expect_token(&Token::LParen)?;
6633+
let partitions = self.parse_comma_separated(Parser::parse_expr)?;
6634+
self.expect_token(&Token::RParen)?;
6635+
AlterTableOperation::DropPartitions {
6636+
partitions,
6637+
if_exists: false,
6638+
}
6639+
} else {
6640+
AlterTableOperation::DropPartition {
6641+
partition: Partition::Expr(self.parse_expr()?),
6642+
}
66376643
}
66386644
} else if self.parse_keyword(Keyword::CONSTRAINT) {
66396645
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);

tests/sqlparser_clickhouse.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,55 @@ fn parse_alter_table_drop_partition_and_part() {
612612
}
613613
_ => unreachable!(),
614614
}
615+
616+
// DROP PARTITION with string literal (no parens)
617+
match clickhouse_and_generic()
618+
.verified_stmt("ALTER TABLE mt DROP PARTITION '2020-11-21'")
619+
{
620+
Statement::AlterTable {
621+
name, operations, ..
622+
} => {
623+
assert_eq!("mt", name.to_string());
624+
assert_eq!(
625+
operations[0],
626+
AlterTableOperation::DropPartition {
627+
partition: Partition::Expr(Expr::Value(Value::SingleQuotedString(
628+
"2020-11-21".to_string()
629+
))),
630+
}
631+
);
632+
}
633+
_ => unreachable!(),
634+
}
635+
636+
// DROP PARTITION with numeric literal (no parens)
637+
match clickhouse_and_generic().verified_stmt("ALTER TABLE visits DROP PARTITION 201901") {
638+
Statement::AlterTable {
639+
name, operations, ..
640+
} => {
641+
assert_eq!("visits", name.to_string());
642+
assert_eq!(
643+
operations[0],
644+
AlterTableOperation::DropPartition {
645+
partition: Partition::Expr(Expr::Value(Value::Number(
646+
"201901".to_string(),
647+
false
648+
))),
649+
}
650+
);
651+
}
652+
_ => unreachable!(),
653+
}
654+
655+
// DROP PARTITION with tuple() function call
656+
clickhouse_and_generic().verified_stmt(
657+
"ALTER TABLE visits DROP PARTITION tuple(toYYYYMM(toDate('2019-01-25')))",
658+
);
659+
660+
// Multiple statements: DROP PARTITION + DROP PART
661+
clickhouse_and_generic().parse_sql_statements(
662+
"ALTER TABLE mt DROP PARTITION '2020-11-21'; ALTER TABLE mt DROP PART 'all_4_4_0'",
663+
).unwrap();
615664
}
616665

617666
#[test]

0 commit comments

Comments
 (0)