Skip to content

Commit 77d71e0

Browse files
committed
feat: impl Drop Index
1 parent 524f58b commit 77d71e0

File tree

15 files changed

+259
-20
lines changed

15 files changed

+259
-20
lines changed

docs/features.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ let kite_sql = DataBaseBuilder::path("./data")
105105
- [x] View
106106
- Drop
107107
- [x] Table
108-
- [ ] Index
108+
- [x] Index
109+
- Tips: `Drop Index table_name.index_name`
109110
- [x] View
110111
- Alert
111112
- [x] Add Column

src/binder/drop_index.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::binder::{lower_ident, Binder};
2+
use crate::errors::DatabaseError;
3+
use crate::planner::operator::drop_index::DropIndexOperator;
4+
use crate::planner::operator::Operator;
5+
use crate::planner::{Childrens, LogicalPlan};
6+
use crate::storage::Transaction;
7+
use crate::types::value::DataValue;
8+
use sqlparser::ast::ObjectName;
9+
use std::sync::Arc;
10+
11+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
12+
pub(crate) fn bind_drop_index(
13+
&mut self,
14+
name: &ObjectName,
15+
if_exists: &bool,
16+
) -> Result<LogicalPlan, DatabaseError> {
17+
let table_name = name.0.first().ok_or(DatabaseError::InvalidIndex)?;
18+
let index_name = name.0.get(1).ok_or(DatabaseError::InvalidIndex)?;
19+
20+
let table_name = Arc::new(lower_ident(table_name));
21+
let index_name = lower_ident(index_name);
22+
23+
Ok(LogicalPlan::new(
24+
Operator::DropIndex(DropIndexOperator {
25+
table_name,
26+
index_name,
27+
if_exists: *if_exists,
28+
}),
29+
Childrens::None,
30+
))
31+
}
32+
}

src/binder/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod create_view;
88
mod delete;
99
mod describe;
1010
mod distinct;
11+
mod drop_index;
1112
mod drop_table;
1213
mod drop_view;
1314
mod explain;
@@ -375,6 +376,7 @@ impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '
375376
match object_type {
376377
ObjectType::Table => self.bind_drop_table(&names[0], if_exists)?,
377378
ObjectType::View => self.bind_drop_view(&names[0], if_exists)?,
379+
ObjectType::Index => self.bind_drop_index(&names[0], if_exists)?,
378380
_ => {
379381
return Err(DatabaseError::UnsupportedStmt(
380382
"only `Table` and `View` are allowed to be Dropped".to_string(),

src/execution/ddl/drop_index.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::execution::{Executor, WriteExecutor};
2+
use crate::planner::operator::drop_index::DropIndexOperator;
3+
use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache};
4+
use crate::throw;
5+
use crate::types::tuple_builder::TupleBuilder;
6+
7+
pub struct DropIndex {
8+
op: DropIndexOperator,
9+
}
10+
11+
impl From<DropIndexOperator> for DropIndex {
12+
fn from(op: DropIndexOperator) -> Self {
13+
Self { op }
14+
}
15+
}
16+
17+
impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for DropIndex {
18+
fn execute_mut(
19+
self,
20+
(table_cache, _, _): (&'a TableCache, &'a ViewCache, &'a StatisticsMetaCache),
21+
transaction: *mut T,
22+
) -> Executor<'a> {
23+
Box::new(
24+
#[coroutine]
25+
move || {
26+
let DropIndexOperator {
27+
table_name,
28+
index_name,
29+
if_exists,
30+
} = self.op;
31+
32+
throw!(unsafe { &mut (*transaction) }.drop_index(
33+
table_cache,
34+
table_name,
35+
&index_name,
36+
if_exists
37+
));
38+
39+
yield Ok(TupleBuilder::build_result(index_name.to_string()));
40+
},
41+
)
42+
}
43+
}

src/execution/ddl/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub(crate) mod create_index;
33
pub(crate) mod create_table;
44
pub(crate) mod create_view;
55
pub(crate) mod drop_column;
6+
pub(crate) mod drop_index;
67
pub(crate) mod drop_table;
78
pub(crate) mod drop_view;
89
pub(crate) mod truncate;

src/execution/dml/analyze.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::fmt::Formatter;
1919
use std::fs::DirEntry;
2020
use std::ops::Coroutine;
2121
use std::ops::CoroutineState;
22+
use std::path::PathBuf;
2223
use std::pin::Pin;
2324
use std::sync::Arc;
2425
use std::{fmt, fs};
@@ -98,10 +99,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
9899
}
99100
drop(coroutine);
100101
let mut values = Vec::with_capacity(builders.len());
101-
let dir_path = dirs::home_dir()
102-
.expect("Your system does not have a Config directory!")
103-
.join(DEFAULT_STATISTICS_META_PATH)
104-
.join(table_name.as_str());
102+
let dir_path = Self::build_statistics_meta_path(&table_name);
105103
// For DEBUG
106104
// println!("Statistics Path: {:#?}", dir_path);
107105
throw!(fs::create_dir_all(&dir_path).map_err(DatabaseError::IO));
@@ -149,6 +147,15 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
149147
}
150148
}
151149

