Skip to content

Commit

Permalink
Refactor/result iter (#251)
Browse files Browse the repository at this point in the history
* refactor: use `ResultIter`(`DatabaseIter`&`TransactionIter`) for `DataBase` result return

* refactor: enumerate the childrens attribute of `LogicalPlan`

* chore: fix word (`macros`)
  • Loading branch information
KKould authored Dec 6, 2024
1 parent f29bfcb commit 5ac7795
Show file tree
Hide file tree
Showing 62 changed files with 1,183 additions and 829 deletions.
6 changes: 3 additions & 3 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.6"
version = "0.0.7"
edition = "2021"
authors = ["Kould <[email protected]>", "Xwg <[email protected]>"]
description = "SQL as a Function for Rust"
Expand All @@ -23,8 +23,8 @@ required-features = ["net"]
doctest = false

[features]
default = ["marcos"]
marcos = []
default = ["macros"]
macros = []
net = ["dep:pgwire", "dep:async-trait", "dep:clap", "dep:env_logger", "dep:futures", "dep:log", "dep:tokio"]

[[bench]]
Expand Down
29 changes: 15 additions & 14 deletions benchmarks/query_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, Criterion};
use fnck_sql::db::DataBaseBuilder;
use fnck_sql::db::{DataBaseBuilder, ResultIter};
use fnck_sql::errors::DatabaseError;
use indicatif::{ProgressBar, ProgressStyle};
use itertools::Itertools;
Expand All @@ -25,23 +25,25 @@ fn query_cases() -> Vec<(&'static str, &'static str)> {
}

fn init_fncksql_query_bench() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path(QUERY_BENCH_FNCK_SQL_PATH)
.build()
.unwrap();
database.run("create table t1 (c1 int primary key, c2 int)")?;
let database = DataBaseBuilder::path(QUERY_BENCH_FNCK_SQL_PATH).build()?;
database
.run("create table t1 (c1 int primary key, c2 int)")?
.done()?;
let pb = ProgressBar::new(TABLE_ROW_NUM);
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] {bar:40.cyan/white} {pos}/{len} {msg}")
.unwrap(),
);
for i in 0..TABLE_ROW_NUM {
let _ = database.run(format!("insert into t1 values({}, {})", i, i + 1).as_str())?;
database
.run(format!("insert into t1 values({}, {})", i, i + 1).as_str())?
.done()?;
pb.set_position(i + 1);
}
pb.finish_with_message("Insert completed!");

let _ = database.run("analyze table t1")?;
database.run("analyze table t1")?.done()?;

Ok(())
}
Expand Down Expand Up @@ -98,19 +100,18 @@ fn query_on_execute(c: &mut Criterion) {
for (name, case) in query_cases() {
c.bench_function(format!("FnckSQL: {} by '{}'", name, case).as_str(), |b| {
b.iter(|| {
let _tuples = database.run(case).unwrap();
for tuple in database.run(case).unwrap() {
let _ = tuple.unwrap();
}
})
});

let connection = sqlite::open(QUERY_BENCH_SQLITE_PATH.to_owned()).unwrap();
c.bench_function(format!("SQLite: {} by '{}'", name, case).as_str(), |b| {
b.iter(|| {
let _tuples = connection
.prepare(case)
.unwrap()
.into_iter()
.map(|row| row.unwrap())
.collect_vec();
for row in connection.prepare(case).unwrap() {
let _ = row.unwrap();
}
})
});
}
Expand Down
26 changes: 14 additions & 12 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use fnck_sql::db::DataBaseBuilder;
use fnck_sql::db::{DataBaseBuilder, ResultIter};
use fnck_sql::errors::DatabaseError;
use fnck_sql::implement_from_tuple;
use fnck_sql::types::value::DataValue;
use itertools::Itertools;

#[derive(Default, Debug, PartialEq)]
struct MyStruct {
Expand All @@ -25,21 +24,24 @@ implement_from_tuple!(
)
);

#[cfg(feature = "marcos")]
#[cfg(feature = "macros")]
fn main() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./hello_world").build()?;

let _ = database.run("create table if not exists my_struct (c1 int primary key, c2 int)")?;
let _ = database.run("insert into my_struct values(0, 0), (1, 1)")?;
let (schema, tuples) = database.run("select * from my_struct")?;
let tuples = tuples
.into_iter()
.map(|tuple| MyStruct::from((&schema, tuple)))
.collect_vec();
database
.run("create table if not exists my_struct (c1 int primary key, c2 int)")?
.done()?;
database
.run("insert into my_struct values(0, 0), (1, 1)")?
.done()?;

println!("{:#?}", tuples);
let iter = database.run("select * from my_struct")?;
let schema = iter.schema().clone();

let _ = database.run("drop table my_struct")?;
for tuple in iter {
println!("{:?}", MyStruct::from((&schema, tuple?)));
}
database.run("drop table my_struct")?.done()?;

Ok(())
}
16 changes: 10 additions & 6 deletions examples/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use fnck_sql::db::DataBaseBuilder;
use fnck_sql::db::{DataBaseBuilder, ResultIter};
use fnck_sql::errors::DatabaseError;

