quill_sql/execution/physical_plan/
drop_table.rs1use crate::catalog::{SchemaRef, EMPTY_SCHEMA_REF};
2use crate::error::{QuillSQLError, QuillSQLResult};
3use crate::execution::{ExecutionContext, VolcanoExecutor};
4use crate::storage::tuple::Tuple;
5use crate::transaction::LockMode;
6use crate::utils::table_ref::TableReference;
7
8#[derive(Debug)]
9pub struct PhysicalDropTable {
10 table: TableReference,
11 if_exists: bool,
12}
13
14impl PhysicalDropTable {
15 pub fn new(table: TableReference, if_exists: bool) -> Self {
16 Self { table, if_exists }
17 }
18
19 fn qualified_name(&self) -> String {
20 self.table.to_string()
21 }
22}
23
24impl VolcanoExecutor for PhysicalDropTable {
25 fn init(&self, _context: &mut ExecutionContext) -> QuillSQLResult<()> {
26 Ok(())
27 }
28
29 fn next(&self, context: &mut ExecutionContext) -> QuillSQLResult<Option<Tuple>> {
30 if context.catalog.try_table_heap(&self.table).is_none() {
31 if self.if_exists {
32 return Ok(None);
33 }
34 return Err(QuillSQLError::Execution(format!(
35 "table {} does not exist",
36 self.qualified_name()
37 )));
38 }
39
40 context
41 .txn_ctx()
42 .ensure_writable(&self.table, "DROP TABLE")?;
43 context
44 .txn_ctx_mut()
45 .lock_table(self.table.clone(), LockMode::Exclusive)?;
46
47 let dropped = context.catalog.drop_table(&self.table)?;
48 if !dropped && !self.if_exists {
49 return Err(QuillSQLError::Execution(format!(
50 "table {} does not exist",
51 self.qualified_name()
52 )));
53 }
54
55 Ok(None)
56 }
57
58 fn output_schema(&self) -> SchemaRef {
59 EMPTY_SCHEMA_REF.clone()
60 }
61}
62
63impl std::fmt::Display for PhysicalDropTable {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 write!(f, "DropTable: {}", self.table)
66 }
67}