1use crate::expr::{Exists, Expr, InList, InSubquery, Like};
8use crate::expr_fn::binary_expr;
9use datafusion_expr_common::operator::Operator;
10use std::ops::{self, Not};
11
12impl ops::Add for Expr {
14 type Output = Self;
15
16 fn add(self, rhs: Self) -> Self {
17 binary_expr(self, Operator::Plus, rhs)
18 }
19}
20
21impl ops::Sub for Expr {
23 type Output = Self;
24
25 fn sub(self, rhs: Self) -> Self {
26 binary_expr(self, Operator::Minus, rhs)
27 }
28}
29
30impl ops::Mul for Expr {
32 type Output = Self;
33
34 fn mul(self, rhs: Self) -> Self {
35 binary_expr(self, Operator::Multiply, rhs)
36 }
37}
38
39impl ops::Div for Expr {
41 type Output = Self;
42
43 fn div(self, rhs: Self) -> Self {
44 binary_expr(self, Operator::Divide, rhs)
45 }
46}
47
48impl ops::Rem for Expr {
50 type Output = Self;
51
52 fn rem(self, rhs: Self) -> Self {
53 binary_expr(self, Operator::Modulo, rhs)
54 }
55}
56
57impl ops::BitAnd for Expr {
59 type Output = Self;
60
61 fn bitand(self, rhs: Self) -> Self {
62 binary_expr(self, Operator::BitwiseAnd, rhs)
63 }
64}
65
66impl ops::BitOr for Expr {
68 type Output = Self;
69
70 fn bitor(self, rhs: Self) -> Self {
71 binary_expr(self, Operator::BitwiseOr, rhs)
72 }
73}
74
75impl ops::BitXor for Expr {
77 type Output = Self;
78
79 fn bitxor(self, rhs: Self) -> Self {
80 binary_expr(self, Operator::BitwiseXor, rhs)
81 }
82}
83
84impl ops::Shl for Expr {
86 type Output = Self;
87
88 fn shl(self, rhs: Self) -> Self::Output {
89 binary_expr(self, Operator::BitwiseShiftLeft, rhs)
90 }
91}
92
93impl ops::Shr for Expr {
95 type Output = Self;
96
97 fn shr(self, rhs: Self) -> Self::Output {
98 binary_expr(self, Operator::BitwiseShiftRight, rhs)
99 }
100}
101
102impl ops::Neg for Expr {
104 type Output = Self;
105
106 fn neg(self) -> Self::Output {
107 Expr::Negative(Box::new(self))
108 }
109}
110
111impl Not for Expr {
113 type Output = Self;
114
115 fn not(self) -> Self::Output {
116 match self {
117 Expr::Like(Like {
118 negated,
119 expr,
120 pattern,
121 escape_char,
122 case_insensitive,
123 }) => Expr::Like(Like::new(
124 !negated,
125 expr,
126 pattern,
127 escape_char,
128 case_insensitive,
129 )),
130 Expr::SimilarTo(Like {
131 negated,
132 expr,
133 pattern,
134 escape_char,
135 case_insensitive,
136 }) => Expr::SimilarTo(Like::new(
137 !negated,
138 expr,
139 pattern,
140 escape_char,
141 case_insensitive,
142 )),
143 Expr::InList(InList {
144 expr,
145 list,
146 negated,
147 }) => Expr::InList(InList::new(expr, list, !negated)),
148 Expr::Exists(Exists { subquery, negated }) => {
149 Expr::Exists(Exists::new(subquery, !negated))
150 }
151 Expr::InSubquery(InSubquery {
152 expr,
153 subquery,
154 negated,
155 }) => Expr::InSubquery(InSubquery::new(expr, subquery, !negated)),
156 _ => Expr::Not(Box::new(self)),
157 }
158 }
159}
160
161#[cfg(test)]
162mod tests {
163 use crate::lit;
164
165 #[test]
166 fn test_operators() {
167 assert_eq!(
169 format!("{}", lit(1u32) + lit(2u32)),
170 "UInt32(1) + UInt32(2)"
171 );
172 assert_eq!(
174 format!("{}", lit(1u32) - lit(2u32)),
175 "UInt32(1) - UInt32(2)"
176 );
177 assert_eq!(
179 format!("{}", lit(1u32) * lit(2u32)),
180 "UInt32(1) * UInt32(2)"
181 );
182 assert_eq!(
184 format!("{}", lit(1u32) / lit(2u32)),
185 "UInt32(1) / UInt32(2)"
186 );
187 assert_eq!(
189 format!("{}", lit(1u32) % lit(2u32)),
190 "UInt32(1) % UInt32(2)"
191 );
192 assert_eq!(
194 format!("{}", lit(1u32) & lit(2u32)),
195 "UInt32(1) & UInt32(2)"
196 );
197 assert_eq!(
199 format!("{}", lit(1u32) | lit(2u32)),
200 "UInt32(1) | UInt32(2)"
201 );
202 assert_eq!(
204 format!("{}", lit(1u32) ^ lit(2u32)),
205 "UInt32(1) BIT_XOR UInt32(2)"
206 );
207 assert_eq!(
209 format!("{}", lit(1u32) << lit(2u32)),
210 "UInt32(1) << UInt32(2)"
211 );
212 assert_eq!(
214 format!("{}", lit(1u32) >> lit(2u32)),
215 "UInt32(1) >> UInt32(2)"
216 );
217 assert_eq!(format!("{}", -lit(1u32)), "(- UInt32(1))");
219 assert_eq!(format!("{}", !lit(1u32)), "NOT UInt32(1)");
221 }
222}