-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add Docs and Examples and helper methods to PhysicalSortExpr
#12589
Merged
+90
−28
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,51 @@ use datafusion_common::Result; | |
use datafusion_expr_common::columnar_value::ColumnarValue; | ||
|
||
/// Represents Sort operation for a column in a RecordBatch | ||
/// | ||
/// Example: | ||
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. |
||
/// ``` | ||
/// # use std::any::Any; | ||
/// # use std::fmt::Display; | ||
/// # use std::hash::Hasher; | ||
/// # use std::sync::Arc; | ||
/// # use arrow::array::RecordBatch; | ||
/// # use datafusion_common::Result; | ||
/// # use arrow::compute::SortOptions; | ||
/// # use arrow::datatypes::{DataType, Schema}; | ||
/// # use datafusion_expr_common::columnar_value::ColumnarValue; | ||
/// # use datafusion_physical_expr_common::physical_expr::PhysicalExpr; | ||
/// # use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr; | ||
/// # // this crate doesn't have a physical expression implementation | ||
/// # // so make a really simple one | ||
/// # #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
/// # struct MyPhysicalExpr; | ||
/// # impl PhysicalExpr for MyPhysicalExpr { | ||
/// # fn as_any(&self) -> &dyn Any {todo!() } | ||
/// # fn data_type(&self, input_schema: &Schema) -> Result<DataType> {todo!()} | ||
/// # fn nullable(&self, input_schema: &Schema) -> Result<bool> {todo!() } | ||
/// # fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {todo!() } | ||
/// # fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {todo!()} | ||
/// # fn with_new_children(self: Arc<Self>, children: Vec<Arc<dyn PhysicalExpr>>) -> Result<Arc<dyn PhysicalExpr>> {todo!()} | ||
/// # fn dyn_hash(&self, _state: &mut dyn Hasher) {todo!()} | ||
/// # } | ||
/// # impl Display for MyPhysicalExpr { | ||
/// # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "a") } | ||
/// # } | ||
/// # impl PartialEq<dyn Any> for MyPhysicalExpr { | ||
/// # fn eq(&self, _other: &dyn Any) -> bool { true } | ||
/// # } | ||
/// # fn col(name: &str) -> Arc<dyn PhysicalExpr> { Arc::new(MyPhysicalExpr) } | ||
/// // Sort by a ASC | ||
/// let options = SortOptions::default(); | ||
/// let sort_expr = PhysicalSortExpr::new(col("a"), options); | ||
/// assert_eq!(sort_expr.to_string(), "a ASC"); | ||
/// | ||
/// // Sort by a DESC NULLS LAST | ||
/// let sort_expr = PhysicalSortExpr::new_default(col("a")) | ||
/// .desc() | ||
/// .nulls_last(); | ||
/// assert_eq!(sort_expr.to_string(), "a DESC NULLS LAST"); | ||
/// ``` | ||
#[derive(Clone, Debug)] | ||
pub struct PhysicalSortExpr { | ||
/// Physical expression representing the column to sort | ||
|
@@ -43,6 +88,35 @@ impl PhysicalSortExpr { | |
pub fn new(expr: Arc<dyn PhysicalExpr>, options: SortOptions) -> Self { | ||
Self { expr, options } | ||
} | ||
|
||
/// Create a new PhysicalSortExpr with default [`SortOptions`] | ||
pub fn new_default(expr: Arc<dyn PhysicalExpr>) -> Self { | ||
Self::new(expr, SortOptions::default()) | ||
} | ||
|
||
/// Set the sort sort options to ASC | ||
pub fn asc(mut self) -> Self { | ||
self.options.descending = false; | ||
self | ||
} | ||
|
||
/// Set the sort sort options to DESC | ||
pub fn desc(mut self) -> Self { | ||
self.options.descending = true; | ||
self | ||
} | ||
|
||
/// Set the sort sort options to NULLS FIRST | ||
pub fn nulls_first(mut self) -> Self { | ||
self.options.nulls_first = true; | ||
self | ||
} | ||
|
||
/// Set the sort sort options to NULLS LAST | ||
pub fn nulls_last(mut self) -> Self { | ||
self.options.nulls_first = false; | ||
self | ||
} | ||
} | ||
|
||
impl PartialEq for PhysicalSortExpr { | ||
|
@@ -60,7 +134,7 @@ impl Hash for PhysicalSortExpr { | |
} | ||
} | ||
|
||
impl std::fmt::Display for PhysicalSortExpr { | ||
impl Display for PhysicalSortExpr { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "{} {}", self.expr, to_str(&self.options)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This shows what I would like to be able to do