-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feat : added truncate table support #19633
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
base: main
Are you sure you want to change the base?
Changes from all commits
c73c0bb
663a69b
9f44f39
aad0378
4a1fb16
a6196a3
12e5920
7b4d016
b331a34
e59cb23
4cfe422
32075e0
760ed22
5c0aeeb
7a9b4b1
e7dc856
2cf2913
fb14364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -1362,6 +1362,53 @@ impl<S: ContextProvider> SqlToRel<'_, S> { | |||
| exec_err!("Function name not provided") | ||||
| } | ||||
| } | ||||
| Statement::Truncate { | ||||
| table_names, | ||||
| partitions, | ||||
| identity, | ||||
| cascade, | ||||
| on_cluster, | ||||
| .. | ||||
| } => { | ||||
| if table_names.len() != 1 { | ||||
| return not_impl_err!( | ||||
| "TRUNCATE with multiple tables is not supported" | ||||
| ); | ||||
| } | ||||
|
|
||||
| let target = &table_names[0]; | ||||
| if target.only { | ||||
| return not_impl_err!("TRUNCATE with ONLY is not supported"); | ||||
| } | ||||
| if partitions.is_some() { | ||||
| return not_impl_err!("TRUNCATE with PARTITION is not supported"); | ||||
| } | ||||
| if identity.is_some() { | ||||
| return not_impl_err!( | ||||
| "TRUNCATE with RESTART/CONTINUE IDENTITY is not supported" | ||||
| ); | ||||
| } | ||||
| if cascade.is_some() { | ||||
| return not_impl_err!( | ||||
| "TRUNCATE with CASCADE/RESTRICT is not supported" | ||||
| ); | ||||
| } | ||||
| if on_cluster.is_some() { | ||||
| return not_impl_err!("TRUNCATE with ON CLUSTER is not supported"); | ||||
| } | ||||
| let table = self.object_name_to_table_reference(target.name.clone())?; | ||||
| let source = self.context_provider.get_table_source(table.clone())?; | ||||
|
|
||||
| Ok(LogicalPlan::Dml(DmlStatement::new( | ||||
| table.clone(), | ||||
| source, | ||||
| WriteOp::Truncate, | ||||
| Arc::new(LogicalPlan::EmptyRelation(EmptyRelation { | ||||
| produce_one_row: false, | ||||
| schema: DFSchemaRef::new(DFSchema::empty()), | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
the schema must match with table_schema. Is this important here ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike DELETE or UPDATE, TRUNCATE is a table-level operation with no filters, assignments. The input relation is only a placeholder to satisfy the DML plan shape and is never inspected or executed for row data. Because no rows or columns are read, the usual “input schema must match table schema” invariant does not apply here.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I verified this and you're right that TRUNCATE doesn't use the input. Physical planner shows:
So functionally the empty schema is fine. The doc comment is aspirational. Could either keep as-is and update the DmlStatement docs to note TRUNCATE is special, or use the table schema for consistency (extra code, no functional benefit). Former seems fine. |
||||
| })), | ||||
| ))) | ||||
| } | ||||
| Statement::CreateIndex(CreateIndex { | ||||
| name, | ||||
| table_name, | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You added the proto enum and conversions and worth adding a roundtrip test to lock it in.
In
datafusion/proto/tests/cases/roundtrip_logical_plan.rs, theroundtrip_logical_plan_dmltest covers INSERT, DELETE, UPDATE, CTAS.Adding TRUNCATE there would catch any future regressions: