diff --git a/src/binder/drop_table.rs b/src/binder/drop_table.rs index 1631c2aa..e2296d2c 100644 --- a/src/binder/drop_table.rs +++ b/src/binder/drop_table.rs @@ -7,13 +7,20 @@ use sqlparser::ast::ObjectName; use std::sync::Arc; impl<'a, T: Transaction> Binder<'a, T> { - pub(crate) fn bind_drop_table(&mut self, name: &ObjectName) -> Result { + pub(crate) fn bind_drop_table( + &mut self, + name: &ObjectName, + if_exists: &bool, + ) -> Result { let name = lower_case_name(name); let (_, name) = split_name(&name)?; let table_name = Arc::new(name.to_string()); let plan = LogicalPlan { - operator: Operator::DropTable(DropTableOperator { table_name }), + operator: Operator::DropTable(DropTableOperator { + table_name, + if_exists: *if_exists, + }), childrens: vec![], }; Ok(plan) diff --git a/src/binder/mod.rs b/src/binder/mod.rs index 5a7780fe..ab25846f 100644 --- a/src/binder/mod.rs +++ b/src/binder/mod.rs @@ -129,9 +129,12 @@ impl<'a, T: Transaction> Binder<'a, T> { .. } => self.bind_create_table(name, columns, constraints, *if_not_exists)?, Statement::Drop { - object_type, names, .. + object_type, + names, + if_exists, + .. } => match object_type { - ObjectType::Table => self.bind_drop_table(&names[0])?, + ObjectType::Table => self.bind_drop_table(&names[0], if_exists)?, _ => todo!(), }, Statement::Insert { diff --git a/src/execution/executor/ddl/drop_table.rs b/src/execution/executor/ddl/drop_table.rs index 1b8c8cde..92366730 100644 --- a/src/execution/executor/ddl/drop_table.rs +++ b/src/execution/executor/ddl/drop_table.rs @@ -25,8 +25,11 @@ impl Executor for DropTable { impl DropTable { #[try_stream(boxed, ok = Tuple, error = ExecutorError)] pub async fn _execute(self, transaction: &mut T) { - let DropTableOperator { table_name } = self.op; + let DropTableOperator { + table_name, + if_exists, + } = self.op; - transaction.drop_table(&table_name)?; + transaction.drop_table(&table_name, if_exists)?; } } diff --git a/src/planner/operator/drop_table.rs b/src/planner/operator/drop_table.rs index b343a457..5d7b022b 100644 --- a/src/planner/operator/drop_table.rs +++ b/src/planner/operator/drop_table.rs @@ -4,4 +4,5 @@ use crate::catalog::TableName; pub struct DropTableOperator { /// Table name to insert to pub table_name: TableName, + pub if_exists: bool, } diff --git a/src/storage/kip.rs b/src/storage/kip.rs index a195f45e..c432f712 100644 --- a/src/storage/kip.rs +++ b/src/storage/kip.rs @@ -237,7 +237,15 @@ impl Transaction for KipTransaction { Ok(table_name) } - fn drop_table(&mut self, table_name: &str) -> Result<(), StorageError> { + fn drop_table(&mut self, table_name: &str, if_exists: bool) -> Result<(), StorageError> { + if self.table(Arc::new(table_name.to_string())).is_none() { + if if_exists { + return Ok(()); + } else { + return Err(StorageError::TableNotFound); + } + } + self.drop_data(table_name)?; let (min, max) = TableCodec::columns_bound(table_name); diff --git a/src/storage/mod.rs b/src/storage/mod.rs index dffc2c5f..96506d7f 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -76,7 +76,7 @@ pub trait Transaction: Sync + Send + 'static { if_not_exists: bool, ) -> Result; - fn drop_table(&mut self, table_name: &str) -> Result<(), StorageError>; + fn drop_table(&mut self, table_name: &str, if_exists: bool) -> Result<(), StorageError>; fn drop_data(&mut self, table_name: &str) -> Result<(), StorageError>; fn table(&self, table_name: TableName) -> Option<&TableCatalog>;