use crate::expr_fn::binary_expr;
use crate::{Expr, Like};
use datafusion_expr_common::operator::Operator;
use std::ops::{self, Not};
impl ops::Add for Expr {
type Output = Self;
fn add(self, rhs: Self) -> Self {
binary_expr(self, Operator::Plus, rhs)
}
}
impl ops::Sub for Expr {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
binary_expr(self, Operator::Minus, rhs)
}
}
impl ops::Mul for Expr {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
binary_expr(self, Operator::Multiply, rhs)
}
}
impl ops::Div for Expr {
type Output = Self;
fn div(self, rhs: Self) -> Self {
binary_expr(self, Operator::Divide, rhs)
}
}
impl ops::Rem for Expr {
type Output = Self;
fn rem(self, rhs: Self) -> Self {
binary_expr(self, Operator::Modulo, rhs)
}
}
impl ops::BitAnd for Expr {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
binary_expr(self, Operator::BitwiseAnd, rhs)
}
}
impl ops::BitOr for Expr {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
binary_expr(self, Operator::BitwiseOr, rhs)
}
}
impl ops::BitXor for Expr {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self {
binary_expr(self, Operator::BitwiseXor, rhs)
}
}
impl ops::Shl for Expr {
type Output = Self;
fn shl(self, rhs: Self) -> Self::Output {
binary_expr(self, Operator::BitwiseShiftLeft, rhs)
}
}
impl ops::Shr for Expr {
type Output = Self;
fn shr(self, rhs: Self) -> Self::Output {
binary_expr(self, Operator::BitwiseShiftRight, rhs)
}
}
impl ops::Neg for Expr {
type Output = Self;
fn neg(self) -> Self::Output {
Expr::Negative(Box::new(self))
}
}
impl Not for Expr {
type Output = Self;
fn not(self) -> Self::Output {
match self {
Expr::Like(Like {
negated,
expr,
pattern,
escape_char,
case_insensitive,
}) => Expr::Like(Like::new(
!negated,
expr,
pattern,
escape_char,
case_insensitive,
)),
Expr::SimilarTo(Like {
negated,
expr,
pattern,
escape_char,
case_insensitive,
}) => Expr::SimilarTo(Like::new(
!negated,
expr,
pattern,
escape_char,
case_insensitive,
)),
_ => Expr::Not(Box::new(self)),
}
}
}
#[cfg(test)]
mod tests {
use crate::lit;
#[test]
fn test_operators() {
assert_eq!(
format!("{}", lit(1u32) + lit(2u32)),
"UInt32(1) + UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) - lit(2u32)),
"UInt32(1) - UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) * lit(2u32)),
"UInt32(1) * UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) / lit(2u32)),
"UInt32(1) / UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) % lit(2u32)),
"UInt32(1) % UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) & lit(2u32)),
"UInt32(1) & UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) | lit(2u32)),
"UInt32(1) | UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) ^ lit(2u32)),
"UInt32(1) BIT_XOR UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) << lit(2u32)),
"UInt32(1) << UInt32(2)"
);
assert_eq!(
format!("{}", lit(1u32) >> lit(2u32)),
"UInt32(1) >> UInt32(2)"
);
assert_eq!(format!("{}", -lit(1u32)), "(- UInt32(1))");
assert_eq!(format!("{}", !lit(1u32)), "NOT UInt32(1)");
}
}