Skip to main content

datafusion_expr_common/
operator.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use std::fmt;
6
7/// Operators applied to expressions
8#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Hash)]
9pub enum Operator {
10    /// Expressions are equal
11    Eq,
12    /// Expressions are not equal
13    NotEq,
14    /// Left side is smaller than right side
15    Lt,
16    /// Left side is smaller or equal to right side
17    LtEq,
18    /// Left side is greater than right side
19    Gt,
20    /// Left side is greater or equal to right side
21    GtEq,
22    /// Addition
23    Plus,
24    /// Subtraction
25    Minus,
26    /// Multiplication
27    Multiply,
28    /// Division
29    Divide,
30    /// Remainder
31    Modulo,
32    /// Logical AND
33    And,
34    /// Logical OR
35    Or,
36    /// `IS DISTINCT FROM` (see [`distinct`])
37    ///
38    /// [`distinct`]: arrow::compute::kernels::cmp::distinct
39    IsDistinctFrom,
40    /// `IS NOT DISTINCT FROM` (see [`not_distinct`])
41    ///
42    /// [`not_distinct`]: arrow::compute::kernels::cmp::not_distinct
43    IsNotDistinctFrom,
44    /// Case sensitive regex match
45    RegexMatch,
46    /// Case insensitive regex match
47    RegexIMatch,
48    /// Case sensitive regex not match
49    RegexNotMatch,
50    /// Case insensitive regex not match
51    RegexNotIMatch,
52    /// Case sensitive pattern match
53    LikeMatch,
54    /// Case insensitive pattern match
55    ILikeMatch,
56    /// Case sensitive pattern not match
57    NotLikeMatch,
58    /// Case insensitive pattern not match
59    NotILikeMatch,
60    /// Bitwise and, like `&`
61    BitwiseAnd,
62    /// Bitwise or, like `|`
63    BitwiseOr,
64    /// Bitwise xor, such as `^` in MySQL or `#` in PostgreSQL
65    BitwiseXor,
66    /// Bitwise right, like `>>`
67    BitwiseShiftRight,
68    /// Bitwise left, like `<<`
69    BitwiseShiftLeft,
70    /// String concatenation, like `||`
71    StringConcat,
72    /// At arrow, like `@>`.
73    ///
74    /// Currently only supported to be used with lists:
75    /// ```sql
76    /// select [1,2,3] @> [1,3]
77    /// ```
78    AtArrow,
79    /// Arrow at, like `<@`.
80    ///
81    /// Currently only supported to be used with lists:
82    /// ```sql
83    /// select [1,3] <@ [1,2,3]
84    /// ```
85    ArrowAt,
86    /// Arrow, like `->`.
87    ///
88    /// Not implemented in DataFusion yet.
89    Arrow,
90    /// Long arrow, like `->>`
91    ///
92    /// Not implemented in DataFusion yet.
93    LongArrow,
94    /// Hash arrow, like `#>`
95    ///
96    /// Not implemented in DataFusion yet.
97    HashArrow,
98    /// Hash long arrow, like `#>>`
99    ///
100    /// Not implemented in DataFusion yet.
101    HashLongArrow,
102    /// At at, like `@@`
103    ///
104    /// Not implemented in DataFusion yet.
105    AtAt,
106    /// Integer division operator, like `DIV` from MySQL or `//` from DuckDB
107    ///
108    /// Not implemented in DataFusion yet.
109    IntegerDivide,
110    /// Hash Minus, like `#-`
111    ///
112    /// Not implemented in DataFusion yet.
113    HashMinus,
114    /// At question, like `@?`
115    ///
116    /// Not implemented in DataFusion yet.
117    AtQuestion,
118    /// Question, like `?`
119    ///
120    /// Not implemented in DataFusion yet.
121    Question,
122    /// Question and, like `?&`
123    ///
124    /// Not implemented in DataFusion yet.
125    QuestionAnd,
126    /// Question pipe, like `?|`
127    ///
128    /// Not implemented in DataFusion yet.
129    QuestionPipe,
130    /// Colon operator, like `:`
131    ///
132    /// Not implemented in DataFusion yet.
133    Colon,
134}
135
136impl Operator {
137    /// If the operator can be negated, return the negated operator
138    /// otherwise return None
139    pub fn negate(&self) -> Option<Operator> {
140        match self {
141            Operator::Eq => Some(Operator::NotEq),
142            Operator::NotEq => Some(Operator::Eq),
143            Operator::Lt => Some(Operator::GtEq),
144            Operator::LtEq => Some(Operator::Gt),
145            Operator::Gt => Some(Operator::LtEq),
146            Operator::GtEq => Some(Operator::Lt),
147            Operator::IsDistinctFrom => Some(Operator::IsNotDistinctFrom),
148            Operator::IsNotDistinctFrom => Some(Operator::IsDistinctFrom),
149            Operator::LikeMatch => Some(Operator::NotLikeMatch),
150            Operator::ILikeMatch => Some(Operator::NotILikeMatch),
151            Operator::NotLikeMatch => Some(Operator::LikeMatch),
152            Operator::NotILikeMatch => Some(Operator::ILikeMatch),
153            Operator::RegexMatch => Some(Operator::RegexNotMatch),
154            Operator::RegexIMatch => Some(Operator::RegexNotIMatch),
155            Operator::RegexNotMatch => Some(Operator::RegexMatch),
156            Operator::RegexNotIMatch => Some(Operator::RegexIMatch),
157            Operator::Plus
158            | Operator::Minus
159            | Operator::Multiply
160            | Operator::Divide
161            | Operator::Modulo
162            | Operator::And
163            | Operator::Or
164            | Operator::BitwiseAnd
165            | Operator::BitwiseOr
166            | Operator::BitwiseXor
167            | Operator::BitwiseShiftRight
168            | Operator::BitwiseShiftLeft
169            | Operator::StringConcat
170            | Operator::AtArrow
171            | Operator::ArrowAt
172            | Operator::Arrow
173            | Operator::LongArrow
174            | Operator::HashArrow
175            | Operator::HashLongArrow
176            | Operator::AtAt
177            | Operator::IntegerDivide
178            | Operator::HashMinus
179            | Operator::AtQuestion
180            | Operator::Question
181            | Operator::QuestionAnd
182            | Operator::QuestionPipe
183            | Operator::Colon => None,
184        }
185    }
186
187    /// Return true if the operator is a numerical operator.
188    ///
189    /// For example, 'Binary(a, +, b)' would be a numerical expression.
190    /// PostgresSQL concept: <https://www.postgresql.org/docs/7.0/operators2198.htm>
191    pub fn is_numerical_operators(&self) -> bool {
192        matches!(
193            self,
194            Operator::Plus
195                | Operator::Minus
196                | Operator::Multiply
197                | Operator::Divide
198                | Operator::Modulo
199        )
200    }
201
202    /// Return true if the comparison operator can be used in interval arithmetic and constraint
203    /// propagation
204    ///
205    /// For example, 'Binary(a, >, b)' expression supports propagation.
206    pub fn supports_propagation(&self) -> bool {
207        matches!(
208            self,
209            Operator::Eq
210                | Operator::NotEq
211                | Operator::Lt
212                | Operator::LtEq
213                | Operator::Gt
214                | Operator::GtEq
215                | Operator::IsDistinctFrom
216                | Operator::IsNotDistinctFrom
217                | Operator::RegexMatch
218                | Operator::RegexIMatch
219                | Operator::RegexNotMatch
220                | Operator::RegexNotIMatch
221        )
222    }
223
224    /// Return true if the operator is a logic operator.
225    ///
226    /// For example, 'Binary(Binary(a, >, b), AND, Binary(a, <, b + 3))' would
227    /// be a logical expression.
228    pub fn is_logic_operator(&self) -> bool {
229        matches!(self, Operator::And | Operator::Or)
230    }
231
232    /// Return the operator where swapping lhs and rhs wouldn't change the result.
233    ///
234    /// For example `Binary(50, >=, a)` could also be represented as `Binary(a, <=, 50)`.
235    pub fn swap(&self) -> Option<Operator> {
236        match self {
237            Operator::Eq => Some(Operator::Eq),
238            Operator::NotEq => Some(Operator::NotEq),
239            Operator::Lt => Some(Operator::Gt),
240            Operator::LtEq => Some(Operator::GtEq),
241            Operator::Gt => Some(Operator::Lt),
242            Operator::GtEq => Some(Operator::LtEq),
243            Operator::AtArrow => Some(Operator::ArrowAt),
244            Operator::ArrowAt => Some(Operator::AtArrow),
245            Operator::IsDistinctFrom => Some(Operator::IsDistinctFrom),
246            Operator::IsNotDistinctFrom => Some(Operator::IsNotDistinctFrom),
247            Operator::Plus
248            | Operator::Minus
249            | Operator::Multiply
250            | Operator::Divide
251            | Operator::Modulo
252            | Operator::And
253            | Operator::Or
254            | Operator::RegexMatch
255            | Operator::RegexIMatch
256            | Operator::RegexNotMatch
257            | Operator::RegexNotIMatch
258            | Operator::LikeMatch
259            | Operator::ILikeMatch
260            | Operator::NotLikeMatch
261            | Operator::NotILikeMatch
262            | Operator::BitwiseAnd
263            | Operator::BitwiseOr
264            | Operator::BitwiseXor
265            | Operator::BitwiseShiftRight
266            | Operator::BitwiseShiftLeft
267            | Operator::StringConcat
268            | Operator::Arrow
269            | Operator::LongArrow
270            | Operator::HashArrow
271            | Operator::HashLongArrow
272            | Operator::AtAt
273            | Operator::IntegerDivide
274            | Operator::HashMinus
275            | Operator::AtQuestion
276            | Operator::Question
277            | Operator::QuestionAnd
278            | Operator::QuestionPipe
279            | Operator::Colon => None,
280        }
281    }
282
283    /// Get the operator precedence
284    /// use <https://www.postgresql.org/docs/7.2/sql-precedence.html> as a reference
285    pub fn precedence(&self) -> u8 {
286        match self {
287            Operator::Or => 5,
288            Operator::And => 10,
289            Operator::Eq | Operator::NotEq | Operator::LtEq | Operator::GtEq => 15,
290            Operator::Lt | Operator::Gt => 20,
291            Operator::LikeMatch
292            | Operator::NotLikeMatch
293            | Operator::ILikeMatch
294            | Operator::NotILikeMatch => 25,
295            Operator::IsDistinctFrom
296            | Operator::IsNotDistinctFrom
297            | Operator::RegexMatch
298            | Operator::RegexNotMatch
299            | Operator::RegexIMatch
300            | Operator::RegexNotIMatch
301            | Operator::BitwiseAnd
302            | Operator::BitwiseOr
303            | Operator::BitwiseShiftLeft
304            | Operator::BitwiseShiftRight
305            | Operator::BitwiseXor
306            | Operator::StringConcat
307            | Operator::AtArrow
308            | Operator::ArrowAt
309            | Operator::Arrow
310            | Operator::LongArrow
311            | Operator::HashArrow
312            | Operator::HashLongArrow
313            | Operator::AtAt
314            | Operator::IntegerDivide
315            | Operator::HashMinus
316            | Operator::AtQuestion
317            | Operator::Question
318            | Operator::QuestionAnd
319            | Operator::QuestionPipe
320            | Operator::Colon => 30,
321            Operator::Plus | Operator::Minus => 40,
322            Operator::Multiply | Operator::Divide | Operator::Modulo => 45,
323        }
324    }
325
326    /// Returns true if the `Expr::BinaryOperator` with this operator
327    /// is guaranteed to return null if either side is null.
328    pub fn returns_null_on_null(&self) -> bool {
329        match self {
330            Operator::Eq
331            | Operator::NotEq
332            | Operator::Lt
333            | Operator::LtEq
334            | Operator::Gt
335            | Operator::GtEq
336            | Operator::Plus
337            | Operator::Minus
338            | Operator::Multiply
339            | Operator::Divide
340            | Operator::Modulo
341            | Operator::RegexMatch
342            | Operator::RegexIMatch
343            | Operator::RegexNotMatch
344            | Operator::RegexNotIMatch
345            | Operator::LikeMatch
346            | Operator::ILikeMatch
347            | Operator::NotLikeMatch
348            | Operator::NotILikeMatch
349            | Operator::BitwiseAnd
350            | Operator::BitwiseOr
351            | Operator::BitwiseXor
352            | Operator::BitwiseShiftRight
353            | Operator::BitwiseShiftLeft
354            | Operator::AtArrow
355            | Operator::ArrowAt
356            | Operator::Arrow
357            | Operator::LongArrow
358            | Operator::HashArrow
359            | Operator::HashLongArrow
360            | Operator::AtAt
361            | Operator::IntegerDivide
362            | Operator::HashMinus
363            | Operator::AtQuestion
364            | Operator::Question
365            | Operator::QuestionAnd
366            | Operator::QuestionPipe
367            | Operator::Colon
368            | Operator::StringConcat => true,
369
370            // E.g. `TRUE OR NULL` is `TRUE`
371            Operator::Or
372            // E.g. `FALSE AND NULL` is `FALSE`
373            | Operator::And
374            // IS DISTINCT FROM and IS NOT DISTINCT FROM always return a TRUE/FALSE value, never NULL
375            | Operator::IsDistinctFrom
376            | Operator::IsNotDistinctFrom => false,
377        }
378    }
379
380    /// Parse an `Operator` from the string name `datafusion-proto` uses on the
381    /// wire (the `Debug` name of the variant, e.g. `"Eq"`).
382    ///
383    /// Returns `None` for names with no binary-operator counterpart. This is
384    /// the canonical proto-string mapping, shared by `datafusion-proto`
385    /// (logical plans) and `PhysicalExpr` decoders such as `BinaryExpr`, so the
386    /// mapping is not duplicated across crates.
387    pub fn from_proto_name(name: &str) -> Option<Operator> {
388        Some(match name {
389            "And" => Operator::And,
390            "Or" => Operator::Or,
391            "Eq" => Operator::Eq,
392            "NotEq" => Operator::NotEq,
393            "LtEq" => Operator::LtEq,
394            "Lt" => Operator::Lt,
395            "Gt" => Operator::Gt,
396            "GtEq" => Operator::GtEq,
397            "Plus" => Operator::Plus,
398            "Minus" => Operator::Minus,
399            "Multiply" => Operator::Multiply,
400            "Divide" => Operator::Divide,
401            "Modulo" => Operator::Modulo,
402            "IsDistinctFrom" => Operator::IsDistinctFrom,
403            "IsNotDistinctFrom" => Operator::IsNotDistinctFrom,
404            "BitwiseAnd" => Operator::BitwiseAnd,
405            "BitwiseOr" => Operator::BitwiseOr,
406            "BitwiseXor" => Operator::BitwiseXor,
407            "BitwiseShiftLeft" => Operator::BitwiseShiftLeft,
408            "BitwiseShiftRight" => Operator::BitwiseShiftRight,
409            "RegexIMatch" => Operator::RegexIMatch,
410            "RegexMatch" => Operator::RegexMatch,
411            "RegexNotIMatch" => Operator::RegexNotIMatch,
412            "RegexNotMatch" => Operator::RegexNotMatch,
413            "LikeMatch" => Operator::LikeMatch,
414            "ILikeMatch" => Operator::ILikeMatch,
415            "NotLikeMatch" => Operator::NotLikeMatch,
416            "NotILikeMatch" => Operator::NotILikeMatch,
417            "StringConcat" => Operator::StringConcat,
418            "AtArrow" => Operator::AtArrow,
419            "ArrowAt" => Operator::ArrowAt,
420            _ => return None,
421        })
422    }
423}
424
425impl fmt::Display for Operator {
426    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
427        let display = match &self {
428            Operator::Eq => "=",
429            Operator::NotEq => "!=",
430            Operator::Lt => "<",
431            Operator::LtEq => "<=",
432            Operator::Gt => ">",
433            Operator::GtEq => ">=",
434            Operator::Plus => "+",
435            Operator::Minus => "-",
436            Operator::Multiply => "*",
437            Operator::Divide => "/",
438            Operator::Modulo => "%",
439            Operator::And => "AND",
440            Operator::Or => "OR",
441            Operator::RegexMatch => "~",
442            Operator::RegexIMatch => "~*",
443            Operator::RegexNotMatch => "!~",
444            Operator::RegexNotIMatch => "!~*",
445            Operator::LikeMatch => "~~",
446            Operator::ILikeMatch => "~~*",
447            Operator::NotLikeMatch => "!~~",
448            Operator::NotILikeMatch => "!~~*",
449            Operator::IsDistinctFrom => "IS DISTINCT FROM",
450            Operator::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
451            Operator::BitwiseAnd => "&",
452            Operator::BitwiseOr => "|",
453            Operator::BitwiseXor => "BIT_XOR",
454            Operator::BitwiseShiftRight => ">>",
455            Operator::BitwiseShiftLeft => "<<",
456            Operator::StringConcat => "||",
457            Operator::AtArrow => "@>",
458            Operator::ArrowAt => "<@",
459            Operator::Arrow => "->",
460            Operator::LongArrow => "->>",
461            Operator::HashArrow => "#>",
462            Operator::HashLongArrow => "#>>",
463            Operator::AtAt => "@@",
464            Operator::IntegerDivide => "DIV",
465            Operator::HashMinus => "#-",
466            Operator::AtQuestion => "@?",
467            Operator::Question => "?",
468            Operator::QuestionAnd => "?&",
469            Operator::QuestionPipe => "?|",
470            Operator::Colon => ":",
471        };
472        write!(f, "{display}")
473    }
474}