Skip to content

Commit

Permalink
chore: replace table creator/dropper with Manipulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiKaiWi committed Oct 14, 2022
1 parent b6ff10d commit 4f543f9
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 319 deletions.
41 changes: 9 additions & 32 deletions interpreters/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,21 @@
//! Interpreter for create statements

use async_trait::async_trait;
use snafu::{Backtrace, ResultExt, Snafu};
use snafu::{ResultExt, Snafu};
use sql::plan::CreateTablePlan;
use table_engine::engine::TableEngineRef;

use crate::{
context::Context,
interpreter::{Create, Interpreter, InterpreterPtr, Output, Result as InterpreterResult},
table_creator::TableCreatorRef,
table_manipulator::{self, TableManipulatorRef},
};

#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum Error {
#[snafu(display("Failed to find catalog, name:{}, err:{}", name, source))]
FindCatalog {
name: String,
source: catalog::manager::Error,
},

#[snafu(display("Catalog not exists, name:{}.\nBacktrace:\n{}", name, backtrace))]
CatalogNotExists { name: String, backtrace: Backtrace },

#[snafu(display("Failed to find schema, name:{}, err:{}", name, source))]
FindSchema {
name: String,
source: catalog::Error,
},

#[snafu(display("Schema not exists, name:{}.\nBacktrace:\n{}", name, backtrace))]
SchemaNotExists { name: String, backtrace: Backtrace },

#[snafu(display("Failed to create table, name:{}, err:{}", table, source))]
SchemaCreateTable {
table: String,
source: catalog::schema::Error,
},

#[snafu(display("Failed to allocate table id, err:{}", source))]
AllocTableId { source: catalog::schema::Error },
#[snafu(display("Failed to create table by table manipulator, err:{}", source))]
ManipulateTable { source: table_manipulator::Error },
}

define_result!(Error);
Expand All @@ -51,30 +27,31 @@ pub struct CreateInterpreter {
ctx: Context,
plan: CreateTablePlan,
table_engine: TableEngineRef,
table_creator: TableCreatorRef,
table_manipulator: TableManipulatorRef,
}

impl CreateInterpreter {
pub fn create(
ctx: Context,
plan: CreateTablePlan,
table_engine: TableEngineRef,
table_creator: TableCreatorRef,
table_manipulator: TableManipulatorRef,
) -> InterpreterPtr {
Box::new(Self {
ctx,
plan,
table_engine,
table_creator,
table_manipulator,
})
}
}

impl CreateInterpreter {
async fn execute_create(self: Box<Self>) -> Result<Output> {
self.table_creator
self.table_manipulator
.create_table(self.ctx, self.plan, self.table_engine)
.await
.context(ManipulateTable)
}
}

Expand Down
44 changes: 9 additions & 35 deletions interpreters/src/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,21 @@
//! Interpreter for drop statements

use async_trait::async_trait;
use snafu::{Backtrace, ResultExt, Snafu};
use snafu::{ResultExt, Snafu};
use sql::plan::DropTablePlan;
use table_engine::engine::TableEngineRef;

use crate::{
context::Context,
interpreter::{Drop, Interpreter, InterpreterPtr, Output, Result as InterpreterResult},
table_dropper::TableDropperRef,
table_manipulator::{self, TableManipulatorRef},
};

#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum Error {
#[snafu(display("Failed to find catalog, name:{}, err:{}", name, source))]
FindCatalog {
name: String,
source: catalog::manager::Error,
},

#[snafu(display("Catalog not exists, name:{}.\nBacktrace:\n{}", name, backtrace))]
CatalogNotExists { name: String, backtrace: Backtrace },

#[snafu(display("Failed to find schema, name:{}, err:{}", name, source))]
FindSchema {
name: String,
source: catalog::Error,
},

#[snafu(display("Schema not exists, name:{}.\nBacktrace:\n{}", name, backtrace))]
SchemaNotExists { name: String, backtrace: Backtrace },

#[snafu(display("Failed to drop table in schema, name:{}, err:{}", table, source))]
SchemaDropTable {
table: String,
source: catalog::schema::Error,
},

#[snafu(display("Failed to drop table, name:{}, err:{}", table, source))]
DropTable {
table: String,
source: table_engine::engine::Error,
},
#[snafu(display("Failed to drop table by table manipulator, err:{}", source))]
ManipulateTable { source: table_manipulator::Error },
}

define_result!(Error);
Expand All @@ -54,30 +27,31 @@ pub struct DropInterpreter {
ctx: Context,
plan: DropTablePlan,
table_engine: TableEngineRef,
table_dropper: TableDropperRef,
table_manipulator: TableManipulatorRef,
}

impl DropInterpreter {
pub fn create(
ctx: Context,
plan: DropTablePlan,
table_engine: TableEngineRef,
table_dropper: TableDropperRef,
table_manipulator: TableManipulatorRef,
) -> InterpreterPtr {
Box::new(Self {
ctx,
plan,
table_engine,
table_dropper,
table_manipulator,
})
}
}

impl DropInterpreter {
async fn execute_drop(self: Box<Self>) -> Result<Output> {
self.table_dropper
self.table_manipulator
.drop_table(self.ctx, self.plan, self.table_engine)
.await
.context(ManipulateTable)
}
}

Expand Down
17 changes: 8 additions & 9 deletions interpreters/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,29 @@ use crate::{
alter_table::AlterTableInterpreter, context::Context, create::CreateInterpreter,
describe::DescribeInterpreter, drop::DropInterpreter, exists::ExistsInterpreter,
insert::InsertInterpreter, interpreter::InterpreterPtr, select::SelectInterpreter,
show::ShowInterpreter, table_creator::TableCreatorRef, table_dropper::TableDropperRef,
show::ShowInterpreter, table_manipulator::TableManipulatorRef,
};

