Skip to content

Commit

Permalink
fix: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Nov 29, 2024
1 parent 28f36eb commit 764c9de
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 63 deletions.
3 changes: 1 addition & 2 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,5 +653,4 @@ pub mod test {

assert!(!set.contains(&e1));
}

}
}
4 changes: 3 additions & 1 deletion lykiadb-lang/src/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ pub enum SqlSource {
impl SqlSource {
pub fn alias(&self) -> &Identifier {
match self {
SqlSource::Collection(collection) => collection.alias.as_ref().unwrap_or(&collection.name),
SqlSource::Collection(collection) => {
collection.alias.as_ref().unwrap_or(&collection.name)
}
SqlSource::Expr(expr) => &expr.alias,

Check warning on line 116 in lykiadb-lang/src/ast/sql.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/sql.rs#L116

Added line #L116 was not covered by tests
}
}
Expand Down
42 changes: 23 additions & 19 deletions lykiadb-lang/src/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,40 @@ impl Spanned for Stmt {
mod test {
use std::collections::HashSet;

use crate::{ast::{expr::{test::create_simple_add_expr, Expr}, stmt::Stmt}, Span};
use crate::{
ast::{
expr::{test::create_simple_add_expr, Expr},
stmt::Stmt,
},
Span,
};

pub fn create_simple_block_stmt(a: Expr, b: Expr) -> Stmt {
Stmt::Block {
Stmt::Block {
body: vec![
Stmt::Expression {
expr: Box::new(a), span: Span::default()
Stmt::Expression {
expr: Box::new(a),
span: Span::default(),
},
Stmt::Expression {
expr: Box::new(b), span: Span::default()
}
],
span: Span::default()
Stmt::Expression {
expr: Box::new(b),
span: Span::default(),
},
],
span: Span::default(),
}
}

#[test]
fn identical_stmts_should_be_equal() {

let s0 = create_simple_block_stmt(
create_simple_add_expr(0, 1.0, 2.0),
create_simple_add_expr(1, 1.0, 2.0)
create_simple_add_expr(1, 1.0, 2.0),
);

let s1 = create_simple_block_stmt(
create_simple_add_expr(0, 1.0, 2.0),
create_simple_add_expr(1, 1.0, 2.0)
create_simple_add_expr(1, 1.0, 2.0),
);

assert_eq!(s0, s1);
Expand All @@ -146,15 +153,14 @@ mod test {

#[test]
fn different_stmts_should_not_be_equal_0() {

let s0 = create_simple_block_stmt(
create_simple_add_expr(0, 1.0, 2.0),
create_simple_add_expr(1, 1.0, 2.0)
create_simple_add_expr(1, 1.0, 2.0),
);

let s1 = create_simple_block_stmt(
create_simple_add_expr(0, 2.0, 1.0),
create_simple_add_expr(1, 1.0, 2.0)
create_simple_add_expr(1, 1.0, 2.0),
);

assert_ne!(s0, s1);
Expand All @@ -168,10 +174,9 @@ mod test {

#[test]
fn different_stmts_should_not_be_equa_1() {

let s0 = create_simple_block_stmt(
create_simple_add_expr(0, 1.0, 2.0),
create_simple_add_expr(0, 10.0, 20.0)
create_simple_add_expr(0, 10.0, 20.0),
);

let s1 = create_simple_block_stmt(
Expand All @@ -187,5 +192,4 @@ mod test {

assert!(!set.contains(&s1));
}

}
}
8 changes: 5 additions & 3 deletions lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,9 @@ impl<'a> Parser<'a> {
}

use crate::ast::sql::{
SqlCollectionIdentifier, SqlCompoundOperator, SqlDelete, SqlDistinct, SqlExpressionSource, SqlFrom, SqlInsert, SqlJoinType, SqlLimitClause, SqlOrderByClause, SqlOrdering, SqlProjection, SqlSelect, SqlSelectCompound, SqlSelectCore, SqlSource, SqlUpdate, SqlValues
SqlCollectionIdentifier, SqlCompoundOperator, SqlDelete, SqlDistinct, SqlExpressionSource,
SqlFrom, SqlInsert, SqlJoinType, SqlLimitClause, SqlOrderByClause, SqlOrdering, SqlProjection,
SqlSelect, SqlSelectCompound, SqlSelectCore, SqlSource, SqlUpdate, SqlValues,
};

macro_rules! optional_with_expected {
Expand Down Expand Up @@ -1296,9 +1298,9 @@ impl<'a> Parser<'a> {
let expr = self.expression()?;
self.expected(skw!(As))?;
let identifier = self.expected(Identifier { dollar: false })?.clone();
return Ok(SqlFrom::Source(SqlSource::Expr(SqlExpressionSource{
return Ok(SqlFrom::Source(SqlSource::Expr(SqlExpressionSource {
expr,
alias: identifier.extract_identifier().unwrap()
alias: identifier.extract_identifier().unwrap(),
})));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-lang/tests/lang/sql/select_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,5 @@ assert_parsing! {
]
}
}

}
9 changes: 3 additions & 6 deletions lykiadb-server/src/engine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum ExecutionError {
Resolve(ResolveError),
Interpret(InterpretError),
Environment(EnvironmentError),
Plan(PlannerError)
Plan(PlannerError),
}

impl From<ParseError> for ExecutionError {
Expand Down Expand Up @@ -140,14 +140,11 @@ pub fn report_error(filename: &str, source: &str, error: ExecutionError) {
ExecutionError::Plan(PlannerError::DuplicateObjectInScope { previous, ident }) => {

Check warning on line 140 in lykiadb-server/src/engine/error.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/engine/error.rs#L140

Added line #L140 was not covered by tests
print(
"Duplicate object in scope",
&format!(
"Object {} is already defined in the scope.",
previous.name
),
&format!("Object {} is already defined in the scope.", previous.name),
previous.span,

Check warning on line 144 in lykiadb-server/src/engine/error.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/engine/error.rs#L142-L144

Added lines #L142 - L144 were not covered by tests
);
}
ExecutionError::Environment(EnvironmentError::Other { message })
ExecutionError::Environment(EnvironmentError::Other { message })
| ExecutionError::Interpret(InterpretError::Other { message }) => {

Check warning on line 148 in lykiadb-server/src/engine/error.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/engine/error.rs#L147-L148

Added lines #L147 - L148 were not covered by tests
print(&message, "", Span::default());
}
Expand Down
5 changes: 3 additions & 2 deletions lykiadb-server/src/plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use lykiadb_lang::{
ast::{
expr::Expr,
sql::{
SqlCollectionIdentifier, SqlCompoundOperator, SqlExpressionSource, SqlJoinType, SqlOrdering, SqlProjection
SqlCollectionIdentifier, SqlCompoundOperator, SqlExpressionSource, SqlJoinType,
SqlOrdering, SqlProjection,
},
},
Identifier,
Expand All @@ -19,7 +20,7 @@ pub enum PlannerError {
ObjectNotFoundInScope(Identifier),
DuplicateObjectInScope {
previous: Identifier,
ident: Identifier
ident: Identifier,
},
}

Expand Down
42 changes: 22 additions & 20 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{engine::{error::ExecutionError, interpreter::{HaltReason, Interpreter}}, value::RV};
use crate::{
engine::{
error::ExecutionError,
interpreter::{HaltReason, Interpreter},
},
value::RV,
};

use lykiadb_lang::ast::{
expr::Expr,
Expand Down Expand Up @@ -51,7 +57,6 @@ impl<'a> Planner<'a> {

// AGGREGATES


// GROUP BY

// PROJECTION
Expand Down Expand Up @@ -96,15 +101,17 @@ impl<'a> Planner<'a> {
if let Some(offset) = &limit.offset {
node = Node::Offset {
source: Box::new(node),
offset: self.eval_constant(offset)?
offset: self
.eval_constant(offset)?
.as_number()
.expect("Offset is not correct")
.floor() as usize,
}
}
node = Node::Limit {
source: Box::new(node),
limit: self.eval_constant(&limit.count)?
limit: self
.eval_constant(&limit.count)?
.as_number()
.expect("Limit is not correct")
.floor() as usize,
Expand All @@ -115,32 +122,27 @@ impl<'a> Planner<'a> {
}

fn build_from(&mut self, from: &SqlFrom, parent_scope: &mut Scope) -> Result<Node, HaltReason> {

let mut scope = Scope::new();

let node = match from {
SqlFrom::Source(source) => {
let wrapped = match source {
SqlSource::Collection(ident) => {
Node::Scan {
source: ident.clone(),
filter: None,
}
}
SqlSource::Expr(expr) => {
Node::EvalScan {
source: expr.clone(),
filter: None,
}
}
SqlSource::Collection(ident) => Node::Scan {
source: ident.clone(),
filter: None,
},
SqlSource::Expr(expr) => Node::EvalScan {
source: expr.clone(),
filter: None,
},

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L134-L137

Added lines #L134 - L137 were not covered by tests
};

if let Err(err) = scope.add_source(source.clone()) {
return Err(HaltReason::Error(ExecutionError::Plan(err)));

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L141

Added line #L141 was not covered by tests
}

Ok(wrapped)
},
}
SqlFrom::Select { subquery, alias } => {
let node = Node::Subquery {
source: Box::new(self.build_select(subquery)?),
Expand Down Expand Up @@ -173,8 +175,8 @@ impl<'a> Planner<'a> {
constraint: constraint.clone().map(|x| *x.clone()),
}),
};
if let Err(err) = parent_scope.merge(scope){

if let Err(err) = parent_scope.merge(scope) {
return Err(HaltReason::Error(ExecutionError::Plan(err)));

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L180

Added line #L180 was not covered by tests
}

Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/plan/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ impl Scope {

Ok(())
}
}
}
16 changes: 10 additions & 6 deletions lykiadb-server/tests/runtime/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ fn test_blocks_1() {

if let ExecutionError::Environment(EnvironmentError::Other { message }) = err_0 {
assert_eq!(message, expected_err_message);
}
else {
panic!("Expected error message: '{}', got: '{:?}'", expected_err_message, err_0);
} else {
panic!(
"Expected error message: '{}', got: '{:?}'",
expected_err_message, err_0
);
}

out.write().unwrap().expect(vec![
Expand All @@ -81,9 +83,11 @@ fn test_blocks_1() {

if let ExecutionError::Environment(EnvironmentError::Other { message }) = err_1 {
assert_eq!(message, expected_err_message);
}
else {
panic!("Expected error message: '{}', got: '{:?}'", expected_err_message, err_1);
} else {
panic!(
"Expected error message: '{}', got: '{:?}'",
expected_err_message, err_1
);
}

out.write().unwrap().expect(vec![
Expand Down
3 changes: 1 addition & 2 deletions lykiadb-server/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ fn expect_plan(query: &str, expected_plan: &str) {
if let Some(Stmt::Expression { expr, .. }) = body.first() {
let generated_plan = planner.build(expr).unwrap();
assert_eq!(expected_plan, generated_plan.to_string().trim());
}
else {
} else {
panic!("Expected expression statement.");
}
}
Expand Down

0 comments on commit 764c9de

Please sign in to comment.