Skip to content

Commit 6914199

Browse files
authored
refactor(logical_plan): unified LogicalPlan classification (#41)
* refactor(logical_plan): unified `LogicalPlan` classification * fix(limit): fix limit field problem and bind_limit error after merging * style(physical): `PhysicalOperator` -> `PhysicalPlan` * style(logical_plan): remove 'Arc' * fix(create_table): fix `create_table.rs` test_crud_sql
1 parent 8952f83 commit 6914199

27 files changed

Lines changed: 203 additions & 413 deletions

src/binder/aggregate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ use sqlparser::ast::{Expr, OrderByExpr};
55
use crate::{
66
expression::ScalarExpression,
77
planner::{
8-
logical_select_plan::LogicalSelectPlan,
98
operator::{aggregate::AggregateOperator, sort::SortField},
109
},
1110
};
11+
use crate::planner::LogicalPlan;
1212

1313
use super::Binder;
1414

1515
impl Binder {
1616
pub fn bind_aggregate(
1717
&mut self,
18-
children: LogicalSelectPlan,
18+
children: LogicalPlan,
1919
agg_calls: Vec<ScalarExpression>,
2020
groupby_exprs: Vec<ScalarExpression>,
21-
) -> LogicalSelectPlan {
21+
) -> LogicalPlan {
2222
AggregateOperator::new(children, agg_calls, groupby_exprs)
2323
}
2424

src/binder/create_table.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ use sqlparser::ast::{ColumnDef, ObjectName};
55
use super::Binder;
66
use crate::binder::{lower_case_name, split_name};
77
use crate::catalog::ColumnCatalog;
8-
use crate::planner::logical_create_table_plan::LogicalCreateTablePlan;
8+
use crate::planner::LogicalPlan;
99
use crate::planner::operator::create_table::CreateTableOperator;
10+
use crate::planner::operator::Operator;
1011

1112
impl Binder {
1213
pub(crate) fn bind_create_table(
1314
&mut self,
1415
name: &ObjectName,
1516
columns: &[ColumnDef],
16-
) -> Result<LogicalCreateTablePlan> {
17+
) -> Result<LogicalPlan> {
1718
let name = lower_case_name(&name);
1819
let (_, table_name) = split_name(&name)?;
1920

@@ -34,11 +35,14 @@ impl Binder {
3435
.map(|col| ColumnCatalog::from(col.clone()))
3536
.collect();
3637

37-
let plan = LogicalCreateTablePlan {
38-
operator: CreateTableOperator {
39-
table_name: table_name.to_string(),
40-
columns
41-
},
38+
let plan = LogicalPlan {
39+
operator: Operator::CreateTable(
40+
CreateTableOperator {
41+
table_name: table_name.to_string(),
42+
columns
43+
}
44+
),
45+
childrens: vec![],
4246
};
4347
Ok(plan)
4448
}
@@ -59,23 +63,26 @@ mod tests {
5963
let stmt = crate::parser::parse_sql(sql).unwrap();
6064
let plan1 = binder.bind(&stmt[0]).unwrap();
6165

62-
let plan2 = LogicalPlan::CreateTable(LogicalCreateTablePlan {
63-
operator: CreateTableOperator {
64-
table_name: "t1".to_string(),
65-
columns: vec![
66-
ColumnCatalog::new(
67-
"id".to_string(),
68-
false,
69-
ColumnDesc::new(LogicalType::Integer, false)
70-
),
71-
ColumnCatalog::new(
72-
"name".to_string(),
73-
false,
74-
ColumnDesc::new(LogicalType::Varchar, false)
75-
)
76-
],
77-
},
78-
});
66+
let plan2 = LogicalPlan {
67+
operator: Operator::CreateTable(
68+
CreateTableOperator {
69+
table_name: "t1".to_string(),
70+
columns: vec![
71+
ColumnCatalog::new(
72+
"id".to_string(),
73+
false,
74+
ColumnDesc::new(LogicalType::Integer, false)
75+
),
76+
ColumnCatalog::new(
77+
"name".to_string(),
78+
false,
79+
ColumnDesc::new(LogicalType::Varchar, false)
80+
)
81+
],
82+
}
83+
),
84+
childrens: vec![],
85+
};
7986

8087
assert_eq!(plan1, plan2);
8188
}

src/binder/insert.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::slice;
2-
use std::sync::Arc;
32
use sqlparser::ast::{Expr, Ident, ObjectName};
43
use anyhow::{Error, Result};
54
use itertools::Itertools;
65
use crate::binder::{Binder, lower_case_name, split_name};
76
use crate::catalog::ColumnCatalog;
87
use crate::expression::ScalarExpression;
9-
use crate::planner::logical_insert_plan::LogicalInsertPlan;
8+
use crate::planner::LogicalPlan;
109
use crate::planner::operator::insert::InsertOperator;
1110
use crate::planner::operator::Operator;
1211
use crate::planner::operator::values::ValuesOperator;
@@ -21,7 +20,7 @@ impl Binder {
2120
name: ObjectName,
2221
idents: &[Ident],
2322
rows: &Vec<Vec<Expr>>
24-
) -> Result<LogicalInsertPlan> {
23+
) -> Result<LogicalPlan> {
2524
let name = lower_case_name(&name);
2625
let (_, table_name) = split_name(&name)?;
2726

@@ -56,15 +55,13 @@ impl Binder {
5655

5756
let values_plan = self.bind_values(rows, col_catalogs.clone());
5857

59-
Ok(LogicalInsertPlan {
60-
operator: Arc::new(
61-
Operator::Insert(
62-
InsertOperator {
63-
table: table_name.to_string(),
64-
}
65-
)
58+
Ok(LogicalPlan {
59+
operator: Operator::Insert(
60+
InsertOperator {
61+
table: table_name.to_string(),
62+
}
6663
),
67-
children: vec![Arc::new(values_plan)],
64+
childrens: vec![values_plan],
6865
})
6966
} else {
7067
Err(anyhow::Error::msg(format!(
@@ -78,13 +75,13 @@ impl Binder {
7875
&mut self,
7976
rows: Vec<Vec<DataValue>>,
8077
col_catalogs: Vec<ColumnCatalog>
81-
) -> LogicalInsertPlan {
82-
LogicalInsertPlan {
83-
operator: Arc::new(Operator::Values(ValuesOperator {
78+
) -> LogicalPlan {
79+
LogicalPlan {
80+
operator: Operator::Values(ValuesOperator {
8481
rows,
8582
col_catalogs,
86-
})),
87-
children: vec![],
83+
}),
84+
childrens: vec![],
8885
}
8986
}
9087
}

src/binder/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,11 @@ impl Binder {
6161

6262
pub fn bind(mut self, stmt: &Statement) -> Result<LogicalPlan> {
6363
let plan = match stmt {
64-
Statement::Query(query) => {
65-
let plan = self.bind_query(query)?;
66-
LogicalPlan::Select(plan)
67-
}
68-
Statement::CreateTable { name, columns, .. } => {
69-
let plan = self.bind_create_table(name, &columns)?;
70-
LogicalPlan::CreateTable(plan)
71-
}
64+
Statement::Query(query) => self.bind_query(query)?,
65+
Statement::CreateTable { name, columns, .. } => self.bind_create_table(name, &columns)?,
7266
Statement::Insert { table_name, columns, source, .. } => {
7367
if let SetExpr::Values(values) = source.body.as_ref() {
74-
let plan = self.bind_insert(table_name.to_owned(), columns, &values.rows)?;
75-
LogicalPlan::Insert(plan)
68+
self.bind_insert(table_name.to_owned(), columns, &values.rows)?
7669
} else {
7770
todo!()
7871
}

src/binder/select.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::{borrow::Borrow, sync::Arc};
1+
use std::borrow::Borrow;
22

33
use crate::{
44
catalog::ColumnRefId,
55
expression::ScalarExpression,
66
planner::{
7-
logical_select_plan::LogicalSelectPlan,
87
operator::{
98
filter::FilterOperator, join::JoinOperator as LJoinOperator, limit::LimitOperator,
109
project::ProjectOperator, Operator,
@@ -23,10 +22,11 @@ use sqlparser::ast::{
2322
Expr, Ident, Join, JoinConstraint, JoinOperator, Offset, OrderByExpr, Query, Select,
2423
SelectItem, SetExpr, TableFactor, TableWithJoins,
2524
};
25+
use crate::planner::LogicalPlan;
2626
use crate::planner::operator::sort::{SortField, SortOperator};
2727

2828
impl Binder {
29-
pub(crate) fn bind_query(&mut self, query: &Query) -> Result<LogicalSelectPlan> {
29+
pub(crate) fn bind_query(&mut self, query: &Query) -> Result<LogicalPlan> {
3030
if let Some(_with) = &query.with {
3131
// TODO support with clause.
3232
}
@@ -51,7 +51,7 @@ impl Binder {
5151
&mut self,
5252
select: &Select,
5353
orderby: &[OrderByExpr],
54-
) -> Result<LogicalSelectPlan> {
54+
) -> Result<LogicalPlan> {
5555
let mut plan = self.bind_table_ref(&select.from)?;
5656

5757
// Resolve scalar function call.
@@ -99,12 +99,12 @@ impl Binder {
9999
Ok(plan)
100100
}
101101

102-
fn bind_table_ref(&mut self, from: &[TableWithJoins]) -> Result<LogicalSelectPlan> {
102+
fn bind_table_ref(&mut self, from: &[TableWithJoins]) -> Result<LogicalPlan> {
103103
assert!(from.len() < 2, "not support yet.");
104104
if from.is_empty() {
105-
return Ok(LogicalSelectPlan {
106-
operator: Arc::new(Operator::Dummy),
107-
children: vec![],
105+
return Ok(LogicalPlan {
106+
operator: Operator::Dummy,
107+
childrens: vec![],
108108
});
109109
}
110110

@@ -120,7 +120,7 @@ impl Binder {
120120
Ok(plan)
121121
}
122122

123-
fn bind_single_table_ref(&mut self, table: &TableFactor) -> Result<LogicalSelectPlan> {
123+
fn bind_single_table_ref(&mut self, table: &TableFactor) -> Result<LogicalPlan> {
124124
let plan = match table {
125125
TableFactor::Table { name, alias, .. } => {
126126
let obj_name = name
@@ -201,23 +201,22 @@ impl Binder {
201201
let mut exprs = vec![];
202202
for ref_id in self.context.bind_table.values().cloned().collect_vec() {
203203
let table = self.context.catalog.get_table(ref_id).unwrap();
204-
for (col_id, col) in &table.all_columns() {
205-
let column_ref_id = ColumnRefId::from_table(ref_id, *col_id);
204+
for (col_id, col) in table.all_columns() {
205+
let column_ref_id = ColumnRefId::from_table(ref_id, col_id);
206206
// self.record_regular_table_column(
207207
// &table.name(),
208208
// col.name(),
209209
// *col_id,
210210
// col.desc().clone(),
211211
// );
212-
let expr = ScalarExpression::ColumnRef((*col).clone());
213-
exprs.push(expr);
212+
exprs.push(ScalarExpression::ColumnRef(col.clone()));
214213
}
215214
}
216215

217216
Ok(exprs)
218217
}
219218

220-
fn bind_join(&mut self, left: LogicalSelectPlan, join: &Join) -> Result<LogicalSelectPlan> {
219+
fn bind_join(&mut self, left: LogicalPlan, join: &Join) -> Result<LogicalPlan> {
221220
let Join {
222221
relation,
223222
join_operator,
@@ -250,9 +249,9 @@ impl Binder {
250249

251250
fn bind_where(
252251
&mut self,
253-
children: LogicalSelectPlan,
252+
children: LogicalPlan,
254253
predicate: &Expr,
255-
) -> Result<LogicalSelectPlan> {
254+
) -> Result<LogicalPlan> {
256255
Ok(FilterOperator::new(
257256
self.bind_expr(predicate)?,
258257
children,
@@ -262,46 +261,46 @@ impl Binder {
262261

263262
fn bind_having(
264263
&mut self,
265-
children: LogicalSelectPlan,
264+
children: LogicalPlan,
266265
having: ScalarExpression,
267-
) -> Result<LogicalSelectPlan> {
266+
) -> Result<LogicalPlan> {
268267
self.validate_having_orderby(&having)?;
269268
Ok(FilterOperator::new(having, children, true))
270269
}
271270

272271
fn bind_project(
273272
&mut self,
274-
children: LogicalSelectPlan,
273+
children: LogicalPlan,
275274
select_list: Vec<ScalarExpression>,
276-
) -> LogicalSelectPlan {
277-
LogicalSelectPlan {
278-
operator: Arc::new(Operator::Project(ProjectOperator {
275+
) -> LogicalPlan {
276+
LogicalPlan {
277+
operator: Operator::Project(ProjectOperator {
279278
columns: select_list,
280-
})),
281-
children: vec![Arc::new(children)],
279+
}),
280+
childrens: vec![children],
282281
}
283282
}
284283

285284
fn bind_sort(
286285
&mut self,
287-
children: LogicalSelectPlan,
286+
children: LogicalPlan,
288287
sort_fields: Vec<SortField>,
289-
) -> LogicalSelectPlan {
290-
LogicalSelectPlan {
291-
operator: Arc::new(Operator::Sort(SortOperator {
288+
) -> LogicalPlan {
289+
LogicalPlan {
290+
operator: Operator::Sort(SortOperator {
292291
sort_fields,
293292
limit: None,
294-
})),
295-
children: vec![Arc::new(children)],
293+
}),
294+
childrens: vec![children],
296295
}
297296
}
298297

299298
fn bind_limit(
300299
&mut self,
301-
children: LogicalSelectPlan,
300+
children: LogicalPlan,
302301
limit_expr: &Option<Expr>,
303302
offset_expr: &Option<Offset>,
304-
) -> Result<LogicalSelectPlan> {
303+
) -> Result<LogicalPlan> {
305304
let mut limit = 0;
306305
let mut offset = 0;
307306
if let Some(expr) = limit_expr {

src/catalog/column.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,4 @@ impl ColumnDesc {
7474
is_primary,
7575
}
7676
}
77-
78-
pub(crate) fn get_datatype(&self) -> LogicalType {
79-
self.column_datatype.clone()
80-
}
8177
}

0 commit comments

Comments
 (0)