datafusion_python/expr/
filter.rs1use 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 fn predicate(&self) -> PyExpr {
63 PyExpr::from(self.filter.predicate.clone())
64 }
65
66 fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
68 Ok(Self::inputs(self))
69 }
70
71 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}