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

Descending sort of boolean series flips number of trues and falses #17557

Closed
2 tasks done
wence- opened this issue Jul 10, 2024 · 1 comment · Fixed by #17558
Closed
2 tasks done

Descending sort of boolean series flips number of trues and falses #17557

wence- opened this issue Jul 10, 2024 · 1 comment · Fixed by #17558
Assignees
Labels
accepted Ready for implementation bug Something isn't working P-high Priority: high python Related to Python Polars rust Related to Rust Polars

Comments

@wence-
Copy link
Collaborator

wence- commented Jul 10, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

use polars::prelude::*;

fn main() -> Result<(), PolarsError> {
    let df = df!("a" => &[true, false, false, false])?.lazy();
    let expr = col("a").sort(SortOptions::default().with_order_descending(true));
    let expect = df.clone().collect()?;
    let got = df.clone().select([expr.clone()]).collect()?;
    println!("Got\n{}", &got);
    println!("Expect\n{}", &expect);
    Ok(())
}

Log output

run ProjectionExec

Issue description

The logic for flipping the order in BooleanChunked.sort_with looks a bit suss.

Probably this is needed:

diff --git a/crates/polars-core/src/chunked_array/ops/sort/mod.rs b/crates/polars-core/src/chunked_array/ops/sort/mod.rs
index c2ef58a23c..5bcbbe0086 100644
--- a/crates/polars-core/src/chunked_array/ops/sort/mod.rs
+++ b/crates/polars-core/src/chunked_array/ops/sort/mod.rs
@@ -610,10 +610,10 @@ impl ChunkSort<BooleanType> for BooleanChunked {
             let len = self.len();
             let n_set = self.sum().unwrap() as usize;
             let mut bitmap = MutableBitmap::with_capacity(len);
-            let (first, second) = if options.descending {
-                (true, false)
+            let (first, second, n_set) = if options.descending {
+                (true, false, len - n_set)
             } else {
-                (false, true)
+                (false, true, n_set)
             };
             bitmap.extend_constant(len - n_set, first);
             bitmap.extend_constant(n_set, second);

Expected behavior

Got
shape: (4, 1)
┌───────┐
│ a     │
│ ---   │
│ bool  │
╞═══════╡
│ true  │
│ true  │
│ true  │
│ false │
└───────┘
Expect
shape: (4, 1)
┌───────┐
│ a     │
│ ---   │
│ bool  │
╞═══════╡
│ true  │
│ false │
│ false │
│ false │
└───────┘

Installed versions

``` [package] name = "polars-doodles" version = "0.1.0" edition = "2021"

[dependencies]
polars = { version = "0.41.3", features = ["lazy"] }
polars-plan = { version = "0.41.3"}


</details>
@wence- wence- added bug Something isn't working needs triage Awaiting prioritization by a maintainer rust Related to Rust Polars labels Jul 10, 2024
@mcrumiller
Copy link
Contributor

mcrumiller commented Jul 10, 2024

Can confirm. Simpler python example:

>>> pl.Series([True, False, False]).sort(descending=True)
shape: (3,)
Series: '' [bool]
[
        true
        true
        false
]

The logic is a very simple fix here. The result returns the number of False items first, regardless of descending param.

@nameexhaustion nameexhaustion added accepted Ready for implementation P-high Priority: high and removed needs triage Awaiting prioritization by a maintainer labels Jul 11, 2024
@nameexhaustion nameexhaustion added the python Related to Python Polars label Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working P-high Priority: high python Related to Python Polars rust Related to Rust Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants