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