-
Notifications
You must be signed in to change notification settings - Fork 794
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 additional documentation and builder APIs to SortOptions
#6441
Changes from all commits
d2cdce7
6b7fcc3
53ae9c2
9f1d298
4f22fd8
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 | ||||
---|---|---|---|---|---|---|
|
@@ -19,7 +19,9 @@ | |||||
//! Arrow logical types | ||||||
|
||||||
mod datatype; | ||||||
|
||||||
pub use datatype::*; | ||||||
use std::fmt::Display; | ||||||
mod datatype_parse; | ||||||
mod error; | ||||||
pub use error::*; | ||||||
|
@@ -35,6 +37,42 @@ use std::ops; | |||||
pub mod ffi; | ||||||
|
||||||
/// Options that define the sort order of a given column | ||||||
/// | ||||||
/// The default sorts equivalently to of `ASC NULLS FIRST` in SQL (i.e. | ||||||
/// ascending order with nulls sorting before any other values). | ||||||
/// | ||||||
/// # Example creation | ||||||
/// ``` | ||||||
/// # use arrow_schema::SortOptions; | ||||||
/// // configure using explicit initialization | ||||||
/// let options = SortOptions { | ||||||
/// descending: false, | ||||||
/// nulls_first: true, | ||||||
/// }; | ||||||
/// // Default is ASC NULLs First | ||||||
/// assert_eq!(options, SortOptions::default()); | ||||||
/// assert_eq!(options.to_string(), "ASC NULLS FIRST"); | ||||||
/// | ||||||
/// // Configure using builder APIs | ||||||
/// let options = SortOptions::default() | ||||||
/// .desc() | ||||||
/// .nulls_first(); | ||||||
/// assert_eq!(options.to_string(), "DESC NULLS FIRST"); | ||||||
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.
Suggested change
If |
||||||
/// | ||||||
/// // configure using explicit field values | ||||||
/// let options = SortOptions::default() | ||||||
/// .with_descending(false) | ||||||
/// .with_nulls_first(false); | ||||||
/// assert_eq!(options.to_string(), "ASC NULLS LAST"); | ||||||
/// ``` | ||||||
/// | ||||||
/// # Example operations | ||||||
/// It is also possible to negate the sort options using the `!` operator. | ||||||
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. I found the use of the |
||||||
/// ``` | ||||||
/// use arrow_schema::SortOptions; | ||||||
/// let options = !SortOptions::default(); | ||||||
/// assert_eq!(options.to_string(), "DESC NULLS LAST"); | ||||||
/// ``` | ||||||
#[derive(Clone, Hash, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)] | ||||||
pub struct SortOptions { | ||||||
/// Whether to sort in descending order | ||||||
|
@@ -43,6 +81,76 @@ pub struct SortOptions { | |||||
pub nulls_first: bool, | ||||||
} | ||||||
|
||||||
impl Display for SortOptions { | ||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||||||
if self.descending { | ||||||
write!(f, "DESC")?; | ||||||
} else { | ||||||
write!(f, "ASC")?; | ||||||
} | ||||||
if self.nulls_first { | ||||||
write!(f, " NULLS FIRST")?; | ||||||
} else { | ||||||
write!(f, " NULLS LAST")?; | ||||||
} | ||||||
Ok(()) | ||||||
} | ||||||
} | ||||||
|
||||||
impl SortOptions { | ||||||
/// Create a new `SortOptions` struct | ||||||
pub fn new(descending: bool, nulls_first: bool) -> Self { | ||||||
Self { | ||||||
descending, | ||||||
nulls_first, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Set this sort options to sort in descending order | ||||||
/// | ||||||
/// See [Self::with_descending] to explicitly set the underlying field | ||||||
pub fn desc(mut self) -> Self { | ||||||
self.descending = true; | ||||||
self | ||||||
} | ||||||
|
||||||
/// Set this sort options to sort in ascending order | ||||||
/// | ||||||
/// See [Self::with_descending] to explicitly set the underlying field | ||||||
pub fn asc(mut self) -> Self { | ||||||
self.descending = false; | ||||||
self | ||||||
} | ||||||
|
||||||
/// Set this sort options to sort nulls first | ||||||
/// | ||||||
/// See [Self::with_nulls_first] to explicitly set the underlying field | ||||||
pub fn nulls_first(mut self) -> Self { | ||||||
self.nulls_first = true; | ||||||
self | ||||||
} | ||||||
|
||||||
/// Set this sort options to sort nulls last | ||||||
/// | ||||||
/// See [Self::with_nulls_first] to explicitly set the underlying field | ||||||
pub fn nulls_last(mut self) -> Self { | ||||||
self.nulls_first = false; | ||||||
self | ||||||
} | ||||||
|
||||||
/// Set this sort options to sort descending if argument is true | ||||||
pub fn with_descending(mut self, descending: bool) -> Self { | ||||||
self.descending = descending; | ||||||
self | ||||||
} | ||||||
|
||||||
/// Set this sort options to sort nulls first if argument is true | ||||||
pub fn with_nulls_first(mut self, nulls_first: bool) -> Self { | ||||||
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. We can keep this API for sure, but to be consistent with what you propose at apache/datafusion#12595 ( 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. it is a good idea -- added in 4f22fd8 |
||||||
self.nulls_first = nulls_first; | ||||||
self | ||||||
} | ||||||
} | ||||||
|
||||||
impl Default for SortOptions { | ||||||
fn default() -> Self { | ||||||
Self { | ||||||
|
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.
Here is an example of using the new API
Not only is this more concise, I believe the intent is easier to reason about now (asc/desc)