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