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 1, 2024
1 parent 037480d commit 11974a1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
40 changes: 22 additions & 18 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,22 @@ impl Display for Expr {
Expr::Delete { .. } => write!(f, "<SqlDelete>"),
Expr::Variable { name, .. } => write!(f, "{}", name),
Expr::Grouping { expr, .. } => write!(f, "({})", expr),
Expr::Literal { value, .. } => {
match value {
Literal::Str(s) => write!(f, "Str(\"{}\")", s),
Literal::Num(n) => write!(f, "Num({:?})", n),
Literal::Bool(b) => write!(f, "{}", b),
Literal::Undefined => write!(f, "undefined"),
Literal::Object(o) => write!(f, "{:?}", o),
Literal::Array(a) => write!(f, "Array({})", a.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(", ")),
Literal::NaN => write!(f, "NaN"),
Literal::Null => write!(f, "null"),
}
Expr::Literal { value, .. } => match value {
Literal::Str(s) => write!(f, "Str(\"{}\")", s),
Literal::Num(n) => write!(f, "Num({:?})", n),
Literal::Bool(b) => write!(f, "{}", b),
Literal::Undefined => write!(f, "undefined"),
Literal::Object(o) => write!(f, "{:?}", o),

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/expr.rs#L480-L482

Added lines #L480 - L482 were not covered by tests
Literal::Array(a) => write!(
f,
"Array({})",
a.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ")
),
Literal::NaN => write!(f, "NaN"),
Literal::Null => write!(f, "null"),

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/expr.rs#L491-L492

Added lines #L491 - L492 were not covered by tests
},
Expr::Function {
name, parameters, ..
Expand Down Expand Up @@ -565,21 +570,20 @@ impl Expr {
return false;
}
match self {
Expr::Select { .. }
Expr::Select { .. }
| Expr::Insert { .. }
| Expr::Delete { .. }
| Expr::Update { .. }
| Expr::Variable { .. }
| Expr::Literal { .. }
| Expr::Function { .. } => false,
//
Expr::Binary { left, right, .. }
| Expr::Logical { left, right, .. } => {
Expr::Binary { left, right, .. } | Expr::Logical { left, right, .. } => {
let rleft = left.walk(visitor);
let rright = right.walk(visitor);

rleft || rright
},
}
//
Expr::Grouping { expr, .. }
| Expr::Unary { expr, .. }

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/expr.rs#L589

Added line #L589 was not covered by tests
Expand All @@ -590,7 +594,7 @@ impl Expr {
let rargs = args.iter().map(|x| x.walk(visitor)).all(|x| x);

rcallee || rargs

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/expr.rs#L592-L596

Added lines #L592 - L596 were not covered by tests
},
}
Expr::Between {
lower,
upper,
Expand All @@ -602,14 +606,14 @@ impl Expr {
let rsubject = subject.walk(visitor);

rlower || rupper || rsubject

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L599 - L608 were not covered by tests
},
}
Expr::Get { object, .. } => object.walk(visitor),
Expr::Set { object, value, .. } => {
let robject = object.walk(visitor);
let rvalue = value.walk(visitor);

robject || rvalue

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/expr.rs#L610-L615

Added lines #L610 - L615 were not covered by tests
},
}
}
}

