Skip to content

Commit

Permalink
feat: Minimal from build
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Aug 24, 2024
1 parent 84a8a31 commit 748540c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 41 deletions.
6 changes: 3 additions & 3 deletions lykiadb-lang/src/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub enum SqlDistinct {
pub enum SqlJoinType {
#[serde(rename = "SqlJoinType::Left")]
Left,
#[serde(rename = "SqlJoinType::LeftOuter")]
LeftOuter,
#[serde(rename = "SqlJoinType::Right")]
Right,
#[serde(rename = "SqlJoinType::Inner")]
Inner,
#[serde(rename = "SqlJoinType::Cross")]
Cross,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -151,7 +151,7 @@ pub enum SqlFrom {
Collection(SqlCollectionIdentifier),
#[serde(rename = "SqlFrom::Select")]
Select {
expr: Box<Expr>,
subquery: Box<SqlSelect>,
alias: Option<Identifier>,
},
#[serde(rename = "SqlFrom::Join")]
Expand Down
3 changes: 0 additions & 3 deletions lykiadb-lang/src/ast/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::{
expr::Expr,
sql::{
SqlFrom, SqlDelete, SqlExpr, SqlInsert, SqlSelect, SqlSelectCore, SqlUpdate,
},
stmt::Stmt,
};

Expand Down
12 changes: 6 additions & 6 deletions lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,18 +1200,18 @@ impl<'a> Parser<'a> {
fn sql_select_from_collection(&mut self) -> ParseResult<SqlFrom> {
if self.match_next(sym!(LeftParen)) {
if self.cmp_tok(&skw!(Select)) {
let expr = self.sql_select()?;
self.expected(sym!(RightParen))?; // closing paren
let subquery = Box::new(self.sql_select_inner()?);
self.expected(sym!(RightParen))?;
let alias: Option<Token> =
optional_with_expected!(self, skw!(As), Identifier { dollar: false });
return Ok(SqlFrom::Select {
expr,
subquery,
alias: alias.map(|t| t.extract_identifier().unwrap()),
});
}
// If the next token is a left paren, then it must be either a select statement or a recursive from
let parsed = self.sql_select_from_join()?; // TODO(vck): Check if using _collection variant makes sense.
self.expected(sym!(RightParen))?; // closing paren
// If the next token is a left paren, then it must be either a select statement or a recursive "from" clause
let parsed = self.sql_select_from_join()?;
self.expected(sym!(RightParen))?;
Ok(parsed)
} else if let Some(collection) = self.sql_collection_identifier()? {
return Ok(SqlFrom::Collection(collection));
Expand Down
5 changes: 1 addition & 4 deletions lykiadb-lang/tests/lang/sql/select_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ assert_parsing! {
"dollar": false,
"name": "u"
},
"expr": {
"@type": "Expr::Select",
"query": {
"subquery": {
"@type": "SqlSelect",
"core": {
"@type": "SqlSelectCore",
Expand Down Expand Up @@ -242,7 +240,6 @@ assert_parsing! {
"limit": null,
"order_by": null
}
}
}
]
},
Expand Down
5 changes: 1 addition & 4 deletions lykiadb-lang/tests/lang/sql/select_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,7 @@ assert_parsing! {
"dollar": false,
"name": "c"
},
"expr": {
"@type": "Expr::Select",
"query": {
"subquery": {
"@type": "SqlSelect",
"core": {
"@type": "SqlSelectCore",
Expand Down Expand Up @@ -632,7 +630,6 @@ assert_parsing! {
},
"limit": null,
"order_by": null
}
}
},
"right": {
Expand Down
39 changes: 26 additions & 13 deletions lykiadb-server/src/plan/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lykiadb_lang::ast::sql::SqlExpr;
use lykiadb_lang::{ast::sql::{SqlCollectionIdentifier, SqlExpr, SqlJoinType, SqlOrdering}, Identifier};
use serde::{Deserialize, Serialize};

pub mod planner;

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L6 was not covered by tests
Expand All @@ -11,11 +12,6 @@ pub enum Aggregate {
Sum(SqlExpr),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Direction {
Ascending,
Descending,
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L16 was not covered by tests
pub enum Plan {
Expand All @@ -41,12 +37,6 @@ pub enum Node {
aliases: Vec<String>,
},

Scan {
collection: String,
filter: Option<SqlExpr>,
alias: Option<String>,
},

Limit {
source: Box<Node>,
limit: usize,
Expand All @@ -59,10 +49,33 @@ pub enum Node {

Order {
source: Box<Node>,
key: Vec<(SqlExpr, Direction)>,
key: Vec<(SqlExpr, SqlOrdering)>,
},

Values {
rows: Vec<Vec<SqlExpr>>,
},

ValuesHandle {
identifier: Identifier
},

Scan {
source: SqlCollectionIdentifier,
filter: Option<SqlExpr>,
},

Join {
left: Box<Node>,
join_type: SqlJoinType,
right: Box<Node>,
constraint: Option<SqlExpr>,
},

Subquery {
source: Box<Node>,
alias: Identifier,
},

Nothing
}
47 changes: 39 additions & 8 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lykiadb_lang::ast::{expr::Expr, sql::{SqlFrom, SqlSelect}};
use lykiadb_lang::{ast::{expr::Expr, sql::{SqlFrom, SqlJoinType, SqlSelect}}, Identifier};

use crate::{engine::interpreter::HaltReason, value::types::RV};
use crate::engine::interpreter::HaltReason;

use super::{Node, Plan};
pub struct Planner;
Expand All @@ -22,20 +22,51 @@ impl Planner {
query,
span: _,
id: _,
} => self.build_select(query),
} => Ok(Plan::Select(self.build_select(query)?)),
_ => panic!("Not implemented yet."),

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L22-L26

Added lines #L22 - L26 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L28 was not covered by tests

fn build_select(&mut self, query: &SqlSelect) -> Result<Plan, HaltReason> {
let mut node: Option<Node> = None;
fn build_select(&mut self, query: &SqlSelect) -> Result<Node, HaltReason> {
let mut node: Node = Node::Nothing;
if let Some(from) = &query.core.from {
node = Some(self.build_from(from)?);
node = self.build_from(from)?;
}
Ok(Plan::Select(Node::Values { rows: vec![vec![]] }))
Ok(node)
}

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L30-L36

Added lines #L30 - L36 were not covered by tests

fn build_from(&mut self, from: &SqlFrom) -> Result<Node, HaltReason> {
Err(HaltReason::Return(RV::Undefined))
match &from {
SqlFrom::Select { subquery, alias } => {
let node = Node::Subquery {
source: Box::new(self.build_select(subquery)?),
alias: alias.clone().unwrap()
};
Ok(node)

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L38-L45

Added lines #L38 - L45 were not covered by tests
}
SqlFrom::Collection(ident) => Ok(Node::Scan {
source: ident.clone(),
filter: None,
}),
SqlFrom::Group { values } => {
let mut froms = values.into_iter();
let mut node = self.build_from(froms.next().unwrap())?;
for right in froms {
node = Node::Join {
left: Box::new(node),
join_type: SqlJoinType::Cross,
right: Box::new(self.build_from(right)?),
constraint: None,

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L47-L59

Added lines #L47 - L59 were not covered by tests
}
}
Ok(node)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L62 was not covered by tests
},
SqlFrom::Join { left , join_type, right, constraint } => Ok(Node::Join {
left: Box::new(self.build_from(left)?),
join_type: join_type.clone(),
right: Box::new(self.build_from(right)?),
constraint: constraint.clone().map(|x| *x.clone())
})

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

View check run for this annotation

Codecov / codecov/patch

lykiadb-server/src/plan/planner.rs#L64-L69

Added lines #L64 - L69 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L71 was not covered by tests
}

0 comments on commit 748540c

Please sign in to comment.