datafusion_python/expr/
sort.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 datafusion::common::DataFusionError;
19use datafusion::logical_expr::logical_plan::Sort;
20use pyo3::{prelude::*, IntoPyObjectExt};
21use std::fmt::{self, Display, Formatter};
22
23use crate::common::df_schema::PyDFSchema;
24use crate::expr::logical_node::LogicalNode;
25use crate::expr::sort_expr::PySortExpr;
26use crate::sql::logical::PyLogicalPlan;
27
28#[pyclass(name = "Sort", module = "datafusion.expr", subclass)]
29#[derive(Clone)]
30pub struct PySort {
31    sort: Sort,
32}
33
34impl From<Sort> for PySort {
35    fn from(sort: Sort) -> PySort {
36        PySort { sort }
37    }
38}
39
40impl 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
48impl 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]
64impl PySort {
65    /// Retrieves the sort expressions for this `Sort`
66    fn sort_exprs(&self) -> PyResult<Vec<PySortExpr>> {
67        Ok(self
68            .sort
69            .expr
70            .iter()
71            .map(|e| PySortExpr::from(e.clone()))
72            .collect())
73    }
74
75    fn get_fetch_val(&self) -> PyResult<Option<usize>> {
76        Ok(self.sort.fetch)
77    }
78
79    /// Retrieves the input `LogicalPlan` to this `Sort` node
80    fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
81        Ok(Self::inputs(self))
82    }
83
84    /// Resulting Schema for this `Sort` node instance
85    fn schema(&self) -> PyDFSchema {
86        self.sort.input.schema().as_ref().clone().into()
87    }
88
89    fn __repr__(&self) -> PyResult<String> {
90        Ok(format!("Sort({})", self))
91    }
92}
93
94impl LogicalNode for PySort {
95    fn inputs(&self) -> Vec<PyLogicalPlan> {
96        vec![PyLogicalPlan::from((*self.sort.input).clone())]
97    }
98
99    fn to_variant<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
100        self.clone().into_bound_py_any(py)
101    }
102}