Skip to content

Support multiple tables in TRUNCATE command #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,10 +1193,10 @@ pub enum Statement {
noscan: bool,
compute_statistics: bool,
},
/// Truncate (Hive)
/// Truncate (Hive and PostgreSQL)
Truncate {
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
table_name: ObjectName,
table_names: Vec<ObjectName>,
partitions: Option<Vec<Expr>>,
/// TABLE - optional keyword;
table: bool,
Expand Down Expand Up @@ -1982,12 +1982,20 @@ impl fmt::Display for Statement {
Ok(())
}
Statement::Truncate {
table_name,
table_names,
partitions,
table,
} => {
let table = if *table { "TABLE " } else { "" };
write!(f, "TRUNCATE {table}{table_name}")?;
write!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can use display_comma_separated here instead of making a a new Vec via join

f,
"TRUNCATE {table}{}",
table_names
.iter()
.map(|t| t.to_string())
.collect::<Vec<_>>()
.join(", ")
)?;
if let Some(ref parts) = partitions {
if !parts.is_empty() {
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,15 @@ impl<'a> Parser<'a> {

pub fn parse_truncate(&mut self) -> Result<Statement, ParserError> {
let table = self.parse_keyword(Keyword::TABLE);
let table_name = self.parse_object_name()?;
let table_names = self.parse_comma_separated(Parser::parse_object_name)?;
let mut partitions = None;
if self.parse_keyword(Keyword::PARTITION) {
self.expect_token(&Token::LParen)?;
partitions = Some(self.parse_comma_separated(Parser::parse_expr)?);
self.expect_token(&Token::RParen)?;
}
Ok(Statement::Truncate {
table_name,
table_names,
partitions,
table,
})
Expand Down
15 changes: 14 additions & 1 deletion tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3375,12 +3375,25 @@ fn parse_truncate() {
let truncate = pg_and_generic().verified_stmt("TRUNCATE db.table_name");
assert_eq!(
Statement::Truncate {
table_name: ObjectName(vec![Ident::new("db"), Ident::new("table_name")]),
table_names: vec![ObjectName(vec![Ident::new("db"), Ident::new("table_name")])],
partitions: None,
table: false
},
truncate
);

let truncate = pg_and_generic().verified_stmt("TRUNCATE TABLE tbl1, db.tbl2");
assert_eq!(
Statement::Truncate {
table_names: vec![
ObjectName(vec![Ident::new("tbl1")]),
ObjectName(vec![Ident::new("db"), Ident::new("tbl2")])
],
partitions: None,
table: true
},
truncate
);
}

#[test]
Expand Down