Skip to content

Commit

Permalink
feat: GROUP BY + HAVING clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 9, 2023
1 parent e42da8a commit 76ecc17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/src/lang/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct SelectCore {
pub projection: Vec<SqlProjection>,
pub from: Option<SqlFrom>,
pub r#where: Option<SqlExpr>,
pub group_by: Option<SqlExpr>,
pub group_by: Option<Vec<SqlExpr>>,
pub having: Option<SqlExpr>,
}

Expand Down
38 changes: 33 additions & 5 deletions server/src/lang/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,16 +520,44 @@ impl<'a> Parser<'a> {
SqlDistinct::All
}*/

let projection = self.sql_projection();
let from = self.sql_from()?;
let r#where = self.sql_where()?;
let group_by = self.sql_group_by()?;
let having = if group_by.is_some() && self.match_next(skw!(Having)) {
Some(SqlExpr::Default(self.expression()?))
} else {
None
};

Ok(SelectCore {
distinct,
projection: self.sql_projection(),
from: self.sql_from()?,
r#where: self.sql_where()?,
group_by: None, // TODO(vck)
having: None, // TODO(vck)
projection,
from,
r#where,
group_by,
having,
})
}

fn sql_group_by(&mut self) -> ParseResult<Option<Vec<SqlExpr>>> {
if self.match_next(skw!(Group)) {
self.expected(skw!(By))?;
let mut groups: Vec<SqlExpr> = vec![];

loop {
let group_expr = self.expression()?;
groups.push(SqlExpr::Default(group_expr));
if !self.match_next(sym!(Comma)) {
break;
}
}
Ok(Some(groups))
} else {
Ok(None)
}
}

fn sql_projection(&mut self) -> Vec<SqlProjection> {
let mut projections: Vec<SqlProjection> = vec![];
loop {
Expand Down

0 comments on commit 76ecc17

Please sign in to comment.