|
| 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_common::DataFusionError; |
| 19 | +use datafusion_expr::logical_plan::Sort; |
| 20 | +use pyo3::prelude::*; |
| 21 | +use std::fmt::{self, Display, Formatter}; |
| 22 | + |
| 23 | +use crate::common::df_schema::PyDFSchema; |
| 24 | +use crate::expr::logical_node::LogicalNode; |
| 25 | +use crate::expr::PyExpr; |
| 26 | +use crate::sql::logical::PyLogicalPlan; |
| 27 | + |
| 28 | +#[pyclass(name = "Sort", module = "datafusion.expr", subclass)] |
| 29 | +#[derive(Clone)] |
| 30 | +pub struct PySort { |
| 31 | + sort: Sort, |
| 32 | +} |
| 33 | + |
| 34 | +impl From<Sort> for PySort { |
| 35 | + fn from(sort: Sort) -> PySort { |
| 36 | + PySort { sort } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl TryFrom<PySort> for Sort { |
| 41 | + type Error = DataFusionError; |
| 42 | + |
| 43 | + fn try_from(agg: PySort) -> Result<Self, Self::Error> { |
| 44 | + Ok(agg.sort) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Display for PySort { |
| 49 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 50 | + write!( |
| 51 | + f, |
| 52 | + "Sort |
| 53 | + \nExpr(s): {:?} |
| 54 | + \nInput: {:?} |
| 55 | + \nSchema: {:?}", |
| 56 | + &self.sort.expr, |
| 57 | + self.sort.input, |
| 58 | + self.sort.input.schema() |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[pymethods] |
| 64 | +impl PySort { |
| 65 | + /// Retrieves the sort expressions for this `Sort` |
| 66 | + fn sort_exprs(&self) -> PyResult<Vec<PyExpr>> { |
| 67 | + Ok(self |
| 68 | + .sort |
| 69 | + .expr |
| 70 | + .iter() |
| 71 | + .map(|e| PyExpr::from(e.clone())) |
| 72 | + .collect()) |
| 73 | + } |
| 74 | + |
| 75 | + /// Retrieves the input `LogicalPlan` to this `Sort` node |
| 76 | + fn input(&self) -> PyLogicalPlan { |
| 77 | + PyLogicalPlan::from((*self.sort.input).clone()) |
| 78 | + } |
| 79 | + |
| 80 | + /// Resulting Schema for this `Sort` node instance |
| 81 | + fn schema(&self) -> PyDFSchema { |
| 82 | + self.sort.input.schema().as_ref().clone().into() |
| 83 | + } |
| 84 | + |
| 85 | + fn __repr__(&self) -> PyResult<String> { |
| 86 | + Ok(format!("Sort({})", self)) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl LogicalNode for PySort { |
| 91 | + fn input(&self) -> Vec<PyLogicalPlan> { |
| 92 | + vec![PyLogicalPlan::from((*self.sort.input).clone())] |
| 93 | + } |
| 94 | +} |
0 commit comments