Skip to content

Commit 8b62d91

Browse files
committed
sql: add truncate statement support
1 parent d6ff3db commit 8b62d91

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

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

554+
self.truncate_to_plan(table_name)
555+
},
546556
Statement::Delete(Delete {
547557
tables,
548558
using,
@@ -1499,6 +1509,26 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
14991509
Ok(plan)
15001510
}
15011511

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

0 commit comments

Comments
 (0)