Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bindings for LIKE type expressions #220

Merged
merged 5 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
Expr,
Filter,
Limit,
Like,
ILike,
Projection,
SimilarTo,
ScalarVariable,
Sort,
TableScan,
Expand All @@ -73,6 +76,9 @@
"Sort",
"Limit",
"Filter",
"Like",
"ILike",
"SimilarTo",
"ScalarVariable",
"Alias",
]
Expand Down
6 changes: 6 additions & 0 deletions datafusion/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
Aggregate,
Sort,
Analyze,
Like,
ILike,
SimilarTo,
ScalarVariable,
Alias,
)
Expand Down Expand Up @@ -79,6 +82,9 @@ def test_class_module_is_datafusion():
Limit,
Filter,
Analyze,
Like,
ILike,
SimilarTo,
ScalarVariable,
Alias,
]:
Expand Down
7 changes: 6 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down
196 changes: 196 additions & 0 deletions src/expr/like.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}