Skip to content

Commit 4bf7385

Browse files
authored
fmt & update readme & update .gitignore (#76)
perfect
1 parent 8bc563a commit 4bf7385

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+3551
-2452
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ Cargo.lock
1818
/.vscode
1919
/.idea
2020
/.obsidian
21+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ implement_from_tuple!(Post, (
146146

147147
### Thanks For
148148
- [Fedomn/sqlrs](https://github.com/Fedomn/sqlrs): 主要参考资料,Optimizer、Executor均参考自sqlrs的设计
149-
- [systemxlabs/tinysql](https://github.com/systemxlabs/tinysql)
149+
- [systemxlabs/bustubx](https://github.com/systemxlabs/bustubx)

src/binder/aggregate.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
use std::collections::HashSet;
21
use ahash::RandomState;
32
use itertools::Itertools;
43
use sqlparser::ast::{Expr, OrderByExpr};
4+
use std::collections::HashSet;
55

6-
use crate::{
7-
expression::ScalarExpression,
8-
planner::{
9-
operator::{aggregate::AggregateOperator, sort::SortField},
10-
},
11-
};
126
use crate::binder::{BindError, InputRefType};
137
use crate::planner::LogicalPlan;
148
use crate::storage::Storage;
9+
use crate::{
10+
expression::ScalarExpression,
11+
planner::operator::{aggregate::AggregateOperator, sort::SortField},
12+
};
1513

1614
use super::Binder;
1715

@@ -40,7 +38,8 @@ impl<S: Storage> Binder<S> {
4038
select_list: &mut [ScalarExpression],
4139
groupby: &[Expr],
4240
) -> Result<(), BindError> {
43-
self.validate_groupby_illegal_column(select_list, groupby).await?;
41+
self.validate_groupby_illegal_column(select_list, groupby)
42+
.await?;
4443

4544
for gb in groupby {
4645
let mut expr = self.bind_expr(gb).await?;
@@ -89,24 +88,25 @@ impl<S: Storage> Binder<S> {
8988
Ok((return_having, return_orderby))
9089
}
9190

92-
fn visit_column_agg_expr(&mut self, expr: &mut ScalarExpression, is_select: bool) -> Result<(), BindError> {
91+
fn visit_column_agg_expr(
92+
&mut self,
93+
expr: &mut ScalarExpression,
94+
is_select: bool,
95+
) -> Result<(), BindError> {
9396
match expr {
9497
ScalarExpression::AggCall {
9598
ty: return_type, ..
9699
} => {
97100
let ty = return_type.clone();
98101
if is_select {
99102
let index = self.context.input_ref_index(InputRefType::AggCall);
100-
let input_ref = ScalarExpression::InputRef {
101-
index,
102-
ty,
103-
};
103+
let input_ref = ScalarExpression::InputRef { index, ty };
104104
match std::mem::replace(expr, input_ref) {
105105
ScalarExpression::AggCall {
106106
kind,
107107
args,
108108
ty,
109-
distinct
109+
distinct,
110110
} => {
111111
self.context.agg_calls.push(ScalarExpression::AggCall {
112112
distinct,
@@ -125,14 +125,13 @@ impl<S: Storage> Binder<S> {
125125
.find_position(|agg_expr| agg_expr == &expr)
126126
.ok_or_else(|| BindError::AggMiss(format!("{:?}", expr)))?;
127127

128-
let _ = std::mem::replace(expr, ScalarExpression::InputRef {
129-
index,
130-
ty,
131-
});
128+
let _ = std::mem::replace(expr, ScalarExpression::InputRef { index, ty });
132129
}
133130
}
134131

135-
ScalarExpression::TypeCast { expr, .. } => self.visit_column_agg_expr(expr, is_select)?,
132+
ScalarExpression::TypeCast { expr, .. } => {
133+
self.visit_column_agg_expr(expr, is_select)?
134+
}
136135
ScalarExpression::IsNull { expr } => self.visit_column_agg_expr(expr, is_select)?,
137136
ScalarExpression::Unary { expr, .. } => self.visit_column_agg_expr(expr, is_select)?,
138137
ScalarExpression::Alias { expr, .. } => self.visit_column_agg_expr(expr, is_select)?,
@@ -185,7 +184,8 @@ impl<S: Storage> Binder<S> {
185184
group_raw_exprs.push(expr);
186185
}
187186
}
188-
let mut group_raw_set: HashSet<&ScalarExpression, RandomState> = HashSet::from_iter(group_raw_exprs.iter());
187+
let mut group_raw_set: HashSet<&ScalarExpression, RandomState> =
188+
HashSet::from_iter(group_raw_exprs.iter());
189189

190190
for expr in select_items {
191191
if expr.has_agg_call(&self.context) {
@@ -195,19 +195,17 @@ impl<S: Storage> Binder<S> {
195195
group_raw_set.remove(expr);
196196

197197
if !group_raw_exprs.iter().contains(expr) {
198-
return Err(BindError::AggMiss(
199-
format!(
200-
"{:?} must appear in the GROUP BY clause or be used in an aggregate function",
201-
expr
202-
)
203-
));
198+
return Err(BindError::AggMiss(format!(
199+
"{:?} must appear in the GROUP BY clause or be used in an aggregate function",
200+
expr
201+
)));
204202
}
205203
}
206204

207205
if !group_raw_set.is_empty() {
208-
return Err(BindError::AggMiss(
209-
format!("In the GROUP BY clause the field must be in the select clause")
210-
));
206+
return Err(BindError::AggMiss(format!(
207+
"In the GROUP BY clause the field must be in the select clause"
208+
)));
211209
}
212210

213211
Ok(())

src/binder/create_table.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use std::collections::HashSet;
2-
use std::sync::Arc;
31
use itertools::Itertools;
42
use sqlparser::ast::{ColumnDef, ObjectName, TableConstraint};
3+
use std::collections::HashSet;
4+
use std::sync::Arc;
55

66
use super::Binder;
7-
use crate::binder::{BindError, lower_case_name, split_name};
7+
use crate::binder::{lower_case_name, split_name, BindError};
88
use crate::catalog::ColumnCatalog;
9-
use crate::planner::LogicalPlan;
109
use crate::planner::operator::create_table::CreateTableOperator;
1110
use crate::planner::operator::Operator;
11+
use crate::planner::LogicalPlan;
1212
use crate::storage::Storage;
1313

1414
impl<S: Storage> Binder<S> {
@@ -17,7 +17,7 @@ impl<S: Storage> Binder<S> {
1717
&mut self,
1818
name: &ObjectName,
1919
columns: &[ColumnDef],
20-
constraints: &[TableConstraint]
20+
constraints: &[TableConstraint],
2121
) -> Result<LogicalPlan, BindError> {
2222
let name = lower_case_name(&name);
2323
let (_, name) = split_name(&name)?;
@@ -36,24 +36,19 @@ impl<S: Storage> Binder<S> {
3636
.map(|col| ColumnCatalog::from(col.clone()))
3737
.collect_vec();
3838

39-
let primary_key_count = columns
40-
.iter()
41-
.filter(|col| col.desc.is_primary)
42-
.count();
39+
let primary_key_count = columns.iter().filter(|col| col.desc.is_primary).count();
4340

4441
if primary_key_count != 1 {
4542
return Err(BindError::InvalidTable(
46-
"The primary key field must exist and have at least one".to_string()
43+
"The primary key field must exist and have at least one".to_string(),
4744
));
4845
}
4946

5047
let plan = LogicalPlan {
51-
operator: Operator::CreateTable(
52-
CreateTableOperator {
53-
table_name,
54-
columns
55-
}
56-
),
48+
operator: Operator::CreateTable(CreateTableOperator {
49+
table_name,
50+
columns,
51+
}),
5752
childrens: vec![],
5853
};
5954
Ok(plan)
@@ -62,12 +57,12 @@ impl<S: Storage> Binder<S> {
6257

6358
#[cfg(test)]
6459
mod tests {
65-
use tempfile::TempDir;
6660
use super::*;
6761
use crate::binder::BinderContext;
6862
use crate::catalog::ColumnDesc;
6963
use crate::storage::kip::KipStorage;
7064
use crate::types::LogicalType;
65+
use tempfile::TempDir;
7166

7267
#[tokio::test]
7368
async fn test_create_bind() {
@@ -84,13 +79,18 @@ mod tests {
8479
assert_eq!(op.table_name, Arc::new("t1".to_string()));
8580
assert_eq!(op.columns[0].name, "id".to_string());
8681
assert_eq!(op.columns[0].nullable, false);
87-
assert_eq!(op.columns[0].desc, ColumnDesc::new(LogicalType::Integer, true, false));
82+
assert_eq!(
83+
op.columns[0].desc,
84+
ColumnDesc::new(LogicalType::Integer, true, false)
85+
);
8886
assert_eq!(op.columns[1].name, "name".to_string());
8987
assert_eq!(op.columns[1].nullable, true);
90-
assert_eq!(op.columns[1].desc, ColumnDesc::new(LogicalType::Varchar(Some(10)), false, false));
88+
assert_eq!(
89+
op.columns[1].desc,
90+
ColumnDesc::new(LogicalType::Varchar(Some(10)), false, false)
91+
);
9192
}
92-
_ => unreachable!()
93+
_ => unreachable!(),
9394
}
94-
9595
}
9696
}

src/binder/delete.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use sqlparser::ast::{Expr, TableFactor, TableWithJoins};
2-
use crate::binder::{Binder, BindError, lower_case_name, split_name};
3-
use crate::planner::LogicalPlan;
1+
use crate::binder::{lower_case_name, split_name, BindError, Binder};
42
use crate::planner::operator::delete::DeleteOperator;
53
use crate::planner::operator::Operator;
4+
use crate::planner::LogicalPlan;
65
use crate::storage::Storage;
6+
use sqlparser::ast::{Expr, TableFactor, TableWithJoins};
77

88
impl<S: Storage> Binder<S> {
99
pub(crate) async fn bind_delete(
@@ -21,15 +21,11 @@ impl<S: Storage> Binder<S> {
2121
}
2222

2323
Ok(LogicalPlan {
24-
operator: Operator::Delete(
25-
DeleteOperator {
26-
table_name
27-
}
28-
),
24+
operator: Operator::Delete(DeleteOperator { table_name }),
2925
childrens: vec![plan],
3026
})
3127
} else {
3228
unreachable!("only table")
3329
}
3430
}
35-
}
31+
}

src/binder/distinct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::binder::Binder;
22
use crate::expression::ScalarExpression;
3-
use crate::planner::LogicalPlan;
43
use crate::planner::operator::aggregate::AggregateOperator;
4+
use crate::planner::LogicalPlan;
55
use crate::storage::Storage;
66

77
impl<S: Storage> Binder<S> {
@@ -12,4 +12,4 @@ impl<S: Storage> Binder<S> {
1212
) -> LogicalPlan {
1313
AggregateOperator::new(children, vec![], select_list)
1414
}
15-
}
15+
}

src/binder/drop_table.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
use std::sync::Arc;
2-
use sqlparser::ast::ObjectName;
3-
use crate::binder::{Binder, BindError, lower_case_name, split_name};
4-
use crate::planner::LogicalPlan;
1+
use crate::binder::{lower_case_name, split_name, BindError, Binder};
52
use crate::planner::operator::drop_table::DropTableOperator;
63
use crate::planner::operator::Operator;
4+
use crate::planner::LogicalPlan;
75
use crate::storage::Storage;
6+
use sqlparser::ast::ObjectName;
7+
use std::sync::Arc;
88

99
impl<S: Storage> Binder<S> {
10-
pub(crate) fn bind_drop_table(
11-
&mut self,
12-
name: &ObjectName
13-
) -> Result<LogicalPlan, BindError> {
10+
pub(crate) fn bind_drop_table(&mut self, name: &ObjectName) -> Result<LogicalPlan, BindError> {
1411
let name = lower_case_name(&name);
1512
let (_, name) = split_name(&name)?;
1613
let table_name = Arc::new(name.to_string());
1714

1815
let plan = LogicalPlan {
19-
operator: Operator::DropTable(
20-
DropTableOperator {
21-
table_name
22-
}
23-
),
16+
operator: Operator::DropTable(DropTableOperator { table_name }),
2417
childrens: vec![],
2518
};
2619
Ok(plan)
2720
}
28-
}
21+
}

0 commit comments

Comments
 (0)