Skip to content

Commit

Permalink
fix: Minimal Range and Is/IsNot support
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Sep 1, 2024
1 parent c173606 commit 3db4d47
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
31 changes: 30 additions & 1 deletion lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lykiadb_lang::ast::expr::{Expr, Operation};
use lykiadb_lang::ast::expr::{Expr, Operation, RangeKind};
use lykiadb_lang::ast::stmt::Stmt;
use lykiadb_lang::ast::visitor::{ExprEvaluator, VisitorMut};
use lykiadb_lang::parser::program::Program;
Expand Down Expand Up @@ -478,6 +478,35 @@ impl VisitorMut<RV, HaltReason> for Interpreter {

Ok(callable)
}
Expr::Range { lower, upper, subject, kind, span, id } => {
let lower_eval = self.visit_expr(lower)?;
let upper_eval = self.visit_expr(upper)?;
let subject_eval = self.visit_expr(subject)?;

if let (RV::Num(lower_num), RV::Num(upper_num), RV::Num(subject_num)) =
(&lower_eval, &upper_eval, &subject_eval)
{
match kind {
RangeKind::Between => {
Ok(RV::Bool(lower_num <= subject_num && subject_num <= upper_num))
}
RangeKind::NotBetween => {
Ok(RV::Bool(lower_num > subject_num || subject_num > upper_num))
}
}
} else {
Err(HaltReason::Error(
InterpretError::Other {
message: format!(
//TODO: Maybe with dates and strings too?
"Range can only be created with numbers. {:?} {:?} {:?}",
lower_eval, upper_eval, subject_eval
),
}
.into(),
))
}
}
Expr::Get {
object,
name,
Expand Down
23 changes: 17 additions & 6 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use lykiadb_lang::{
use lykiadb_lang::
ast::{
expr::Expr,
sql::{SqlFrom, SqlJoinType, SqlSelect},
},
Identifier,
};
use serde_json::Value;

};
use crate::engine::interpreter::HaltReason;

use super::{Node, Plan};
Expand Down Expand Up @@ -40,9 +36,24 @@ impl Planner {

fn build_select(&mut self, query: &SqlSelect) -> Result<Node, HaltReason> {
let mut node: Node = Node::Nothing;
// FROM/JOIN
if let Some(from) = &query.core.from {
node = self.build_from(from)?;
}
// WHERE
if let Some(where_clause) = &query.core.r#where {
// TODO: Traverse expression
node = Node::Filter {
source: Box::new(node),
predicate: *where_clause.clone(),
}

}
// GROUP BY
// HAVING
// SELECT
// ORDER BY
// LIMIT/OFFSET
Ok(node)
}

Expand Down
11 changes: 9 additions & 2 deletions lykiadb-server/src/value/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ pub fn eval_binary(left_eval: RV, right_eval: RV, operation: Operation) -> RV {
- Add support for function operations
*/
match operation {
Operation::IsEqual => RV::Bool(left_eval == right_eval),
Operation::IsNotEqual => RV::Bool(left_eval != right_eval),
Operation::Is | Operation::IsEqual => RV::Bool(left_eval == right_eval),
Operation::IsNot | Operation::IsNotEqual => RV::Bool(left_eval != right_eval),
Operation::Less => RV::Bool(left_eval < right_eval),
Operation::LessEqual => RV::Bool(left_eval <= right_eval),
Operation::Greater => RV::Bool(left_eval > right_eval),
Expand All @@ -375,6 +375,13 @@ pub fn eval_binary(left_eval: RV, right_eval: RV, operation: Operation) -> RV {
Operation::Subtract => left_eval - right_eval,
Operation::Multiply => left_eval * right_eval,
Operation::Divide => left_eval / right_eval,
// TODO: Implement operations:
/*
Operation::Like
Operation::NotLike
Operation::In
Operation::NotIn
*/
_ => RV::Undefined,
}
}
Expand Down

0 comments on commit 3db4d47

Please sign in to comment.