fn main() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./transaction").build()?;
let mut tx_1 = database.new_transaction()?;
let mut transaction = database.new_transaction()?;

let _ = tx_1.run("create table if not exists t1 (c1 int primary key, c2 int)")?;
let _ = tx_1.run("insert into t1 values(0, 0), (1, 1)")?;
transaction
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
.done()?;
transaction
.run("insert into t1 values(0, 0), (1, 1)")?
.done()?;

assert!(database.run("select * from t1").is_err());

tx_1.commit()?;
transaction.commit()?;

assert!(database.run("select * from t1").is_ok());

let _ = database.run("drop table t1")?;
database.run("drop table t1")?.done()?;

Ok(())
}
15 changes: 10 additions & 5 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;
use clap::Parser;
use fnck_sql::db::{DBTransaction, DataBaseBuilder, Database};
use fnck_sql::db::{DBTransaction, DataBaseBuilder, Database, ResultIter};
use fnck_sql::errors::DatabaseError;
use fnck_sql::storage::rocksdb::RocksStorage;
use fnck_sql::types::tuple::{Schema, Tuple};
Expand Down Expand Up @@ -172,14 +172,19 @@ impl SimpleQueryHandler for SessionBackend {
_ => {
let mut guard = self.tx.lock();

let (schema, tuples) = if let Some(transaction) = guard.as_mut() {
unsafe { transaction.as_mut().run(query) }
let iter = if let Some(transaction) = guard.as_mut() {
unsafe { transaction.as_mut().run(query) }.map(Box::new)
as Result<Box<dyn ResultIter>, _>
} else {
self.inner.run(query)
self.inner.run(query).map(Box::new)
}
.map_err(|e| PgWireError::ApiError(Box::new(e)))?;

Ok(vec![Response::Query(encode_tuples(&schema, tuples)?)])
let mut tuples = Vec::new();
for tuple in iter {
tuples.push(tuple.map_err(|e| PgWireError::ApiError(Box::new(e)))?);
}
Ok(vec![Response::Query(encode_tuples(iter.schema(), tuples)?)])
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/binder/alter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::planner::operator::alter_table::add_column::AddColumnOperator;
use crate::planner::operator::alter_table::drop_column::DropColumnOperator;
use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;

impl<T: Transaction> Binder<'_, '_, T> {
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
if_not_exists: *if_not_exists,
column,
}),
vec![plan],
Childrens::Only(plan),
)
}
AlterTableOperation::DropColumn {
Expand All @@ -60,7 +60,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
if_exists: *if_exists,
column_name,
}),
vec![plan],
Childrens::Only(plan),
)
}
AlterTableOperation::DropPrimaryKey => todo!(),
Expand Down
4 changes: 2 additions & 2 deletions src/binder/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::errors::DatabaseError;
use crate::planner::operator::analyze::AnalyzeOperator;
use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use sqlparser::ast::ObjectName;
use std::sync::Arc;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
table_name,
index_metas,
}),
vec![scan_op],
Childrens::Only(scan_op),
))
}
}
5 changes: 3 additions & 2 deletions src/binder/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::errors::DatabaseError;
use crate::planner::operator::copy_from_file::CopyFromFileOperator;
use crate::planner::operator::copy_to_file::CopyToFileOperator;
use crate::planner::operator::Operator;
use crate::planner::Childrens;
use fnck_sql_serde_macros::ReferenceSerialization;
use serde::{Deserialize, Serialize};
use sqlparser::ast::{CopyOption, CopySource, CopyTarget};
Expand Down Expand Up @@ -98,7 +99,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
target: ext_source,
schema_ref,
}),
vec![],
Childrens::None,
))
} else {
// COPY <dest_table> FROM <source_file>
Expand All @@ -108,7 +109,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
schema_ref,
table: table_name.to_string(),
}),
vec![],
Childrens::None,
))
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/binder/create_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::expression::ScalarExpression;
use crate::planner::operator::create_index::CreateIndexOperator;
use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::index::IndexType;
use sqlparser::ast::{ObjectName, OrderByExpr};
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
if_not_exists,
ty,
}),
vec![plan],
Childrens::Only(plan),
))
}
}
4 changes: 2 additions & 2 deletions src/binder/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::errors::DatabaseError;
use crate::expression::ScalarExpression;
use crate::planner::operator::create_table::CreateTableOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::LogicalType;

