Skip to content

Commit

Permalink
refactor: enumerate the childrens attribute of LogicalPlan
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Dec 6, 2024
1 parent 04b4a70 commit 4013547
Show file tree
Hide file tree
Showing 47 changed files with 419 additions and 300 deletions.
2 changes: 1 addition & 1 deletion 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 Down
15 changes: 8 additions & 7 deletions benchmarks/query_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ 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()?;
database.run("create table t1 (c1 int primary key, c2 int)")?.done()?;
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 {
database.run(format!("insert into t1 values({}, {})", i, i + 1).as_str())?.done()?;
database
.run(format!("insert into t1 values({}, {})", i, i + 1).as_str())?
.done()?;
pb.set_position(i + 1);
}
pb.finish_with_message("Insert completed!");
Expand Down Expand Up @@ -106,9 +109,7 @@ fn query_on_execute(c: &mut Criterion) {
let connection = sqlite::open(QUERY_BENCH_SQLITE_PATH.to_owned()).unwrap();
c.bench_function(format!("SQLite: {} by '{}'", name, case).as_str(), |b| {
b.iter(|| {
for row in connection
.prepare(case)
.unwrap() {
for row in connection.prepare(case).unwrap() {
let _ = row.unwrap();
}
})
Expand Down
6 changes: 4 additions & 2 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ impl SimpleQueryHandler for SessionBackend {
let mut guard = self.tx.lock();

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

let mut tuples = Vec::new();
for tuple in iter {
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,
))
}
}
4 changes: 2 additions & 2 deletions src/binder/explain.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::binder::Binder;
use crate::errors::DatabaseError;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;

impl<T: Transaction> Binder<'_, '_, T> {
pub(crate) fn bind_explain(&mut self, plan: LogicalPlan) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::Explain, vec![plan]))
Ok(LogicalPlan::new(Operator::Explain, Childrens::Only(plan)))
}
}
6 changes: 3 additions & 3 deletions src/binder/insert.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::insert::InsertOperator;
use crate::planner::operator::values::ValuesOperator;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::tuple::SchemaRef;
use crate::types::value::DataValue;
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
is_overwrite,
is_mapping_by_name,
}),
vec![values_plan],
Childrens::Only(values_plan),
))
}

Expand All @@ -110,7 +110,7 @@ impl<T: Transaction> Binder<'_, '_, T> {
) -> LogicalPlan {
LogicalPlan::new(
Operator::Values(ValuesOperator { rows, schema_ref }),
vec![],
Childrens::None,
)
}
}
18 changes: 12 additions & 6 deletions src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::planner::operator::insert::InsertOperator;
use crate::planner::operator::join::JoinCondition;
use crate::planner::operator::sort::{SortField, SortOperator};
use crate::planner::operator::union::UnionOperator;
use crate::planner::{LogicalPlan, SchemaOutput};
use crate::planner::{Childrens, LogicalPlan, SchemaOutput};
use crate::storage::Transaction;
use crate::types::tuple::{Schema, SchemaRef};
use crate::types::{ColumnId, LogicalType};
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
orderby: &[OrderByExpr],
) -> Result<LogicalPlan, DatabaseError> {
let mut plan = if select.from.is_empty() {
LogicalPlan::new(Operator::Dummy, vec![])
LogicalPlan::new(Operator::Dummy, Childrens::None)
} else {
let mut plan = self.bind_table_ref(&select.from[0])?;

Expand Down Expand Up @@ -152,7 +152,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
is_overwrite: false,
is_mapping_by_name: true,
}),
vec![plan],
Childrens::Only(plan),
)
}

Expand Down Expand Up @@ -224,7 +224,13 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
.collect_vec();

Ok(self.bind_distinct(
LogicalPlan::new(union_op, vec![left_plan, right_plan]),
LogicalPlan::new(
union_op,
Childrens::Twins {
left: left_plan,
right: right_plan,
},
),
distinct_exprs,
))
}
Expand Down Expand Up @@ -669,7 +675,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {

Ok(LogicalPlan::new(
Operator::Project(ProjectOperator { exprs: select_list }),
vec![children],
Childrens::Only(children),
))
}

Expand All @@ -681,7 +687,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
sort_fields,
limit: None,
}),
vec![children],
Childrens::Only(children),
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/binder/show.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::binder::Binder;
use crate::errors::DatabaseError;
use crate::planner::operator::Operator;
use crate::planner::LogicalPlan;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;

impl<T: Transaction> Binder<'_, '_, T> {
pub(crate) fn bind_show_tables(&mut self) -> Result<LogicalPlan, DatabaseError> {
Ok(LogicalPlan::new(Operator::Show, vec![]))
Ok(LogicalPlan::new(Operator::Show, Childrens::None))
}
}
4 changes: 2 additions & 2 deletions src/binder/truncate.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::truncate::TruncateOperator;
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::Truncate(TruncateOperator { table_name }),
vec![],
Childrens::None,
))
}
}
Loading

0 comments on commit 4013547

Please sign in to comment.