Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement unary_op (+, -, NOT, !) in ast_builder #669

Merged
merged 3 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/src/ast_builder/expr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod binary_op;
mod is_null;
mod nested;
mod unary_op;

pub mod aggregate;
pub mod extract;
Expand All @@ -10,7 +11,7 @@ pub use nested::nested;

use {
crate::{
ast::{AstLiteral, BinaryOperator, DateTimeField, Expr},
ast::{AstLiteral, BinaryOperator, DateTimeField, Expr, UnaryOperator},
parse_sql::parse_expr,
result::{Error, Result},
translate::translate_expr,
Expand All @@ -31,6 +32,10 @@ pub enum ExprNode {
op: BinaryOperator,
right: Box<ExprNode>,
},
UnaryOp {
op: UnaryOperator,
expr: Box<ExprNode>,
},
Extract {
field: DateTimeField,
expr: Box<ExprNode>,
Expand Down Expand Up @@ -61,6 +66,10 @@ impl TryFrom<ExprNode> for Expr {

Ok(Expr::BinaryOp { left, op, right })
}
ExprNode::UnaryOp { op, expr } => {
let expr = Expr::try_from(*expr).map(Box::new)?;
Ok(Expr::UnaryOp { op, expr })
}
ExprNode::Extract { field, expr } => {
let expr = Expr::try_from(*expr).map(Box::new)?;
Ok(Expr::Extract { field, expr })
Expand Down
69 changes: 69 additions & 0 deletions core/src/ast_builder/expr/unary_op.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use {super::ExprNode, crate::ast::UnaryOperator};

impl ExprNode {
pub fn plus(self) -> Self {
plus(self)
}
pub fn minus(self) -> Self {
minus(self)
}
#[allow(clippy::should_implement_trait)]
pub fn not(self) -> Self {
not(self)
}
pub fn factorial(self) -> Self {
factorial(self)
}
}

pub fn plus<T: Into<ExprNode>>(expr: T) -> ExprNode {
ExprNode::UnaryOp {
op: UnaryOperator::Plus,
expr: Box::new(expr.into()),
}
}

pub fn minus<T: Into<ExprNode>>(expr: T) -> ExprNode {
ExprNode::UnaryOp {
op: UnaryOperator::Minus,
expr: Box::new(expr.into()),
}
}

pub fn not<T: Into<ExprNode>>(expr: T) -> ExprNode {
ExprNode::UnaryOp {
op: UnaryOperator::Not,
expr: Box::new(expr.into()),
}
}

pub fn factorial<T: Into<ExprNode>>(expr: T) -> ExprNode {
ExprNode::UnaryOp {
op: UnaryOperator::Factorial,
expr: Box::new(expr.into()),
}
}

#[cfg(test)]
mod tests {
use crate::ast_builder::{col, num, test_expr};

#[test]
fn unary_op() {
let actual = num(5).plus();
let expected = "+5";
test_expr(actual, expected);

let actual = num(10).minus();
let expected = "-10";
test_expr(actual, expected);

let actual = (col("count").gt(num(5))).not();
let expected = "NOT count > 5";
test_expr(actual, expected);

let actual = num(10).factorial();
let expected = "10!";
test_expr(actual, expected);
}
}