vortex_expr/
operators.rs

1use core::fmt;
2use std::fmt::{Display, Formatter};
3
4use vortex_array::compute;
5
6#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub enum Operator {
9    // comparison
10    Eq,
11    NotEq,
12    Gt,
13    Gte,
14    Lt,
15    Lte,
16    // boolean algebra
17    And,
18    Or,
19}
20
21impl Display for Operator {
22    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
23        let display = match &self {
24            Operator::Eq => "=",
25            Operator::NotEq => "!=",
26            Operator::Gt => ">",
27            Operator::Gte => ">=",
28            Operator::Lt => "<",
29            Operator::Lte => "<=",
30            Operator::And => "and",
31            Operator::Or => "or",
32        };
33        Display::fmt(display, f)
34    }
35}
36
37impl Operator {
38    pub fn inverse(self) -> Option<Self> {
39        match self {
40            Operator::Eq => Some(Operator::NotEq),
41            Operator::NotEq => Some(Operator::Eq),
42            Operator::Gt => Some(Operator::Lte),
43            Operator::Gte => Some(Operator::Lt),
44            Operator::Lt => Some(Operator::Gte),
45            Operator::Lte => Some(Operator::Gt),
46            Operator::And | Operator::Or => None,
47        }
48    }
49
50    /// Change the sides of the operator, where changing lhs and rhs won't change the result of the operation
51    pub fn swap(self) -> Self {
52        match self {
53            Operator::Eq => Operator::Eq,
54            Operator::NotEq => Operator::NotEq,
55            Operator::Gt => Operator::Lt,
56            Operator::Gte => Operator::Lte,
57            Operator::Lt => Operator::Gt,
58            Operator::Lte => Operator::Gte,
59            Operator::And => Operator::And,
60            Operator::Or => Operator::Or,
61        }
62    }
63
64    pub fn maybe_cmp_operator(self) -> Option<compute::Operator> {
65        match self {
66            Operator::Eq => Some(compute::Operator::Eq),
67            Operator::NotEq => Some(compute::Operator::NotEq),
68            Operator::Lt => Some(compute::Operator::Lt),
69            Operator::Lte => Some(compute::Operator::Lte),
70            Operator::Gt => Some(compute::Operator::Gt),
71            Operator::Gte => Some(compute::Operator::Gte),
72            _ => None,
73        }
74    }
75}