datafusion_python/expr/
sort.rs1use 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 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 fn input(&self) -> PyResult<Vec<PyLogicalPlan>> {
89 Ok(Self::inputs(self))
90 }
91
92 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}