-
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 6 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| // specific language governing permissions and limitations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! Tests for DELETE and UPDATE planning to verify filter and assignment extraction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! Tests for DELETE, UPDATE, and TRUNCATE planning to verify filter and assignment extraction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::any::Any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::sync::{Arc, Mutex}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -165,6 +165,66 @@ impl TableProvider for CaptureUpdateProvider { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// A TableProvider that captures whether truncate() was called. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct CaptureTruncateProvider { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema: SchemaRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| truncate_called: Arc<Mutex<bool>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl CaptureTruncateProvider { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn new(schema: SchemaRef) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| truncate_called: Arc::new(Mutex::new(false)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn was_truncated(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *self.truncate_called.lock().unwrap() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl std::fmt::Debug for CaptureTruncateProvider { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f.debug_struct("CaptureTruncateProvider") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .field("schema", &self.schema) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .finish() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[async_trait] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl TableProvider for CaptureTruncateProvider { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn as_any(&self) -> &dyn Any { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn schema(&self) -> SchemaRef { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Arc::clone(&self.schema) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn table_type(&self) -> TableType { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TableType::Base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async fn scan( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| &self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _state: &dyn Session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _projection: Option<&Vec<usize>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _filters: &[Expr], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _limit: Option<usize>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Arc::new(EmptyExec::new(Arc::clone(&self.schema)))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async fn truncate(&self, _state: &dyn Session) -> Result<Arc<dyn ExecutionPlan>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *self.truncate_called.lock().unwrap() = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Arc::new(EmptyExec::new(Arc::new(Schema::new(vec![ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Field::new("count", DataType::UInt64, false), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]))))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+220
to
+227
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. TableProvider::truncate docs say it returns an ExecutionPlan producing {count: UInt64}. This test returns EmptyExec which produces zero rows, it validates the hook was called, but not the contract. Could tighten this to return an actual row and assert on it:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_schema() -> SchemaRef { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Arc::new(Schema::new(vec![ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Field::new("id", DataType::Int32, false), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -269,6 +329,23 @@ async fn test_update_assignments() -> Result<()> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async fn test_truncate_calls_provider() -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let provider = Arc::new(CaptureTruncateProvider::new(test_schema())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let ctx = SessionContext::new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx.register_table("t", Arc::clone(&provider) as Arc<dyn TableProvider>)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx.sql("TRUNCATE TABLE t").await?.collect().await?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider.was_truncated(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "truncate() should be called on the TableProvider" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async fn test_unsupported_table_delete() -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let schema = test_schema(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -295,3 +372,18 @@ async fn test_unsupported_table_update() -> Result<()> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(result.is_err() || result.unwrap().collect().await.is_err()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async fn test_unsupported_table_truncate() -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let schema = test_schema(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let ctx = SessionContext::new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let empty_table = datafusion::datasource::empty::EmptyTable::new(schema); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx.register_table("empty_t", Arc::new(empty_table))?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let result = ctx.sql("TRUNCATE TABLE empty_t").await; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(result.is_err() || result.unwrap().collect().await.is_err()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 |
|---|---|---|
|
|
@@ -728,6 +728,7 @@ impl From<&WriteOp> for protobuf::dml_node::Type { | |
| WriteOp::Delete => protobuf::dml_node::Type::Delete, | ||
| WriteOp::Update => protobuf::dml_node::Type::Update, | ||
| WriteOp::Ctas => protobuf::dml_node::Type::Ctas, | ||
| WriteOp::Truncate => protobuf::dml_node::Type::Truncate, | ||
|
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. You added the proto enum and conversions and worth adding a roundtrip test to lock it in. In Adding TRUNCATE there would catch any future regressions: |
||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1362,6 +1362,28 @@ impl<S: ContextProvider> SqlToRel<'_, S> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exec_err!("Function name not provided") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Statement::Truncate { table_names, .. } => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if table_names.len() != 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return not_impl_err!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "TRUNCATE with multiple tables is not supported" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Statement::Truncate { table_names, .. } => { | |
| if table_names.len() != 1 { | |
| return not_impl_err!( | |
| "TRUNCATE with multiple tables is not supported" | |
| ); | |
| } | |
| 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"); | |
| } | |
| // ... rest | |
| } |
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.
If you add the option rejection, some negative tests would round it out:
statement error TRUNCATE with CASCADE/RESTRICT is not supported
TRUNCATE TABLE t1 CASCADE;
statement error TRUNCATE with multiple tables is not supported
TRUNCATE TABLE t1, t2;
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.
| /// The relation that determines the tuples to add/remove/modify the schema must match with table_schema |
the schema must match with table_schema. Is this important here ?
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.
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.
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.
I verified this and you're right that TRUNCATE doesn't use the input.
Physical planner shows:
- DELETE/UPDATE: extract filters/assignments from input
- TRUNCATE: ignores input entirely, just calls provider.truncate()
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.
Outdated
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.
This should use DmlStatement::new() rather than constructing the struct directly. The constructor sets output_schema: make_count_schema() which gives you the {count: UInt64} that DML ops return.
Also noticed this causes a proto roundtrip mismatch: encode uses the struct directly but decode uses the constructor (at mod.rs:978), so you'd get different plans before/after serialization.
Something like:
| Ok(LogicalPlan::Dml(DmlStatement { | |
| table_name: table.clone(), | |
| target: source, | |
| op: WriteOp::Truncate, | |
| input: Arc::new(LogicalPlan::EmptyRelation(EmptyRelation { | |
| produce_one_row: false, | |
| schema: DFSchemaRef::new(DFSchema::empty()), | |
| })), | |
| output_schema: DFSchemaRef::new(DFSchema::empty()), | |
| })) | |
| } | |
| Ok(LogicalPlan::Dml(DmlStatement::new( | |
| table.clone(), | |
| source, | |
| WriteOp::Truncate, | |
| Arc::new(LogicalPlan::EmptyRelation(EmptyRelation { | |
| produce_one_row: false, | |
| schema: DFSchemaRef::new(DFSchema::empty()), | |
| })), | |
| ))) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| ########## | ||
| ## Truncate Tests | ||
| ########## | ||
|
|
||
| statement ok | ||
| create table t1(a int, b varchar, c double, d int); | ||
|
|
||
| statement ok | ||
| insert into t1 values (1, 'abc', 3.14, 4), (2, 'def', 2.71, 5); | ||
|
|
||
| # Truncate all rows from table | ||
| query TT | ||
| explain truncate table t1; | ||
| ---- | ||
| logical_plan | ||
| 01)Dml: op=[Truncate] table=[t1] | ||
| 02)--EmptyRelation: rows=0 | ||
| physical_plan_error | ||
| 01)TRUNCATE operation on table 't1' | ||
| 02)caused by | ||
| 03)This feature is not implemented: TRUNCATE not supported for Base | ||
|
|
||
| # Test TRUNCATE with fully qualified table name | ||
| statement ok | ||
| create schema test_schema; | ||
|
|
||
| statement ok | ||
| create table test_schema.t5(a int); | ||
|
|
||
| query TT | ||
| explain truncate table test_schema.t5; | ||
| ---- | ||
| logical_plan | ||
| 01)Dml: op=[Truncate] table=[test_schema.t5] | ||
| 02)--EmptyRelation: rows=0 | ||
| physical_plan_error | ||
| 01)TRUNCATE operation on table 'test_schema.t5' | ||
| 02)caused by | ||
| 03)This feature is not implemented: TRUNCATE not supported for Base |
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.
nit: DELETE/UPDATE messages say "... for {} table" but this one drops "table"