use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum Operator {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
Plus,
Minus,
Multiply,
Divide,
Modulo,
And,
Or,
IsDistinctFrom,
IsNotDistinctFrom,
RegexMatch,
RegexIMatch,
RegexNotMatch,
RegexNotIMatch,
LikeMatch,
ILikeMatch,
NotLikeMatch,
NotILikeMatch,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
BitwiseShiftRight,
BitwiseShiftLeft,
StringConcat,
AtArrow,
ArrowAt,
Arrow,
LongArrow,
HashArrow,
HashLongArrow,
AtAt,
IntegerDivide,
HashMinus,
AtQuestion,
Question,
QuestionAnd,
QuestionPipe,
}
impl Operator {
pub fn negate(&self) -> Option<Operator> {
match self {
Operator::Eq => Some(Operator::NotEq),
Operator::NotEq => Some(Operator::Eq),
Operator::Lt => Some(Operator::GtEq),
Operator::LtEq => Some(Operator::Gt),
Operator::Gt => Some(Operator::LtEq),
Operator::GtEq => Some(Operator::Lt),
Operator::IsDistinctFrom => Some(Operator::IsNotDistinctFrom),
Operator::IsNotDistinctFrom => Some(Operator::IsDistinctFrom),
Operator::LikeMatch => Some(Operator::NotLikeMatch),
Operator::ILikeMatch => Some(Operator::NotILikeMatch),
Operator::NotLikeMatch => Some(Operator::LikeMatch),
Operator::NotILikeMatch => Some(Operator::ILikeMatch),
Operator::Plus
| Operator::Minus
| Operator::Multiply
| Operator::Divide
| Operator::Modulo
| Operator::And
| Operator::Or
| Operator::RegexMatch
| Operator::RegexIMatch
| Operator::RegexNotMatch
| Operator::RegexNotIMatch
| Operator::BitwiseAnd
| Operator::BitwiseOr
| Operator::BitwiseXor
| Operator::BitwiseShiftRight
| Operator::BitwiseShiftLeft
| Operator::StringConcat
| Operator::AtArrow
| Operator::ArrowAt
| Operator::Arrow
| Operator::LongArrow
| Operator::HashArrow
| Operator::HashLongArrow
| Operator::AtAt
| Operator::IntegerDivide
| Operator::HashMinus
| Operator::AtQuestion
| Operator::Question
| Operator::QuestionAnd
| Operator::QuestionPipe => None,
}
}
pub fn is_numerical_operators(&self) -> bool {
matches!(
self,
Operator::Plus
| Operator::Minus
| Operator::Multiply
| Operator::Divide
| Operator::Modulo
)
}
pub fn supports_propagation(&self) -> bool {
matches!(
self,
Operator::Eq
| Operator::NotEq
| Operator::Lt
| Operator::LtEq
| Operator::Gt
| Operator::GtEq
| Operator::IsDistinctFrom
| Operator::IsNotDistinctFrom
| Operator::RegexMatch
| Operator::RegexIMatch
| Operator::RegexNotMatch
| Operator::RegexNotIMatch
)
}
#[deprecated(since = "43.0.0", note = "please use `supports_propagation` instead")]
pub fn is_comparison_operator(&self) -> bool {
self.supports_propagation()
}
pub fn is_logic_operator(&self) -> bool {
matches!(self, Operator::And | Operator::Or)
}
pub fn swap(&self) -> Option<Operator> {
match self {
Operator::Eq => Some(Operator::Eq),
Operator::NotEq => Some(Operator::NotEq),
Operator::Lt => Some(Operator::Gt),
Operator::LtEq => Some(Operator::GtEq),
Operator::Gt => Some(Operator::Lt),
Operator::GtEq => Some(Operator::LtEq),
Operator::AtArrow => Some(Operator::ArrowAt),
Operator::ArrowAt => Some(Operator::AtArrow),
Operator::IsDistinctFrom
| Operator::IsNotDistinctFrom
| Operator::Plus
| Operator::Minus
| Operator::Multiply
| Operator::Divide
| Operator::Modulo
| Operator::And
| Operator::Or
| Operator::RegexMatch
| Operator::RegexIMatch
| Operator::RegexNotMatch
| Operator::RegexNotIMatch
| Operator::LikeMatch
| Operator::ILikeMatch
| Operator::NotLikeMatch
| Operator::NotILikeMatch
| Operator::BitwiseAnd
| Operator::BitwiseOr
| Operator::BitwiseXor
| Operator::BitwiseShiftRight
| Operator::BitwiseShiftLeft
| Operator::StringConcat
| Operator::Arrow
| Operator::LongArrow
| Operator::HashArrow
| Operator::HashLongArrow
| Operator::AtAt
| Operator::IntegerDivide
| Operator::HashMinus
| Operator::AtQuestion
| Operator::Question
| Operator::QuestionAnd
| Operator::QuestionPipe => None,
}
}
pub fn precedence(&self) -> u8 {
match self {
Operator::Or => 5,
Operator::And => 10,
Operator::Eq | Operator::NotEq | Operator::LtEq | Operator::GtEq => 15,
Operator::Lt | Operator::Gt => 20,
Operator::LikeMatch
| Operator::NotLikeMatch
| Operator::ILikeMatch
| Operator::NotILikeMatch => 25,
Operator::IsDistinctFrom
| Operator::IsNotDistinctFrom
| Operator::RegexMatch
| Operator::RegexNotMatch
| Operator::RegexIMatch
| Operator::RegexNotIMatch
| Operator::BitwiseAnd
| Operator::BitwiseOr
| Operator::BitwiseShiftLeft
| Operator::BitwiseShiftRight
| Operator::BitwiseXor
| Operator::StringConcat
| Operator::AtArrow
| Operator::ArrowAt
| Operator::Arrow
| Operator::LongArrow
| Operator::HashArrow
| Operator::HashLongArrow
| Operator::AtAt
| Operator::IntegerDivide
| Operator::HashMinus
| Operator::AtQuestion
| Operator::Question
| Operator::QuestionAnd
| Operator::QuestionPipe => 30,
Operator::Plus | Operator::Minus => 40,
Operator::Multiply | Operator::Divide | Operator::Modulo => 45,
}
}
}
impl fmt::Display for Operator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let display = match &self {
Operator::Eq => "=",
Operator::NotEq => "!=",
Operator::Lt => "<",
Operator::LtEq => "<=",
Operator::Gt => ">",
Operator::GtEq => ">=",
Operator::Plus => "+",
Operator::Minus => "-",
Operator::Multiply => "*",
Operator::Divide => "/",
Operator::Modulo => "%",
Operator::And => "AND",
Operator::Or => "OR",
Operator::RegexMatch => "~",
Operator::RegexIMatch => "~*",
Operator::RegexNotMatch => "!~",
Operator::RegexNotIMatch => "!~*",
Operator::LikeMatch => "~~",
Operator::ILikeMatch => "~~*",
Operator::NotLikeMatch => "!~~",
Operator::NotILikeMatch => "!~~*",
Operator::IsDistinctFrom => "IS DISTINCT FROM",
Operator::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
Operator::BitwiseAnd => "&",
Operator::BitwiseOr => "|",
Operator::BitwiseXor => "BIT_XOR",
Operator::BitwiseShiftRight => ">>",
Operator::BitwiseShiftLeft => "<<",
Operator::StringConcat => "||",
Operator::AtArrow => "@>",
Operator::ArrowAt => "<@",
Operator::Arrow => "->",
Operator::LongArrow => "->>",
Operator::HashArrow => "#>",
Operator::HashLongArrow => "#>>",
Operator::AtAt => "@@",
Operator::IntegerDivide => "DIV",
Operator::HashMinus => "#-",
Operator::AtQuestion => "@?",
Operator::Question => "?",
Operator::QuestionAnd => "?&",
Operator::QuestionPipe => "?|",
};
write!(f, "{display}")
}
}