Skip to content

Commit 7ed2e66

Browse files
authored
Perf/serialize (#255)
* perf: use `byteorder` on `DataValue::to_raw` & `DataValue::from_raw` * perf: Use `bumpalo` to control memory allocation of `Sort` and `TableCodec` * chore: simplify `Tuple::deserialize_from` & `ScalaExpression::eval` & `HepGraph::node_iter` & `HepOptimizer::apply_batch` * perf: encode the tablename prefix in `TableCodec` into a hash, enabling it to apply rocksdb prefix range * chore: simplify parameter on execute sql
1 parent 9f8a58f commit 7ed2e66

Some content is hidden

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

77 files changed

+1434
-1285
lines changed

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "fnck_sql"
5-
version = "0.0.8"
5+
version = "0.0.9"
66
edition = "2021"
77
authors = ["Kould <kould2333@gmail.com>", "Xwg <loloxwg@gmail.com>"]
88
description = "SQL as a Function for Rust"
@@ -35,12 +35,13 @@ harness = false
3535
[dependencies]
3636
ahash = { version = "0.8" }
3737
bincode = { version = "1" }
38-
bytes = { version = "1" }
38+
bumpalo = { version = "3", features = ["allocator-api2", "collections", "std"] }
39+
byteorder = { version = "1" }
3940
chrono = { version = "0.4" }
4041
comfy-table = { version = "7" }
4142
csv = { version = "1" }
42-
encode_unicode = { version = "1" }
4343
dirs = { version = "5" }
44+
fixedbitset = { version = "0.4" }
4445
itertools = { version = "0.12" }
4546
ordered-float = { version = "4" }
4647
paste = { version = "1" }
@@ -68,10 +69,8 @@ tokio = { version = "1.36", features = ["full"], optional = true
6869

6970

7071
[dev-dependencies]
71-
cargo-tarpaulin = { version = "0.27" }
7272
criterion = { version = "0.5", features = ["html_reports"] }
7373
indicatif = { version = "0.17" }
74-
rand_distr = { version = "0.4" }
7574
tempfile = { version = "3.10" }
7675
# Benchmark
7776
sqlite = { version = "0.34" }

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ run `cargo run -p tpcc --release` to run tpcc
7373
- Tips: TPC-C currently only supports single thread
7474
```shell
7575
<90th Percentile RT (MaxRT)>
76-
New-Order : 0.003 (0.012)
77-
Payment : 0.001 (0.003)
78-
Order-Status : 0.054 (0.188)
79-
Delivery : 0.021 (0.049)
80-
Stock-Level : 0.004 (0.006)
76+
New-Order : 0.002 (0.004)
77+
Payment : 0.001 (0.025)
78+
Order-Status : 0.053 (0.175)
79+
Delivery : 0.022 (0.027)
80+
Stock-Level : 0.003 (0.019)
8181
<TpmC>
82-
7345 Tpmc
82+
7815 tpmC
8383
```
8484
#### 👉[check more](tpcc/README.md)
8585

src/binder/aggregate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ use itertools::Itertools;
33
use sqlparser::ast::{Expr, OrderByExpr};
44
use std::collections::HashSet;
55

6+
use super::{Binder, QueryBindStep};
67
use crate::errors::DatabaseError;
78
use crate::expression::function::scala::ScalarFunction;
89
use crate::planner::LogicalPlan;
910
use crate::storage::Transaction;
11+
use crate::types::value::DataValue;
1012
use crate::{
1113
expression::ScalarExpression,
1214
planner::operator::{aggregate::AggregateOperator, sort::SortField},
1315
};
1416

15-
use super::{Binder, QueryBindStep};
16-
17-
impl<T: Transaction> Binder<'_, '_, T> {
17+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1818
pub fn bind_aggregate(
1919
&mut self,
2020
children: LogicalPlan,

src/binder/alter_table.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use crate::planner::operator::table_scan::TableScanOperator;
1111
use crate::planner::operator::Operator;
1212
use crate::planner::{Childrens, LogicalPlan};
1313
use crate::storage::Transaction;
14+
use crate::types::value::DataValue;
1415

15-
impl<T: Transaction> Binder<'_, '_, T> {
16+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1617
pub(crate) fn bind_alter_table(
1718
&mut self,
1819
name: &ObjectName,

src/binder/analyze.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::planner::operator::table_scan::TableScanOperator;
55
use crate::planner::operator::Operator;
66
use crate::planner::{Childrens, LogicalPlan};
77
use crate::storage::Transaction;
8+
use crate::types::value::DataValue;
89
use sqlparser::ast::ObjectName;
910
use std::sync::Arc;
1011

11-
impl<T: Transaction> Binder<'_, '_, T> {
12+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1213
pub(crate) fn bind_analyze(&mut self, name: &ObjectName) -> Result<LogicalPlan, DatabaseError> {
1314
let table_name = Arc::new(lower_case_name(name)?);
1415

src/binder/copy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::*;
66
use crate::errors::DatabaseError;
77
use crate::planner::operator::copy_from_file::CopyFromFileOperator;
88
use crate::planner::operator::copy_to_file::CopyToFileOperator;
9+
use crate::planner::operator::table_scan::TableScanOperator;
910
use crate::planner::operator::Operator;
1011
use crate::planner::Childrens;
1112
use fnck_sql_serde_macros::ReferenceSerialization;
@@ -63,7 +64,7 @@ impl FromStr for ExtSource {
6364
}
6465
}
6566

66-
impl<T: Transaction> Binder<'_, '_, T> {
67+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
6768
pub(super) fn bind_copy(
6869
&mut self,
6970
source: CopySource,
@@ -96,11 +97,10 @@ impl<T: Transaction> Binder<'_, '_, T> {
9697
// COPY <source_table> TO <dest_file>
9798
Ok(LogicalPlan::new(
9899
Operator::CopyToFile(CopyToFileOperator {
99-
table: table.name.to_string(),
100100
target: ext_source,
101101
schema_ref,
102102
}),
103-
Childrens::None,
103+
Childrens::Only(TableScanOperator::build(table_name, table)),
104104
))
105105
} else {
106106
// COPY <dest_table> FROM <source_file>

src/binder/create_index.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use crate::planner::operator::Operator;
77
use crate::planner::{Childrens, LogicalPlan};
88
use crate::storage::Transaction;
99
use crate::types::index::IndexType;
10+
use crate::types::value::DataValue;
1011
use sqlparser::ast::{ObjectName, OrderByExpr};
1112
use std::sync::Arc;
1213

13-
impl<T: Transaction> Binder<'_, '_, T> {
14+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1415
pub(crate) fn bind_create_index(
1516
&mut self,
1617
table_name: &ObjectName,

src/binder/create_table.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ use crate::planner::operator::create_table::CreateTableOperator;
1212
use crate::planner::operator::Operator;
1313
use crate::planner::{Childrens, LogicalPlan};
1414
use crate::storage::Transaction;
15+
use crate::types::value::DataValue;
1516
use crate::types::LogicalType;
1617

17-
impl<T: Transaction> Binder<'_, '_, T> {
18+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1819
// TODO: TableConstraint
1920
pub(crate) fn bind_create_table(
2021
&mut self,
@@ -157,7 +158,6 @@ mod tests {
157158
use crate::types::LogicalType;
158159
use crate::utils::lru::SharedLruCache;
159160
use sqlparser::ast::CharLengthUnits;
160-
use std::cell::RefCell;
161161
use std::hash::RandomState;
162162
use std::sync::atomic::AtomicUsize;
163163
use tempfile::TempDir;
@@ -173,7 +173,6 @@ mod tests {
173173
let table_functions = Default::default();
174174

175175
let sql = "create table t1 (id int primary key, name varchar(10) null)";
176-
let args = RefCell::new(Vec::new());
177176
let mut binder = Binder::new(
178177
BinderContext::new(
179178
&table_cache,
@@ -183,7 +182,7 @@ mod tests {
183182
&table_functions,
184183
Arc::new(AtomicUsize::new(0)),
185184
),
186-
&args,
185+
&[],
187186
None,
188187
);
189188
let stmt = crate::parser::parse_sql(sql).unwrap();

src/binder/create_view.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ use crate::planner::operator::create_view::CreateViewOperator;
77
use crate::planner::operator::Operator;
88
use crate::planner::{Childrens, LogicalPlan};
99
use crate::storage::Transaction;
10+
use crate::types::value::DataValue;
1011
use itertools::Itertools;
1112
use sqlparser::ast::{Ident, ObjectName, Query};
1213
use std::sync::Arc;
1314
use ulid::Ulid;
1415

15-
impl<T: Transaction> Binder<'_, '_, T> {
16+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1617
pub(crate) fn bind_create_view(
1718
&mut self,
1819
or_replace: &bool,

src/binder/delete.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use crate::planner::operator::table_scan::TableScanOperator;
55
use crate::planner::operator::Operator;
66
use crate::planner::{Childrens, LogicalPlan};
77
use crate::storage::Transaction;
8+
use crate::types::value::DataValue;
89
use itertools::Itertools;
910
use sqlparser::ast::{Expr, TableAlias, TableFactor, TableWithJoins};
1011
use std::sync::Arc;
1112

12-
impl<T: Transaction> Binder<'_, '_, T> {
13+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1314
pub(crate) fn bind_delete(
1415
&mut self,
1516
from: &TableWithJoins,

src/binder/describe.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::planner::operator::describe::DescribeOperator;
44
use crate::planner::operator::Operator;
55
use crate::planner::{Childrens, LogicalPlan};
66
use crate::storage::Transaction;
7+
use crate::types::value::DataValue;
78
use sqlparser::ast::ObjectName;
89
use std::sync::Arc;
910

10-
impl<T: Transaction> Binder<'_, '_, T> {
11+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1112
pub(crate) fn bind_describe(
1213
&mut self,
1314
name: &ObjectName,

src/binder/distinct.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use crate::expression::ScalarExpression;
33
use crate::planner::operator::aggregate::AggregateOperator;
44
use crate::planner::LogicalPlan;
55
use crate::storage::Transaction;
6+
use crate::types::value::DataValue;
67

7-
impl<T: Transaction> Binder<'_, '_, T> {
8+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
89
pub fn bind_distinct(
910
&mut self,
1011
children: LogicalPlan,

src/binder/drop_table.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::planner::operator::drop_table::DropTableOperator;
44
use crate::planner::operator::Operator;
55
use crate::planner::{Childrens, LogicalPlan};
66
use crate::storage::Transaction;
7+
use crate::types::value::DataValue;
78
use sqlparser::ast::ObjectName;
89
use std::sync::Arc;
910

10-
impl<T: Transaction> Binder<'_, '_, T> {
11+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1112
pub(crate) fn bind_drop_table(
1213
&mut self,
1314
name: &ObjectName,

src/binder/drop_view.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use crate::planner::operator::drop_view::DropViewOperator;
44
use crate::planner::operator::Operator;
55
use crate::planner::{Childrens, LogicalPlan};
66
use crate::storage::Transaction;
7+
use crate::types::value::DataValue;
78
use sqlparser::ast::ObjectName;
89
use std::sync::Arc;
910

10-
impl<T: Transaction> Binder<'_, '_, T> {
11+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1112
pub(crate) fn bind_drop_view(
1213
&mut self,
1314
name: &ObjectName,

src/binder/explain.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use crate::errors::DatabaseError;
33
use crate::planner::operator::Operator;
44
use crate::planner::{Childrens, LogicalPlan};
55
use crate::storage::Transaction;
6+
use crate::types::value::DataValue;
67

7-
impl<T: Transaction> Binder<'_, '_, T> {
8+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
89
pub(crate) fn bind_explain(&mut self, plan: LogicalPlan) -> Result<LogicalPlan, DatabaseError> {
910
Ok(LogicalPlan::new(Operator::Explain, Childrens::Only(plan)))
1011
}

src/binder/expr.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ macro_rules! try_default {
4040
};
4141
}
4242

43-
impl<'a, T: Transaction> Binder<'a, '_, T> {
43+
impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T, A> {
4444
pub(crate) fn bind_expr(&mut self, expr: &Expr) -> Result<ScalarExpression, DatabaseError> {
4545
match expr {
4646
Expr::Identifier(ident) => {
@@ -50,14 +50,11 @@ impl<'a, T: Transaction> Binder<'a, '_, T> {
5050
Expr::BinaryOp { left, right, op } => self.bind_binary_op_internal(left, right, op),
5151
Expr::Value(v) => {
5252
let value = if let Value::Placeholder(name) = v {
53-
let (i, _) = self
54-
.args
55-
.borrow()
53+
self.args
54+
.as_ref()
5655
.iter()
57-
.enumerate()
58-
.find(|(_, (key, _))| key == name)
59-
.ok_or_else(|| DatabaseError::ParametersNotFound(name.to_string()))?;
60-
self.args.borrow_mut().remove(i).1
56+
.find_map(|(key, value)| (key == name).then(|| value.clone()))
57+
.ok_or_else(|| DatabaseError::ParametersNotFound(name.to_string()))?
6158
} else {
6259
v.into()
6360
};

src/binder/insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sqlparser::ast::{Expr, Ident, ObjectName};
1212
use std::slice;
1313
use std::sync::Arc;
1414

15-
impl<T: Transaction> Binder<'_, '_, T> {
15+
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
1616
pub(crate) fn bind_insert(
1717
&mut self,
1818
name: &ObjectName,

src/binder/mod.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ mod truncate;
1919
mod update;
2020

2121
use sqlparser::ast::{Ident, ObjectName, ObjectType, SetExpr, Statement};
22-
use std::cell::RefCell;
2322
use std::collections::{BTreeMap, HashMap, HashSet};
2423
use std::sync::atomic::{AtomicUsize, Ordering};
2524
use std::sync::Arc;
2625

2726
use crate::catalog::view::View;
2827
use crate::catalog::{ColumnRef, TableCatalog, TableName};
29-
use crate::db::{Args, ScalaFunctions, TableFunctions};
28+
use crate::db::{ScalaFunctions, TableFunctions};
3029
use crate::errors::DatabaseError;
3130
use crate::expression::ScalarExpression;
3231
use crate::planner::operator::join::JoinType;
3332
use crate::planner::{LogicalPlan, SchemaOutput};
3433
use crate::storage::{TableCache, Transaction, ViewCache};
3534
use crate::types::tuple::SchemaRef;
35+
use crate::types::value::DataValue;
3636

3737
pub enum InputRefType {
3838
AggCall,
@@ -313,18 +313,18 @@ impl<'a, T: Transaction> BinderContext<'a, T> {
313313
}
314314
}
315315

316-
pub struct Binder<'a, 'b, T: Transaction> {
316+
pub struct Binder<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> {
317317
context: BinderContext<'a, T>,
318318
table_schema_buf: HashMap<TableName, Option<SchemaOutput>>,
319-
args: &'a RefCell<Args>,
320-
pub(crate) parent: Option<&'b Binder<'a, 'b, T>>,
319+
args: &'a A,
320+
pub(crate) parent: Option<&'b Binder<'a, 'b, T, A>>,
321321
}
322322

323-
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
323+
impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, 'b, T, A> {
324324
pub fn new(
325325
context: BinderContext<'a, T>,
326-
args: &'a RefCell<Args>,
327-
parent: Option<&'b Binder<'a, 'b, T>>,
326+
args: &'a A,
327+
parent: Option<&'b Binder<'a, 'b, T, A>>,
328328
) -> Self {
329329
Binder {
330330
context,
@@ -488,7 +488,6 @@ pub mod test {
488488
use crate::types::ColumnId;
489489
use crate::types::LogicalType::Integer;
490490
use crate::utils::lru::SharedLruCache;
491-
use std::cell::RefCell;
492491
use std::hash::RandomState;
493492
use std::path::PathBuf;
494493
use std::sync::atomic::AtomicUsize;
@@ -507,7 +506,6 @@ pub mod test {
507506
let scala_functions = Default::default();
508507
let table_functions = Default::default();
509508
let transaction = self.storage.transaction()?;
510-
let args = RefCell::new(Vec::new());
511509
let mut binder = Binder::new(
512510
BinderContext::new(
513511
&self.table_cache,
@@ -517,7 +515,7 @@ pub mod test {
517515
&table_functions,
518516
Arc::new(AtomicUsize::new(0)),
519517
),
520-
&args,
518+
&[],
521519
None,
522520
);
523521
let stmt = crate::parser::parse_sql(sql)?;

src/binder/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use sqlparser::ast::{
3838
TableWithJoins,
3939
};
4040

41-
impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
41+
impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, 'b, T, A> {
4242
pub(crate) fn bind_query(&mut self, query: &Query) -> Result<LogicalPlan, DatabaseError> {
4343
let origin_step = self.context.step_now();
4444

0 commit comments

Comments
 (0)