datafusion_python/expr/
sort.rs1use 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 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 fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
81 Ok(Self::inputs(self))
82 }
83
84 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}