Skip to content

Commit 394cca8

Browse files
feat: add docs 01-03 (#19)
* simplify executor for 01-03 to 01-05 Signed-off-by: Runji Wang <[email protected]> * 01-03: refine docs Signed-off-by: Runji Wang <[email protected]> Signed-off-by: Runji Wang <[email protected]>
1 parent 17bbac1 commit 394cca8

File tree

17 files changed

+353
-246
lines changed

17 files changed

+353
-246
lines changed

code/01-03/src/db.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ use std::sync::Arc;
44

55
use crate::binder::{BindError, Binder};
66
use crate::catalog::{CatalogRef, DatabaseCatalog};
7-
use crate::executor::{ExecuteError, ExecutorBuilder};
7+
use crate::executor::{ExecuteError, Executor};
88
use crate::parser::{parse, ParserError};
99

1010
/// The database instance.
1111
pub struct Database {
1212
catalog: CatalogRef,
13-
executor_builder: ExecutorBuilder,
1413
}
1514

1615
impl Default for Database {
@@ -23,24 +22,21 @@ impl Database {
2322
/// Create a new database instance.
2423
pub fn new() -> Self {
2524
let catalog = Arc::new(DatabaseCatalog::new());
26-
Database {
27-
catalog: catalog.clone(),
28-
executor_builder: ExecutorBuilder::new(catalog),
29-
}
25+
Database { catalog }
3026
}
3127

3228
/// Run SQL queries and return the outputs.
3329
pub fn run(&self, sql: &str) -> Result<Vec<String>, Error> {
3430
// parse
3531
let stmts = parse(sql)?;
3632
let mut binder = Binder::new(self.catalog.clone());
33+
let executor = Executor::new(self.catalog.clone());
3734

3835
let mut outputs = vec![];
3936
for stmt in stmts {
4037
let bound_stmt = binder.bind(&stmt)?;
4138
debug!("{:#?}", bound_stmt);
42-
let mut executor = self.executor_builder.build(bound_stmt);
43-
let output = executor.execute()?;
39+
let output = executor.execute(bound_stmt)?;
4440
outputs.push(output);
4541
}
4642
Ok(outputs)

code/01-03/src/executor/create.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
use super::*;
22
use crate::binder::BoundCreateTable;
33

4-
/// The executor of `CREATE TABLE` statement.
5-
pub struct CreateTableExecutor {
6-
pub stmt: BoundCreateTable,
7-
pub catalog: CatalogRef,
8-
}
9-
10-
impl Executor for CreateTableExecutor {
11-
fn execute(&mut self) -> Result<String, ExecuteError> {
12-
let schema = self.catalog.get_schema(self.stmt.schema_id).unwrap();
13-
let table_id = schema.add_table(&self.stmt.table_name).unwrap();
4+
impl Executor {
5+
pub fn execute_create_table(&self, stmt: BoundCreateTable) -> Result<String, ExecuteError> {
6+
let schema = self.catalog.get_schema(stmt.schema_id).unwrap();
7+
let table_id = schema.add_table(&stmt.table_name).unwrap();
148
let table = schema.get_table(table_id).unwrap();
15-
for (name, desc) in &self.stmt.columns {
9+
for (name, desc) in &stmt.columns {
1610
table.add_column(name, desc.clone()).unwrap();
1711
}
1812
Ok(String::new())

code/01-03/src/executor/mod.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,26 @@ use crate::catalog::CatalogRef;
66
mod create;
77
mod select;
88

9-
use self::create::*;
10-
use self::select::*;
11-
129
/// The error type of execution.
1310
#[derive(thiserror::Error, Debug)]
1411
pub enum ExecuteError {}
1512

16-
pub trait Executor {
17-
fn execute(&mut self) -> Result<String, ExecuteError>;
18-
}
19-
20-
/// A type-erased executor object.
21-
pub type BoxedExecutor = Box<dyn Executor>;
22-
23-
/// The builder of executor.
24-
pub struct ExecutorBuilder {
13+
/// Execute the bound AST.
14+
pub struct Executor {
2515
catalog: CatalogRef,
2616
}
2717

28-
impl ExecutorBuilder {
29-
/// Create a new executor builder.
30-
pub fn new(catalog: CatalogRef) -> ExecutorBuilder {
31-
ExecutorBuilder { catalog }
18+
impl Executor {
19+
/// Create a new executor.
20+
pub fn new(catalog: CatalogRef) -> Executor {
21+
Executor { catalog }
3222
}
3323

34-
/// Build executor from a [BoundStatement].
35-
pub fn build(&self, stmt: BoundStatement) -> BoxedExecutor {
24+
/// Execute a bound statement.
25+
pub fn execute(&self, stmt: BoundStatement) -> Result<String, ExecuteError> {
3626
match stmt {
37-
BoundStatement::CreateTable(stmt) => Box::new(CreateTableExecutor {
38-
stmt,
39-
catalog: self.catalog.clone(),
40-
}),
41-
BoundStatement::Select(stmt) => Box::new(SelectExecutor { stmt }),
27+
BoundStatement::CreateTable(stmt) => self.execute_create_table(stmt),
28+
BoundStatement::Select(stmt) => self.execute_select(stmt),
4229
}
4330
}
4431
}

code/01-03/src/executor/select.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ use super::*;
22
use crate::binder::BoundSelect;
33
use crate::parser::Value;
44

5-
/// The executor of `SELECT` statement.
6-
pub struct SelectExecutor {
7-
pub stmt: BoundSelect,
8-
}
9-
10-
impl Executor for SelectExecutor {
11-
fn execute(&mut self) -> Result<String, ExecuteError> {
5+
impl Executor {
6+
pub fn execute_select(&self, stmt: BoundSelect) -> Result<String, ExecuteError> {
127
let mut output = String::new();
13-
for v in &self.stmt.values {
8+
for v in &stmt.values {
149
output += " ";
1510
match v {
1611
Value::SingleQuotedString(s) => output += s,

code/01-04/src/db.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use std::sync::Arc;
55
use crate::array::DataChunk;
66
use crate::binder::{BindError, Binder};
77
use crate::catalog::{CatalogRef, DatabaseCatalog};
8-
use crate::executor::{ExecuteError, ExecutorBuilder};
8+
use crate::executor::{ExecuteError, Executor};
99
use crate::parser::{parse, ParserError};
10-
use crate::storage::InMemoryStorage;
10+
use crate::storage::{InMemoryStorage, StorageRef};
1111

1212
/// The database instance.
1313
pub struct Database {
1414
catalog: CatalogRef,
15-
executor_builder: ExecutorBuilder,
15+
storage: StorageRef,
1616
}
1717

1818
impl Default for Database {
@@ -26,24 +26,21 @@ impl Database {
2626
pub fn new() -> Self {
2727
let catalog = Arc::new(DatabaseCatalog::new());
2828
let storage = Arc::new(InMemoryStorage::new());
29-
Database {
30-
catalog: catalog.clone(),
31-
executor_builder: ExecutorBuilder::new(catalog, storage),
32-
}
29+
Database { catalog, storage }
3330
}
3431

3532
/// Run SQL queries and return the outputs.
3633
pub fn run(&self, sql: &str) -> Result<Vec<DataChunk>, Error> {
3734
// parse
3835
let stmts = parse(sql)?;
3936
let mut binder = Binder::new(self.catalog.clone());
37+
let executor = Executor::new(self.catalog.clone(), self.storage.clone());
4038

4139
let mut outputs = vec![];
4240
for stmt in stmts {
4341
let bound_stmt = binder.bind(&stmt)?;
4442
debug!("{:#?}", bound_stmt);
45-
let mut executor = self.executor_builder.build(bound_stmt);
46-
let output = executor.execute()?;
43+
let output = executor.execute(bound_stmt)?;
4744
outputs.push(output);
4845
}
4946
Ok(outputs)

code/01-04/src/executor/create.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
use super::*;
22
use crate::binder::BoundCreateTable;
33
use crate::catalog::TableRefId;
4-
use crate::storage::StorageRef;
54

6-
/// The executor of `CREATE TABLE` statement.
7-
pub struct CreateTableExecutor {
8-
pub stmt: BoundCreateTable,
9-
pub catalog: CatalogRef,
10-
pub storage: StorageRef,
11-
}
12-
13-
impl Executor for CreateTableExecutor {
14-
fn execute(&mut self) -> Result<DataChunk, ExecuteError> {
15-
let schema = self.catalog.get_schema(self.stmt.schema_id).unwrap();
16-
let table_id = schema.add_table(&self.stmt.table_name).unwrap();
5+
impl Executor {
6+
pub fn execute_create_table(&self, stmt: BoundCreateTable) -> Result<DataChunk, ExecuteError> {
7+
let schema = self.catalog.get_schema(stmt.schema_id).unwrap();
8+
let table_id = schema.add_table(&stmt.table_name).unwrap();
179
let table = schema.get_table(table_id).unwrap();
18-
for (name, desc) in &self.stmt.columns {
10+
for (name, desc) in &stmt.columns {
1911
table.add_column(name, desc.clone()).unwrap();
2012
}
2113
self.storage
22-
.add_table(TableRefId::new(self.stmt.schema_id, table_id))?;
14+
.add_table(TableRefId::new(stmt.schema_id, table_id))?;
2315
Ok(DataChunk::single(1))
2416
}
2517
}

code/01-04/src/executor/mod.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,30 @@ use crate::storage::{StorageError, StorageRef};
88
mod create;
99
mod select;
1010

11-
use self::create::*;
12-
use self::select::*;
13-
1411
/// The error type of execution.
1512
#[derive(thiserror::Error, Debug)]
1613
pub enum ExecuteError {
1714
#[error("storage error: {0}")]
1815
Storage(#[from] StorageError),
1916
}
2017

21-
pub trait Executor {
22-
fn execute(&mut self) -> Result<DataChunk, ExecuteError>;
23-
}
24-
25-
/// A type-erased executor object.
26-
pub type BoxedExecutor = Box<dyn Executor>;
27-
28-
/// The builder of executor.
29-
pub struct ExecutorBuilder {
18+
/// Execute the bound AST.
19+
pub struct Executor {
3020
catalog: CatalogRef,
3121
storage: StorageRef,
3222
}
3323

34-
impl ExecutorBuilder {
35-
/// Create a new executor builder.
36-
pub fn new(catalog: CatalogRef, storage: StorageRef) -> ExecutorBuilder {
37-
ExecutorBuilder { catalog, storage }
24+
impl Executor {
25+
/// Create a new executor.
26+
pub fn new(catalog: CatalogRef, storage: StorageRef) -> Executor {
27+
Executor { catalog, storage }
3828
}
3929

40-
/// Build executor from a [BoundStatement].
41-
pub fn build(&self, stmt: BoundStatement) -> BoxedExecutor {
30+
/// Execute a bound statement.
31+
pub fn execute(&self, stmt: BoundStatement) -> Result<DataChunk, ExecuteError> {
4232
match stmt {
43-
BoundStatement::CreateTable(stmt) => Box::new(CreateTableExecutor {
44-
stmt,
45-
catalog: self.catalog.clone(),
46-
storage: self.storage.clone(),
47-
}),
48-
BoundStatement::Select(stmt) => Box::new(SelectExecutor { stmt }),
33+
BoundStatement::CreateTable(stmt) => self.execute_create_table(stmt),
34+
BoundStatement::Select(stmt) => self.execute_select(stmt),
4935
}
5036
}
5137
}

code/01-04/src/executor/select.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ use super::*;
22
use crate::array::ArrayImpl;
33
use crate::binder::BoundSelect;
44

5-
/// The executor of `SELECT` statement.
6-
pub struct SelectExecutor {
7-
pub stmt: BoundSelect,
8-
}
9-
10-
impl Executor for SelectExecutor {
11-
fn execute(&mut self) -> Result<DataChunk, ExecuteError> {
12-
let chunk = self.stmt.values.iter().map(ArrayImpl::from).collect();
5+
impl Executor {
6+
pub fn execute_select(&self, stmt: BoundSelect) -> Result<DataChunk, ExecuteError> {
7+
let chunk = stmt.values.iter().map(ArrayImpl::from).collect();
138
Ok(chunk)
149
}
1510
}

code/01-05/src/db.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ use std::sync::Arc;
55
use crate::array::DataChunk;
66
use crate::binder::{BindError, Binder};
77
use crate::catalog::{CatalogRef, DatabaseCatalog};
8-
use crate::executor::{ExecuteError, ExecutorBuilder};
8+
use crate::executor::{ExecuteError, Executor};
99
use crate::parser::{parse, ParserError};
10-
use crate::storage::InMemoryStorage;
10+
use crate::storage::{InMemoryStorage, StorageRef};
1111

1212
/// The database instance.
1313
pub struct Database {
1414
catalog: CatalogRef,
15-
executor_builder: ExecutorBuilder,
15+
storage: StorageRef,
1616
}
1717

1818
impl Default for Database {
@@ -26,24 +26,21 @@ impl Database {
2626
pub fn new() -> Self {
2727
let catalog = Arc::new(DatabaseCatalog::new());
2828
let storage = Arc::new(InMemoryStorage::new());
29-
Database {
30-
catalog: catalog.clone(),
31-
executor_builder: ExecutorBuilder::new(catalog, storage),
32-
}
29+
Database { catalog, storage }
3330
}
3431

3532
/// Run SQL queries and return the outputs.
3633
pub fn run(&self, sql: &str) -> Result<Vec<DataChunk>, Error> {
3734
// parse
3835
let stmts = parse(sql)?;
3936
let mut binder = Binder::new(self.catalog.clone());
37+
let executor = Executor::new(self.catalog.clone(), self.storage.clone());
4038

4139
let mut outputs = vec![];
4240
for stmt in stmts {
4341
let bound_stmt = binder.bind(&stmt)?;
4442
debug!("{:#?}", bound_stmt);
45-
let mut executor = self.executor_builder.build(bound_stmt);
46-
let output = executor.execute()?;
43+
let output = executor.execute(bound_stmt)?;
4744
outputs.push(output);
4845
}
4946
Ok(outputs)

code/01-05/src/executor/create.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
use super::*;
22
use crate::binder::BoundCreateTable;
33
use crate::catalog::TableRefId;
4-
use crate::storage::StorageRef;
54

6-
/// The executor of `CREATE TABLE` statement.
7-
pub struct CreateTableExecutor {
8-
pub stmt: BoundCreateTable,
9-
pub catalog: CatalogRef,
10-
pub storage: StorageRef,
11-
}
12-
13-
impl Executor for CreateTableExecutor {
14-
fn execute(&mut self) -> Result<DataChunk, ExecuteError> {
15-
let schema = self.catalog.get_schema(self.stmt.schema_id).unwrap();
16-
let table_id = schema.add_table(&self.stmt.table_name).unwrap();
5+
impl Executor {
6+
pub fn execute_create_table(&self, stmt: BoundCreateTable) -> Result<DataChunk, ExecuteError> {
7+
let schema = self.catalog.get_schema(stmt.schema_id).unwrap();
8+
let table_id = schema.add_table(&stmt.table_name).unwrap();
179
let table = schema.get_table(table_id).unwrap();
18-
for (name, desc) in &self.stmt.columns {
10+
for (name, desc) in &stmt.columns {
1911
table.add_column(name, desc.clone()).unwrap();
2012
}
2113
self.storage
22-
.add_table(TableRefId::new(self.stmt.schema_id, table_id))?;
14+
.add_table(TableRefId::new(stmt.schema_id, table_id))?;
2315
Ok(DataChunk::single(1))
2416
}
2517
}

0 commit comments

Comments
 (0)