Skip to main content

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(
26    from_py_object,
27    frozen,
28    name = "Like",
29    module = "datafusion.expr",
30    subclass
31)]
32#[derive(Clone)]
33pub struct PyLike {
34    like: Like,
35}
36
37impl From<Like> for PyLike {
38    fn from(like: Like) -> PyLike {
39        PyLike { like }
40    }
41}
42
43impl From<PyLike> for Like {
44    fn from(like: PyLike) -> Self {
45        like.like
46    }
47}
48
49impl Display for PyLike {
50    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
51        write!(
52            f,
53            "Like
54            Negated: {:?}
55            Expr: {:?}
56            Pattern: {:?}
57            Escape_Char: {:?}",
58            &self.negated(),
59            &self.expr(),
60            &self.pattern(),
61            &self.escape_char()
62        )
63    }
64}
65
66#[pymethods]
67impl PyLike {
68    fn negated(&self) -> PyResult<bool> {
69        Ok(self.like.negated)
70    }
71
72    fn expr(&self) -> PyResult<PyExpr> {
73        Ok((*self.like.expr).clone().into())
74    }
75
76    fn pattern(&self) -> PyResult<PyExpr> {
77        Ok((*self.like.pattern).clone().into())
78    }
79
80    fn escape_char(&self) -> PyResult<Option<char>> {
81        Ok(self.like.escape_char)
82    }
83
84    fn __repr__(&self) -> String {
85        format!("Like({self})")
86    }
87}
88
89#[pyclass(
90    from_py_object,
91    frozen,
92    name = "ILike",
93    module = "datafusion.expr",
94    subclass
95)]
96#[derive(Clone)]
97pub struct PyILike {
98    like: Like,
99}
100
101impl From<Like> for PyILike {
102    fn from(like: Like) -> PyILike {
103        PyILike { like }
104    }
105}
106
107impl From<PyILike> for Like {
108    fn from(like: PyILike) -> Self {
109        like.like
110    }
111}
112
113impl Display for PyILike {
114    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
115        write!(
116            f,
117            "ILike
118            Negated: {:?}
119            Expr: {:?}
120            Pattern: {:?}
121            Escape_Char: {:?}",
122            &self.negated(),
123            &self.expr(),
124            &self.pattern(),
125            &self.escape_char()
126        )
127    }
128}
129
130#[pymethods]
131impl PyILike {
132    fn negated(&self) -> PyResult<bool> {
133        Ok(self.like.negated)
134    }
135
136    fn expr(&self) -> PyResult<PyExpr> {
137        Ok((*self.like.expr).clone().into())
138    }
139
140    fn pattern(&self) -> PyResult<PyExpr> {
141        Ok((*self.like.pattern).clone().into())
142    }
143
144    fn escape_char(&self) -> PyResult<Option<char>> {
145        Ok(self.like.escape_char)
146    }
147
148    fn __repr__(&self) -> String {
149        format!("Like({self})")
150    }
151}
152
153#[pyclass(
154    from_py_object,
155    frozen,
156    name = "SimilarTo",
157    module = "datafusion.expr",
158    subclass
159)]
160#[derive(Clone)]
161pub struct PySimilarTo {
162    like: Like,
163}
164
165impl From<Like> for PySimilarTo {
166    fn from(like: Like) -> PySimilarTo {
167        PySimilarTo { like }
168    }
169}
170
171impl From<PySimilarTo> for Like {
172    fn from(like: PySimilarTo) -> Self {
173        like.like
174    }
175}
176
177impl Display for PySimilarTo {
178    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
179        write!(
180            f,
181            "SimilarTo
182            Negated: {:?}
183            Expr: {:?}
184            Pattern: {:?}
185            Escape_Char: {:?}",
186            &self.negated(),
187            &self.expr(),
188            &self.pattern(),
189            &self.escape_char()
190        )
191    }
192}
193
194#[pymethods]
195impl PySimilarTo {
196    fn negated(&self) -> PyResult<bool> {
197        Ok(self.like.negated)
198    }
199
200    fn expr(&self) -> PyResult<PyExpr> {
201        Ok((*self.like.expr).clone().into())
202    }
203
204    fn pattern(&self) -> PyResult<PyExpr> {
205        Ok((*self.like.pattern).clone().into())
206    }
207
208    fn escape_char(&self) -> PyResult<Option<char>> {
209        Ok(self.like.escape_char)
210    }
211
212    fn __repr__(&self) -> String {
213        format!("Like({self})")
214    }
215}