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