diff --git a/lykiadb-lang/src/ast/expr.rs b/lykiadb-lang/src/ast/expr.rs index 484f475..b897cb6 100644 --- a/lykiadb-lang/src/ast/expr.rs +++ b/lykiadb-lang/src/ast/expr.rs @@ -474,17 +474,22 @@ impl Display for Expr { Expr::Delete { .. } => write!(f, ""), 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::>().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), + Literal::Array(a) => write!( + f, + "Array({})", + a.iter() + .map(|x| x.to_string()) + .collect::>() + .join(", ") + ), + Literal::NaN => write!(f, "NaN"), + Literal::Null => write!(f, "null"), }, Expr::Function { name, parameters, .. @@ -565,7 +570,7 @@ impl Expr { return false; } match self { - Expr::Select { .. } + Expr::Select { .. } | Expr::Insert { .. } | Expr::Delete { .. } | Expr::Update { .. } @@ -573,13 +578,12 @@ impl Expr { | 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, .. } @@ -590,7 +594,7 @@ impl Expr { let rargs = args.iter().map(|x| x.walk(visitor)).all(|x| x); rcallee || rargs - }, + } Expr::Between { lower, upper, @@ -602,14 +606,14 @@ impl Expr { let rsubject = subject.walk(visitor); rlower || rupper || rsubject - }, + } Expr::Get { object, .. } => object.walk(visitor), Expr::Set { object, value, .. } => { let robject = object.walk(visitor); let rvalue = value.walk(visitor); robject || rvalue - }, + } } } diff --git a/lykiadb-server/src/plan/mod.rs b/lykiadb-server/src/plan/mod.rs index 104560c..3d50429 100644 --- a/lykiadb-server/src/plan/mod.rs +++ b/lykiadb-server/src/plan/mod.rs @@ -39,7 +39,7 @@ pub enum Aggregate { pub enum ExprOverride { Subquery(usize), Aggregate, - Field + Field, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -47,7 +47,7 @@ pub enum IntermediateExpr { Constant(RV), Expr { expr: Expr, - overrides: HashMap + overrides: HashMap, }, } @@ -84,7 +84,7 @@ pub enum Node { Filter { source: Box, predicate: IntermediateExpr, - subqueries: Vec + subqueries: Vec, }, Projection { @@ -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) } @@ -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) @@ -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)?; @@ -292,7 +296,7 @@ impl Node { source.expr, Self::NEWLINE ) - } + } _ => "".fmt(f), } } diff --git a/lykiadb-server/src/plan/planner.rs b/lykiadb-server/src/plan/planner.rs index 7bb0095..7d92af2 100644 --- a/lykiadb-server/src/plan/planner.rs +++ b/lykiadb-server/src/plan/planner.rs @@ -51,11 +51,12 @@ impl<'a> Planner<'a> { // WHERE if let Some(predicate) = &core.r#where { - let (expr, subqueries): (IntermediateExpr, Vec) = self.build_expr(predicate.as_ref(), true, false)?; + let (expr, subqueries): (IntermediateExpr, Vec) = + self.build_expr(predicate.as_ref(), true, false)?; node = Node::Filter { source: Box::new(node), predicate: expr, - subqueries + subqueries, } } @@ -88,23 +89,26 @@ 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), HaltReason> { + fn build_expr( + &mut self, + expr: &Expr, + allow_subqueries: bool, + allow_aggregates: bool, + ) -> Result<(IntermediateExpr, Vec), HaltReason> { // TODO(vck): Implement this - let mut subqueries:Vec = vec![]; + let mut subqueries: Vec = 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, Expr::Select { query, .. } => { if !allow_subqueries { return false; // Err(HaltReason::Error(ExecutionError::Plan("Subqueries are not allowed here".to_string()))); @@ -112,20 +116,23 @@ impl<'a> Planner<'a> { 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 { 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 { @@ -135,7 +142,7 @@ impl<'a> Planner<'a> { node = Node::Order { source: Box::new(node), - key: order_key + key: order_key, }; } @@ -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) } @@ -211,7 +218,10 @@ 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)?), @@ -219,7 +229,7 @@ impl<'a> Planner<'a> { right: Box::new(self.build_from(right, &mut scope)?), constraint: constraint.map(|x| x.0), }) - }, + } }; if let Err(err) = parent_scope.merge(scope) {