Skip to content

Commit 3b74dde

Browse files
authored
Add Python wrapper for LogicalPlan::Sort (#196)
1 parent 6c3cb97 commit 3b74dde

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

datafusion/tests/test_imports.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Expr,
3636
Projection,
3737
TableScan,
38+
Sort,
3839
)
3940

4041

@@ -55,7 +56,7 @@ def test_class_module_is_datafusion():
5556
]:
5657
assert klass.__module__ == "datafusion"
5758

58-
for klass in [Expr, Projection, TableScan]:
59+
for klass in [Expr, Projection, TableScan, Sort]:
5960
assert klass.__module__ == "datafusion.expr"
6061

6162
for klass in [DFField, DFSchema]:

src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use datafusion::scalar::ScalarValue;
2626

2727
pub mod logical_node;
2828
pub mod projection;
29+
pub mod sort;
2930
pub mod table_scan;
3031

3132
/// A PyExpr that can be used on a DataFrame
@@ -143,5 +144,6 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
143144
m.add_class::<PyExpr>()?;
144145
m.add_class::<table_scan::PyTableScan>()?;
145146
m.add_class::<projection::PyProjection>()?;
147+
m.add_class::<sort::PySort>()?;
146148
Ok(())
147149
}

src/expr/sort.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
18+
use datafusion_common::DataFusionError;
19+
use datafusion_expr::logical_plan::Sort;
20+
use pyo3::prelude::*;
21+
use std::fmt::{self, Display, Formatter};
22+
23+
use crate::common::df_schema::PyDFSchema;
24+
use crate::expr::logical_node::LogicalNode;
25+
use crate::expr::PyExpr;
26+
use crate::sql::logical::PyLogicalPlan;
27+
28+
#[pyclass(name = "Sort", module = "datafusion.expr", subclass)]
29+
#[derive(Clone)]
30+
pub struct PySort {
31+
sort: Sort,
32+
}
33+
34+
impl From<Sort> for PySort {
35+
fn from(sort: Sort) -> PySort {
36+
PySort { sort }
37+
}
38+
}
39+
40+
impl 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+
48+
impl 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]
64+
impl PySort {
65+
/// Retrieves the sort expressions for this `Sort`
66+
fn sort_exprs(&self) -> PyResult<Vec<PyExpr>> {
67+
Ok(self
68+
.sort
69+
.expr
70+
.iter()
71+
.map(|e| PyExpr::from(e.clone()))
72+
.collect())
73+
}
74+
75+
/// Retrieves the input `LogicalPlan` to this `Sort` node
76+
fn input(&self) -> PyLogicalPlan {
77+
PyLogicalPlan::from((*self.sort.input).clone())
78+
}
79+
80+
/// Resulting Schema for this `Sort` node instance
81+
fn schema(&self) -> PyDFSchema {
82+
self.sort.input.schema().as_ref().clone().into()
83+
}
84+
85+
fn __repr__(&self) -> PyResult<String> {
86+
Ok(format!("Sort({})", self))
87+
}
88+
}
89+
90+
impl LogicalNode for PySort {
91+
fn input(&self) -> Vec<PyLogicalPlan> {
92+
vec![PyLogicalPlan::from((*self.sort.input).clone())]
93+
}
94+
}

0 commit comments

Comments
 (0)