8000 Add Python wrapper for LogicalPlan::Filter (#192) · apache/datafusion-python@3124278 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3124278

Browse files
andygroveviirya
andauthored
Add Python wrapper for LogicalPlan::Filter (#192)
* Add Python wrapper for LogicalPlan::Filter * clippy * clippy * Update src/expr/filter.rs Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com> --------- Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com>
1 parent b8ef9bf commit 3124278

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

datafusion/tests/test_imports.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Expr,
3636
Projection,
3737
TableScan,
38+
Filter,
3839
Limit,
3940
Aggregate,
4041
Sort,
@@ -58,7 +59,7 @@ def test_class_module_is_datafusion():
5859
]:
5960
assert klass.__module__ == "datafusion"
6061

61-
for klass in [Expr, Projection, TableScan, Aggregate, Sort, Limit]:
62+
for klass in [Expr, Projection, TableScan, Aggregate, Sort, Limit, Filter]:
6263
assert klass.__module__ == "datafusion.expr"
6364

6465
for klass in [DFField, DFSchema]:

src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use datafusion_expr::{col, lit, Cast, Expr, GetIndexedField};
2525
use datafusion::scalar::ScalarValue;
2626

2727
pub mod aggregate;
28+
pub mod filter;
2829
pub mod limit;
2930
pub mod logical_node;
3031
pub mod projection;
@@ -146,6 +147,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
146147
m.add_class::<PyExpr>()?;
147148
m.add_class::<table_scan::PyTableScan>()?;
148149
m.add_class::<projection::PyProjection>()?;
150+
m.add_class::<filter::PyFilter>()?;
149151
m.add_class::<limit::PyLimit>()?;
150152
m.add_class::<aggregate::PyAggregate>()?;
151153
m.add_class::<sort::PySort>()?;

src/expr/filter.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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::logical_plan::Filter;
19+
use pyo3::prelude::*;
20+
use std::fmt::{self, Display, Formatter};
21+
22+
use crate::common::df_schema::PyDFSchema;
23+
use crate::expr::logical_node::LogicalNode;
24+
use crate::expr::PyExpr;
25+
use crate::sql::logical::PyLogicalPlan;
26+
27+
#[pyclass(name = "Filter", module = "datafusion.expr", subclass)]
28+
#[derive(Clone)]
29+
pub struct PyFilter {
30+
filter: Filter,
31+
}
32+
33+
impl From<Filter> for PyFilter {
34+
fn from(filter: Filter) -> PyFilter {
35+
PyFilter { filter }
36+
}
37+
}
38+
39+
impl From<PyFilter> for Filter {
40+
fn from(filter: PyFilter) -> Self {
41+
filter.filter
42+
}
43+
}
44+
45+
impl Display for PyFilter {
46+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
47+
write!(
48+
f,
49+
"Filter
50+
\nPredicate: {:?}
51+
\nInput: {:?}",
52+
&self.filter.predicate, &self.filter.input
53+
)
54+
}
55+
}
56+
57+
#[pymethods]
58+
impl PyFilter {
59+
/// Retrieves the predicate expression for this `Filter`
60+
fn predicate(&self) -> PyExpr {
61+
PyExpr::from(self.filter.predicate.clone())
62+
}
63+
64+
/// Retrieves the input `LogicalPlan` to this `Filter` node
65+
fn input(&self) -> PyLogicalPlan {
66+
PyLogicalPlan::from((*self.filter.input).clone())
67+
}
68+
69+
/// Resulting Schema for this `Filter` node instance
70+
fn schema(&self) -> PyDFSchema {
71+
self.filter.input.schema().as_ref().clone().into()
72+
}
73+
74+
fn __repr__(&self) -> String {
75+
format!("Filter({})", self)
76+
}
77+
}
78+
79+
impl LogicalNode for PyFilter {
80+
fn input(&self) -> Vec<PyLogicalPlan> {
81+
vec![PyLogicalPlan::from((*self.filter.input).clone())]
82+
}
83+
}

0 commit comments

Comments
 (0)
0