-
Notifications
You must be signed in to change notification settings - Fork 71
Port sort logic to the datafusion planner #505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
863fcb3
f565c2a
75933b8
e882408
5f7e8c6
b6c534e
82b4234
ee99096
5d1a561
2932a28
05c6a85
f9f569e
0840870
8d81c44
0a75c32
74d3451
8aaf8fe
6bbb8d7
36b8460
315fad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use crate::expression::PyExpr; | ||
|
|
||
| use datafusion_expr::logical_plan::Sort; | ||
| pub use datafusion_expr::{logical_plan::LogicalPlan, Expr}; | ||
|
|
||
| use crate::sql::exceptions::py_type_err; | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pyclass(name = "Sort", module = "dask_planner", subclass)] | ||
| #[derive(Clone)] | ||
| pub struct PySort { | ||
| sort: Sort, | ||
| } | ||
|
|
||
| impl PySort { | ||
| /// Returns if a sort expressions denotes an ascending sort | ||
| fn is_ascending(&self, expr: Expr) -> bool { | ||
| match expr { | ||
| Expr::Sort { | ||
| expr: _, | ||
| asc, | ||
| nulls_first: _, | ||
| } => asc, | ||
|
||
| _ => panic!("Provided expression is not a sort epxression"), | ||
ayushdg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| /// Returns if nulls should be placed first in a sort expression | ||
| fn is_nulls_first(&self, expr: Expr) -> bool { | ||
ayushdg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match &expr { | ||
| Expr::Sort { | ||
| expr: _, | ||
| asc: _, | ||
| nulls_first, | ||
| } => nulls_first.clone(), | ||
| _ => panic!("Provided expression is not a sort epxression"), | ||
| } | ||
| } | ||
| } | ||
| #[pymethods] | ||
| impl PySort { | ||
| /// Returns a Vec of the sort expressions | ||
| #[pyo3(name = "getCollation")] | ||
| pub fn sort_expressions(&self) -> PyResult<Vec<PyExpr>> { | ||
| let mut sort_exprs: Vec<PyExpr> = Vec::new(); | ||
| for expr in &self.sort.expr { | ||
| sort_exprs.push(PyExpr::from(expr.clone(), Some(self.sort.input.clone()))); | ||
| } | ||
| Ok(sort_exprs) | ||
| } | ||
|
|
||
| #[pyo3(name = "getAscending")] | ||
| pub fn get_ascending(&self) -> PyResult<Vec<bool>> { | ||
| let mut is_ascending: Vec<bool> = Vec::new(); | ||
| for sortexpr in &self.sort.expr { | ||
| is_ascending.push(self.is_ascending(sortexpr.clone())) | ||
| } | ||
| Ok(is_ascending) | ||
| } | ||
| #[pyo3(name = "getNullsFirst")] | ||
| pub fn get_nulls_first(&self) -> PyResult<Vec<bool>> { | ||
| let nulls_first: Vec<bool> = self | ||
| .sort | ||
| .expr | ||
| .iter() | ||
| .map(|sortexpr| self.is_nulls_first(sortexpr.clone())) | ||
| .collect::<Vec<bool>>(); | ||
ayushdg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Ok(nulls_first) | ||
ayushdg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| impl From<LogicalPlan> for PySort { | ||
| fn from(logical_plan: LogicalPlan) -> PySort { | ||
| match logical_plan { | ||
| LogicalPlan::Sort(srt) => PySort { sort: srt }, | ||
| _ => panic!("something went wrong here"), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to change just for reference #510 |
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this approach is fine for this PR but I would like to follow up with a helper method for these conversions.