|
| 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