Skip to content

Commit

Permalink
WIP Refs and lifetimes...
Browse files Browse the repository at this point in the history
  • Loading branch information
PakhomovAlexander committed Nov 23, 2024
1 parent cce1626 commit 4f88273
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/catalog/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(dead_code)]
use std::collections::HashMap;

#[derive(Clone)]
pub struct Catalog {
store: MemoryCatalogStore,
}
Expand Down
66 changes: 33 additions & 33 deletions src/optimizer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#![allow(dead_code)]

use core::panic;
use std::collections::HashMap;
use std::{collections::HashMap, rc::Rc};

use crate::{
analyzer::{LogicalNode, LogicalPlan, Operator},
catalog::{Catalog, ColumnSchema, TableId},
};

struct Optimizer {
catalog: Catalog,
storage: StorageEngine,
struct Optimizer<'a> {
catalog: &'a Catalog,
storage: &'a StorageEngine,
}

impl Optimizer {
impl Optimizer<'_> {
pub fn optimize(&self, l_plan: LogicalPlan) -> PhysicalPlan {
let root_node = l_plan.root;

let mut builder = PhysicalPlanBuilder {
catalog: self.catalog.clone(),
storage: self.storage.clone(),
catalog: &self.catalog,
storage: &self.storage,
};

let root_op = builder.walk(&root_node);
Expand All @@ -30,17 +30,17 @@ impl Optimizer {
}
}

pub fn new(catalog: Catalog, storage: StorageEngine) -> Optimizer {
pub fn new(catalog: &Catalog, storage: &StorageEngine) -> Self {
Optimizer { catalog, storage }
}
}

struct PhysicalPlanBuilder {
catalog: Catalog,
storage: StorageEngine,
struct PhysicalPlanBuilder<'a> {
catalog: &'a Catalog,
storage: &'a StorageEngine,
}

impl PhysicalPlanBuilder {
impl PhysicalPlanBuilder<'_> {
fn walk(&mut self, node: &LogicalNode) -> Vec<Op> {
match &node.op {
Operator::Projec(info) => {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl PhysicalPlanBuilder {
vec![Op::FullScan(
FullScanInfo {
name: info.table.table_name.clone(),
engine: self.storage.clone(),
engine: &self.storage,
state: FullScanState {
curr_pos: 0,
iterator: None,
Expand All @@ -88,15 +88,15 @@ impl PhysicalPlanBuilder {
}

#[derive(Debug, PartialEq, Clone)]
enum Op {
Project(ProjectInfo, Vec<Op>),
Filter(FilterInfo, Vec<Op>),
FullScan(FullScanInfo, Vec<Op>),
enum Op<'a> {
Project(ProjectInfo, Vec<Op<'a>>),
Filter(FilterInfo, Vec<Op<'a>>),
FullScan(FullScanInfo<'a>, Vec<Op<'a>>),
}

/// Vulcano pipiline model
impl Op {
fn full_scan(table_name: &str, engine: StorageEngine) -> Op {
impl Op<'_> {
fn full_scan(table_name: &str, engine: &StorageEngine) -> Op<'_> {
Op::FullScan(
FullScanInfo {
name: table_name.to_string(),
Expand Down Expand Up @@ -213,7 +213,7 @@ enum Val {
Null,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq)] // TODO: remove PartialEq
enum StorageEngine {
Memory(MemoryStorageEngine),
}
Expand Down Expand Up @@ -263,10 +263,10 @@ impl PhysicalPlan {
}

#[derive(Debug, PartialEq, Clone)]
struct FullScanInfo {
struct FullScanInfo<'a> {
name: String,
state: FullScanState,
engine: StorageEngine,
engine: &'a StorageEngine,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -298,7 +298,7 @@ struct FilterInfo {}

#[cfg(test)]
mod tests {
use std::vec;
use std::{cell::RefCell, rc::Rc, vec};

use crate::{
analyzer::{Analyzer, LogicalPlan},
Expand All @@ -321,17 +321,17 @@ mod tests {
fn simple_test() {
let l_plan = analyze("SELECT col1 FROM table1");

let mut catalog = Catalog::mem();
let storage = StorageEngine::mem();
let mut catalog = RefCell::new(Catalog::mem());
let storage = RefCell::new(StorageEngine::mem());

let ts = TableSchemaBuilder::public()
.table("table1")
.col("col1", DataType::Int)
.build();
let _ = catalog.register_table(&ts);
let _ = catalog.borrow_mut().register_table(&ts);
let cs = ts.get_column("col1").unwrap();

let optimizer = Optimizer::new(catalog.clone(), storage.clone());
let optimizer = Optimizer::new(&catalog.borrow(), &storage.borrow());

let p_plan = optimizer.optimize(l_plan);

Expand All @@ -340,7 +340,7 @@ mod tests {
PhysicalPlan {
root: Op::project(
vec![Column::new(&cs)],
vec![Op::full_scan("table1", storage)]
vec![Op::full_scan("table1", &storage.borrow())]
)
}
);
Expand All @@ -350,16 +350,16 @@ mod tests {
fn execute_pipeline() {
let l_plan = analyze("SELECT col1 FROM table1");

let mut catalog = Catalog::mem();
let mut storage = StorageEngine::mem();
let mut catalog = RefCell::new(Catalog::mem());
let mut storage = RefCell::new(StorageEngine::mem());

let ts = TableSchemaBuilder::public()
.table("table1")
.col("col1", DataType::Int)
.build();
let _ = catalog.register_table(&ts);
let _ = catalog.borrow_mut().register_table(&ts);

storage.insert(
storage.borrow_mut().insert(
"table1",
vec![
Tuple::new(vec![("col1", Val::Int(1))]),
Expand All @@ -369,7 +369,7 @@ mod tests {
],
);

let optimizer = Optimizer::new(catalog, storage);
let optimizer = Optimizer::new(&catalog.borrow(), &storage.borrow());

let mut p_plan = optimizer.optimize(l_plan);

Expand Down

0 comments on commit 4f88273

Please sign in to comment.