Skip to content

Commit 8786f0f

Browse files
authored
Refactor the executor to support transaction detachment (#82)
* refactor: refactor the executor to support transaction detachment * style: `inputs` restore * style: code fmt * rollback: `Text` LogicalType
1 parent 82d1ce4 commit 8786f0f

Some content is hidden

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

46 files changed

+779
-1147
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ lazy_static = "1.4.0"
3838
comfy-table = "7.0.1"
3939
bytes = "1.5.0"
4040
kip_db = "0.1.2-alpha.17"
41-
async-recursion = "1.0.5"
4241
rust_decimal = "1"
4342
csv = "1"
4443

src/binder/aggregate.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use std::collections::HashSet;
55

66
use crate::binder::{BindError, InputRefType};
77
use crate::planner::LogicalPlan;
8-
use crate::storage::Storage;
8+
use crate::storage::Transaction;
99
use crate::{
1010
expression::ScalarExpression,
1111
planner::operator::{aggregate::AggregateOperator, sort::SortField},
1212
};
1313

1414
use super::Binder;
1515

16-
impl<S: Storage> Binder<S> {
16+
impl<'a, T: Transaction> Binder<'a, T> {
1717
pub fn bind_aggregate(
1818
&mut self,
1919
children: LogicalPlan,
@@ -33,29 +33,28 @@ impl<S: Storage> Binder<S> {
3333
Ok(())
3434
}
3535

36-
pub async fn extract_group_by_aggregate(
36+
pub fn extract_group_by_aggregate(
3737
&mut self,
3838
select_list: &mut [ScalarExpression],
3939
groupby: &[Expr],
4040
) -> Result<(), BindError> {
41-
self.validate_groupby_illegal_column(select_list, groupby)
42-
.await?;
41+
self.validate_groupby_illegal_column(select_list, groupby)?;
4342

4443
for gb in groupby {
45-
let mut expr = self.bind_expr(gb).await?;
44+
let mut expr = self.bind_expr(gb)?;
4645
self.visit_group_by_expr(select_list, &mut expr);
4746
}
4847
Ok(())
4948
}
5049

51-
pub async fn extract_having_orderby_aggregate(
50+
pub fn extract_having_orderby_aggregate(
5251
&mut self,
5352
having: &Option<Expr>,
5453
orderbys: &[OrderByExpr],
5554
) -> Result<(Option<ScalarExpression>, Option<Vec<SortField>>), BindError> {
5655
// Extract having expression.
5756
let return_having = if let Some(having) = having {
58-
let mut having = self.bind_expr(having).await?;
57+
let mut having = self.bind_expr(having)?;
5958
self.visit_column_agg_expr(&mut having, false)?;
6059

6160
Some(having)
@@ -72,7 +71,7 @@ impl<S: Storage> Binder<S> {
7271
asc,
7372
nulls_first,
7473
} = orderby;
75-
let mut expr = self.bind_expr(expr).await?;
74+
let mut expr = self.bind_expr(expr)?;
7675
self.visit_column_agg_expr(&mut expr, false)?;
7776

7877
return_orderby.push(SortField::new(
@@ -156,14 +155,14 @@ impl<S: Storage> Binder<S> {
156155
/// e.g. SELECT a,count(b) FROM t GROUP BY a. it's ok.
157156
/// SELECT a,b FROM t GROUP BY a. it's error.
158157
/// SELECT a,count(b) FROM t GROUP BY b. it's error.
159-
async fn validate_groupby_illegal_column(
158+
fn validate_groupby_illegal_column(
160159
&mut self,
161160
select_items: &[ScalarExpression],
162161
groupby: &[Expr],
163162
) -> Result<(), BindError> {
164163
let mut group_raw_exprs = vec![];
165164
for expr in groupby {
166-
let expr = self.bind_expr(expr).await?;
165+
let expr = self.bind_expr(expr)?;
167166

168167
if let ScalarExpression::Alias { alias, .. } = expr {
169168
let alias_expr = select_items.iter().find(|column| {

src/binder/copy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ impl std::fmt::Display for FileFormat {
4444

4545
impl FromStr for ExtSource {
4646
type Err = ();
47-
fn from_str(_s: &str) -> std::result::Result<Self, Self::Err> {
47+
fn from_str(_s: &str) -> Result<Self, Self::Err> {
4848
Err(())
4949
}
5050
}
5151

52-
impl<S: Storage> Binder<S> {
53-
pub(super) async fn bind_copy(
52+
impl<'a, T: Transaction> Binder<'a, T> {
53+
pub(super) fn bind_copy(
5454
&mut self,
5555
source: CopySource,
5656
to: bool,
@@ -69,7 +69,7 @@ impl<S: Storage> Binder<S> {
6969
}
7070
};
7171

72-
if let Some(table) = self.context.storage.table(&table_name.to_string()).await {
72+
if let Some(table) = self.context.transaction.table(&table_name.to_string()) {
7373
let cols = table.all_columns();
7474
let ext_source = ExtSource {
7575
path: match target {

src/binder/create_table.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use crate::catalog::ColumnCatalog;
99
use crate::planner::operator::create_table::CreateTableOperator;
1010
use crate::planner::operator::Operator;
1111
use crate::planner::LogicalPlan;
12-
use crate::storage::Storage;
12+
use crate::storage::Transaction;
1313

14-
impl<S: Storage> Binder<S> {
14+
impl<'a, T: Transaction> Binder<'a, T> {
1515
// TODO: TableConstraint
1616
pub(crate) fn bind_create_table(
1717
&mut self,
@@ -60,19 +60,22 @@ mod tests {
6060
use super::*;
6161
use crate::binder::BinderContext;
6262
use crate::catalog::ColumnDesc;
63+
use crate::execution::ExecutorError;
6364
use crate::storage::kip::KipStorage;
65+
use crate::storage::Storage;
6466
use crate::types::LogicalType;
6567
use tempfile::TempDir;
6668

6769
#[tokio::test]
68-
async fn test_create_bind() {
70+
async fn test_create_bind() -> Result<(), ExecutorError> {
6971
let temp_dir = TempDir::new().expect("unable to create temporary working directory");
70-
let storage = KipStorage::new(temp_dir.path()).await.unwrap();
72+
let storage = KipStorage::new(temp_dir.path()).await?;
73+
let transaction = storage.transaction().await?;
7174

7275
let sql = "create table t1 (id int primary key, name varchar(10) null)";
73-
let binder = Binder::new(BinderContext::new(storage));
76+
let binder = Binder::new(BinderContext::new(&transaction));
7477
let stmt = crate::parser::parse_sql(sql).unwrap();
75-
let plan1 = binder.bind(&stmt[0]).await.unwrap();
78+
let plan1 = binder.bind(&stmt[0]).unwrap();
7679

7780
match plan1.operator {
7881
Operator::CreateTable(op) => {
@@ -92,5 +95,7 @@ mod tests {
9295
}
9396
_ => unreachable!(),
9497
}
98+
99+
Ok(())
95100
}
96101
}

src/binder/delete.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ use crate::binder::{lower_case_name, split_name, BindError, Binder};
22
use crate::planner::operator::delete::DeleteOperator;
33
use crate::planner::operator::Operator;
44
use crate::planner::LogicalPlan;
5-
use crate::storage::Storage;
5+
use crate::storage::Transaction;
66
use sqlparser::ast::{Expr, TableFactor, TableWithJoins};
77

8-
impl<S: Storage> Binder<S> {
9-
pub(crate) async fn bind_delete(
8+
impl<'a, T: Transaction> Binder<'a, T> {
9+
pub(crate) fn bind_delete(
1010
&mut self,
1111
from: &TableWithJoins,
1212
selection: &Option<Expr>,
1313
) -> Result<LogicalPlan, BindError> {
1414
if let TableFactor::Table { name, .. } = &from.relation {
1515
let name = lower_case_name(name);
1616
let (_, name) = split_name(&name)?;
17-
let (table_name, mut plan) = self._bind_single_table_ref(None, name).await?;
17+
let (table_name, mut plan) = self._bind_single_table_ref(None, name)?;
1818

1919
if let Some(predicate) = selection {
20-
plan = self.bind_where(plan, predicate).await?;
20+
plan = self.bind_where(plan, predicate)?;
2121
}
2222

2323
Ok(LogicalPlan {

src/binder/distinct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::binder::Binder;
22
use crate::expression::ScalarExpression;
33
use crate::planner::operator::aggregate::AggregateOperator;
44
use crate::planner::LogicalPlan;
5-
use crate::storage::Storage;
5+
use crate::storage::Transaction;
66

7-
impl<S: Storage> Binder<S> {
7+
impl<'a, T: Transaction> Binder<'a, T> {
88
pub fn bind_distinct(
99
&mut self,
1010
children: LogicalPlan,

src/binder/drop_table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use crate::binder::{lower_case_name, split_name, BindError, Binder};
22
use crate::planner::operator::drop_table::DropTableOperator;
33
use crate::planner::operator::Operator;
44
use crate::planner::LogicalPlan;
5-
use crate::storage::Storage;
5+
use crate::storage::Transaction;
66
use sqlparser::ast::ObjectName;
77
use std::sync::Arc;
88

9-
impl<S: Storage> Binder<S> {
9+
impl<'a, T: Transaction> Binder<'a, T> {
1010
pub(crate) fn bind_drop_table(&mut self, name: &ObjectName) -> Result<LogicalPlan, BindError> {
1111
let name = lower_case_name(&name);
1212
let (_, name) = split_name(&name)?;

src/binder/expr.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::binder::BindError;
22
use crate::expression::agg::AggKind;
3-
use async_recursion::async_recursion;
43
use itertools::Itertools;
54
use sqlparser::ast::{
65
BinaryOperator, Expr, Function, FunctionArg, FunctionArgExpr, Ident, UnaryOperator,
@@ -10,35 +9,29 @@ use std::sync::Arc;
109

1110
use super::Binder;
1211
use crate::expression::ScalarExpression;
13-
use crate::storage::Storage;
12+
use crate::storage::Transaction;
1413
use crate::types::value::DataValue;
1514
use crate::types::LogicalType;
1615

17-
impl<S: Storage> Binder<S> {
18-
#[async_recursion]
19-
pub(crate) async fn bind_expr(&mut self, expr: &Expr) -> Result<ScalarExpression, BindError> {
16+
impl<'a, T: Transaction> Binder<'a, T> {
17+
pub(crate) fn bind_expr(&mut self, expr: &Expr) -> Result<ScalarExpression, BindError> {
2018
match expr {
2119
Expr::Identifier(ident) => {
2220
self.bind_column_ref_from_identifiers(slice::from_ref(ident), None)
23-
.await
24-
}
25-
Expr::CompoundIdentifier(idents) => {
26-
self.bind_column_ref_from_identifiers(idents, None).await
27-
}
28-
Expr::BinaryOp { left, right, op } => {
29-
self.bind_binary_op_internal(left, right, op).await
3021
}
22+
Expr::CompoundIdentifier(idents) => self.bind_column_ref_from_identifiers(idents, None),
23+
Expr::BinaryOp { left, right, op } => self.bind_binary_op_internal(left, right, op),
3124
Expr::Value(v) => Ok(ScalarExpression::Constant(Arc::new(v.into()))),
32-
Expr::Function(func) => self.bind_agg_call(func).await,
33-
Expr::Nested(expr) => self.bind_expr(expr).await,
34-
Expr::UnaryOp { expr, op } => self.bind_unary_op_internal(expr, op).await,
25+
Expr::Function(func) => self.bind_agg_call(func),
26+
Expr::Nested(expr) => self.bind_expr(expr),
27+
Expr::UnaryOp { expr, op } => self.bind_unary_op_internal(expr, op),
3528
_ => {
3629
todo!()
3730
}
3831
}
3932
}
4033

41-
pub async fn bind_column_ref_from_identifiers(
34+
pub fn bind_column_ref_from_identifiers(
4235
&mut self,
4336
idents: &[Ident],
4437
bind_table_name: Option<&String>,
@@ -66,9 +59,8 @@ impl<S: Storage> Binder<S> {
6659
if let Some(table) = table_name.or(bind_table_name) {
6760
let table_catalog = self
6861
.context
69-
.storage
62+
.transaction
7063
.table(table)
71-
.await
7264
.ok_or_else(|| BindError::InvalidTable(table.to_string()))?;
7365

7466
let column_catalog = table_catalog
@@ -100,14 +92,14 @@ impl<S: Storage> Binder<S> {
10092
}
10193
}
10294

103-
async fn bind_binary_op_internal(
95+
fn bind_binary_op_internal(
10496
&mut self,
10597
left: &Expr,
10698
right: &Expr,
10799
op: &BinaryOperator,
108100
) -> Result<ScalarExpression, BindError> {
109-
let left_expr = Box::new(self.bind_expr(left).await?);
110-
let right_expr = Box::new(self.bind_expr(right).await?);
101+
let left_expr = Box::new(self.bind_expr(left)?);
102+
let right_expr = Box::new(self.bind_expr(right)?);
111103

112104
let ty = match op {
113105
BinaryOperator::Plus
@@ -137,12 +129,12 @@ impl<S: Storage> Binder<S> {
137129
})
138130
}
139131

140-
async fn bind_unary_op_internal(
132+
fn bind_unary_op_internal(
141133
&mut self,
142134
expr: &Expr,
143135
op: &UnaryOperator,
144136
) -> Result<ScalarExpression, BindError> {
145-
let expr = Box::new(self.bind_expr(expr).await?);
137+
let expr = Box::new(self.bind_expr(expr)?);
146138
let ty = if let UnaryOperator::Not = op {
147139
LogicalType::Boolean
148140
} else {
@@ -156,7 +148,7 @@ impl<S: Storage> Binder<S> {
156148
})
157149
}
158150

159-
async fn bind_agg_call(&mut self, func: &Function) -> Result<ScalarExpression, BindError> {
151+
fn bind_agg_call(&mut self, func: &Function) -> Result<ScalarExpression, BindError> {
160152
let mut args = Vec::with_capacity(func.args.len());
161153

162154
for arg in func.args.iter() {
@@ -165,7 +157,7 @@ impl<S: Storage> Binder<S> {
165157
FunctionArg::Unnamed(arg) => arg,
166158
};
167159
match arg_expr {
168-
FunctionArgExpr::Expr(expr) => args.push(self.bind_expr(expr).await?),
160+
FunctionArgExpr::Expr(expr) => args.push(self.bind_expr(expr)?),
169161
FunctionArgExpr::Wildcard => args.push(Self::wildcard_expr()),
170162
_ => todo!(),
171163
}

src/binder/insert.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use crate::planner::operator::insert::InsertOperator;
66
use crate::planner::operator::values::ValuesOperator;
77
use crate::planner::operator::Operator;
88
use crate::planner::LogicalPlan;
9-
use crate::storage::Storage;
9+
use crate::storage::Transaction;
1010
use crate::types::value::{DataValue, ValueRef};
1111
use sqlparser::ast::{Expr, Ident, ObjectName};
1212
use std::slice;
1313
use std::sync::Arc;
1414

15-
impl<S: Storage> Binder<S> {
16-
pub(crate) async fn bind_insert(
15+
impl<'a, T: Transaction> Binder<'a, T> {
16+
pub(crate) fn bind_insert(
1717
&mut self,
1818
name: ObjectName,
1919
idents: &[Ident],
@@ -24,21 +24,18 @@ impl<S: Storage> Binder<S> {
2424
let (_, name) = split_name(&name)?;
2525
let table_name = Arc::new(name.to_string());
2626

27-
if let Some(table) = self.context.storage.table(&table_name).await {
27+
if let Some(table) = self.context.transaction.table(&table_name) {
2828
let mut columns = Vec::new();
2929

3030
if idents.is_empty() {
3131
columns = table.all_columns();
3232
} else {
3333
let bind_table_name = Some(table_name.to_string());
3434
for ident in idents {
35-
match self
36-
.bind_column_ref_from_identifiers(
37-
slice::from_ref(ident),
38-
bind_table_name.as_ref(),
39-
)
40-
.await?
41-
{
35+
match self.bind_column_ref_from_identifiers(
36+
slice::from_ref(ident),
37+
bind_table_name.as_ref(),
38+
)? {
4239
ScalarExpression::ColumnRef(catalog) => columns.push(catalog),
4340
_ => unreachable!(),
4441
}
@@ -50,7 +47,7 @@ impl<S: Storage> Binder<S> {
5047
let mut row = Vec::with_capacity(expr_row.len());
5148

5249
for (i, expr) in expr_row.into_iter().enumerate() {
53-
match &self.bind_expr(expr).await? {
50+
match &self.bind_expr(expr)? {
5451
ScalarExpression::Constant(value) => {
5552
// Check if the value length is too long
5653
value.check_len(columns[i].datatype())?;

0 commit comments

Comments
 (0)