Skip to content

Commit 55f367e

Browse files
authored
refactor: remove HepGraph, and directly modify LogicalPlan to reduce transformation overhead during optimization (#298)
1 parent 7fdb7d0 commit 55f367e

24 files changed

Lines changed: 928 additions & 1001 deletions

Cargo.lock

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "kite_sql"
5-
version = "0.1.4"
5+
version = "0.1.5"
66
edition = "2021"
77
authors = ["Kould <kould2333@gmail.com>", "Xwg <loloxwg@gmail.com>"]
88
description = "SQL as a Function for Rust"
@@ -48,7 +48,6 @@ itertools = { version = "0.12" }
4848
ordered-float = { version = "4", features = ["serde"] }
4949
paste = { version = "1" }
5050
parking_lot = { version = "0.12", features = ["arc_lock"] }
51-
petgraph = { version = "0.6" }
5251
recursive = { version = "0.1" }
5352
regex = { version = "1" }
5453
rust_decimal = { version = "1" }

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test-wasm:
1919

2020
## Run the sqllogictest harness against the configured .slt suite.
2121
test-slt:
22-
$(CARGO) run -p sqllogictest-test -- --path $(SQLLOGIC_PATH)
22+
$(CARGO) run -p sqllogictest-test -- --path "$(SQLLOGIC_PATH)"
2323

2424
## Convenience target to run every suite in sequence.
2525
test-all: test test-wasm test-slt

examples/hello_world.rs

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,81 +12,85 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#![cfg(not(target_arch = "wasm32"))]
15+
#[cfg(not(target_arch = "wasm32"))]
16+
mod app {
17+
use kite_sql::db::{DataBaseBuilder, ResultIter};
18+
use kite_sql::errors::DatabaseError;
19+
use kite_sql::implement_from_tuple;
20+
use kite_sql::types::value::DataValue;
1621

17-
use kite_sql::db::{DataBaseBuilder, ResultIter};
18-
use kite_sql::errors::DatabaseError;
19-
use kite_sql::implement_from_tuple;
20-
use kite_sql::types::value::DataValue;
21-
22-
#[derive(Default, Debug, PartialEq)]
23-
struct MyStruct {
24-
pub c1: i32,
25-
pub c2: String,
26-
}
22+
#[derive(Default, Debug, PartialEq)]
23+
pub struct MyStruct {
24+
pub c1: i32,
25+
pub c2: String,
26+
}
2727

28-
implement_from_tuple!(
29-
MyStruct, (
30-
c1: i32 => |inner: &mut MyStruct, value| {
31-
if let DataValue::Int32(val) = value {
32-
inner.c1 = val;
28+
implement_from_tuple!(
29+
MyStruct, (
30+
c1: i32 => |inner: &mut MyStruct, value| {
31+
if let DataValue::Int32(val) = value {
32+
inner.c1 = val;
33+
}
34+
},
35+
c2: String => |inner: &mut MyStruct, value| {
36+
if let DataValue::Utf8 { value, .. } = value {
37+
inner.c2 = value;
38+
}
3339
}
34-
},
35-
c2: String => |inner: &mut MyStruct, value| {
36-
if let DataValue::Utf8 { value, .. } = value {
37-
inner.c2 = value;
38-
}
39-
}
40-
)
41-
);
40+
)
41+
);
4242

43-
#[cfg(feature = "macros")]
44-
fn main() -> Result<(), DatabaseError> {
45-
let database = DataBaseBuilder::path("./example_data/hello_world").build()?;
43+
pub fn run() -> Result<(), DatabaseError> {
44+
let database = DataBaseBuilder::path("./example_data/hello_world").build()?;
4645

47-
// 1) Create table and insert multiple rows with mixed types.
48-
database
49-
.run(
50-
"create table if not exists my_struct (
51-
c1 int primary key,
52-
c2 varchar,
53-
c3 int
54-
)",
55-
)?
56-
.done()?;
57-
database
58-
.run(
59-
r#"
60-
insert into my_struct values
61-
(0, 'zero', 0),
62-
(1, 'one', 1),
63-
(2, 'two', 2)
64-
"#,
65-
)?
66-
.done()?;
46+
database
47+
.run(
48+
"create table if not exists my_struct (
49+
c1 int primary key,
50+
c2 varchar,
51+
c3 int
52+
)",
53+
)?
54+
.done()?;
55+
database
56+
.run(
57+
r#"
58+
insert into my_struct values
59+
(0, 'zero', 0),
60+
(1, 'one', 1),
61+
(2, 'two', 2)
62+
"#,
63+
)?
64+
.done()?;
6765

68-
// 2) Update and delete demo.
69-
database
70-
.run("update my_struct set c3 = c3 + 10 where c1 = 1")?
71-
.done()?;
72-
database.run("delete from my_struct where c1 = 2")?.done()?;
66+
database
67+
.run("update my_struct set c3 = c3 + 10 where c1 = 1")?
68+
.done()?;
69+
database.run("delete from my_struct where c1 = 2")?.done()?;
7370

74-
// 3) Query and deserialize into Rust struct.
75-
let iter = database.run("select * from my_struct")?;
76-
let schema = iter.schema().clone();
71+
let iter = database.run("select * from my_struct")?;
72+
let schema = iter.schema().clone();
7773

78-
for tuple in iter {
79-
println!("{:?}", MyStruct::from((&schema, tuple?)));
80-
}
74+
for tuple in iter {
75+
println!("{:?}", MyStruct::from((&schema, tuple?)));
76+
}
77+
78+
let mut agg = database.run("select count(*) from my_struct")?;
79+
if let Some(count_row) = agg.next() {
80+
println!("row count = {:?}", count_row?);
81+
}
82+
agg.done()?;
8183

82-
// 4) Aggregate example.
83-
let mut agg = database.run("select count(*) from my_struct")?;
84-
if let Some(count_row) = agg.next() {
85-
println!("row count = {:?}", count_row?);
84+
database.run("drop table my_struct")?.done()?;
85+
86+
Ok(())
8687
}
87-
agg.done()?;
88+
}
8889

89-
database.run("drop table my_struct")?.done()?;
90+
#[cfg(target_arch = "wasm32")]
91+
fn main() {}
9092

91-
Ok(())
93+
#[cfg(all(not(target_arch = "wasm32"), feature = "macros"))]
94+
fn main() -> Result<(), kite_sql::errors::DatabaseError> {
95+
app::run()
9296
}

examples/transaction.rs

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,62 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#![cfg(not(target_arch = "wasm32"))]
15+
#[cfg(not(target_arch = "wasm32"))]
16+
mod app {
17+
use kite_sql::db::{DataBaseBuilder, ResultIter};
18+
use kite_sql::errors::DatabaseError;
19+
use kite_sql::types::tuple::Tuple;
20+
use kite_sql::types::value::DataValue;
1621

17-
use kite_sql::db::{DataBaseBuilder, ResultIter};
18-
use kite_sql::errors::DatabaseError;
19-
use kite_sql::types::tuple::Tuple;
20-
use kite_sql::types::value::DataValue;
22+
pub fn run() -> Result<(), DatabaseError> {
23+
let database = DataBaseBuilder::path("./example_data/transaction").build_optimistic()?;
24+
database
25+
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
26+
.done()?;
27+
let mut transaction = database.new_transaction()?;
2128

22-
fn main() -> Result<(), DatabaseError> {
23-
let database = DataBaseBuilder::path("./example_data/transaction").build_optimistic()?;
24-
database
25-
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
26-
.done()?;
27-
let mut transaction = database.new_transaction()?;
29+
transaction
30+
.run("insert into t1 values(0, 0), (1, 1)")?
31+
.done()?;
2832

29-
transaction
30-
.run("insert into t1 values(0, 0), (1, 1)")?
31-
.done()?;
33+
assert!(database.run("select * from t1")?.next().is_none());
3234

33-
assert!(database.run("select * from t1")?.next().is_none());
35+
transaction.commit()?;
3436

35-
transaction.commit()?;
37+
let mut iter = database.run("select * from t1")?;
38+
assert_eq!(
39+
iter.next().unwrap()?,
40+
Tuple::new(None, vec![DataValue::Int32(0), DataValue::Int32(0)])
41+
);
42+
assert_eq!(
43+
iter.next().unwrap()?,
44+
Tuple::new(None, vec![DataValue::Int32(1), DataValue::Int32(1)])
45+
);
46+
assert!(iter.next().is_none());
3647

37-
let mut iter = database.run("select * from t1")?;
38-
assert_eq!(
39-
iter.next().unwrap()?,
40-
Tuple::new(None, vec![DataValue::Int32(0), DataValue::Int32(0)])
41-
);
42-
assert_eq!(
43-
iter.next().unwrap()?,
44-
Tuple::new(None, vec![DataValue::Int32(1), DataValue::Int32(1)])
45-
);
46-
assert!(iter.next().is_none());
48+
let mut tx2 = database.new_transaction()?;
49+
tx2.run("update t1 set c2 = 99 where c1 = 0")?.done()?;
50+
assert_eq!(
51+
database
52+
.run("select c2 from t1 where c1 = 0")?
53+
.next()
54+
.unwrap()?
55+
.values[0]
56+
.i32(),
57+
Some(0)
58+
);
59+
drop(tx2);
4760

48-
// Scenario: another transaction updates but does not commit; changes stay invisible.
49-
let mut tx2 = database.new_transaction()?;
50-
tx2.run("update t1 set c2 = 99 where c1 = 0")?.done()?;
51-
assert_eq!(
52-
database
53-
.run("select c2 from t1 where c1 = 0")?
54-
.next()
55-
.unwrap()?
56-
.values[0]
57-
.i32(),
58-
Some(0)
59-
);
60-
// rollback
61-
drop(tx2);
61+
database.run("drop table t1")?.done()?;
62+
63+
Ok(())
64+
}
65+
}
6266

63-
database.run("drop table t1")?.done()?;
67+
#[cfg(target_arch = "wasm32")]
68+
fn main() {}
6469

65-
Ok(())
70+
#[cfg(not(target_arch = "wasm32"))]
71+
fn main() -> Result<(), kite_sql::errors::DatabaseError> {
72+
app::run()
6673
}

src/db.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,8 @@ impl<S: Storage> State<S> {
211211
/// Limit(1)
212212
/// Project(a,b)
213213
let source_plan = binder.bind(stmt)?;
214-
// println!("source_plan plan: {:#?}", source_plan);
215-
216214
let best_plan = Self::default_optimizer(source_plan)
217215
.find_best(Some(&transaction.meta_loader(meta_cache)))?;
218-
// println!("best_plan plan: {:#?}", best_plan);
219216

220217
Ok(best_plan)
221218
}
@@ -356,7 +353,7 @@ impl<S: Storage> Database<S> {
356353
self.state.prepare(sql)
357354
}
358355

359-
fn execute<A: AsRef<[(&'static str, DataValue)]>>(
356+
pub fn execute<A: AsRef<[(&'static str, DataValue)]>>(
360357
&self,
361358
statement: &Statement,
362359
params: A,

0 commit comments

Comments
 (0)