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(not(feature = "std"))]
16use alloc::{string::String, vec::Vec};
17
18#[cfg(feature = "serde")]
19use serde::{Deserialize, Serialize};
20
21#[cfg(feature = "visitor")]
22use sqlparser_derive::{Visit, VisitMut};
23
24use super::display_separated;
25
26/// Unary operators
27#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
30pub enum UnaryOperator {
31    /// Plus, e.g. `+9`
32    Plus,
33    /// Minus, e.g. `-9`
34    Minus,
35    /// Not, e.g. `NOT(true)`
36    Not,
37    /// Bitwise Not, e.g. `~9` (PostgreSQL-specific)
38    PGBitwiseNot,
39    /// Square root, e.g. `|/9` (PostgreSQL-specific)
40    PGSquareRoot,
41    /// Cube root, e.g. `||/27` (PostgreSQL-specific)
42    PGCubeRoot,
43    /// Factorial, e.g. `9!` (PostgreSQL-specific)
44    PGPostfixFactorial,
45    /// Factorial, e.g. `!!9` (PostgreSQL-specific)
46    PGPrefixFactorial,
47    /// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
48    PGAbs,
49}
50
51impl fmt::Display for UnaryOperator {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        f.write_str(match self {
54            UnaryOperator::Plus => "+",
55            UnaryOperator::Minus => "-",
56            UnaryOperator::Not => "NOT",
57            UnaryOperator::PGBitwiseNot => "~",
58            UnaryOperator::PGSquareRoot => "|/",
59            UnaryOperator::PGCubeRoot => "||/",
60            UnaryOperator::PGPostfixFactorial => "!",
61            UnaryOperator::PGPrefixFactorial => "!!",
62            UnaryOperator::PGAbs => "@",
63        })
64    }
65}
66
67/// Binary operators
68#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
69#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
70#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
71pub enum BinaryOperator {
72    /// Plus, e.g. `a + b`
73    Plus,
74    /// Minus, e.g. `a - b`
75    Minus,
76    /// Multiply, e.g. `a * b`
77    Multiply,
78    /// Divide, e.g. `a / b`
79    Divide,
80    /// Modulo, e.g. `a % b`
81    Modulo,
82    /// String/Array Concat operator, e.g. `a || b`
83    StringConcat,
84    /// Greater than, e.g. `a > b`
85    Gt,
86    /// Less than, e.g. `a < b`
87    Lt,
88    /// Greater equal, e.g. `a >= b`
89    GtEq,
90    /// Less equal, e.g. `a <= b`
91    LtEq,
92    /// Spaceship, e.g. `a <=> b`
93    Spaceship,
94    /// Equal, e.g. `a = b`
95    Eq,
96    /// Not equal, e.g. `a <> b`
97    NotEq,
98    /// And, e.g. `a AND b`
99    And,
100    /// Or, e.g. `a OR b`
101    Or,
102    /// XOR, e.g. `a XOR b`
103    Xor,
104    /// Bitwise or, e.g. `a | b`
105    BitwiseOr,
106    /// Bitwise and, e.g. `a & b`
107    BitwiseAnd,
108    /// Bitwise XOR, e.g. `a ^ b`
109    BitwiseXor,
110    /// Integer division operator `//` in DuckDB
111    DuckIntegerDivide,
112    /// MySQL [`DIV`](https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html) integer division
113    MyIntegerDivide,
114    /// Support for custom operators (built by parsers outside this crate)
115    Custom(String),
116    /// Bitwise XOR, e.g. `a # b` (PostgreSQL-specific)
117    PGBitwiseXor,
118    /// Bitwise shift left, e.g. `a << b` (PostgreSQL-specific)
119    PGBitwiseShiftLeft,
120    /// Bitwise shift right, e.g. `a >> b` (PostgreSQL-specific)
121    PGBitwiseShiftRight,
122    /// Exponent, e.g. `a ^ b` (PostgreSQL-specific)
123    PGExp,
124    /// Overlap operator, e.g. `a && b` (PostgreSQL-specific)
125    PGOverlap,
126    /// String matches regular expression (case sensitively), e.g. `a ~ b` (PostgreSQL-specific)
127    PGRegexMatch,
128    /// String matches regular expression (case insensitively), e.g. `a ~* b` (PostgreSQL-specific)
129    PGRegexIMatch,
130    /// String does not match regular expression (case sensitively), e.g. `a !~ b` (PostgreSQL-specific)
131    PGRegexNotMatch,
132    /// String does not match regular expression (case insensitively), e.g. `a !~* b` (PostgreSQL-specific)
133    PGRegexNotIMatch,
134    /// PostgreSQL-specific custom operator.
135    ///
136    /// See [CREATE OPERATOR](https://www.postgresql.org/docs/current/sql-createoperator.html)
137    /// for more information.
138    PGCustomBinaryOperator(Vec<String>),
139}
140
141impl fmt::Display for BinaryOperator {
142    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143        match self {
144            BinaryOperator::Plus => f.write_str("+"),
145            BinaryOperator::Minus => f.write_str("-"),
146            BinaryOperator::Multiply => f.write_str("*"),
147            BinaryOperator::Divide => f.write_str("/"),
148            BinaryOperator::Modulo => f.write_str("%"),
149            BinaryOperator::StringConcat => f.write_str("||"),
150            BinaryOperator::Gt => f.write_str(">"),
151            BinaryOperator::Lt => f.write_str("<"),
152            BinaryOperator::GtEq => f.write_str(">="),
153            BinaryOperator::LtEq => f.write_str("<="),
154            BinaryOperator::Spaceship => f.write_str("<=>"),
155            BinaryOperator::Eq => f.write_str("="),
156            BinaryOperator::NotEq => f.write_str("<>"),
157            BinaryOperator::And => f.write_str("AND"),
158            BinaryOperator::Or => f.write_str("OR"),
159            BinaryOperator::Xor => f.write_str("XOR"),
160            BinaryOperator::BitwiseOr => f.write_str("|"),
161            BinaryOperator::BitwiseAnd => f.write_str("&"),
162            BinaryOperator::BitwiseXor => f.write_str("^"),
163            BinaryOperator::DuckIntegerDivide => f.write_str("//"),
164            BinaryOperator::MyIntegerDivide => f.write_str("DIV"),
165            BinaryOperator::Custom(s) => f.write_str(s),
166            BinaryOperator::PGBitwiseXor => f.write_str("#"),
167            BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
168            BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
169            BinaryOperator::PGExp => f.write_str("^"),
170            BinaryOperator::PGOverlap => f.write_str("&&"),
171            BinaryOperator::PGRegexMatch => f.write_str("~"),
172            BinaryOperator::PGRegexIMatch => f.write_str("~*"),
173            BinaryOperator::PGRegexNotMatch => f.write_str("!~"),
174            BinaryOperator::PGRegexNotIMatch => f.write_str("!~*"),
175            BinaryOperator::PGCustomBinaryOperator(idents) => {
176                write!(f, "OPERATOR({})", display_separated(idents, "."))
177            }
178        }
179    }
180}