Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions datafusion/core/tests/repro_sort_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use datafusion::prelude::*;
use datafusion_common::Result;
use datafusion_expr::SortOptions;

#[test]
fn test_sort_api_usage() -> Result<()> {
let expr = col("a");

// Old API: sort(asc, nulls_first)
// "True, False" -> Ascending, Nulls Last
let sort_expr = expr.clone().sort(true, false);

assert_eq!(sort_expr.asc, true);
assert_eq!(sort_expr.nulls_first, false);

// New API: sort_by with SortOptions
// Descending, Nulls First
let sort_expr = expr.clone().sort_by(SortOptions::new().desc().nulls_first());

assert_eq!(sort_expr.asc, false);
assert_eq!(sort_expr.nulls_first, true);

// New API: Ascending, Nulls Last (default)
let sort_expr = expr.sort_by(SortOptions::new());

assert_eq!(sort_expr.asc, true);
assert_eq!(sort_expr.nulls_first, false);

Ok(())
}


11 changes: 11 additions & 0 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,17 @@ impl Expr {
Sort::new(self, asc, nulls_first)
}

/// Create a sort configuration from an existing expression using `SortOptions`.
///
/// ```
/// # use datafusion_expr::{col, SortOptions};
/// let sort_expr = col("foo").sort_by(SortOptions::new().desc().nulls_first());
/// ```
pub fn sort_by(self, options: crate::sort_options::SortOptions) -> Sort {
Sort::new(self, !options.descending, options.nulls_first)
}


/// Return `IsTrue(Box(self))`
pub fn is_true(self) -> Expr {
Expr::IsTrue(Box::new(self))
Expand Down
2 changes: 2 additions & 0 deletions datafusion/expr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub mod dml {
pub mod planner;
pub mod registry;
pub mod simplify;
pub mod sort_options;
pub mod sort_properties {
pub use datafusion_expr_common::sort_properties::*;
}
Expand Down Expand Up @@ -128,6 +129,7 @@ pub use udaf::{
pub use udf::{ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl};
pub use udwf::{LimitEffect, ReversedUDWF, WindowUDF, WindowUDFImpl};
pub use window_frame::{WindowFrame, WindowFrameBound, WindowFrameUnits};
pub use sort_options::SortOptions;

#[cfg(test)]
#[ctor::ctor]
Expand Down
53 changes: 53 additions & 0 deletions datafusion/expr/src/sort_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use arrow::compute::SortOptions as ArrowSortOptions;

/// Options for sorting.
///
/// This struct implements a builder pattern for creating `SortOptions`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SortOptions {
pub descending: bool,
pub nulls_first: bool,
}

impl SortOptions {
/// Create a new `SortOptions` with default values (Ascending, Nulls Last).
pub fn new() -> Self {
Self {
descending: false,
nulls_first: false,
}
}
Comment on lines 14 to 19
Copy link

@Banyc Banyc Feb 9, 2026

Choose a reason for hiding this comment

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

dangerous; this is again hiding information from use sites; clankers can never understand the idea, other than copy-and-pasting the training data 80% of which is a bunch of

Copy link

Choose a reason for hiding this comment

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

clankers be like: wow builder pattern yay


/// Set sort order to descending.
pub fn desc(mut self) -> Self {
self.descending = true;
self
}

/// Set sort order to ascending.
pub fn asc(mut self) -> Self {
self.descending = false;
self
}

/// Set nulls to come first.
pub fn nulls_first(mut self) -> Self {
self.nulls_first = true;
self
}

/// Set nulls to come last.
pub fn nulls_last(mut self) -> Self {
self.nulls_first = false;
self
}
}

impl From<SortOptions> for ArrowSortOptions {
fn from(options: SortOptions) -> Self {
ArrowSortOptions {
descending: options.descending,
nulls_first: options.nulls_first,
}
}
}
Loading