This is an automated email from the ASF dual-hosted git repository.
agrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion-python.git
The following commit(s) were added to refs/heads/main by this push:
new fa0d9d0 Bindings for LIKE type expressions (#220)
fa0d9d0 is described below
commit fa0d9d00514bb35424e2c8768c38984bc1cc43b9
Author: Jeremy Dyer <[email protected]>
AuthorDate: Wed Feb 22 23:27:49 2023 -0500
Bindings for LIKE type expressions (#220)
* Bindings for LIKE type expressions
* cargo fmt
* remove conflict expression
---
datafusion/__init__.py | 6 ++
datafusion/tests/test_imports.py | 6 ++
src/expr.rs | 7 +-
src/expr/like.rs | 196 +++++++++++++++++++++++++++++++++++++++
4 files changed, 214 insertions(+), 1 deletion(-)
diff --git a/datafusion/__init__.py b/datafusion/__init__.py
index 8ce6301..01680e7 100644
--- a/datafusion/__init__.py
+++ b/datafusion/__init__.py
@@ -46,7 +46,10 @@ from .expr import (
Expr,
Filter,
Limit,
+ Like,
+ ILike,
Projection,
+ SimilarTo,
ScalarVariable,
Sort,
TableScan,
@@ -73,6 +76,9 @@ __all__ = [
"Sort",
"Limit",
"Filter",
+ "Like",
+ "ILike",
+ "SimilarTo",
"ScalarVariable",
"Alias",
]
diff --git a/datafusion/tests/test_imports.py b/datafusion/tests/test_imports.py
index 83d64f3..0e39fd5 100644
--- a/datafusion/tests/test_imports.py
+++ b/datafusion/tests/test_imports.py
@@ -44,6 +44,9 @@ from datafusion.expr import (
Aggregate,
Sort,
Analyze,
+ Like,
+ ILike,
+ SimilarTo,
ScalarVariable,
Alias,
)
@@ -79,6 +82,9 @@ def test_class_module_is_datafusion():
Limit,
Filter,
Analyze,
+ Like,
+ ILike,
+ SimilarTo,
ScalarVariable,
Alias,
]:
diff --git a/src/expr.rs b/src/expr.rs
index a656e79..cdc81e7 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -30,6 +30,7 @@ use crate::expr::literal::PyLiteral;
use datafusion::scalar::ScalarValue;
use self::alias::PyAlias;
+use self::like::{PyILike, PyLike, PySimilarTo};
use self::scalar_variable::PyScalarVariable;
pub mod aggregate;
@@ -40,6 +41,7 @@ pub mod binary_expr;
pub mod column;
pub mod empty_relation;
pub mod filter;
+pub mod like;
pub mod limit;
pub mod literal;
pub mod logical_node;
@@ -51,7 +53,7 @@ pub mod table_scan;
/// A PyExpr that can be used on a DataFrame
#[pyclass(name = "Expr", module = "datafusion.expr", subclass)]
#[derive(Debug, Clone)]
-pub(crate) struct PyExpr {
+pub struct PyExpr {
pub(crate) expr: Expr,
}
@@ -198,6 +200,9 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<PyBinaryExpr>()?;
m.add_class::<PyLiteral>()?;
m.add_class::<PyAggregateFunction>()?;
+ m.add_class::<PyLike>()?;
+ m.add_class::<PyILike>()?;
+ m.add_class::<PySimilarTo>()?;
m.add_class::<PyScalarVariable>()?;
m.add_class::<alias::PyAlias>()?;
// operators
diff --git a/src/expr/like.rs b/src/expr/like.rs
new file mode 100644
index 0000000..6ed3c24
--- /dev/null
+++ b/src/expr/like.rs
@@ -0,0 +1,196 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use datafusion_expr::expr::Like;
+use pyo3::prelude::*;
+use std::fmt::{self, Display, Formatter};
+
+use crate::expr::PyExpr;
+
+#[pyclass(name = "Like", module = "datafusion.expr", subclass)]
+#[derive(Clone)]
+pub struct PyLike {
+ like: Like,
+}
+
+impl From<Like> for PyLike {
+ fn from(like: Like) -> PyLike {
+ PyLike { like }
+ }
+}
+
+impl From<PyLike> for Like {
+ fn from(like: PyLike) -> Self {
+ like.like
+ }
+}
+
+impl Display for PyLike {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(
+ f,
+ "Like
+ Negated: {:?}
+ Expr: {:?}
+ Pattern: {:?}
+ Escape_Char: {:?}",
+ &self.negated(),
+ &self.expr(),
+ &self.pattern(),
+ &self.escape_char()
+ )
+ }
+}
+
+#[pymethods]
+impl PyLike {
+ fn negated(&self) -> PyResult<bool> {
+ Ok(self.like.negated)
+ }
+
+ fn expr(&self) -> PyResult<PyExpr> {
+ Ok((*self.like.expr).clone().into())
+ }
+
+ fn pattern(&self) -> PyResult<PyExpr> {
+ Ok((*self.like.pattern).clone().into())
+ }
+
+ fn escape_char(&self) -> PyResult<Option<char>> {
+ Ok(self.like.escape_char)
+ }
+
+ fn __repr__(&self) -> String {
+ format!("Like({})", self)
+ }
+}
+
+#[pyclass(name = "ILike", module = "datafusion.expr", subclass)]
+#[derive(Clone)]
+pub struct PyILike {
+ like: Like,
+}
+
+impl From<Like> for PyILike {
+ fn from(like: Like) -> PyILike {
+ PyILike { like }
+ }
+}
+
+impl From<PyILike> for Like {
+ fn from(like: PyILike) -> Self {
+ like.like
+ }
+}
+
+impl Display for PyILike {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(
+ f,
+ "ILike
+ Negated: {:?}
+ Expr: {:?}
+ Pattern: {:?}
+ Escape_Char: {:?}",
+ &self.negated(),
+ &self.expr(),
+ &self.pattern(),
+ &self.escape_char()
+ )
+ }
+}
+
+#[pymethods]
+impl PyILike {
+ fn negated(&self) -> PyResult<bool> {
+ Ok(self.like.negated)
+ }
+
+ fn expr(&self) -> PyResult<PyExpr> {
+ Ok((*self.like.expr).clone().into())
+ }
+
+ fn pattern(&self) -> PyResult<PyExpr> {
+ Ok((*self.like.pattern).clone().into())
+ }
+
+ fn escape_char(&self) -> PyResult<Option<char>> {
+ Ok(self.like.escape_char)
+ }
+
+ fn __repr__(&self) -> String {
+ format!("Like({})", self)
+ }
+}
+
+#[pyclass(name = "SimilarTo", module = "datafusion.expr", subclass)]
+#[derive(Clone)]
+pub struct PySimilarTo {
+ like: Like,
+}
+
+impl From<Like> for PySimilarTo {
+ fn from(like: Like) -> PySimilarTo {
+ PySimilarTo { like }
+ }
+}
+
+impl From<PySimilarTo> for Like {
+ fn from(like: PySimilarTo) -> Self {
+ like.like
+ }
+}
+
+impl Display for PySimilarTo {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(
+ f,
+ "SimilarTo
+ Negated: {:?}
+ Expr: {:?}
+ Pattern: {:?}
+ Escape_Char: {:?}",
+ &self.negated(),
+ &self.expr(),
+ &self.pattern(),
+ &self.escape_char()
+ )
+ }
+}
+
+#[pymethods]
+impl PySimilarTo {
+ fn negated(&self) -> PyResult<bool> {
+ Ok(self.like.negated)
+ }
+
+ fn expr(&self) -> PyResult<PyExpr> {
+ Ok((*self.like.expr).clone().into())
+ }
+
+ fn pattern(&self) -> PyResult<PyExpr> {
+ Ok((*self.like.pattern).clone().into())
+ }
+
+ fn escape_char(&self) -> PyResult<Option<char>> {
+ Ok(self.like.escape_char)
+ }
+
+ fn __repr__(&self) -> String {
+ format!("Like({})", self)
+ }
+}