datafusion_python/expr/
filter.rs

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
18use datafusion::logical_expr::logical_plan::Filter;
19use pyo3::{prelude::*, IntoPyObjectExt};
20use std::fmt::{self, Display, Formatter};
21
22use crate::common::df_schema::PyDFSchema;
23use crate::expr::logical_node::LogicalNode;
24use crate::expr::PyExpr;
25use crate::sql::logical::PyLogicalPlan;
26
27#[pyclass(name = "Filter", module = "datafusion.expr", subclass)]
28#[derive(Clone)]
29pub struct PyFilter {
30    filter: Filter,
31}
32
33impl From<Filter> for PyFilter {
34    fn from(filter: Filter) -> PyFilter {
35        PyFilter { filter }
36    }
37}
38
39impl From<PyFilter> for Filter {
40    fn from(filter: PyFilter) -> Self {
41        filter.filter
42    }
43}
44
45impl Display for PyFilter {
46    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
47        write!(
48            f,
49            "Filter
50            Predicate: {:?}
51            Input: {:?}",
52            &self.filter.predicate, &self.filter.input
53        )
54    }
55}
56
57#[pymethods]
58impl 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) -> PyResult<Vec<PyLogicalPlan>> {
66        Ok(Self::inputs(self))
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
79impl LogicalNode for PyFilter {
80    fn inputs(&self) -> Vec<PyLogicalPlan> {
81        vec![PyLogicalPlan::from((*self.filter.input).clone())]
82    }
83
84    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
85        self.clone().into_bound_py_any(py)
86    }
87}