Skip to content

Commit

Permalink
feat: support drop table if exists (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
guojidan authored Dec 8, 2023
1 parent eb9ab5a commit 8cd12e8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/binder/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogicalPlan, BindError> {
pub(crate) fn bind_drop_table(
&mut self,
name: &ObjectName,
if_exists: &bool,
) -> Result<LogicalPlan, BindError> {
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)
Expand Down
7 changes: 5 additions & 2 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions src/execution/executor/ddl/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ impl<T: Transaction> Executor<T> for DropTable {
impl DropTable {
#[try_stream(boxed, ok = Tuple, error = ExecutorError)]
pub async fn _execute<T: Transaction>(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)?;
}
}
1 change: 1 addition & 0 deletions src/planner/operator/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use crate::catalog::TableName;
pub struct DropTableOperator {
/// Table name to insert to
pub table_name: TableName,
pub if_exists: bool,
}
10 changes: 9 additions & 1 deletion src/storage/kip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait Transaction: Sync + Send + 'static {
if_not_exists: bool,
) -> Result<TableName, StorageError>;

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>;

Expand Down

0 comments on commit 8cd12e8

Please sign in to comment.