risingwave_sqlparser/ast/
operator.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use core::fmt;
14
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18use super::Ident;
19
20/// Unary operators
21#[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    /// Qualified, e.g. `OPERATOR(pg_catalog.+) 9` (PostgreSQL-specific)
29    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/// Binary operators
48#[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/// Qualified custom operator
98/// <https://www.postgresql.org/docs/15/sql-expressions.html#SQL-EXPRESSIONS-OPERATOR-CALLS>
99#[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}