Skip to content

Postgres: Apply ONLY keyword per table in TRUNCATE stmt #1872

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

Merged
merged 1 commit into from
Jun 6, 2025
Merged
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: 9 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3014,9 +3014,6 @@ pub enum Statement {
/// TABLE - optional keyword;
table: bool,
/// Postgres-specific option
/// [ TRUNCATE TABLE ONLY ]
only: bool,
/// Postgres-specific option
/// [ RESTART IDENTITY | CONTINUE IDENTITY ]
identity: Option<TruncateIdentityOption>,
/// Postgres-specific option
Expand Down Expand Up @@ -3405,7 +3402,7 @@ pub enum Statement {
purge: bool,
/// MySQL-specific "TEMPORARY" keyword
temporary: bool,
/// MySQL-specific drop index syntax, which requires table specification
/// MySQL-specific drop index syntax, which requires table specification
/// See <https://dev.mysql.com/doc/refman/8.4/en/drop-index.html>
table: Option<ObjectName>,
},
Expand Down Expand Up @@ -4422,17 +4419,15 @@ impl fmt::Display for Statement {
table_names,
partitions,
table,
only,
identity,
cascade,
on_cluster,
} => {
let table = if *table { "TABLE " } else { "" };
let only = if *only { "ONLY " } else { "" };

write!(
f,
"TRUNCATE {table}{only}{table_names}",
"TRUNCATE {table}{table_names}",
table_names = display_comma_separated(table_names)
)?;

Expand Down Expand Up @@ -6097,10 +6092,17 @@ pub struct TruncateTableTarget {
/// name of the table being truncated
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
pub name: ObjectName,
/// Postgres-specific option
/// [ TRUNCATE TABLE ONLY ]
/// <https://www.postgresql.org/docs/current/sql-truncate.html>
pub only: bool,
}

impl fmt::Display for TruncateTableTarget {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.only {
write!(f, "ONLY ")?;
};
write!(f, "{}", self.name)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ impl Spanned for Statement {
table_names,
partitions,
table: _,
only: _,
identity: _,
cascade: _,
on_cluster: _,
Expand Down
8 changes: 4 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,13 @@ impl<'a> Parser<'a> {

pub fn parse_truncate(&mut self) -> Result<Statement, ParserError> {
let table = self.parse_keyword(Keyword::TABLE);
let only = self.parse_keyword(Keyword::ONLY);

let table_names = self
.parse_comma_separated(|p| p.parse_object_name(false))?
.parse_comma_separated(|p| {
Ok((p.parse_keyword(Keyword::ONLY), p.parse_object_name(false)?))
})?
.into_iter()
.map(|n| TruncateTableTarget { name: n })
.map(|(only, name)| TruncateTableTarget { name, only })
.collect();

let mut partitions = None;
Expand Down Expand Up @@ -996,7 +997,6 @@ impl<'a> Parser<'a> {
table_names,
partitions,
table,
only,
identity,
cascade,
on_cluster,
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15294,3 +15294,31 @@ fn test_open() {
})
);
}

#[test]
fn parse_truncate_only() {
let truncate = all_dialects().verified_stmt("TRUNCATE TABLE employee, ONLY dept");

let table_names = vec![
TruncateTableTarget {
name: ObjectName::from(vec![Ident::new("employee")]),
only: false,
},
TruncateTableTarget {
name: ObjectName::from(vec![Ident::new("dept")]),
only: true,
},
];

assert_eq!(
Statement::Truncate {
table_names,
partitions: None,
table: true,
identity: None,
cascade: None,
on_cluster: None,
},
truncate
);
}
7 changes: 4 additions & 3 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4790,13 +4790,13 @@ fn parse_truncate() {
let table_name = ObjectName::from(vec![Ident::new("db"), Ident::new("table_name")]);
let table_names = vec![TruncateTableTarget {
name: table_name.clone(),
only: false,
}];
assert_eq!(
Statement::Truncate {
table_names,
partitions: None,
table: false,
only: false,
identity: None,
cascade: None,
on_cluster: None,
Expand All @@ -4813,14 +4813,14 @@ fn parse_truncate_with_options() {
let table_name = ObjectName::from(vec![Ident::new("db"), Ident::new("table_name")]);
let table_names = vec![TruncateTableTarget {
name: table_name.clone(),
only: true,
}];

assert_eq!(
Statement::Truncate {
table_names,
partitions: None,
table: true,
only: true,
identity: Some(TruncateIdentityOption::Restart),
cascade: Some(CascadeOption::Cascade),
on_cluster: None,
Expand All @@ -4841,9 +4841,11 @@ fn parse_truncate_with_table_list() {
let table_names = vec![
TruncateTableTarget {
name: table_name_a.clone(),
only: false,
},
TruncateTableTarget {
name: table_name_b.clone(),
only: false,
},
];

Expand All @@ -4852,7 +4854,6 @@ fn parse_truncate_with_table_list() {
table_names,
partitions: None,
table: true,
only: false,
identity: Some(TruncateIdentityOption::Restart),
cascade: Some(CascadeOption::Cascade),
on_cluster: None,
Expand Down