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