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 /// String matches pattern (case sensitively), e.g. `a ~~ b` (PostgreSQL-specific)
135 PGLikeMatch,
136 /// String matches pattern (case insensitively), e.g. `a ~~* b` (PostgreSQL-specific)
137 PGILikeMatch,
138 /// String does not match pattern (case sensitively), e.g. `a !~~ b` (PostgreSQL-specific)
139 PGNotLikeMatch,
140 /// String does not match pattern (case insensitively), e.g. `a !~~* b` (PostgreSQL-specific)
141 PGNotILikeMatch,
142 /// String "starts with", eg: `a ^@ b` (PostgreSQL-specific)
143 PGStartsWith,
144 /// PostgreSQL-specific custom operator.
145 ///
146 /// See [CREATE OPERATOR](https://www.postgresql.org/docs/current/sql-createoperator.html)
147 /// for more information.
148 PGCustomBinaryOperator(Vec<String>),
149}
150
151impl fmt::Display for BinaryOperator {
152 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153 match self {
154 BinaryOperator::Plus => f.write_str("+"),
155 BinaryOperator::Minus => f.write_str("-"),
156 BinaryOperator::Multiply => f.write_str("*"),
157 BinaryOperator::Divide => f.write_str("/"),
158 BinaryOperator::Modulo => f.write_str("%"),
159 BinaryOperator::StringConcat => f.write_str("||"),
160 BinaryOperator::Gt => f.write_str(">"),
161 BinaryOperator::Lt => f.write_str("<"),
162 BinaryOperator::GtEq => f.write_str(">="),
163 BinaryOperator::LtEq => f.write_str("<="),
164 BinaryOperator::Spaceship => f.write_str("<=>"),
165 BinaryOperator::Eq => f.write_str("="),
166 BinaryOperator::NotEq => f.write_str("<>"),
167 BinaryOperator::And => f.write_str("AND"),
168 BinaryOperator::Or => f.write_str("OR"),
169 BinaryOperator::Xor => f.write_str("XOR"),
170 BinaryOperator::BitwiseOr => f.write_str("|"),
171 BinaryOperator::BitwiseAnd => f.write_str("&"),
172 BinaryOperator::BitwiseXor => f.write_str("^"),
173 BinaryOperator::DuckIntegerDivide => f.write_str("//"),
174 BinaryOperator::MyIntegerDivide => f.write_str("DIV"),
175 BinaryOperator::Custom(s) => f.write_str(s),
176 BinaryOperator::PGBitwiseXor => f.write_str("#"),
177 BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
178 BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
179 BinaryOperator::PGExp => f.write_str("^"),
180 BinaryOperator::PGOverlap => f.write_str("&&"),
181 BinaryOperator::PGRegexMatch => f.write_str("~"),
182 BinaryOperator::PGRegexIMatch => f.write_str("~*"),
183 BinaryOperator::PGRegexNotMatch => f.write_str("!~"),
184 BinaryOperator::PGRegexNotIMatch => f.write_str("!~*"),
185 BinaryOperator::PGLikeMatch => f.write_str("~~"),
186 BinaryOperator::PGILikeMatch => f.write_str("~~*"),
187 BinaryOperator::PGNotLikeMatch => f.write_str("!~~"),
188 BinaryOperator::PGNotILikeMatch => f.write_str("!~~*"),
189 BinaryOperator::PGStartsWith => f.write_str("^@"),
190 BinaryOperator::PGCustomBinaryOperator(idents) => {
191 write!(f, "OPERATOR({})", display_separated(idents, "."))
192 }
193 }
194 }
195}