Skip to content

Commit d75ef7c

Browse files
committed
Integrate new query plan repr into core
1 parent c90bf2e commit d75ef7c

File tree

13 files changed

+935
-466
lines changed

13 files changed

+935
-466
lines changed

crates/core/src/host/instance_env.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use spacetimedb_sats::buffer::BufWriter;
2121
use spacetimedb_sats::db::def::{IndexDef, IndexType};
2222
use spacetimedb_sats::relation::{FieldExpr, FieldName};
2323
use spacetimedb_sats::{ProductType, Typespace};
24-
use spacetimedb_vm::expr::{Code, ColumnOp};
24+
use spacetimedb_vm::expr::{Code, ColumnOp, SourceBuilder};
2525

2626
#[derive(Clone)]
2727
pub struct InstanceEnv {
@@ -368,11 +368,16 @@ impl InstanceEnv {
368368
filter,
369369
)
370370
.map_err(NodesError::DecodeFilter)?;
371-
let q = spacetimedb_vm::dsl::query(&*schema).with_select(filter_to_column_op(&schema.table_name, filter));
371+
372+
let mut sources = SourceBuilder::default();
373+
let source_expr = sources.add_table_schema(&schema);
374+
let mut sources = sources.into_source_set();
375+
376+
let q = spacetimedb_vm::dsl::query(source_expr).with_select(filter_to_column_op(&schema.table_name, filter));
372377
//TODO: How pass the `caller` here?
373378
let mut tx: TxMode = tx.into();
374379
let p = &mut DbProgram::new(ctx, stdb, &mut tx, AuthCtx::for_current(self.dbic.identity));
375-
let results = match spacetimedb_vm::eval::run_ast(p, q.into()) {
380+
let results = match spacetimedb_vm::eval::run_ast(p, q.into(), &mut sources) {
376381
Code::Table(table) => table,
377382
_ => unreachable!("query should always return a table"),
378383
};

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66
use spacetimedb_lib::buffer::DecodeError;
77
use spacetimedb_lib::identity::AuthCtx;
88
use spacetimedb_lib::{bsatn, Address, ModuleDef, TableDesc};
9-
use spacetimedb_vm::expr::CrudExpr;
9+
use spacetimedb_vm::expr::{CrudExpr, SourceSet};
1010

1111
use super::instrumentation::CallTimes;
1212
use crate::database_instance_context::DatabaseInstanceContext;
@@ -268,8 +268,9 @@ impl<T: WasmModule> Module for WasmModuleHostActor<T> {
268268
let auth = AuthCtx::new(self.database_instance_context.identity, caller_identity);
269269
log::debug!("One-off query: {query}");
270270
let ctx = &ExecutionContext::sql(db.address());
271-
let compiled = db.with_read_only(ctx, |tx| {
272-
sql::compiler::compile_sql(db, tx, &query)?
271+
let (compiled, mut sources): (Vec<_>, Box<SourceSet>) = db.with_read_only(ctx, |tx| {
272+
let (ast, sources) = sql::compiler::compile_sql_merge_sources(db, tx, &query)?;
273+
let compiled = ast
273274
.into_iter()
274275
.map(|expr| {
275276
if matches!(expr, CrudExpr::Query { .. }) {
@@ -278,10 +279,11 @@ impl<T: WasmModule> Module for WasmModuleHostActor<T> {
278279
Err(anyhow!("One-off queries are not allowed to modify the database"))
279280
}
280281
})
281-
.collect::<Result<_, _>>()
282+
.collect::<Result<_, _>>()?;
283+
Ok::<_, DBError>((compiled, sources))
282284
})?;
283285

284-
sql::execute::execute_sql(db, compiled, auth)
286+
sql::execute::execute_sql(db, compiled, auth, &mut sources)
285287
}
286288

287289
fn clear_table(&self, table_name: String) -> Result<(), anyhow::Error> {

0 commit comments

Comments
 (0)