150+
impl Analyze {
151+
pub fn build_statistics_meta_path(table_name: &TableName) -> PathBuf {
152+
dirs::home_dir()
153+
.expect("Your system does not have a Config directory!")
154+
.join(DEFAULT_STATISTICS_META_PATH)
155+
.join(table_name.as_str())
156+
}
157+
}
158+
152159
impl fmt::Display for AnalyzeOperator {
153160
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
154161
let indexes = self.index_metas.iter().map(|index| &index.name).join(", ");

src/execution/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::execution::ddl::create_index::CreateIndex;
1010
use crate::execution::ddl::create_table::CreateTable;
1111
use crate::execution::ddl::create_view::CreateView;
1212
use crate::execution::ddl::drop_column::DropColumn;
13+
use crate::execution::ddl::drop_index::DropIndex;
1314
use crate::execution::ddl::drop_table::DropTable;
1415
use crate::execution::ddl::drop_view::DropView;
1516
use crate::execution::ddl::truncate::Truncate;
@@ -194,6 +195,7 @@ pub fn build_write<'a, T: Transaction + 'a>(
194195
Operator::CreateView(op) => CreateView::from(op).execute_mut(cache, transaction),
195196
Operator::DropTable(op) => DropTable::from(op).execute_mut(cache, transaction),
196197
Operator::DropView(op) => DropView::from(op).execute_mut(cache, transaction),
198+
Operator::DropIndex(op) => DropIndex::from(op).execute_mut(cache, transaction),
197199
Operator::Truncate(op) => Truncate::from(op).execute_mut(cache, transaction),
198200
Operator::CopyFromFile(op) => CopyFromFile::from(op).execute_mut(cache, transaction),
199201
Operator::CopyToFile(op) => {

src/optimizer/rule/normalization/column_pruning.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl ColumnPruning {
152152
| Operator::CreateView(_)
153153
| Operator::DropTable(_)
154154
| Operator::DropView(_)
155+
| Operator::DropIndex(_)
155156
| Operator::Truncate(_)
156157
| Operator::ShowTable
157158
| Operator::ShowView

src/optimizer/rule/normalization/compilation_in_advance.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl ExpressionRemapper {
104104
| Operator::CreateView(_)
105105
| Operator::DropTable(_)
106106
| Operator::DropView(_)
107+
| Operator::DropIndex(_)
107108
| Operator::Truncate(_)
108109
| Operator::CopyFromFile(_)
109110
| Operator::CopyToFile(_)
@@ -212,6 +213,7 @@ impl EvaluatorBind {
212213
| Operator::CreateView(_)
213214
| Operator::DropTable(_)
214215
| Operator::DropView(_)
216+
| Operator::DropIndex(_)
215217
| Operator::Truncate(_)
216218
| Operator::CopyFromFile(_)
217219
| Operator::CopyToFile(_)

src/planner/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ impl LogicalPlan {
221221
Operator::DropView(_) => SchemaOutput::Schema(vec![ColumnRef::from(
222222
ColumnCatalog::new_dummy("DROP VIEW SUCCESS".to_string()),
223223
)]),
224+
Operator::DropIndex(_) => SchemaOutput::Schema(vec![ColumnRef::from(
225+
ColumnCatalog::new_dummy("DROP INDEX SUCCESS".to_string()),
226+
)]),
224227
Operator::Truncate(_) => SchemaOutput::Schema(vec![ColumnRef::from(
225228
ColumnCatalog::new_dummy("TRUNCATE TABLE SUCCESS".to_string()),
226229
)]),

0 commit comments

Comments
 (0)