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 2, 2024
1 parent 78d702d commit 3bcb6c1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 58 deletions.
10 changes: 8 additions & 2 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ impl Display for Expr {
}

impl Expr {
pub fn walk<V, E>(&self, visitor: &mut impl FnMut(&Expr) -> Option<Result<V,E>>) -> Option<Result<V,E>> {
pub fn walk<V, E>(
&self,
visitor: &mut impl FnMut(&Expr) -> Option<Result<V, E>>,
) -> Option<Result<V, E>> {
let result = visitor(self);
if result.is_none() {
return None;
Expand Down Expand Up @@ -595,7 +598,10 @@ impl Expr {
//
Expr::Call { callee, args, .. } => {
let rcallee = callee.walk(visitor);
let rargs = args.iter().map(|x| x.walk(visitor)).fold(None, |acc, x| acc.or(x));
let rargs = args
.iter()
.map(|x| x.walk(visitor))
.fold(None, |acc, x| acc.or(x));

rcallee.or(rargs)

Check warning on line 606 in lykiadb-lang/src/ast/expr.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/expr.rs#L599-L606

Added lines #L599 - L606 were not covered by tests
}
Expand Down
12 changes: 8 additions & 4 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use lykiadb_lang::tokenizer::scanner::Scanner;
use lykiadb_lang::Span;
use lykiadb_lang::Spanned;
use lykiadb_lang::{Literal, Locals, Scopes};
use pretty_assertions::assert_eq;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use pretty_assertions::assert_eq;

use super::error::ExecutionError;
use super::stdlib::stdlib;
Expand Down Expand Up @@ -341,7 +341,9 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
let mut planner = Planner::new(self); // TODO(vck): Use the existing context
let plan = planner.build(e)?;
if let Some(out) = &self.output {
out.write().unwrap().push(RV::Str(Arc::new(plan.to_string().trim().to_string())));
out.write()
.unwrap()
.push(RV::Str(Arc::new(plan.to_string().trim().to_string())));
}
Ok(RV::Undefined)
}
Expand Down Expand Up @@ -700,7 +702,6 @@ impl Default for Output {
}

impl Output {

pub fn new() -> Output {
Output { out: Vec::new() }
}
Expand All @@ -712,7 +713,10 @@ impl Output {
pub fn expect(&mut self, rv: Vec<RV>) {
if rv.len() == 1 {
if let Some(first) = rv.first() {
assert_eq!(self.out.first().unwrap_or(&RV::Undefined).to_string(), first.to_string());
assert_eq!(
self.out.first().unwrap_or(&RV::Undefined).to_string(),
first.to_string()
);
}

Check warning on line 720 in lykiadb-server/src/engine/interpreter.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/engine/interpreter.rs#L720

Added line #L720 was not covered by tests
}
assert_eq!(self.out, rv)
Expand Down
66 changes: 34 additions & 32 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ use crate::{
value::RV,
};

use lykiadb_lang::{ast::{
expr::Expr,
sql::{SqlFrom, SqlJoinType, SqlProjection, SqlSelect, SqlSelectCore, SqlSource},
visitor::VisitorMut,
}, Spanned};
use lykiadb_lang::{
ast::{
expr::Expr,
sql::{SqlFrom, SqlJoinType, SqlProjection, SqlSelect, SqlSelectCore, SqlSource},
visitor::VisitorMut,
},
Spanned,
};

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

Expand Down Expand Up @@ -100,35 +103,34 @@ impl<'a> Planner<'a> {
let mut subqueries: Vec<Node> = vec![];
let mut overrides = HashMap::new();

let result = expr.walk::<(), HaltReason>(&mut |e: &Expr| {
match e {
Expr::Get {
id, object, name, ..
} => {
println!("Get {}.({})", object, name);
None
},
Expr::Variable { name, id, .. } =>
{
println!("Variable {}", name);
None
},
Expr::Call {
callee, args, id, ..
} => {
println!("Call {}({:?})", callee, args);
None
},
Expr::Select { query, .. } => {
if !allow_subqueries {
return Some(Err(HaltReason::Error(ExecutionError::Plan(PlannerError::SubqueryNotAllowed(expr.get_span())))));
}
let subquery = self.build_select(query);
subqueries.push(subquery.unwrap());
None
let result = expr.walk::<(), HaltReason>(&mut |e: &Expr| match e {
Expr::Get {
id, object, name, ..
} => {
println!("Get {}.({})", object, name);
None
}
Expr::Variable { name, id, .. } => {
println!("Variable {}", name);
None
}
Expr::Call {
callee, args, id, ..
} => {
println!("Call {}({:?})", callee, args);
None

Check warning on line 121 in lykiadb-server/src/plan/planner.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L118-L121

Added lines #L118 - L121 were not covered by tests
}
Expr::Select { query, .. } => {
if !allow_subqueries {
return Some(Err(HaltReason::Error(ExecutionError::Plan(
PlannerError::SubqueryNotAllowed(expr.get_span()),
))));
}
_ => Some(Ok(())),
let subquery = self.build_select(query);
subqueries.push(subquery.unwrap());
None
}
_ => Some(Ok(())),
});

if let Some(Err(err)) = result {
Expand Down
4 changes: 2 additions & 2 deletions lykiadb-server/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Display for RV {
write!(f, "{}", item)?;

Check warning on line 107 in lykiadb-server/src/value/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/value/mod.rs#L96-L107

Added lines #L96 - L107 were not covered by tests
}
write!(f, "]")

Check warning on line 109 in lykiadb-server/src/value/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/value/mod.rs#L109

Added line #L109 was not covered by tests
},
}
RV::Object(obj) => {
let obj = (obj as &RwLock<FxHashMap<String, RV>>).read().unwrap();
write!(f, "{{")?;
Expand All @@ -118,7 +118,7 @@ impl Display for RV {
write!(f, "{}: {}", key, value)?;

Check warning on line 118 in lykiadb-server/src/value/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/value/mod.rs#L111-L118

Added lines #L111 - L118 were not covered by tests
}
write!(f, "}}")

Check warning on line 120 in lykiadb-server/src/value/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/value/mod.rs#L120

Added line #L120 was not covered by tests
},
}
RV::Callable(_) => write!(f, "<Callable>"),

Check warning on line 122 in lykiadb-server/src/value/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/value/mod.rs#L122

Added line #L122 was not covered by tests
}
}
Expand Down
37 changes: 19 additions & 18 deletions lykiadb-server/tests/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use lykiadb_server::{
engine::interpreter::test_helpers::{assert_err, assert_out},
value::RV,
};
use std::sync::Arc;
use lykiadb_server::{engine::interpreter::test_helpers::{assert_err, assert_out}, value::RV};

fn expect_plan(query: &str, expected_plan: &str) {
assert_out(query,
vec![
RV::Str(Arc::new(expected_plan.to_string())),
],
);
assert_out(query, vec![RV::Str(Arc::new(expected_plan.to_string()))]);
}

pub fn run_test(input: &str) {
Expand All @@ -23,23 +22,25 @@ pub fn run_test(input: &str) {
.trim()
.to_string();

let flags = directives_and_input[..directives_end - 1].trim().split(",").map(|flag| {
let kv: Vec<&str> = flag.split("=").collect();
return (kv[0].trim(), kv[1].trim());
}).fold(std::collections::HashMap::new(), |mut acc, (k, v)| {
acc.insert(k, v);
acc
});
let flags = directives_and_input[..directives_end - 1]
.trim()
.split(",")
.map(|flag| {
let kv: Vec<&str> = flag.split("=").collect();
return (kv[0].trim(), kv[1].trim());
})
.fold(std::collections::HashMap::new(), |mut acc, (k, v)| {
acc.insert(k, v);
acc
});

let io_parts: Vec<&str> = rest.split("---").collect();

if flags.get("expect") == Some(&"error") {
assert_err(io_parts[0].trim(),
io_parts[1].trim()
);
assert_err(io_parts[0].trim(), io_parts[1].trim());
continue;
}

expect_plan(io_parts[0].trim(), io_parts[1].trim());
}
}

0 comments on commit 3bcb6c1

Please sign in to comment.