1use std::fmt;
6
7#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Hash)]
9pub enum Operator {
10 Eq,
12 NotEq,
14 Lt,
16 LtEq,
18 Gt,
20 GtEq,
22 Plus,
24 Minus,
26 Multiply,
28 Divide,
30 Modulo,
32 And,
34 Or,
36 IsDistinctFrom,
40 IsNotDistinctFrom,
44 RegexMatch,
46 RegexIMatch,
48 RegexNotMatch,
50 RegexNotIMatch,
52 LikeMatch,
54 ILikeMatch,
56 NotLikeMatch,
58 NotILikeMatch,
60 BitwiseAnd,
62 BitwiseOr,
64 BitwiseXor,
66 BitwiseShiftRight,
68 BitwiseShiftLeft,
70 StringConcat,
72 AtArrow,
79 ArrowAt,
86 Arrow,
90 LongArrow,
94 HashArrow,
98 HashLongArrow,
102 AtAt,
106 IntegerDivide,
110 HashMinus,
114 AtQuestion,
118 Question,
122 QuestionAnd,
126 QuestionPipe,
130 Colon,
134}
135
136impl Operator {
137 pub fn negate(&self) -> Option<Operator> {
140 match self {
141 Operator::Eq => Some(Operator::NotEq),
142 Operator::NotEq => Some(Operator::Eq),
143 Operator::Lt => Some(Operator::GtEq),
144 Operator::LtEq => Some(Operator::Gt),
145 Operator::Gt => Some(Operator::LtEq),
146 Operator::GtEq => Some(Operator::Lt),
147 Operator::IsDistinctFrom => Some(Operator::IsNotDistinctFrom),
148 Operator::IsNotDistinctFrom => Some(Operator::IsDistinctFrom),
149 Operator::LikeMatch => Some(Operator::NotLikeMatch),
150 Operator::ILikeMatch => Some(Operator::NotILikeMatch),
151 Operator::NotLikeMatch => Some(Operator::LikeMatch),
152 Operator::NotILikeMatch => Some(Operator::ILikeMatch),
153 Operator::RegexMatch => Some(Operator::RegexNotMatch),
154 Operator::RegexIMatch => Some(Operator::RegexNotIMatch),
155 Operator::RegexNotMatch => Some(Operator::RegexMatch),
156 Operator::RegexNotIMatch => Some(Operator::RegexIMatch),
157 Operator::Plus
158 | Operator::Minus
159 | Operator::Multiply
160 | Operator::Divide
161 | Operator::Modulo
162 | Operator::And
163 | Operator::Or
164 | Operator::BitwiseAnd
165 | Operator::BitwiseOr
166 | Operator::BitwiseXor
167 | Operator::BitwiseShiftRight
168 | Operator::BitwiseShiftLeft
169 | Operator::StringConcat
170 | Operator::AtArrow
171 | Operator::ArrowAt
172 | Operator::Arrow
173 | Operator::LongArrow
174 | Operator::HashArrow
175 | Operator::HashLongArrow
176 | Operator::AtAt
177 | Operator::IntegerDivide
178 | Operator::HashMinus
179 | Operator::AtQuestion
180 | Operator::Question
181 | Operator::QuestionAnd
182 | Operator::QuestionPipe
183 | Operator::Colon => None,
184 }
185 }
186
187 pub fn is_numerical_operators(&self) -> bool {
192 matches!(
193 self,
194 Operator::Plus
195 | Operator::Minus
196 | Operator::Multiply
197 | Operator::Divide
198 | Operator::Modulo
199 )
200 }
201
202 pub fn supports_propagation(&self) -> bool {
207 matches!(
208 self,
209 Operator::Eq
210 | Operator::NotEq
211 | Operator::Lt
212 | Operator::LtEq
213 | Operator::Gt
214 | Operator::GtEq
215 | Operator::IsDistinctFrom
216 | Operator::IsNotDistinctFrom
217 | Operator::RegexMatch
218 | Operator::RegexIMatch
219 | Operator::RegexNotMatch
220 | Operator::RegexNotIMatch
221 )
222 }
223
224 pub fn is_logic_operator(&self) -> bool {
229 matches!(self, Operator::And | Operator::Or)
230 }
231
232 pub fn swap(&self) -> Option<Operator> {
236 match self {
237 Operator::Eq => Some(Operator::Eq),
238 Operator::NotEq => Some(Operator::NotEq),
239 Operator::Lt => Some(Operator::Gt),
240 Operator::LtEq => Some(Operator::GtEq),
241 Operator::Gt => Some(Operator::Lt),
242 Operator::GtEq => Some(Operator::LtEq),
243 Operator::AtArrow => Some(Operator::ArrowAt),
244 Operator::ArrowAt => Some(Operator::AtArrow),
245 Operator::IsDistinctFrom => Some(Operator::IsDistinctFrom),
246 Operator::IsNotDistinctFrom => Some(Operator::IsNotDistinctFrom),
247 Operator::Plus
248 | Operator::Minus
249 | Operator::Multiply
250 | Operator::Divide
251 | Operator::Modulo
252 | Operator::And
253 | Operator::Or
254 | Operator::RegexMatch
255 | Operator::RegexIMatch
256 | Operator::RegexNotMatch
257 | Operator::RegexNotIMatch
258 | Operator::LikeMatch
259 | Operator::ILikeMatch
260 | Operator::NotLikeMatch
261 | Operator::NotILikeMatch
262 | Operator::BitwiseAnd
263 | Operator::BitwiseOr
264 | Operator::BitwiseXor
265 | Operator::BitwiseShiftRight
266 | Operator::BitwiseShiftLeft
267 | Operator::StringConcat
268 | Operator::Arrow
269 | Operator::LongArrow
270 | Operator::HashArrow
271 | Operator::HashLongArrow
272 | Operator::AtAt
273 | Operator::IntegerDivide
274 | Operator::HashMinus
275 | Operator::AtQuestion
276 | Operator::Question
277 | Operator::QuestionAnd
278 | Operator::QuestionPipe
279 | Operator::Colon => None,
280 }
281 }
282
283 pub fn precedence(&self) -> u8 {
286 match self {
287 Operator::Or => 5,
288 Operator::And => 10,
289 Operator::Eq | Operator::NotEq | Operator::LtEq | Operator::GtEq => 15,
290 Operator::Lt | Operator::Gt => 20,
291 Operator::LikeMatch
292 | Operator::NotLikeMatch
293 | Operator::ILikeMatch
294 | Operator::NotILikeMatch => 25,
295 Operator::IsDistinctFrom
296 | Operator::IsNotDistinctFrom
297 | Operator::RegexMatch
298 | Operator::RegexNotMatch
299 | Operator::RegexIMatch
300 | Operator::RegexNotIMatch
301 | Operator::BitwiseAnd
302 | Operator::BitwiseOr
303 | Operator::BitwiseShiftLeft
304 | Operator::BitwiseShiftRight
305 | Operator::BitwiseXor
306 | Operator::StringConcat
307 | Operator::AtArrow
308 | Operator::ArrowAt
309 | Operator::Arrow
310 | Operator::LongArrow
311 | Operator::HashArrow
312 | Operator::HashLongArrow
313 | Operator::AtAt
314 | Operator::IntegerDivide
315 | Operator::HashMinus
316 | Operator::AtQuestion
317 | Operator::Question
318 | Operator::QuestionAnd
319 | Operator::QuestionPipe
320 | Operator::Colon => 30,
321 Operator::Plus | Operator::Minus => 40,
322 Operator::Multiply | Operator::Divide | Operator::Modulo => 45,
323 }
324 }
325
326 pub fn returns_null_on_null(&self) -> bool {
329 match self {
330 Operator::Eq
331 | Operator::NotEq
332 | Operator::Lt
333 | Operator::LtEq
334 | Operator::Gt
335 | Operator::GtEq
336 | Operator::Plus
337 | Operator::Minus
338 | Operator::Multiply
339 | Operator::Divide
340 | Operator::Modulo
341 | Operator::RegexMatch
342 | Operator::RegexIMatch
343 | Operator::RegexNotMatch
344 | Operator::RegexNotIMatch
345 | Operator::LikeMatch
346 | Operator::ILikeMatch
347 | Operator::NotLikeMatch
348 | Operator::NotILikeMatch
349 | Operator::BitwiseAnd
350 | Operator::BitwiseOr
351 | Operator::BitwiseXor
352 | Operator::BitwiseShiftRight
353 | Operator::BitwiseShiftLeft
354 | Operator::AtArrow
355 | Operator::ArrowAt
356 | Operator::Arrow
357 | Operator::LongArrow
358 | Operator::HashArrow
359 | Operator::HashLongArrow
360 | Operator::AtAt
361 | Operator::IntegerDivide
362 | Operator::HashMinus
363 | Operator::AtQuestion
364 | Operator::Question
365 | Operator::QuestionAnd
366 | Operator::QuestionPipe
367 | Operator::Colon
368 | Operator::StringConcat => true,
369
370 Operator::Or
372 | Operator::And
374 | Operator::IsDistinctFrom
376 | Operator::IsNotDistinctFrom => false,
377 }
378 }
379
380 pub fn from_proto_name(name: &str) -> Option<Operator> {
388 Some(match name {
389 "And" => Operator::And,
390 "Or" => Operator::Or,
391 "Eq" => Operator::Eq,
392 "NotEq" => Operator::NotEq,
393 "LtEq" => Operator::LtEq,
394 "Lt" => Operator::Lt,
395 "Gt" => Operator::Gt,
396 "GtEq" => Operator::GtEq,
397 "Plus" => Operator::Plus,
398 "Minus" => Operator::Minus,
399 "Multiply" => Operator::Multiply,
400 "Divide" => Operator::Divide,
401 "Modulo" => Operator::Modulo,
402 "IsDistinctFrom" => Operator::IsDistinctFrom,
403 "IsNotDistinctFrom" => Operator::IsNotDistinctFrom,
404 "BitwiseAnd" => Operator::BitwiseAnd,
405 "BitwiseOr" => Operator::BitwiseOr,
406 "BitwiseXor" => Operator::BitwiseXor,
407 "BitwiseShiftLeft" => Operator::BitwiseShiftLeft,
408 "BitwiseShiftRight" => Operator::BitwiseShiftRight,
409 "RegexIMatch" => Operator::RegexIMatch,
410 "RegexMatch" => Operator::RegexMatch,
411 "RegexNotIMatch" => Operator::RegexNotIMatch,
412 "RegexNotMatch" => Operator::RegexNotMatch,
413 "LikeMatch" => Operator::LikeMatch,
414 "ILikeMatch" => Operator::ILikeMatch,
415 "NotLikeMatch" => Operator::NotLikeMatch,
416 "NotILikeMatch" => Operator::NotILikeMatch,
417 "StringConcat" => Operator::StringConcat,
418 "AtArrow" => Operator::AtArrow,
419 "ArrowAt" => Operator::ArrowAt,
420 _ => return None,
421 })
422 }
423}
424
425impl fmt::Display for Operator {
426 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
427 let display = match &self {
428 Operator::Eq => "=",
429 Operator::NotEq => "!=",
430 Operator::Lt => "<",
431 Operator::LtEq => "<=",
432 Operator::Gt => ">",
433 Operator::GtEq => ">=",
434 Operator::Plus => "+",
435 Operator::Minus => "-",
436 Operator::Multiply => "*",
437 Operator::Divide => "/",
438 Operator::Modulo => "%",
439 Operator::And => "AND",
440 Operator::Or => "OR",
441 Operator::RegexMatch => "~",
442 Operator::RegexIMatch => "~*",
443 Operator::RegexNotMatch => "!~",
444 Operator::RegexNotIMatch => "!~*",
445 Operator::LikeMatch => "~~",
446 Operator::ILikeMatch => "~~*",
447 Operator::NotLikeMatch => "!~~",
448 Operator::NotILikeMatch => "!~~*",
449 Operator::IsDistinctFrom => "IS DISTINCT FROM",
450 Operator::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
451 Operator::BitwiseAnd => "&",
452 Operator::BitwiseOr => "|",
453 Operator::BitwiseXor => "BIT_XOR",
454 Operator::BitwiseShiftRight => ">>",
455 Operator::BitwiseShiftLeft => "<<",
456 Operator::StringConcat => "||",
457 Operator::AtArrow => "@>",
458 Operator::ArrowAt => "<@",
459 Operator::Arrow => "->",
460 Operator::LongArrow => "->>",
461 Operator::HashArrow => "#>",
462 Operator::HashLongArrow => "#>>",
463 Operator::AtAt => "@@",
464 Operator::IntegerDivide => "DIV",
465 Operator::HashMinus => "#-",
466 Operator::AtQuestion => "@?",
467 Operator::Question => "?",
468 Operator::QuestionAnd => "?&",
469 Operator::QuestionPipe => "?|",
470 Operator::Colon => ":",
471 };
472 write!(f, "{display}")
473 }
474}