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