Skip to content

Commit 7ea6796

Browse files
committed
sql: add truncate statement support
1 parent d6ff3db commit 7ea6796

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

datafusion/expr/src/logical_plan/dml.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub enum WriteOp {
119119
Delete,
120120
Update,
121121
Ctas,
122+
Truncate,
122123
}
123124

124125
impl WriteOp {
@@ -130,6 +131,7 @@ impl WriteOp {
130131
WriteOp::Delete => "Delete",
131132
WriteOp::Update => "Update",
132133
WriteOp::Ctas => "Ctas",
134+
WriteOp::Truncate => "Truncate",
133135
}
134136
}
135137
}

datafusion/sql/src/statement.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,21 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
542542
}
543543
self.update_to_plan(table, assignments, from, selection)
544544
}
545+
Statement::Truncate {
546+
table_name,
547+
partitions,
548+
table,
549+
} => {
550+
if !table {
551+
plan_err!("Truncate of non-tables not yet supported")?;
552+
}
553+
554+
if partitions.is_some() {
555+
plan_err!("Partition clause not supported")?;
556+
}
545557

558+
self.truncate_to_plan(table_name)
559+
}
546560
Statement::Delete(Delete {
547561
tables,
548562
using,
@@ -1499,6 +1513,23 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
14991513
Ok(plan)
15001514
}
15011515

1516+
fn truncate_to_plan(&self, table_name: ObjectName) -> Result<LogicalPlan> {
1517+
// Do a table lookup to verify the table exists
1518+
let table_ref = self.object_name_to_table_reference(table_name.clone())?;
1519+
let table_source = self.context_provider.get_table_source(table_ref.clone())?;
1520+
let schema = (*table_source.schema()).clone();
1521+
let schema = DFSchema::try_from(schema)?;
1522+
let scan = LogicalPlanBuilder::empty(false).build()?;
1523+
1524+
let plan = LogicalPlan::Dml(DmlStatement::new(
1525+
table_ref,
1526+
schema.into(),
1527+
WriteOp::Truncate,
1528+
Arc::new(scan),
1529+
));
1530+
Ok(plan)
1531+
}
1532+
15021533
fn show_columns_to_plan(
15031534
&self,
15041535
extended: bool,

0 commit comments

Comments
 (0)