Skip to content

Commit eaa2861

Browse files
query engine integration
1 parent 10598ba commit eaa2861

File tree

23 files changed

+2220
-1269
lines changed

23 files changed

+2220
-1269
lines changed

Cargo.lock

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919
"crates/paths",
2020
"crates/physical-plan",
2121
"crates/primitives",
22+
"crates/query",
2223
"crates/sats",
2324
"crates/schema",
2425
"crates/sdk",
@@ -106,6 +107,7 @@ spacetimedb-metrics = { path = "crates/metrics", version = "1.0.0-rc2" }
106107
spacetimedb-paths = { path = "crates/paths", version = "1.0.0-rc2" }
107108
spacetimedb-physical-plan = { path = "crates/physical-plan", version = "1.0.0-rc2" }
108109
spacetimedb-primitives = { path = "crates/primitives", version = "1.0.0-rc2" }
110+
spacetimedb-query = { path = "crates/query", version = "1.0.0-rc2" }
109111
spacetimedb-sats = { path = "crates/sats", version = "1.0.0-rc2" }
110112
spacetimedb-schema = { path = "crates/schema", version = "1.0.0-rc2" }
111113
spacetimedb-standalone = { path = "crates/standalone", version = "1.0.0-rc2" }

crates/bench/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ bench = false
3131
spacetimedb-client-api = { path = "../client-api" }
3232
spacetimedb-core = { path = "../core", features = ["test"] }
3333
spacetimedb-data-structures.workspace = true
34+
spacetimedb-execution = { path = "../execution" }
3435
spacetimedb-lib = { path = "../lib" }
3536
spacetimedb-paths.workspace = true
3637
spacetimedb-primitives = { path = "../primitives" }
38+
spacetimedb-query = { path = "../query" }
3739
spacetimedb-sats = { path = "../sats" }
3840
spacetimedb-schema = { workspace = true, features = ["test"] }
3941
spacetimedb-standalone = { path = "../standalone" }

crates/bench/benches/subscription.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ use spacetimedb::execution_context::Workload;
44
use spacetimedb::host::module_host::DatabaseTableUpdate;
55
use spacetimedb::identity::AuthCtx;
66
use spacetimedb::messages::websocket::BsatnFormat;
7+
use spacetimedb::sql::ast::SchemaViewer;
78
use spacetimedb::subscription::query::compile_read_only_query;
89
use spacetimedb::subscription::subscription::ExecutionSet;
910
use spacetimedb::{db::relational_db::RelationalDB, messages::websocket::Compression};
1011
use spacetimedb_bench::database::BenchDatabase as _;
1112
use spacetimedb_bench::spacetime_raw::SpacetimeRaw;
1213
use spacetimedb_primitives::{col_list, TableId};
14+
use spacetimedb_query::SubscribePlan;
1315
use spacetimedb_sats::{product, AlgebraicType, AlgebraicValue, ProductValue};
1416

