Skip to content

Commit

Permalink
Add Python wrapper for LogicalPlan::Filter (#192)
Browse files Browse the repository at this point in the history
* Add Python wrapper for LogicalPlan::Filter

* clippy

* clippy

* Update src/expr/filter.rs

Co-authored-by: Liang-Chi Hsieh <[email protected]>

---------

Co-authored-by: Liang-Chi Hsieh <[email protected]>
  • Loading branch information
andygrove and viirya committed Feb 19, 2023
1 parent b8ef9bf commit 3124278
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion datafusion/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Expr,
Projection,
TableScan,
Filter,
Limit,
Aggregate,
Sort,
Expand All @@ -58,7 +59,7 @@ def test_class_module_is_datafusion():
]:
assert klass.__module__ == "datafusion"

for klass in [Expr, Projection, TableScan, Aggregate, Sort, Limit]:
for klass in [Expr, Projection, TableScan, Aggregate, Sort, Limit, Filter]:
assert klass.__module__ == "datafusion.expr"

for klass in [DFField, DFSchema]:
Expand Down
2 changes: 2 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use datafusion_expr::{col, lit, Cast, Expr, GetIndexedField};
use datafusion::scalar::ScalarValue;

pub mod aggregate;
pub mod filter;
pub mod limit;
pub mod logical_node;
pub mod projection;
Expand Down Expand Up @@ -146,6 +147,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
m.add_class::<PyExpr>()?;
m.add_class::<table_scan::PyTableScan>()?;
m.add_class::<projection::PyProjection>()?;
m.add_class::<filter::PyFilter>()?;
m.add_class::<limit::PyLimit>()?;
m.add_class::<aggregate::PyAggregate>()?;
m.add_class::<sort::PySort>()?;
Expand Down
83 changes: 83 additions & 0 deletions src/expr/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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::logical_plan::Filter;
use pyo3::prelude::*;
use std::fmt::{self, Display, Formatter};

use crate::common::df_schema::PyDFSchema;
use crate::expr::logical_node::LogicalNode;
use crate::expr::PyExpr;
use crate::sql::logical::PyLogicalPlan;

#[pyclass(name = "Filter", module = "datafusion.expr", subclass)]
#[derive(Clone)]
pub struct PyFilter {
filter: Filter,
}

impl From<Filter> for PyFilter {
fn from(filter: Filter) -> PyFilter {
PyFilter { filter }
}
}

impl From<PyFilter> for Filter {
fn from(filter: PyFilter) -> Self {
filter.filter
}
}

impl Display for PyFilter {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"Filter
\nPredicate: {:?}
\nInput: {:?}",
&self.filter.predicate, &self.filter.input
)
}
}

#[pymethods]
impl PyFilter {
/// Retrieves the predicate expression for this `Filter`
fn predicate(&self) -> PyExpr {
PyExpr::from(self.filter.predicate.clone())
}

/// Retrieves the input `LogicalPlan` to this `Filter` node
fn input(&self) -> PyLogicalPlan {
PyLogicalPlan::from((*self.filter.input).clone())
}

/// Resulting Schema for this `Filter` node instance
fn schema(&self) -> PyDFSchema {
self.filter.input.schema().as_ref().clone().into()
}

fn __repr__(&self) -> String {
format!("Filter({})", self)
}
}

impl LogicalNode for PyFilter {
fn input(&self) -> Vec<PyLogicalPlan> {
vec![PyLogicalPlan::from((*self.filter.input).clone())]
}
}

0 comments on commit 3124278

Please sign in to comment.