Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ let kite_sql = DataBaseBuilder::path("./data")
- [x] View
- Drop
- [x] Table
- [ ] Index
- [x] Index
- Tips: `Drop Index table_name.index_name`
- [x] View
- Alert
- [x] Add Column
Expand Down
35 changes: 35 additions & 0 deletions src/binder/drop_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::binder::{lower_ident, Binder};
use crate::errors::DatabaseError;
use crate::planner::operator::drop_index::DropIndexOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_drop_index(
&mut self,
name: &ObjectName,
if_exists: &bool,
) -> Result<LogicalPlan, DatabaseError> {
let table_name = name
.0
.first()
.ok_or(DatabaseError::InvalidTable(name.to_string()))?;
let index_name = name.0.get(1).ok_or(DatabaseError::InvalidIndex)?;

let table_name = Arc::new(lower_ident(table_name));
let index_name = lower_ident(index_name);

Ok(LogicalPlan::new(
Operator::DropIndex(DropIndexOperator {
table_name,
index_name,
if_exists: *if_exists,
}),
Childrens::None,
))
}
}
2 changes: 2 additions & 0 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod create_view;
mod delete;
mod describe;
mod distinct;
mod drop_index;
mod drop_table;
mod drop_view;
mod explain;
Expand Down Expand Up @@ -375,6 +376,7 @@ impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '
match object_type {
ObjectType::Table => self.bind_drop_table(&names[0], if_exists)?,
ObjectType::View => self.bind_drop_view(&names[0], if_exists)?,
ObjectType::Index => self.bind_drop_index(&names[0], if_exists)?,
_ => {
return Err(DatabaseError::UnsupportedStmt(
"only `Table` and `View` are allowed to be Dropped".to_string(),
Expand Down
43 changes: 43 additions & 0 deletions src/execution/ddl/drop_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::execution::{Executor, WriteExecutor};
use crate::planner::operator::drop_index::DropIndexOperator;
use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache};
use crate::throw;
use crate::types::tuple_builder::TupleBuilder;

pub struct DropIndex {
op: DropIndexOperator,
}

impl From<DropIndexOperator> for DropIndex {
fn from(op: DropIndexOperator) -> Self {
Self { op }
}
}

impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for DropIndex {
fn execute_mut(
self,
(table_cache, _, _): (&'a TableCache, &'a ViewCache, &'a StatisticsMetaCache),
transaction: *mut T,
) -> Executor<'a> {
Box::new(
#[coroutine]
move || {
let DropIndexOperator {
table_name,
index_name,
if_exists,
} = self.op;

throw!(unsafe { &mut (*transaction) }.drop_index(
table_cache,
table_name,
&index_name,
if_exists
));

yield Ok(TupleBuilder::build_result(index_name.to_string()));
},
)
}
}
1 change: 1 addition & 0 deletions src/execution/ddl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub(crate) mod create_index;
pub(crate) mod create_table;
pub(crate) mod create_view;
pub(crate) mod drop_column;
pub(crate) mod drop_index;
pub(crate) mod drop_table;
pub(crate) mod drop_view;
pub(crate) mod truncate;
15 changes: 11 additions & 4 deletions src/execution/dml/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::fmt::Formatter;
use std::fs::DirEntry;
use std::ops::Coroutine;
use std::ops::CoroutineState;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;
use std::{fmt, fs};
Expand Down Expand Up @@ -98,10 +99,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
}
drop(coroutine);
let mut values = Vec::with_capacity(builders.len());
let dir_path = dirs::home_dir()
.expect("Your system does not have a Config directory!")
.join(DEFAULT_STATISTICS_META_PATH)
.join(table_name.as_str());
let dir_path = Self::build_statistics_meta_path(&table_name);
// For DEBUG
// println!("Statistics Path: {:#?}", dir_path);
throw!(fs::create_dir_all(&dir_path).map_err(DatabaseError::IO));
Expand Down Expand Up @@ -149,6 +147,15 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for Analyze {
}
}

impl Analyze {
pub fn build_statistics_meta_path(table_name: &TableName) -> PathBuf {
dirs::home_dir()
.expect("Your system does not have a Config directory!")
.join(DEFAULT_STATISTICS_META_PATH)
.join(table_name.as_str())
}
}

impl fmt::Display for AnalyzeOperator {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let indexes = self.index_metas.iter().map(|index| &index.name).join(", ");
Expand Down
2 changes: 2 additions & 0 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::execution::ddl::create_index::CreateIndex;
use crate::execution::ddl::create_table::CreateTable;
use crate::execution::ddl::create_view::CreateView;
use crate::execution::ddl::drop_column::DropColumn;
use crate::execution::ddl::drop_index::DropIndex;
use crate::execution::ddl::drop_table::DropTable;
use crate::execution::ddl::drop_view::DropView;
use crate::execution::ddl::truncate::Truncate;
Expand Down Expand Up @@ -194,6 +195,7 @@ pub fn build_write<'a, T: Transaction + 'a>(
Operator::CreateView(op) => CreateView::from(op).execute_mut(cache, transaction),
Operator::DropTable(op) => DropTable::from(op).execute_mut(cache, transaction),
Operator::DropView(op) => DropView::from(op).execute_mut(cache, transaction),
Operator::DropIndex(op) => DropIndex::from(op).execute_mut(cache, transaction),
Operator::Truncate(op) => Truncate::from(op).execute_mut(cache, transaction),
Operator::CopyFromFile(op) => CopyFromFile::from(op).execute_mut(cache, transaction),
Operator::CopyToFile(op) => {
Expand Down
1 change: 1 addition & 0 deletions src/optimizer/rule/normalization/column_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl ColumnPruning {
| Operator::CreateView(_)
| Operator::DropTable(_)
| Operator::DropView(_)
| Operator::DropIndex(_)
| Operator::Truncate(_)
| Operator::ShowTable
| Operator::ShowView
Expand Down
2 changes: 2 additions & 0 deletions src/optimizer/rule/normalization/compilation_in_advance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl ExpressionRemapper {
| Operator::CreateView(_)
| Operator::DropTable(_)
| Operator::DropView(_)
| Operator::DropIndex(_)
| Operator::Truncate(_)
| Operator::CopyFromFile(_)
| Operator::CopyToFile(_)
Expand Down Expand Up @@ -212,6 +213,7 @@ impl EvaluatorBind {
| Operator::CreateView(_)
| Operator::DropTable(_)
| Operator::DropView(_)
| Operator::DropIndex(_)
| Operator::Truncate(_)
| Operator::CopyFromFile(_)
| Operator::CopyToFile(_)
Expand Down
3 changes: 3 additions & 0 deletions src/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ impl LogicalPlan {
Operator::DropView(_) => SchemaOutput::Schema(vec![ColumnRef::from(
ColumnCatalog::new_dummy("DROP VIEW SUCCESS".to_string()),
)]),
Operator::DropIndex(_) => SchemaOutput::Schema(vec![ColumnRef::from(
ColumnCatalog::new_dummy("DROP INDEX SUCCESS".to_string()),
)]),
Operator::Truncate(_) => SchemaOutput::Schema(vec![ColumnRef::from(
ColumnCatalog::new_dummy("TRUNCATE TABLE SUCCESS".to_string()),
)]),
Expand Down
43 changes: 43 additions & 0 deletions src/planner/operator/drop_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::catalog::TableName;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use kite_sql_serde_macros::ReferenceSerialization;
use std::fmt;
use std::fmt::Formatter;

#[derive(Debug, PartialEq, Eq, Clone, Hash, ReferenceSerialization)]
pub struct DropIndexOperator {
pub table_name: TableName,
pub index_name: String,
pub if_exists: bool,
}

impl DropIndexOperator {
pub fn build(
table_name: TableName,
index_name: String,
if_exists: bool,
childrens: Childrens,
) -> LogicalPlan {
LogicalPlan::new(
Operator::DropIndex(DropIndexOperator {
table_name,
index_name,
if_exists,
}),
childrens,
)
}
}

impl fmt::Display for DropIndexOperator {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"Drop Index {} On {}, If Exists: {}",
self.index_name, self.table_name, self.if_exists
)?;

Ok(())
}
}
6 changes: 6 additions & 0 deletions src/planner/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod create_table;
pub mod create_view;
pub mod delete;
pub mod describe;
pub mod drop_index;
pub mod drop_table;
pub mod drop_view;
pub mod filter;
Expand Down Expand Up @@ -39,6 +40,7 @@ use crate::planner::operator::create_table::CreateTableOperator;
use crate::planner::operator::create_view::CreateViewOperator;
use crate::planner::operator::delete::DeleteOperator;
use crate::planner::operator::describe::DescribeOperator;
use crate::planner::operator::drop_index::DropIndexOperator;
use crate::planner::operator::drop_table::DropTableOperator;
use crate::planner::operator::drop_view::DropViewOperator;
use crate::planner::operator::function_scan::FunctionScanOperator;
Expand Down Expand Up @@ -85,6 +87,7 @@ pub enum Operator {
CreateView(CreateViewOperator),
DropTable(DropTableOperator),
DropView(DropViewOperator),
DropIndex(DropIndexOperator),
Truncate(TruncateOperator),
// Copy
CopyFromFile(CopyFromFileOperator),
Expand Down Expand Up @@ -174,6 +177,7 @@ impl Operator {
| Operator::CreateView(_)
| Operator::DropTable(_)
| Operator::DropView(_)
| Operator::DropIndex(_)
| Operator::Truncate(_)
| Operator::CopyFromFile(_)
| Operator::CopyToFile(_) => None,
Expand Down Expand Up @@ -248,6 +252,7 @@ impl Operator {
| Operator::CreateView(_)
| Operator::DropTable(_)
| Operator::DropView(_)
| Operator::DropIndex(_)
| Operator::Truncate(_)
| Operator::CopyFromFile(_)
| Operator::CopyToFile(_) => vec![],
Expand Down Expand Up @@ -283,6 +288,7 @@ impl fmt::Display for Operator {
Operator::CreateView(op) => write!(f, "{}", op),
Operator::DropTable(op) => write!(f, "{}", op),
Operator::DropView(op) => write!(f, "{}", op),
Operator::DropIndex(op) => write!(f, "{}", op),
Operator::Truncate(op) => write!(f, "{}", op),
Operator::CopyFromFile(op) => write!(f, "{}", op),
Operator::CopyToFile(op) => write!(f, "{}", op),
Expand Down
Loading
Loading