Skip to content

Commit e071416

Browse files
fix: make all tasks pass test 01-01 (#7)
* update rust toolchain to nightly-2022-01-20 Signed-off-by: Runji Wang <[email protected]> * 01-03: pass 01-01 test Signed-off-by: Runji Wang <[email protected]> * 01-04: pass 01-01 test Signed-off-by: Runji Wang <[email protected]> * 01-05: pass 01-01 test Signed-off-by: Runji Wang <[email protected]> * 01-06: pass 01-01 test Signed-off-by: Runji Wang <[email protected]> * fix cargo clippy Signed-off-by: Runji Wang <[email protected]>
1 parent d804c71 commit e071416

File tree

29 files changed

+288
-16
lines changed

29 files changed

+288
-16
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use self::statement::*;
1313
#[derive(Debug, PartialEq, Clone)]
1414
pub enum BoundStatement {
1515
CreateTable(BoundCreateTable),
16+
Select(BoundSelect),
1617
}
1718

1819
/// The error type of bind operations.
@@ -46,10 +47,10 @@ impl Binder {
4647

4748
/// Bind a statement.
4849
pub fn bind(&mut self, stmt: &Statement) -> Result<BoundStatement, BindError> {
50+
use Statement::*;
4951
match stmt {
50-
Statement::CreateTable { .. } => {
51-
Ok(BoundStatement::CreateTable(self.bind_create_table(stmt)?))
52-
}
52+
CreateTable { .. } => Ok(BoundStatement::CreateTable(self.bind_create_table(stmt)?)),
53+
Query(query) => Ok(BoundStatement::Select(self.bind_select(query)?)),
5354
_ => todo!("bind statement: {:#?}", stmt),
5455
}
5556
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::*;
22

33
mod create_table;
4+
mod select;
45

56
pub use self::create_table::*;
7+
pub use self::select::*;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use super::*;
2+
use crate::parser::{Expr, Query, SelectItem, SetExpr, Value};
3+
4+
/// A bound `SELECT` statement.
5+
#[derive(Debug, PartialEq, Clone)]
6+
pub struct BoundSelect {
7+
pub values: Vec<Value>,
8+
}
9+
10+
impl Binder {
11+
pub fn bind_select(&mut self, query: &Query) -> Result<BoundSelect, BindError> {
12+
match &query.body {
13+
SetExpr::Select(select) => {
14+
let mut values = vec![];
15+
for item in &select.projection {
16+
match item {
17+
SelectItem::UnnamedExpr(Expr::Value(v)) => values.push(v.clone()),
18+
_ => todo!("not supported statement: {:#?}", query),
19+
}
20+
}
21+
Ok(BoundSelect { values })
22+
}
23+
_ => todo!("not supported statement: {:#?}", query),
24+
}
25+
}
26+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use crate::binder::BoundStatement;
44
use crate::catalog::CatalogRef;
55

66
mod create;
7+
mod select;
78

89
use self::create::*;
10+
use self::select::*;
911

1012
/// The error type of execution.
1113
#[derive(thiserror::Error, Debug)]
@@ -36,6 +38,7 @@ impl ExecutorBuilder {
3638
stmt,
3739
catalog: self.catalog.clone(),
3840
}),
41+
BoundStatement::Select(stmt) => Box::new(SelectExecutor { stmt }),
3942
}
4043
}
4144
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use super::*;
2+
use crate::binder::BoundSelect;
3+
use crate::parser::Value;
4+
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> {
12+
let mut output = String::new();
13+
for v in &self.stmt.values {
14+
output += " ";
15+
match v {
16+
Value::SingleQuotedString(s) => output += s,
17+
Value::Number(s, _) => output += s,
18+
_ => todo!("not supported value: {:#?}", v),
19+
}
20+
}
21+
Ok(output.trim().to_string())
22+
}
23+
}

code/01-03/src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use test_case::test_case;
44

55
use crate::{Database, Error};
66

7+
#[test_case("01-01.slt")]
78
#[test_case("01-03.slt")]
89
fn test(name: &str) {
910
init_logger();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,16 @@ impl ArrayImpl {
232232
self.len() == 0
233233
}
234234
}
235+
236+
/// Create a single element array from data value.
237+
impl From<&DataValue> for ArrayImpl {
238+
fn from(val: &DataValue) -> Self {
239+
match val {
240+
DataValue::Null => Self::Int32([None].into_iter().collect()),
241+
&DataValue::Bool(v) => Self::Bool([v].into_iter().collect()),
242+
&DataValue::Int32(v) => Self::Int32([v].into_iter().collect()),
243+
&DataValue::Float64(v) => Self::Float64([v].into_iter().collect()),
244+
DataValue::String(v) => Self::Utf8([Some(v)].into_iter().collect()),
245+
}
246+
}
247+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use self::statement::*;
1313
#[derive(Debug, PartialEq, Clone)]
1414
pub enum BoundStatement {
1515
CreateTable(BoundCreateTable),
16+
Select(BoundSelect),
1617
}
1718

1819
/// The error type of bind operations.
@@ -46,10 +47,10 @@ impl Binder {
4647

4748
/// Bind a statement.
4849
pub fn bind(&mut self, stmt: &Statement) -> Result<BoundStatement, BindError> {
50+
use Statement::*;
4951
match stmt {
50-
Statement::CreateTable { .. } => {
51-
Ok(BoundStatement::CreateTable(self.bind_create_table(stmt)?))
52-
}
52+
CreateTable { .. } => Ok(BoundStatement::CreateTable(self.bind_create_table(stmt)?)),
53+
Query(query) => Ok(BoundStatement::Select(self.bind_select(query)?)),
5354
_ => todo!("bind statement: {:#?}", stmt),
5455
}
5556
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::*;
22

33
mod create_table;
4+
mod select;
45

56
pub use self::create_table::*;
7+
pub use self::select::*;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use super::*;
2+
use crate::parser::{Expr, Query, SelectItem, SetExpr};
3+
use crate::types::DataValue;
4+
5+
/// A bound `SELECT` statement.
6+
#[derive(Debug, PartialEq, Clone)]
7+
pub struct BoundSelect {
8+
pub values: Vec<DataValue>,
9+
}
10+
11+
impl Binder {
12+
pub fn bind_select(&mut self, query: &Query) -> Result<BoundSelect, BindError> {
13+
match &query.body {
14+
SetExpr::Select(select) => {
15+
let mut values = vec![];
16+
for item in &select.projection {
17+
match item {
18+
SelectItem::UnnamedExpr(Expr::Value(v)) => values.push(v.into()),
19+
_ => todo!("not supported statement: {:#?}", query),
20+
}
21+
}
22+
Ok(BoundSelect { values })
23+
}
24+
_ => todo!("not supported statement: {:#?}", query),
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)