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