Skip to content

Commit

Permalink
Perf/serialize (#255)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KKould authored Dec 16, 2024
1 parent 9f8a58f commit 7ed2e66
Show file tree
Hide file tree
Showing 77 changed files with 1,434 additions and 1,285 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "fnck_sql"
version = "0.0.8"
version = "0.0.9"
edition = "2021"
authors = ["Kould <[email protected]>", "Xwg <[email protected]>"]
description = "SQL as a Function for Rust"
Expand Down Expand Up @@ -35,12 +35,13 @@ harness = false
[dependencies]
ahash = { version = "0.8" }
bincode = { version = "1" }
bytes = { version = "1" }
bumpalo = { version = "3", features = ["allocator-api2", "collections", "std"] }
byteorder = { version = "1" }
chrono = { version = "0.4" }
comfy-table = { version = "7" }
csv = { version = "1" }
encode_unicode = { version = "1" }
dirs = { version = "5" }
fixedbitset = { version = "0.4" }
itertools = { version = "0.12" }
ordered-float = { version = "4" }
paste = { version = "1" }
Expand Down Expand Up @@ -68,10 +69,8 @@ tokio = { version = "1.36", features = ["full"], optional = true


[dev-dependencies]
cargo-tarpaulin = { version = "0.27" }
criterion = { version = "0.5", features = ["html_reports"] }
indicatif = { version = "0.17" }
rand_distr = { version = "0.4" }
tempfile = { version = "3.10" }
# Benchmark
sqlite = { version = "0.34" }
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ run `cargo run -p tpcc --release` to run tpcc
- Tips: TPC-C currently only supports single thread
```shell
<90th Percentile RT (MaxRT)>
New-Order : 0.003 (0.012)
Payment : 0.001 (0.003)
Order-Status : 0.054 (0.188)
Delivery : 0.021 (0.049)
Stock-Level : 0.004 (0.006)
New-Order : 0.002 (0.004)
Payment : 0.001 (0.025)
Order-Status : 0.053 (0.175)
Delivery : 0.022 (0.027)
Stock-Level : 0.003 (0.019)
<TpmC>
7345 Tpmc
7815 tpmC
```
#### 👉[check more](tpcc/README.md)

Expand Down
6 changes: 3 additions & 3 deletions src/binder/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use itertools::Itertools;
use sqlparser::ast::{Expr, OrderByExpr};
use std::collections::HashSet;

use super::{Binder, QueryBindStep};
use crate::errors::DatabaseError;
use crate::expression::function::scala::ScalarFunction;
use crate::planner::LogicalPlan;
use crate::storage::Transaction;
use crate::types::value::DataValue;
use crate::{
expression::ScalarExpression,
planner::operator::{aggregate::AggregateOperator, sort::SortField},
};

use super::{Binder, QueryBindStep};

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub fn bind_aggregate(
&mut self,
children: LogicalPlan,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/alter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_alter_table(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

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

Expand Down
6 changes: 3 additions & 3 deletions src/binder/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
use crate::errors::DatabaseError;
use crate::planner::operator::copy_from_file::CopyFromFileOperator;
use crate::planner::operator::copy_to_file::CopyToFileOperator;
use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::Childrens;
use fnck_sql_serde_macros::ReferenceSerialization;
Expand Down Expand Up @@ -63,7 +64,7 @@ impl FromStr for ExtSource {
}
}

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(super) fn bind_copy(
&mut self,
source: CopySource,
Expand Down Expand Up @@ -96,11 +97,10 @@ impl<T: Transaction> Binder<'_, '_, T> {
// COPY <source_table> TO <dest_file>
Ok(LogicalPlan::new(
Operator::CopyToFile(CopyToFileOperator {
table: table.name.to_string(),
target: ext_source,
schema_ref,
}),
Childrens::None,
Childrens::Only(TableScanOperator::build(table_name, table)),
))
} else {
// COPY <dest_table> FROM <source_file>
Expand Down
3 changes: 2 additions & 1 deletion src/binder/create_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::index::IndexType;
use crate::types::value::DataValue;
use sqlparser::ast::{ObjectName, OrderByExpr};
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_create_index(
&mut self,
table_name: &ObjectName,
Expand Down
7 changes: 3 additions & 4 deletions src/binder/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use crate::planner::operator::create_table::CreateTableOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use crate::types::LogicalType;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
// TODO: TableConstraint
pub(crate) fn bind_create_table(
&mut self,
Expand Down Expand Up @@ -157,7 +158,6 @@ mod tests {
use crate::types::LogicalType;
use crate::utils::lru::SharedLruCache;
use sqlparser::ast::CharLengthUnits;
use std::cell::RefCell;
use std::hash::RandomState;
use std::sync::atomic::AtomicUsize;
use tempfile::TempDir;
Expand All @@ -173,7 +173,6 @@ mod tests {
let table_functions = Default::default();

let sql = "create table t1 (id int primary key, name varchar(10) null)";
let args = RefCell::new(Vec::new());
let mut binder = Binder::new(
BinderContext::new(
&table_cache,
Expand All @@ -183,7 +182,7 @@ mod tests {
&table_functions,
Arc::new(AtomicUsize::new(0)),
),
&args,
&[],
None,
);
let stmt = crate::parser::parse_sql(sql).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/binder/create_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use crate::planner::operator::create_view::CreateViewOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use itertools::Itertools;
use sqlparser::ast::{Ident, ObjectName, Query};
use std::sync::Arc;
use ulid::Ulid;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_create_view(
&mut self,
or_replace: &bool,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use itertools::Itertools;
use sqlparser::ast::{Expr, TableAlias, TableFactor, TableWithJoins};
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_delete(
&mut self,
from: &TableWithJoins,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::describe::DescribeOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_describe(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::expression::ScalarExpression;
use crate::planner::operator::aggregate::AggregateOperator;
use crate::planner::LogicalPlan;
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub fn bind_distinct(
&mut self,
children: LogicalPlan,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::drop_table::DropTableOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_drop_table(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/drop_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::planner::operator::drop_view::DropViewOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use sqlparser::ast::ObjectName;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_drop_view(
&mut self,
name: &ObjectName,
Expand Down
3 changes: 2 additions & 1 deletion src/binder/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::errors::DatabaseError;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_explain(&mut self, plan: LogicalPlan) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::Explain, Childrens::Only(plan)))
}
Expand Down
13 changes: 5 additions & 8 deletions src/binder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ macro_rules! try_default {
};
}

impl<'a, T: Transaction> Binder<'a, '_, T> {
impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T, A> {
pub(crate) fn bind_expr(&mut self, expr: &Expr) -> Result<ScalarExpression, DatabaseError> {
match expr {
Expr::Identifier(ident) => {
Expand All @@ -50,14 +50,11 @@ impl<'a, T: Transaction> Binder<'a, '_, T> {
Expr::BinaryOp { left, right, op } => self.bind_binary_op_internal(left, right, op),
Expr::Value(v) => {
let value = if let Value::Placeholder(name) = v {
let (i, _) = self
.args
.borrow()
self.args
.as_ref()
.iter()
.enumerate()
.find(|(_, (key, _))| key == name)
.ok_or_else(|| DatabaseError::ParametersNotFound(name.to_string()))?;
self.args.borrow_mut().remove(i).1
.find_map(|(key, value)| (key == name).then(|| value.clone()))
.ok_or_else(|| DatabaseError::ParametersNotFound(name.to_string()))?
} else {
v.into()
};
Expand Down
2 changes: 1 addition & 1 deletion src/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sqlparser::ast::{Expr, Ident, ObjectName};
use std::slice;
use std::sync::Arc;

impl<T: Transaction> Binder<'_, '_, T> {
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_insert(
&mut self,
name: &ObjectName,
Expand Down
20 changes: 9 additions & 11 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ mod truncate;
mod update;

use sqlparser::ast::{Ident, ObjectName, ObjectType, SetExpr, Statement};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use crate::catalog::view::View;
use crate::catalog::{ColumnRef, TableCatalog, TableName};
use crate::db::{Args, ScalaFunctions, TableFunctions};
use crate::db::{ScalaFunctions, TableFunctions};
use crate::errors::DatabaseError;
use crate::expression::ScalarExpression;
use crate::planner::operator::join::JoinType;
use crate::planner::{LogicalPlan, SchemaOutput};
use crate::storage::{TableCache, Transaction, ViewCache};
use crate::types::tuple::SchemaRef;
use crate::types::value::DataValue;

pub enum InputRefType {
AggCall,
Expand Down Expand Up @@ -313,18 +313,18 @@ impl<'a, T: Transaction> BinderContext<'a, T> {
}
}

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

impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, 'b, T, A> {
pub fn new(
context: BinderContext<'a, T>,
args: &'a RefCell<Args>,
parent: Option<&'b Binder<'a, 'b, T>>,
args: &'a A,
parent: Option<&'b Binder<'a, 'b, T, A>>,
) -> Self {
Binder {
context,
Expand Down Expand Up @@ -488,7 +488,6 @@ pub mod test {
use crate::types::ColumnId;
use crate::types::LogicalType::Integer;
use crate::utils::lru::SharedLruCache;
use std::cell::RefCell;
use std::hash::RandomState;
use std::path::PathBuf;
use std::sync::atomic::AtomicUsize;
Expand All @@ -507,7 +506,6 @@ pub mod test {
let scala_functions = Default::default();
let table_functions = Default::default();
let transaction = self.storage.transaction()?;
let args = RefCell::new(Vec::new());
let mut binder = Binder::new(
BinderContext::new(
&self.table_cache,
Expand All @@ -517,7 +515,7 @@ pub mod test {
&table_functions,
Arc::new(AtomicUsize::new(0)),
),
&args,
&[],
None,
);
let stmt = crate::parser::parse_sql(sql)?;
Expand Down
2 changes: 1 addition & 1 deletion src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sqlparser::ast::{
TableWithJoins,
};

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

Expand Down
Loading

0 comments on commit 7ed2e66

Please sign in to comment.