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 std::fmt;
14
15use super::Ident;
16
17/// Unary operators
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub enum UnaryOperator {
20    Plus,
21    Minus,
22    Not,
23    Custom(String),
24    /// Qualified, e.g. `OPERATOR(pg_catalog.+) 9` (PostgreSQL-specific)
25    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/// Binary operators
44#[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/// Qualified custom operator
93/// <https://www.postgresql.org/docs/15/sql-expressions.html#SQL-EXPRESSIONS-OPERATOR-CALLS>
94#[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}