Skip to main content

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