Skip to content

Commit

Permalink
feat: Minimal planning
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Aug 18, 2024
1 parent 45b338d commit ad70dc1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lykiadb-lang/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use super::{
stmt::Stmt,
};

pub trait ExprEvaluator<O, E> {
fn eval(&mut self, e: &Expr) -> Result<O, E>;
}

pub trait Visitor<O, E> {
fn visit_expr(&self, e: &Expr) -> Result<O, E>;
fn visit_stmt(&self, s: &Stmt) -> Result<O, E>;
Expand Down
22 changes: 12 additions & 10 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lykiadb_lang::ast::expr::{Expr, Operation};
use lykiadb_lang::ast::stmt::Stmt;
use lykiadb_lang::ast::visitor::VisitorMut;
use lykiadb_lang::ast::visitor::{ExprEvaluator, VisitorMut};
use lykiadb_lang::parser::program::Program;
use lykiadb_lang::parser::resolver::Resolver;
use lykiadb_lang::parser::Parser;
Expand Down Expand Up @@ -71,12 +71,6 @@ pub enum InterpretError {
span: Span,
property: String,
},
/*AssignmentToUndefined {
token: Token,
},
VariableNotFound {
token: Token,
},*/
Other {
message: String,
}, // TODO(vck): Refactor this
Expand Down Expand Up @@ -180,7 +174,12 @@ pub struct Interpreter {
env_man: Shared<Environment>,
source_processor: SourceProcessor,
current_program: Option<Arc<Program>>,
planner: Planner
}

impl ExprEvaluator<RV, HaltReason> for Interpreter {
fn eval(&mut self, e: &Expr) -> Result<RV, HaltReason> {
self.visit_expr(e)
}
}

impl Interpreter {
Expand All @@ -193,7 +192,6 @@ impl Interpreter {
loop_stack: LoopStack::new(),
source_processor: SourceProcessor::new(),
current_program: None,
planner: Planner::new()
}
}

Expand Down Expand Up @@ -328,7 +326,11 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
Expr::Select { .. } |
Expr::Insert { .. } |
Expr::Update { .. } |
Expr::Delete { .. } => /*self.planner.build(&e)*/ Ok(RV::NaN),
Expr::Delete { .. } => {
let mut planner = Planner::new();
let plan = planner.build(e)?;
Ok(RV::Undefined)
},
Expr::Literal {
value,
raw: _,
Expand Down
1 change: 0 additions & 1 deletion lykiadb-server/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ impl TcpConnection {

pub async fn read(&mut self) -> Result<Option<Message>, CommunicationError> {
loop {
// TODO(vck): Replace .to_vec call with something cheaper
if let Ok(parsed) = bson::from_slice::<Message>(&self.read_buffer) {
self.read_buffer.clear();
return Ok(Some(parsed));
Expand Down
42 changes: 42 additions & 0 deletions lykiadb-server/src/plan/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use lykiadb_lang::ast::sql::SqlExpr;
use serde::{Deserialize, Serialize};
pub mod planner;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Aggregate {
Average(SqlExpr),
Count(SqlExpr),
Max(SqlExpr),
Min(SqlExpr),
Sum(SqlExpr),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Direction {
Ascending,
Descending,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Plan {
Select(Node)
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Node {
Aggregate { source: Box<Node>, group_by: Vec<SqlExpr>, aggregates: Vec<Aggregate> },

Filter { source: Box<Node>, predicate: SqlExpr },

Projection { source: Box<Node>, expressions: Vec<SqlExpr>, aliases: Vec<String> },

Scan { collection: String, filter: Option<SqlExpr>, alias: Option<String> },

Limit { source: Box<Node>, limit: usize },

Offset { source: Box<Node>, offset: usize },

Order { source: Box<Node>, key: Vec<(SqlExpr, Direction)> },

Values { rows: Vec<Vec<SqlExpr>> },
}
28 changes: 28 additions & 0 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use lykiadb_lang::ast::{expr::Expr, sql::SqlSelect, visitor::ExprEvaluator};

use crate::{engine::interpreter::HaltReason, value::types::RV};

use super::{Node, Plan};
pub struct Planner;

impl Planner {
pub fn new() -> Planner {
Planner {}
}

pub fn build(&mut self, expr: &Expr) -> Result<Plan, HaltReason> {
match expr {
Expr::Select {
query,
span: _,
id: _ } => {
self.build_select(query)
},
_ => panic!("Not implemented yet.")
}
}

fn build_select(&mut self, query: &SqlSelect) -> Result<Plan, HaltReason> {
Ok(Plan::Select(Node::Values { rows: vec![vec![]] }))
}
}
Empty file removed lykiadb-server/src/plan/tree.rs
Empty file.
6 changes: 6 additions & 0 deletions lykiadb-server/src/value/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl Default for Environment {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EnvironmentError {
/*AssignmentToUndefined {
token: Token,
},
VariableNotFound {
token: Token,
},*/
Other {
message: String,
}
Expand Down

0 comments on commit ad70dc1

Please sign in to comment.