1517
fn create_table_location(db: &RelationalDB) -> Result<TableId, DBError> {
@@ -99,6 +101,18 @@ fn eval(c: &mut Criterion) {
99101
let ins_rhs = insert_op(rhs, "location", new_rhs_row);
100102
let update = [&ins_lhs, &ins_rhs];
101103

104+
// A benchmark runner for the new query engine
105+
let bench_query = |c: &mut Criterion, name, sql| {
106+
c.bench_function(name, |b| {
107+
let tx = raw.db.begin_tx(Workload::Subscribe);
108+
let auth = AuthCtx::for_testing();
109+
let schema_viewer = &SchemaViewer::new(&raw.db, &tx, &auth);
110+
let plan = SubscribePlan::compile(sql, schema_viewer).unwrap();
111+
112+
b.iter(|| drop(black_box(plan.execute_bsatn(&tx))))
113+
});
114+
};
115+
102116
let bench_eval = |c: &mut Criterion, name, sql| {
103117
c.bench_function(name, |b| {
104118
let tx = raw.db.begin_tx(Workload::Update);
@@ -116,6 +130,19 @@ fn eval(c: &mut Criterion) {
116130
});
117131
};
118132

133+
// Join 1M rows on the left with 12K rows on the right.
134+
// Note, this should use an index join so as not to read the entire footprint table.
135+
let name = format!(
136+
r#"
137+
select f.*
138+
from location l join footprint f on l.entity_id = f.entity_id
139+
where l.chunk_index = {chunk_index}
140+
"#
141+
);
142+
143+
bench_query(c, "footprint-scan", "select * from footprint");
144+
bench_query(c, "footprint-semijoin", &name);
145+
119146
// To profile this benchmark for 30s
120147
// samply record -r 10000000 cargo bench --bench=subscription --profile=profiling -- full-scan --exact --profile-time=30
121148
// Iterate 1M rows.
@@ -124,7 +151,7 @@ fn eval(c: &mut Criterion) {
124151
// To profile this benchmark for 30s
125152
// samply record -r 10000000 cargo bench --bench=subscription --profile=profiling -- full-join --exact --profile-time=30
126153
// Join 1M rows on the left with 12K rows on the right.
127-
// Note, this should use an index join so as not to read the entire lhs table.
154+
// Note, this should use an index join so as not to read the entire footprint table.
128155
let name = format!(
129156
r#"
130157
select footprint.*

crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ spacetimedb-table.workspace = true
2929
spacetimedb-vm.workspace = true
3030
spacetimedb-snapshot.workspace = true
3131
spacetimedb-expr.workspace = true
32+
spacetimedb-execution.workspace = true
3233

3334
anyhow = { workspace = true, features = ["backtrace"] }
3435
arrayvec.workspace = true

crates/core/src/db/datastore/locking_tx_datastore/tx.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use super::{
66
SharedReadGuard,
77
};
88
use crate::execution_context::ExecutionContext;
9+
use spacetimedb_execution::Datastore;
910
use spacetimedb_primitives::{ColList, TableId};
1011
use spacetimedb_sats::AlgebraicValue;
1112
use spacetimedb_schema::schema::TableSchema;
13+
use spacetimedb_table::blob_store::BlobStore;
14+
use spacetimedb_table::table::Table;
1215
use std::num::NonZeroU64;
1316
use std::sync::Arc;
1417
use std::{
@@ -23,6 +26,16 @@ pub struct TxId {
2326
pub(crate) ctx: ExecutionContext,
2427
}
2528

29+
impl Datastore for TxId {
30+
fn blob_store(&self) -> &dyn BlobStore {
31+
&self.committed_state_shared_lock.blob_store
32+
}
33+
34+
fn table(&self, table_id: TableId) -> Option<&Table> {
35+
self.committed_state_shared_lock.get_table(table_id)
36+
}
37+
}
38+
2639
impl StateView for TxId {
2740
fn get_schema(&self, table_id: TableId) -> Option<&Arc<TableSchema>> {
2841
self.committed_state_shared_lock.get_schema(table_id)

crates/core/src/sql/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use crate::db::relational_db::RelationalDB;
33
use crate::sql::ast::SchemaViewer;
44
use spacetimedb_expr::check::parse_and_type_sub;
55
use spacetimedb_expr::errors::TypingError;
6-
use spacetimedb_expr::expr::Project;
6+
use spacetimedb_expr::expr::ProjectName;
77
use spacetimedb_lib::db::raw_def::v9::RawRowLevelSecurityDefV9;
88
use spacetimedb_lib::identity::AuthCtx;
99
use spacetimedb_schema::schema::RowLevelSecuritySchema;
1010

1111
pub struct RowLevelExpr {
12-
pub sql: Project,
12+
pub sql: ProjectName,
1313
pub def: RowLevelSecuritySchema,
1414
}
1515

crates/execution/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ license-file = "LICENSE"
77
description = "The SpacetimeDB query engine"
88

99
[dependencies]
10-
spacetimedb-expr.workspace = true
10+
anyhow.workspace = true
1111
spacetimedb-lib.workspace = true
12+
spacetimedb-physical-plan.workspace = true
1213
spacetimedb-primitives.workspace = true
14+
spacetimedb-sql-parser.workspace = true
1315
spacetimedb-table.workspace = true

0 commit comments

Comments
 (0)