Skip to main content

datafusion_expr/
operation.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
5//! This module contains implementations of operations (unary, binary etc.) for DataFusion expressions.
6
7use crate::expr::{Exists, Expr, InList, InSubquery, Like};
8use crate::expr_fn::binary_expr;
9use datafusion_expr_common::operator::Operator;
10use std::ops::{self, Not};
11
12/// Support `<expr> + <expr>` fluent style
13impl ops::Add for Expr {
14    type Output = Self;
15
16    fn add(self, rhs: Self) -> Self {
17        binary_expr(self, Operator::Plus, rhs)
18    }
19}
20
21/// Support `<expr> - <expr>` fluent style
22impl ops::Sub for Expr {
23    type Output = Self;
24
25    fn sub(self, rhs: Self) -> Self {
26        binary_expr(self, Operator::Minus, rhs)
27    }
28}
29
30/// Support `<expr> * <expr>` fluent style
31impl ops::Mul for Expr {
32    type Output = Self;
33
34    fn mul(self, rhs: Self) -> Self {
35        binary_expr(self, Operator::Multiply, rhs)
36    }
37}
38
39/// Support `<expr> / <expr>` fluent style
40impl ops::Div for Expr {
41    type Output = Self;
42
43    fn div(self, rhs: Self) -> Self {
44        binary_expr(self, Operator::Divide, rhs)
45    }
46}
47
48/// Support `<expr> % <expr>` fluent style
49impl ops::Rem for Expr {
50    type Output = Self;
51
52    fn rem(self, rhs: Self) -> Self {
53        binary_expr(self, Operator::Modulo, rhs)
54    }
55}
56
57/// Support `<expr> & <expr>` fluent style
58impl ops::BitAnd for Expr {
59    type Output = Self;
60
61    fn bitand(self, rhs: Self) -> Self {
62        binary_expr(self, Operator::BitwiseAnd, rhs)
63    }
64}
65
66/// Support `<expr> | <expr>` fluent style
67impl ops::BitOr for Expr {
68    type Output = Self;
69
70    fn bitor(self, rhs: Self) -> Self {
71        binary_expr(self, Operator::BitwiseOr, rhs)
72    }
73}
74
75/// Support `<expr> ^ <expr>` fluent style
76impl ops::BitXor for Expr {
77    type Output = Self;
78
79    fn bitxor(self, rhs: Self) -> Self {
80        binary_expr(self, Operator::BitwiseXor, rhs)
81    }
82}
83
84/// Support `<expr> << <expr>` fluent style
85impl ops::Shl for Expr {
86    type Output = Self;
87
88    fn shl(self, rhs: Self) -> Self::Output {
89        binary_expr(self, Operator::BitwiseShiftLeft, rhs)
90    }
91}
92
93/// Support `<expr> >> <expr>` fluent style
94impl ops::Shr for Expr {
95    type Output = Self;
96
97    fn shr(self, rhs: Self) -> Self::Output {
98        binary_expr(self, Operator::BitwiseShiftRight, rhs)
99    }
100}
101
102/// Support `- <expr>` fluent style
103impl ops::Neg for Expr {
104    type Output = Self;
105
106    fn neg(self) -> Self::Output {
107        Expr::Negative(Box::new(self))
108    }
109}
110
111/// Support `NOT <expr>` fluent style
112impl Not for Expr {
113    type Output = Self;
114
115    fn not(self) -> Self::Output {
116        match self {
117            Expr::Like(Like {
118                negated,
119                expr,
120                pattern,
121                escape_char,
122                case_insensitive,
123            }) => Expr::Like(Like::new(
124                !negated,
125                expr,
126                pattern,
127                escape_char,
128                case_insensitive,
129            )),
130            Expr::SimilarTo(Like {
131                negated,
132                expr,
133                pattern,
134                escape_char,
135                case_insensitive,
136            }) => Expr::SimilarTo(Like::new(
137                !negated,
138                expr,
139                pattern,
140                escape_char,
141                case_insensitive,
142            )),
143            Expr::InList(InList {
144                expr,
145                list,
146                negated,
147            }) => Expr::InList(InList::new(expr, list, !negated)),
148            Expr::Exists(Exists { subquery, negated }) => {
149                Expr::Exists(Exists::new(subquery, !negated))
150            }
151            Expr::InSubquery(InSubquery {
152                expr,
153                subquery,
154                negated,
155            }) => Expr::InSubquery(InSubquery::new(expr, subquery, !negated)),
156            _ => Expr::Not(Box::new(self)),
157        }
158    }
159}
160
161#[cfg(test)]
162mod tests {
163    use crate::lit;
164
165    #[test]
166    fn test_operators() {
167        // Add
168        assert_eq!(
169            format!("{}", lit(1u32) + lit(2u32)),
170            "UInt32(1) + UInt32(2)"
171        );
172        // Sub
173        assert_eq!(
174            format!("{}", lit(1u32) - lit(2u32)),
175            "UInt32(1) - UInt32(2)"
176        );
177        // Mul
178        assert_eq!(
179            format!("{}", lit(1u32) * lit(2u32)),
180            "UInt32(1) * UInt32(2)"
181        );
182        // Div
183        assert_eq!(
184            format!("{}", lit(1u32) / lit(2u32)),
185            "UInt32(1) / UInt32(2)"
186        );
187        // Rem
188        assert_eq!(
189            format!("{}", lit(1u32) % lit(2u32)),
190            "UInt32(1) % UInt32(2)"
191        );
192        // BitAnd
193        assert_eq!(
194            format!("{}", lit(1u32) & lit(2u32)),
195            "UInt32(1) & UInt32(2)"
196        );
197        // BitOr
198        assert_eq!(
199            format!("{}", lit(1u32) | lit(2u32)),
200            "UInt32(1) | UInt32(2)"
201        );
202        // BitXor
203        assert_eq!(
204            format!("{}", lit(1u32) ^ lit(2u32)),
205            "UInt32(1) BIT_XOR UInt32(2)"
206        );
207        // Shl
208        assert_eq!(
209            format!("{}", lit(1u32) << lit(2u32)),
210            "UInt32(1) << UInt32(2)"
211        );
212        // Shr
213        assert_eq!(
214            format!("{}", lit(1u32) >> lit(2u32)),
215            "UInt32(1) >> UInt32(2)"
216        );
217        // Neg
218        assert_eq!(format!("{}", -lit(1u32)), "(- UInt32(1))");
219        // Not
220        assert_eq!(format!("{}", !lit(1u32)), "NOT UInt32(1)");
221    }
222}