Skip to content

Commit

Permalink
chore(vm): Pass database address to the compiler (#637)
Browse files Browse the repository at this point in the history
The database address will be needed to optimize joins.
In particular it will be used to look up table size metrics.
  • Loading branch information
joshua-spacetime authored Dec 8, 2023
1 parent 09b2d0f commit f276b25
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
8 changes: 4 additions & 4 deletions crates/core/src/sql/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn compile_sql(db: &RelationalDB, tx: &MutTxId, sql_text: &str) -> Result<Ve
let mut results = Vec::with_capacity(ast.len());

for sql in ast {
results.push(compile_statement(sql).map_err(|error| DBError::Plan {
results.push(compile_statement(db, sql).map_err(|error| DBError::Plan {
sql: sql_text.to_string(),
error,
})?);
Expand Down Expand Up @@ -248,7 +248,7 @@ fn compile_drop(name: String, kind: DbType, table_access: StAccess) -> Result<Cr
}

/// Compiles a `SQL` clause
fn compile_statement(statement: SqlAst) -> Result<CrudExpr, PlanError> {
fn compile_statement(db: &RelationalDB, statement: SqlAst) -> Result<CrudExpr, PlanError> {
let q = match statement {
SqlAst::Select {
from,
Expand All @@ -270,7 +270,7 @@ fn compile_statement(statement: SqlAst) -> Result<CrudExpr, PlanError> {
} => compile_drop(name, kind, table_access)?,
};

Ok(q.optimize())
Ok(q.optimize(Some(db.address())))
}

#[cfg(test)]
Expand Down Expand Up @@ -1052,7 +1052,7 @@ mod tests {

// Optimize the query plan for the incremental update.
let expr = query::to_mem_table(expr, &insert);
let expr = expr.optimize();
let expr = expr.optimize(Some(db.address()));

let QueryExpr {
source:
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/subscription/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ impl<'a> IncrementalJoin<'a> {
let mut inserts = {
// Replan query after replacing left table with virtual table,
// since join order may need to be reversed.
let lhs_virt = query::to_mem_table(self.expr.clone(), &self.lhs.inserts()).optimize();
let lhs_virt = query::to_mem_table(self.expr.clone(), &self.lhs.inserts()).optimize(Some(db.address()));
let rhs_virt = self.to_mem_table_rhs(self.rhs.inserts());

// {A+ join B}
Expand All @@ -617,7 +617,7 @@ impl<'a> IncrementalJoin<'a> {
let mut deletes = {
// Replan query after replacing left table with virtual table,
// since join order may need to be reversed.
let lhs_virt = query::to_mem_table(self.expr.clone(), &self.lhs.deletes()).optimize();
let lhs_virt = query::to_mem_table(self.expr.clone(), &self.lhs.deletes()).optimize(Some(db.address()));
let rhs_virt = self.to_mem_table_rhs(self.rhs.deletes());

// {A- join B}
Expand Down
5 changes: 5 additions & 0 deletions crates/core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use std::ops::RangeBounds;

use itertools::Itertools;
use spacetimedb_lib::Address;
use tracing::debug;

use crate::db::cursor::{CatalogCursor, IndexCursor, TableCursor};
Expand Down Expand Up @@ -395,6 +396,10 @@ impl<'db, 'tx> DbProgram<'db, 'tx> {
}

impl ProgramVm for DbProgram<'_, '_> {
fn address(&self) -> Option<Address> {
Some(self.db.address())
}

fn env(&self) -> &EnvDb {
&self.env
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn build_typed<P: ProgramVm>(p: &mut P, node: Expr) -> ExprOpt {
}
}
Expr::Crud(q) => {
let q = q.optimize();
let q = q.optimize(p.address());
match q {
CrudExpr::Query(q) => {
let source = build_query_opt(q);
Expand Down
14 changes: 7 additions & 7 deletions crates/vm/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::errors::{ErrorKind, ErrorLang, ErrorType, ErrorVm};
use crate::functions::{FunDef, Param};
use crate::operator::{Op, OpCmp, OpLogic, OpQuery};
use crate::types::Ty;
use spacetimedb_lib::Identity;
use spacetimedb_lib::{Address, Identity};
use spacetimedb_primitives::*;
use spacetimedb_sats::algebraic_type::AlgebraicType;
use spacetimedb_sats::algebraic_value::AlgebraicValue;
Expand Down Expand Up @@ -476,9 +476,9 @@ pub enum CrudExpr {
}

impl CrudExpr {
pub fn optimize(self) -> Self {
pub fn optimize(self, db: Option<Address>) -> Self {
match self {
CrudExpr::Query(x) => CrudExpr::Query(x.optimize()),
CrudExpr::Query(x) => CrudExpr::Query(x.optimize(db)),
_ => self,
}
}
Expand Down Expand Up @@ -1129,7 +1129,7 @@ impl QueryExpr {
//
// Ex. SELECT Left.* FROM Left JOIN Right ON Left.id = Right.id ...
// where `Left` has an index defined on `id`.
fn try_index_join(mut query: QueryExpr) -> QueryExpr {
fn try_index_join(mut query: QueryExpr, _db: Option<Address>) -> QueryExpr {
// We expect 2 and only 2 operations - a join followed by a wildcard projection.
if query.query.len() != 2 {
return query;
Expand Down Expand Up @@ -1325,7 +1325,7 @@ impl QueryExpr {
})
}

pub fn optimize(self) -> Self {
pub fn optimize(self, db: Option<Address>) -> Self {
let mut q = Self {
source: self.source.clone(),
query: Vec::with_capacity(self.query.len()),
Expand All @@ -1350,11 +1350,11 @@ impl QueryExpr {
Query::Select(op) => {
q = Self::optimize_select(q, op, &tables);
}
Query::JoinInner(join) => q = q.with_join_inner(join.rhs.optimize(), join.col_lhs, join.col_rhs),
Query::JoinInner(join) => q = q.with_join_inner(join.rhs.optimize(db), join.col_lhs, join.col_rhs),
_ => q.query.push(query),
};
}
Self::try_index_join(q)
Self::try_index_join(q, db)
}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/vm/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! It carries an [EnvDb] with the functions, idents, types.
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::Address;
use spacetimedb_sats::relation::{MemTable, RelIter, Relation, Table};
use std::collections::HashMap;

Expand Down Expand Up @@ -66,6 +67,7 @@ pub trait ProgramVm {
env.functions.ops = ops
}

fn address(&self) -> Option<Address>;
fn env(&self) -> &EnvDb;
fn env_mut(&mut self) -> &mut EnvDb;
fn ctx(&self) -> &dyn ProgramVm;
Expand Down Expand Up @@ -151,6 +153,10 @@ impl Program {
}

impl ProgramVm for Program {
fn address(&self) -> Option<Address> {
None
}

fn env(&self) -> &EnvDb {
&self.env
}
Expand Down

0 comments on commit f276b25

Please sign in to comment.