Expand Down
38 changes: 21 additions & 17 deletions lykiadb-server/src/plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ pub enum Aggregate {
pub enum ExprOverride {
Subquery(usize),
Aggregate,
Field
Field,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]

Check warning on line 45 in lykiadb-server/src/plan/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/mod.rs#L45

Added line #L45 was not covered by tests
pub enum IntermediateExpr {
Constant(RV),
Expr {
expr: Expr,
overrides: HashMap<usize, ExprOverride>
overrides: HashMap<usize, ExprOverride>,
},
}

Expand Down Expand Up @@ -84,7 +84,7 @@ pub enum Node {
Filter {
source: Box<Node>,
predicate: IntermediateExpr,
subqueries: Vec<Node>
subqueries: Vec<Node>,
},

Projection {
Expand Down Expand Up @@ -196,19 +196,17 @@ impl Node {

source._fmt_recursive(f, indent + 1)
}
Node::Filter { source, predicate, subqueries } => {
write!(
f,
"{}- filter [{}]{}",
indent_str,
predicate,
Self::NEWLINE
)?;
Node::Filter {
source,
predicate,
subqueries,
} => {
write!(f, "{}- filter [{}]{}", indent_str, predicate, Self::NEWLINE)?;
if !subqueries.is_empty() {
write!(f, "{} > subqueries{}", indent_str, Self::NEWLINE)?;
subqueries.iter().try_for_each(|subquery| {
subquery._fmt_recursive(f, indent + 2)
})?;
subqueries
.iter()
.try_for_each(|subquery| subquery._fmt_recursive(f, indent + 2))?;
}
source._fmt_recursive(f, indent + 1)
}
Expand All @@ -217,7 +215,10 @@ impl Node {
f,
"{}- subquery [{}]{}",
indent_str,
alias.as_ref().map(|x| x.name.clone()).unwrap_or("unnamed".to_string()),
alias
.as_ref()
.map(|x| x.name.clone())
.unwrap_or("unnamed".to_string()),
Self::NEWLINE
)?;
source._fmt_recursive(f, indent + 1)
Expand Down Expand Up @@ -278,7 +279,10 @@ impl Node {
"{}- join [type={:?}, {}]{}",
indent_str,
join_type,
constraint.as_ref().map(|x| x.to_string()).unwrap_or("None".to_string()),
constraint
.as_ref()
.map(|x| x.to_string())
.unwrap_or("None".to_string()),
Self::NEWLINE
)?;
left._fmt_recursive(f, indent + 1)?;
Expand All @@ -292,7 +296,7 @@ impl Node {
source.expr,
Self::NEWLINE
)
}
}
_ => "<NotImplementedYet>".fmt(f),
}
}
Expand Down
54 changes: 32 additions & 22 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ impl<'a> Planner<'a> {

// WHERE
if let Some(predicate) = &core.r#where {
let (expr, subqueries): (IntermediateExpr, Vec<Node>) = self.build_expr(predicate.as_ref(), true, false)?;
let (expr, subqueries): (IntermediateExpr, Vec<Node>) =
self.build_expr(predicate.as_ref(), true, false)?;
node = Node::Filter {
source: Box::new(node),
predicate: expr,
subqueries
subqueries,
}
}

Expand Down Expand Up @@ -88,44 +89,50 @@ impl<'a> Planner<'a> {
self.interpreter.visit_expr(expr)
}

fn build_expr(&mut self, expr: &Expr, allow_subqueries: bool, allow_aggregates: bool) -> Result<(IntermediateExpr, Vec<Node>), HaltReason> {
fn build_expr(
&mut self,
expr: &Expr,
allow_subqueries: bool,
allow_aggregates: bool,
) -> Result<(IntermediateExpr, Vec<Node>), HaltReason> {
// TODO(vck): Implement this

let mut subqueries:Vec<Node> = vec![];
let mut subqueries: Vec<Node> = vec![];
let mut overrides = HashMap::new();

expr.walk(&mut |expr: &Expr| {
match expr {
Expr::Get { id, object, name, .. } => {
false
}
Expr::Variable { name, id, .. } => {
false
}
Expr::Call { callee, args, id, .. } => {
false
},
Expr::Get {
id, object, name, ..
} => false,
Expr::Variable { name, id, .. } => false,
Expr::Call {
callee, args, id, ..
} => false,

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L110-L111

Added lines #L110 - L111 were not covered by tests
Expr::Select { query, .. } => {
if !allow_subqueries {
return false; // Err(HaltReason::Error(ExecutionError::Plan("Subqueries are not allowed here".to_string())));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L114 was not covered by tests
}
let subquery = self.build_select(query);
subqueries.push(subquery.unwrap());
false
},
_ => {
true
}
_ => true,
}
});
Ok((IntermediateExpr::Expr { expr: expr.clone(), overrides }, subqueries))
Ok((
IntermediateExpr::Expr {
expr: expr.clone(),
overrides,
},
subqueries,
))
}

fn build_select(&mut self, query: &SqlSelect) -> Result<Node, HaltReason> {
let mut node: Node = self.build_select_core(&query.core)?;

if let Some(order_by) = &query.order_by {

let mut order_key = vec![];

for key in order_by {
Expand All @@ -135,7 +142,7 @@ impl<'a> Planner<'a> {

node = Node::Order {
source: Box::new(node),
key: order_key
key: order_key,
};
}

Expand Down Expand Up @@ -188,7 +195,7 @@ impl<'a> Planner<'a> {
SqlFrom::Select { subquery, alias } => {
let node = Node::Subquery {
source: Box::new(self.build_select(subquery)?),
alias: alias.clone()
alias: alias.clone(),
};
Ok(node)
}
Expand All @@ -211,15 +218,18 @@ impl<'a> Planner<'a> {
right,
constraint,
} => {
let constraint = constraint.as_ref().map(|x| self.build_expr(x, false, false)).transpose()?;
let constraint = constraint
.as_ref()
.map(|x| self.build_expr(x, false, false))
.transpose()?;

Ok(Node::Join {
left: Box::new(self.build_from(left, &mut scope)?),
join_type: join_type.clone(),
right: Box::new(self.build_from(right, &mut scope)?),
constraint: constraint.map(|x| x.0),
})
},
}
};

if let Err(err) = parent_scope.merge(scope) {
Expand Down

0 comments on commit 11974a1

Please sign in to comment.