datafusion_python/expr/
like.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::{self, Display, Formatter};
19
20use datafusion::logical_expr::expr::Like;
21use pyo3::prelude::*;
22
23use crate::expr::PyExpr;
24
25#[pyclass(frozen, name = "Like", module = "datafusion.expr", subclass)]
26#[derive(Clone)]
27pub struct PyLike {
28    like: Like,
29}
30
31impl From<Like> for PyLike {
32    fn from(like: Like) -> PyLike {
33        PyLike { like }
34    }
35}
36
37impl From<PyLike> for Like {
38    fn from(like: PyLike) -> Self {
39        like.like
40    }
41}
42
43impl Display for PyLike {
44    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
45        write!(
46            f,
47            "Like
48            Negated: {:?}
49            Expr: {:?}
50            Pattern: {:?}
51            Escape_Char: {:?}",
52            &self.negated(),
53            &self.expr(),
54            &self.pattern(),
55            &self.escape_char()
56        )
57    }
58}
59
60#[pymethods]
61impl PyLike {
62    fn negated(&self) -> PyResult<bool> {
63        Ok(self.like.negated)
64    }
65
66    fn expr(&self) -> PyResult<PyExpr> {
67        Ok((*self.like.expr).clone().into())
68    }
69
70    fn pattern(&self) -> PyResult<PyExpr> {
71        Ok((*self.like.pattern).clone().into())
72    }
73
74    fn escape_char(&self) -> PyResult<Option<char>> {
75        Ok(self.like.escape_char)
76    }
77
78    fn __repr__(&self) -> String {
79        format!("Like({self})")
80    }
81}
82
83#[pyclass(frozen, name = "ILike", module = "datafusion.expr", subclass)]
84#[derive(Clone)]
85pub struct PyILike {
86    like: Like,
87}
88
89impl From<Like> for PyILike {
90    fn from(like: Like) -> PyILike {
91        PyILike { like }
92    }
93}
94
95impl From<PyILike> for Like {
96    fn from(like: PyILike) -> Self {
97        like.like
98    }
99}
100
101impl Display for PyILike {
102    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
103        write!(
104            f,
105            "ILike
106            Negated: {:?}
107            Expr: {:?}
108            Pattern: {:?}
109            Escape_Char: {:?}",
110            &self.negated(),
111            &self.expr(),
112            &self.pattern(),
113            &self.escape_char()
114        )
115    }
116}
117
118#[pymethods]
119impl PyILike {
120    fn negated(&self) -> PyResult<bool> {
121        Ok(self.like.negated)
122    }
123
124    fn expr(&self) -> PyResult<PyExpr> {
125        Ok((*self.like.expr).clone().into())
126    }
127
128    fn pattern(&self) -> PyResult<PyExpr> {
129        Ok((*self.like.pattern).clone().into())
130    }
131
132    fn escape_char(&self) -> PyResult<Option<char>> {
133        Ok(self.like.escape_char)
134    }
135
136    fn __repr__(&self) -> String {
137        format!("Like({self})")
138    }
139}
140
141#[pyclass(frozen, name = "SimilarTo", module = "datafusion.expr", subclass)]
142#[derive(Clone)]
143pub struct PySimilarTo {
144    like: Like,
145}
146
147impl From<Like> for PySimilarTo {
148    fn from(like: Like) -> PySimilarTo {
149        PySimilarTo { like }
150    }
151}
152
153impl From<PySimilarTo> for Like {
154    fn from(like: PySimilarTo) -> Self {
155        like.like
156    }
157}
158
159impl Display for PySimilarTo {
160    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
161        write!(
162            f,
163            "SimilarTo
164            Negated: {:?}
165            Expr: {:?}
166            Pattern: {:?}
167            Escape_Char: {:?}",
168            &self.negated(),
169            &self.expr(),
170            &self.pattern(),
171            &self.escape_char()
172        )
173    }
174}
175
176#[pymethods]
177impl PySimilarTo {
178    fn negated(&self) -> PyResult<bool> {
179        Ok(self.like.negated)
180    }
181
182    fn expr(&self) -> PyResult<PyExpr> {
183        Ok((*self.like.expr).clone().into())
184    }
185
186    fn pattern(&self) -> PyResult<PyExpr> {
187        Ok((*self.like.pattern).clone().into())
188    }
189
190    fn escape_char(&self) -> PyResult<Option<char>> {
191        Ok(self.like.escape_char)
192    }
193
194    fn __repr__(&self) -> String {
195        format!("Like({self})")
196    }
197}