/// A factory to create interpreters
pub struct Factory<Q> {
query_executor: Q,
catalog_manager: ManagerRef,
table_engine: TableEngineRef,
table_creator: TableCreatorRef,
table_dropper: TableDropperRef,
table_manipulator: TableManipulatorRef,
}

impl<Q: Executor + 'static> Factory<Q> {
pub fn new(
query_executor: Q,
catalog_manager: ManagerRef,
table_engine: TableEngineRef,
table_creator: TableCreatorRef,
table_dropper: TableDropperRef,
table_manipulator: TableManipulatorRef,
) -> Self {
Self {
query_executor,
catalog_manager,
table_engine,
table_creator,
table_dropper,
table_manipulator,
}
}

Expand All @@ -45,9 +42,11 @@ impl<Q: Executor + 'static> Factory<Q> {
Plan::Query(p) => SelectInterpreter::create(ctx, p, self.query_executor),
Plan::Insert(p) => InsertInterpreter::create(ctx, p),
Plan::Create(p) => {
CreateInterpreter::create(ctx, p, self.table_engine, self.table_creator)
CreateInterpreter::create(ctx, p, self.table_engine, self.table_manipulator)
}
Plan::Drop(p) => {
DropInterpreter::create(ctx, p, self.table_engine, self.table_manipulator)
}
Plan::Drop(p) => DropInterpreter::create(ctx, p, self.table_engine, self.table_dropper),
Plan::Describe(p) => DescribeInterpreter::create(p),
Plan::AlterTable(p) => AlterTableInterpreter::create(p),
Plan::Show(p) => ShowInterpreter::create(ctx, p, self.catalog_manager),
Expand Down
3 changes: 1 addition & 2 deletions interpreters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub mod insert;
pub mod interpreter;
pub mod select;
pub mod show;
pub mod table_creator;
pub mod table_dropper;
pub mod table_manipulator;

mod show_create;

Expand Down
45 changes: 15 additions & 30 deletions interpreters/src/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use sql::{

use crate::{
context::Context,
create::{CatalogNotExists, FindCatalog, FindSchema, SchemaNotExists},
interpreter::{
Interpreter, InterpreterPtr, Output, Result as InterpreterResult, ShowCreateTable,
ShowDatabases, ShowTables,
Expand All @@ -32,7 +31,7 @@ const SHOW_DATABASES_COLUMN_SCHEMA: &str = "Schemas";
#[snafu(visibility(pub(crate)))]
pub enum Error {
#[snafu(display(
"Unsupported show create type, type: {:?}, err:{}",
"Unsupported show create type, type:{:?}.\nBacktrace:{}",
obj_type,
backtrace
))]
Expand All @@ -58,29 +57,21 @@ pub enum Error {
#[snafu(display("Failed to fetch databases, err:{}", source))]
FetchDatabases { source: catalog::Error },

#[snafu(display("Catalog does not exist, catalog:{}.\nBacktrace\n:{}", name, backtrace))]
CatalogNotExists { name: String, backtrace: Backtrace },

#[snafu(display("Schema does not exist, schema:{}.\nBacktrace\n:{}", name, backtrace))]
SchemaNotExists { name: String, backtrace: Backtrace },

#[snafu(display("Failed to fetch catalog, err:{}", source))]
FetchCatalog { source: crate::create::Error },
FetchCatalog { source: catalog::manager::Error },

#[snafu(display("Failed to fetch schema, err:{}", source))]
FetchSchema { source: crate::create::Error },

#[snafu(display("From create::Error, err:{}", source))]
FromCreateError { source: crate::create::Error },
FetchSchema { source: catalog::Error },
}

define_result!(Error);

impl From<crate::create::Error> for Error {
fn from(error: crate::create::Error) -> Self {
use crate::create::Error::*;
match error {
FindCatalog { .. } | CatalogNotExists { .. } => Error::FetchCatalog { source: error },
FindSchema { .. } | SchemaNotExists { .. } => Error::FetchSchema { source: error },
other => Error::FromCreateError { source: other },
}
}
}

pub struct ShowInterpreter {
ctx: Context,
plan: ShowPlan,
Expand Down Expand Up @@ -175,15 +166,12 @@ fn get_default_catalog(
catalog_manager: &ManagerRef,
) -> Result<Arc<dyn Catalog + Send + Sync>> {
let default_catalog = ctx.default_catalog();
let catalog = catalog_manager
catalog_manager
.catalog_by_name(default_catalog)
.context(FindCatalog {
name: default_catalog,
})?
.context(FetchCatalog)?
.context(CatalogNotExists {
name: default_catalog,
})?;
Ok(catalog)
})
}

fn get_default_schema(
Expand All @@ -193,13 +181,10 @@ fn get_default_schema(
let catalog = get_default_catalog(ctx, catalog_manager)?;

let default_schema = ctx.default_schema();
let schema = catalog
catalog
.schema_by_name(default_schema)
.context(FindSchema {
name: default_schema,
})?
.context(FetchSchema)?
.context(SchemaNotExists {
name: default_schema,
})?;
Ok(schema)
})
}
21 changes: 0 additions & 21 deletions interpreters/src/table_creator/meta_based.rs

This file was deleted.

24 changes: 0 additions & 24 deletions interpreters/src/table_creator/mod.rs

This file was deleted.

Loading

0 comments on commit 4f543f9

Please sign in to comment.