Skip to content
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

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 10 additions & 40 deletions arrow-row/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,10 +1491,7 @@ mod tests {

let converter = RowConverter::new(vec![SortField::new_with_options(
DataType::Boolean,
SortOptions {
descending: true,
nulls_first: false,
},
SortOptions::default().desc().with_nulls_first(false),
Copy link
Contributor Author

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)

)])
.unwrap();

Expand Down Expand Up @@ -1612,10 +1609,7 @@ mod tests {

let converter = RowConverter::new(vec![SortField::new_with_options(
DataType::Binary,
SortOptions {
descending: true,
nulls_first: false,
},
SortOptions::default().desc().with_nulls_first(false),
)])
.unwrap();
let rows = converter.convert_columns(&[Arc::clone(&col)]).unwrap();
Expand Down Expand Up @@ -1694,10 +1688,7 @@ mod tests {

let converter = RowConverter::new(vec![SortField::new_with_options(
a.data_type().clone(),
SortOptions {
descending: true,
nulls_first: false,
},
SortOptions::default().desc().with_nulls_first(false),
)])
.unwrap();

Expand All @@ -1712,10 +1703,7 @@ mod tests {

let converter = RowConverter::new(vec![SortField::new_with_options(
a.data_type().clone(),
SortOptions {
descending: true,
nulls_first: true,
},
SortOptions::default().desc().with_nulls_first(true),
)])
.unwrap();

Expand Down Expand Up @@ -1888,10 +1876,7 @@ mod tests {
back[0].to_data().validate_full().unwrap();
assert_eq!(&back[0], &list);

let options = SortOptions {
descending: false,
nulls_first: false,
};
let options = SortOptions::default().asc().with_nulls_first(false);
let field = SortField::new_with_options(d.clone(), options);
let converter = RowConverter::new(vec![field]).unwrap();
let rows = converter.convert_columns(&[Arc::clone(&list)]).unwrap();
Expand All @@ -1908,10 +1893,7 @@ mod tests {
back[0].to_data().validate_full().unwrap();
assert_eq!(&back[0], &list);

let options = SortOptions {
descending: true,
nulls_first: false,
};
let options = SortOptions::default().desc().with_nulls_first(false);
let field = SortField::new_with_options(d.clone(), options);
let converter = RowConverter::new(vec![field]).unwrap();
let rows = converter.convert_columns(&[Arc::clone(&list)]).unwrap();
Expand All @@ -1928,10 +1910,7 @@ mod tests {
back[0].to_data().validate_full().unwrap();
assert_eq!(&back[0], &list);

let options = SortOptions {
descending: true,
nulls_first: true,
};
let options = SortOptions::default().desc().with_nulls_first(true);
let field = SortField::new_with_options(d, options);
let converter = RowConverter::new(vec![field]).unwrap();
let rows = converter.convert_columns(&[Arc::clone(&list)]).unwrap();
Expand Down Expand Up @@ -1991,10 +1970,7 @@ mod tests {
// null
// [[1, 2]]
// ]
let options = SortOptions {
descending: false,
nulls_first: true,
};
let options = SortOptions::default().asc().with_nulls_first(true);
let field = SortField::new_with_options(d.clone(), options);
let converter = RowConverter::new(vec![field]).unwrap();
let rows = converter.convert_columns(&[Arc::clone(&list)]).unwrap();
Expand All @@ -2010,10 +1986,7 @@ mod tests {
back[0].to_data().validate_full().unwrap();
assert_eq!(&back[0], &list);

let options = SortOptions {
descending: true,
nulls_first: true,
};
let options = SortOptions::default().desc().with_nulls_first(true);
let field = SortField::new_with_options(d.clone(), options);
let converter = RowConverter::new(vec![field]).unwrap();
let rows = converter.convert_columns(&[Arc::clone(&list)]).unwrap();
Expand All @@ -2029,10 +2002,7 @@ mod tests {
back[0].to_data().validate_full().unwrap();
assert_eq!(&back[0], &list);

let options = SortOptions {
descending: true,
nulls_first: false,
};
let options = SortOptions::default().desc().with_nulls_first(false);
let field = SortField::new_with_options(d, options);
let converter = RowConverter::new(vec![field]).unwrap();
let rows = converter.convert_columns(&[Arc::clone(&list)]).unwrap();
Expand Down
81 changes: 81 additions & 0 deletions arrow-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
//! Arrow logical types

mod datatype;

pub use datatype::*;
use std::fmt::Display;
mod datatype_parse;
mod error;
pub use error::*;
Expand All @@ -34,6 +36,35 @@ 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;
/// let options = SortOptions {
/// descending: false,
/// nulls_first: true,
/// };
/// // Default is ASC NULLs First
/// assert_eq!(options, SortOptions::default());
/// assert_eq!(options.to_string(), "ASC");
///
/// // Configure using builder APIs
/// let options = SortOptions::default()
/// .desc()
/// .with_nulls_first(true);
/// assert_eq!(options.to_string(), "DESC NULLS FIRST");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// assert_eq!(options.to_string(), "DESC NULLS FIRST");
/// assert_eq!(options.to_string(), "DESC");

If fmt continues to print nothing for the default.

/// ```
///
/// # Example operations
/// It is also possible to negate the sort options using the `!` operator.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the use of the ! in DataFusion a bit surprising at first

/// ```
/// 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
Expand All @@ -42,6 +73,56 @@ 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 {
// purposely don't display default NULLs value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default for nulls_first doesn't seem to be documented, so wouldn't it be clearer to print the value here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are right -- it is more consistent even if more verbose

} 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
pub fn desc(mut self) -> Self {
self.descending = true;
self
}

/// Set this sort options to sort in ascending order
pub fn asc(mut self) -> Self {
self.descending = 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 (nulls_first() / nulls_last() API for PhysicalSortExpr), should we add nulls_first() and nulls_last() here, too?

Copy link
Contributor Author

@alamb alamb Sep 24, 2024

Choose a reason for hiding this comment

The 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 {
Expand Down
Loading