Expand Down Expand Up @@ -90,7 +90,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
columns,
if_not_exists,
}),
vec![],
Childrens::None,
))
}

Expand Down
4 changes: 2 additions & 2 deletions src/binder/create_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::errors::DatabaseError;
use crate::expression::{AliasType, ScalarExpression};
use crate::planner::operator::create_view::CreateViewOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use itertools::Itertools;
use sqlparser::ast::{Ident, ObjectName, Query};
Expand Down Expand Up @@ -56,7 +56,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
},
or_replace: *or_replace,
}),
vec![],
Childrens::None,
))
}
}
4 changes: 2 additions & 2 deletions src/binder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::errors::DatabaseError;
use crate::planner::operator::delete::DeleteOperator;
use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use itertools::Itertools;
use sqlparser::ast::{Expr, TableAlias, TableFactor, TableWithJoins};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
table_name,
primary_keys,
}),
vec![plan],
Childrens::Only(plan),
))
} else {
unreachable!("only table")
Expand Down
4 changes: 2 additions & 2 deletions src/binder/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::binder::{lower_case_name, Binder};
use crate::errors::DatabaseError;
use crate::planner::operator::describe::DescribeOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use sqlparser::ast::ObjectName;
use std::sync::Arc;
Expand All @@ -16,7 +16,7 @@ impl<T: Transaction> Binder<'_, '_, T> {

Ok(LogicalPlan::new(
Operator::Describe(DescribeOperator { table_name }),
vec![],
Childrens::None,
))
}
}
4 changes: 2 additions & 2 deletions src/binder/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::binder::{lower_case_name, Binder};
use crate::errors::DatabaseError;
use crate::planner::operator::drop_table::DropTableOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use sqlparser::ast::ObjectName;
use std::sync::Arc;
Expand All @@ -20,7 +20,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
table_name,
if_exists: *if_exists,
}),
vec![],
Childrens::None,
))
}
}
4 changes: 2 additions & 2 deletions src/binder/drop_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::binder::{lower_case_name, Binder};
use crate::errors::DatabaseError;
use crate::planner::operator::drop_view::DropViewOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use sqlparser::ast::ObjectName;
use std::sync::Arc;
Expand All @@ -20,7 +20,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
view_name,
if_exists: *if_exists,
}),
vec![],
Childrens::None,
))
}
}
Loading

0 comments on commit 5ac7795

Please sign in to comment.