Skip to content

Commit

Permalink
fix: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 6, 2024
1 parent 19bb033 commit 0e819e9
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 80 deletions.
11 changes: 6 additions & 5 deletions lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl<'a> Parser<'a> {

fn expect_get_path(&mut self, initial: Box<Expr>, tok: TokenType) -> ParseResult<Box<Expr>> {
let mut expr = initial;

loop {
if self.match_next(&sym!(LeftParen)) {
expr = self.finish_call(expr)?;
Expand All @@ -600,7 +600,7 @@ impl<'a> Parser<'a> {
} else {
break;
}
};
}

Ok(expr)
}
Expand All @@ -609,11 +609,12 @@ impl<'a> Parser<'a> {
let expr = self.primary()?;

if let Expr::Variable { name, span, id } = expr.as_ref() {
if !name.dollar
{
if !name.dollar {
let next_tok = &self.peek_bw(0).tok_type;

if (next_tok == &sym!(Dot) || next_tok != &sym!(LeftParen)) && self.in_select_depth > 0 {
if (next_tok == &sym!(Dot) || next_tok != &sym!(LeftParen))
&& self.in_select_depth > 0
{
let head = name.clone();
let mut tail: Vec<crate::Identifier> = vec![];
while self.match_next(&sym!(Dot)) {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-lang/src/tokenizer/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'a> Scanner<'a> {

fn scan_double_token(&mut self, start: usize, c: char) -> Token {
self.advance();

if self.match_next(':') && c == ':' {
Token {
tok_type: sym!(DoubleColon),
Expand Down
8 changes: 7 additions & 1 deletion lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,13 @@ impl Output {
}
// TODO(vck): Remove this
pub fn expect_str(&mut self, rv: Vec<String>) {
assert_eq!(self.out.iter().map(|x| x.to_string()).collect::<Vec<String>>(), rv)
assert_eq!(
self.out
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>(),
rv
)
}
}

Expand Down
58 changes: 35 additions & 23 deletions lykiadb-server/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl Runtime {
}

pub mod test_helpers {
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use std::sync::Arc;
use pretty_assertions::assert_eq;

use crate::engine::{Runtime, RuntimeMode, Interpreter, error::ExecutionError};
use crate::engine::{error::ExecutionError, Interpreter, Runtime, RuntimeMode};
use crate::util::{alloc_shared, Shared};
use crate::value::RV;

Expand All @@ -70,17 +70,16 @@ pub mod test_helpers {
pub fn new() -> RuntimeTester {
let out = alloc_shared(Output::new());

RuntimeTester {
RuntimeTester {
out: out.clone(),
runtime: Runtime::new(RuntimeMode::File, Interpreter::new(Some(out), true)),
runtime: Runtime::new(RuntimeMode::File, Interpreter::new(Some(out), true)),
}
}

pub fn test_file(input: &str) {
let parts: Vec<&str> = input.split("#[").collect();

for part in parts[1..].iter() {

let mut tester = RuntimeTester::new();

let directives_and_input = part.trim();
Expand Down Expand Up @@ -117,37 +116,50 @@ pub mod test_helpers {
}

fn run_case(&mut self, case_parts: Vec<String>, flags: HashMap<&str, &str>) {

assert!(case_parts.len() > 1, "Expected at least one input/output pair");

assert!(
case_parts.len() > 1,
"Expected at least one input/output pair"
);

let mut errors: Vec<ExecutionError> = vec![];

let result = self.runtime.interpret(&case_parts[0]);

if let Err(err) = result {
errors.push(err);
}

for part in &case_parts[1..] {
if part.starts_with("err") {
assert_eq!(errors.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("\n"), part[3..].trim());
}

else if part.starts_with('>') {
assert_eq!(
errors
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join("\n"),
part[3..].trim()
);
} else if part.starts_with('>') {
let result = self.runtime.interpret(part[1..].trim());

if let Err(err) = result {
errors.push(err);
}
}
else if flags.get("run") == Some(&"plan") {
} else if flags.get("run") == Some(&"plan") {
// TODO(vck): Remove this
self.out.write().unwrap().expect(vec![RV::Str(Arc::new(part.to_string()))]);
self.out
.write()
.unwrap()
.expect(vec![RV::Str(Arc::new(part.to_string()))]);
} else {
self.out.write().unwrap().expect_str(
part.to_string()
.split('\n')
.map(|x| x.to_string())
.collect(),
);
}
else {
self.out.write().unwrap().expect_str(part.to_string().split('\n').map(|x| x.to_string()).collect());
}
}
}
}
}
}
58 changes: 44 additions & 14 deletions lykiadb-server/src/engine/stdlib/fib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,40 @@ mod tests {
#[test]
fn test_basic_fibonacci() {
let mut interpreter = Interpreter::new(None, true);

// Test first few Fibonacci numbers
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(0.0)]).unwrap(), RV::Num(0.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(1.0)]).unwrap(), RV::Num(1.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(2.0)]).unwrap(), RV::Num(1.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(3.0)]).unwrap(), RV::Num(2.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(4.0)]).unwrap(), RV::Num(3.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(5.0)]).unwrap(), RV::Num(5.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(6.0)]).unwrap(), RV::Num(8.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(7.0)]).unwrap(), RV::Num(13.0));
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(0.0)]).unwrap(),
RV::Num(0.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(1.0)]).unwrap(),
RV::Num(1.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(2.0)]).unwrap(),
RV::Num(1.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(3.0)]).unwrap(),
RV::Num(2.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(4.0)]).unwrap(),
RV::Num(3.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(5.0)]).unwrap(),
RV::Num(5.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(6.0)]).unwrap(),
RV::Num(8.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(7.0)]).unwrap(),
RV::Num(13.0)
);
}

