risingwave_sqlparser/ast/
operator.rs1use std::fmt;
14
15use super::Ident;
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub enum UnaryOperator {
20 Plus,
21 Minus,
22 Not,
23 Custom(String),
24 PGQualified(Box<QualifiedOperator>),
26}
27
28impl fmt::Display for UnaryOperator {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 if let UnaryOperator::PGQualified(op) = self {
31 return op.fmt(f);
32 }
33 f.write_str(match self {
34 UnaryOperator::Plus => "+",
35 UnaryOperator::Minus => "-",
36 UnaryOperator::Not => "NOT",
37 UnaryOperator::Custom(name) => name,
38 UnaryOperator::PGQualified(_) => unreachable!(),
39 })
40 }
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Hash)]
45pub enum BinaryOperator {
46 Plus,
47 Minus,
48 Multiply,
49 Divide,
50 Modulo,
51 Gt,
52 Lt,
53 GtEq,
54 LtEq,
55 Eq,
56 NotEq,
57 And,
58 Or,
59 Xor,
60 Pow,
61 Custom(String),
62 PGQualified(Box<QualifiedOperator>),
63}
64
65impl fmt::Display for BinaryOperator {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 if let BinaryOperator::PGQualified(op) = self {
68 return op.fmt(f);
69 }
70 f.write_str(match self {
71 BinaryOperator::Plus => "+",
72 BinaryOperator::Minus => "-",
73 BinaryOperator::Multiply => "*",
74 BinaryOperator::Divide => "/",
75 BinaryOperator::Modulo => "%",
76 BinaryOperator::Gt => ">",
77 BinaryOperator::Lt => "<",
78 BinaryOperator::GtEq => ">=",
79 BinaryOperator::LtEq => "<=",
80 BinaryOperator::Eq => "=",
81 BinaryOperator::NotEq => "<>",
82 BinaryOperator::And => "AND",
83 BinaryOperator::Or => "OR",
84 BinaryOperator::Xor => "XOR",
85 BinaryOperator::Pow => "^",
86 BinaryOperator::Custom(name) => name,
87 BinaryOperator::PGQualified(_) => unreachable!(),
88 })
89 }
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, Hash)]
95pub struct QualifiedOperator {
96 pub schema: Option<Ident>,
97 pub name: String,
98}
99
100impl fmt::Display for QualifiedOperator {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 f.write_str("OPERATOR(")?;
103 if let Some(ident) = &self.schema {
104 write!(f, "{ident}.")?;
105 }
106 f.write_str(&self.name)?;
107 f.write_str(")")
108 }
109}