8000 Bindings for LIKE type expressions (#220) · chenqin/arrow-datafusion-python@fa0d9d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa0d9d0

Browse files
authored
Bindings for LIKE type expressions (apache#220)
* Bindings for LIKE type expressions * cargo fmt * remove conflict expression
1 parent 9e552eb commit fa0d9d0

File tree

4 files changed

+214
-1
lines changed

4 files changed

+214
-1
lines changed

datafusion/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
Expr,
4747
Filter,
4848
Limit,
49+
Like,
50+
ILike,
4951
Projection,
52+
SimilarTo,
5053
ScalarVariable,
5154
Sort,
5255
TableScan,
@@ -73,6 +76,9 @@
7376
"Sort",
7477
"Limit",
7578
"Filter",
79+
"Like",
80+
"ILike",
81+
"SimilarTo",
7682
"ScalarVariable",
7783
"Alias",
7884
]

datafusion/tests/test_imports.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
Aggregate,
4545
Sort,
4646
Analyze,
47+
Like,
48+
ILike,
49+
SimilarTo,
4750
ScalarVariable,
4851
Alias,
4952
)
@@ -79,6 +82,9 @@ def test_class_module_is_datafusion():
7982
Limit,
8083
Filter,
8184
Analyze,
85+
Like,
86+
ILike,
87+
SimilarTo,
8288
ScalarVariable,
8389
Alias,
8490
]:

src/expr.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::expr::literal::PyLiteral;
3030
use datafusion::scalar::ScalarValue;
3131

3232
use self::alias::PyAlias;
33+
use self::like::{PyILike, PyLike, PySimilarTo};
3334
use self::scalar_variable::PyScalarVariable;
3435

3536
pub mod aggregate;
@@ -40,6 +41,7 @@ pub mod binary_expr;
4041
pub mod column;
4142
pub mod empty_relation;
4243
pub mod filter;
44+
pub mod like;
4345
pub mod limit;
4446
pub mod literal;
4547
pub mod logical_node;
@@ -51,7 +53,7 @@ pub mod table_scan;
5153
/// A PyExpr that can be used on a DataFrame
5254
#[pyclass(name = "Expr", module = "datafusion.expr", subclass)]
5355
#[derive(Debug, Clone)]
54-
pub(crate) struct PyExpr {
56+
pub struct PyExpr {
5557
pub(crate) expr: Expr,
5658
}
5759

@@ -198,6 +200,9 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
198200
m.add_class::<PyBinaryExpr>()?;
199201
m.add_class::<PyLiteral>()?;
200202
m.add_class::<PyAggregateFunction>()?;
203+
m.add_class::<PyLike>()?;
204+
m.add_class::<PyILike>()?;
205+
m.add_class::<PySimilarTo>()?;
201206
m.add_class::<PyScalarVariable>()?;
202207
m.add_class::<alias::PyAlias>()?;
203208
// operators

src/expr/like.rs

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
18+
use datafusion_expr::expr::Like;
19+
use pyo3::prelude::*;
20+
use std::fmt::{self, Display, Formatter};
21+
22+
use crate::expr::PyExpr;
23+
24+
#[pyclass(name = "Like", module = "datafusion.expr", subclass)]
25+
#[derive(Clone)]
26+
pub struct PyLike {
27+
like: Like,
28+
}
29+
30+
impl From<Like> for PyLike {
31+
fn from(like: Like) -> PyLike {
32+
PyLike { like }
33+
}
34+
}
35+
36+
impl From<PyLike> for Like {
37+
fn from(like: PyLike) -> Self {
38+
like.like
39+
}
40+
}
41+
42+
impl 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]
60+
impl 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)]
84+
pub struct PyILike {
85+
like: Like,
86+
}
87+
88+
impl From<Like> for PyILike {
89+
fn from(like: Like) -> PyILike {
90+
PyILike { like }
91+
}
92+
}
93+
94+
impl From<PyILike> for Like {
95+
fn from(like: PyILike) -> Self {
96+
like.like
97+
}
98+
}
99+
100+
impl 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]
118+
impl 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)]
142+
pub struct PySimilarTo {
143+
like: Like,
144+
}
145+
146+
impl From<Like> for PySimilarTo {
147+
fn from(like: Like) -> PySimilarTo {
148+
PySimilarTo { like }
149+
}
150+
}
151+
152+
impl From<PySimilarTo> for Like {
153+
fn from(like: PySimilarTo) -> Self {
154+
like.like
155+
}
156+
}
157+
158+
impl 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]
176+
impl 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+
}

0 commit comments

Comments
 (0)
0