#[test]
Expand All @@ -48,7 +72,7 @@ mod tests {
// Test with non-numeric input
let result = nt_fib(&mut interpreter, &[RV::Bool(true)]);
assert!(result.is_err());

let err = result.unwrap_err();
match err {
HaltReason::Error(e) => {
Expand All @@ -61,9 +85,15 @@ mod tests {
#[test]
fn test_negative_input() {
let mut interpreter = Interpreter::new(None, true);

// Negative numbers should return themselves as per implementation
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(-1.0)]).unwrap(), RV::Num(-1.0));
assert_eq!(nt_fib(&mut interpreter, &[RV::Num(-5.0)]).unwrap(), RV::Num(-5.0));
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(-1.0)]).unwrap(),
RV::Num(-1.0)
);
assert_eq!(
nt_fib(&mut interpreter, &[RV::Num(-5.0)]).unwrap(),
RV::Num(-5.0)
);
}
}
}
36 changes: 25 additions & 11 deletions lykiadb-server/src/engine/stdlib/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod tests {
nt_json_encode(&mut interpreter, &[RV::Num(42.0)]).unwrap(),
RV::Str(Arc::new("42.0".to_string()))
);

assert_eq!(
nt_json_encode(&mut interpreter, &[RV::Bool(true)]).unwrap(),
RV::Str(Arc::new("true".to_string()))
Expand All @@ -79,7 +79,7 @@ mod tests {
arr.push(RV::Num(1.0));
arr.push(RV::Str(Arc::new("test".to_string())));
let array_rv = RV::Array(alloc_shared(arr));

assert_eq!(
nt_json_encode(&mut interpreter, &[array_rv]).unwrap(),
RV::Str(Arc::new("[1.0,\"test\"]".to_string()))
Expand Down Expand Up @@ -113,7 +113,11 @@ mod tests {
);

assert_eq!(
nt_json_decode(&mut interpreter, &[RV::Str(Arc::new("\"hello\"".to_string()))]).unwrap(),
nt_json_decode(
&mut interpreter,
&[RV::Str(Arc::new("\"hello\"".to_string()))]
)
.unwrap(),
RV::Str(Arc::new("hello".to_string()))
);

Expand All @@ -125,9 +129,10 @@ mod tests {
// Test array
let array_result = nt_json_decode(
&mut interpreter,
&[RV::Str(Arc::new("[1.0, \"test\"]".to_string()))]
).unwrap();

&[RV::Str(Arc::new("[1.0, \"test\"]".to_string()))],
)
.unwrap();

if let RV::Array(arr) = array_result {
let arr = arr.read().unwrap();
assert_eq!(arr.len(), 2);
Expand All @@ -140,14 +145,20 @@ mod tests {
// Test object
let object_result = nt_json_decode(
&mut interpreter,
&[RV::Str(Arc::new("{\"key\": 123.0, \"msg\": \"value\"}".to_string()))]
).unwrap();
&[RV::Str(Arc::new(
"{\"key\": 123.0, \"msg\": \"value\"}".to_string(),
))],
)
.unwrap();

if let RV::Object(obj) = object_result {
let obj = obj.read().unwrap();
assert_eq!(obj.len(), 2);
assert_eq!(obj.get("key").unwrap(), &RV::Num(123.0));
assert_eq!(obj.get("msg").unwrap(), &RV::Str(Arc::new("value".to_string())));
assert_eq!(
obj.get("msg").unwrap(),
&RV::Str(Arc::new("value".to_string()))
);
} else {
panic!("Expected object result");
}
Expand All @@ -159,8 +170,11 @@ mod tests {
));

assert!(matches!(
nt_json_decode(&mut interpreter, &[RV::Str(Arc::new("invalid json".to_string()))]),
nt_json_decode(
&mut interpreter,
&[RV::Str(Arc::new("invalid json".to_string()))]
),
Err(HaltReason::Error(_))
));
}
}
}
4 changes: 1 addition & 3 deletions lykiadb-server/src/plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ pub enum Aggregate {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum IntermediateExpr {
Constant(RV),
Expr {
expr: Expr,
},
Expr { expr: Expr },
}

impl Display for IntermediateExpr {
Expand Down
22 changes: 9 additions & 13 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use lykiadb_lang::{
Spanned,
};

use super::{scope::Scope,
IntermediateExpr, Node, Plan, PlannerError};
use super::{scope::Scope, IntermediateExpr, Node, Plan, PlannerError};

pub struct Planner<'a> {
interpreter: &'a mut Interpreter,
Expand Down Expand Up @@ -49,8 +48,7 @@ impl<'a> Planner<'a> {

// WHERE
if let Some(predicate) = &core.r#where {
let (expr, subqueries): (
IntermediateExpr, Vec<Node>) =
let (expr, subqueries): (IntermediateExpr, Vec<Node>) =
self.build_expr(predicate.as_ref(), true, false)?;
node = Node::Filter {
source: Box::new(node),
Expand Down Expand Up @@ -98,8 +96,7 @@ impl<'a> Planner<'a> {
expr: &Expr,
allow_subqueries: bool,
allow_aggregates: bool,
) -> Result<(
IntermediateExpr, Vec<Node>), HaltReason> {
) -> Result<(IntermediateExpr, Vec<Node>), HaltReason> {
// TODO(vck): Implement this

let mut subqueries: Vec<Node> = vec![];
Expand All @@ -110,7 +107,11 @@ impl<'a> Planner<'a> {
None
}
Expr::FieldPath { head, tail, .. } => {
println!("FieldPath {} {}", head, tail.iter().map(|x| x.to_string()).collect::<String>());
println!(
"FieldPath {} {}",
head,
tail.iter().map(|x| x.to_string()).collect::<String>()
);
None
}
Expr::Call { callee, args, .. } => {
Expand All @@ -134,12 +135,7 @@ impl<'a> Planner<'a> {
return Err(err);
}

Ok((
IntermediateExpr::Expr {
expr: expr.clone(),
},
subqueries,
))
Ok((IntermediateExpr::Expr { expr: expr.clone() }, subqueries))
}

fn build_select(&mut self, query: &SqlSelect) -> Result<Node, HaltReason> {
Expand Down
Loading

0 comments on commit 0e819e9

Please sign in to comment.