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

feat: Implement sorted flags for struct series #21290

Merged
merged 4 commits into from
Feb 19, 2025

Conversation

JakubValtar
Copy link
Contributor

This PR implements sorted flags for struct series.

import polars as pl
s = pl.Series("a", [{"x": 3 }, {"x": 1 }, {"x": 2 }]).sort()
assert s.flags["SORTED_ASC"]

It removes a duplicate StructChunked::arg_sort function, it exists in ChunkSort<StructType> for StructChunked with the same name and implementation.

Closes #13484

Already implemented in `impl ChunkSort<StructType> for StructChunked`
@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Feb 16, 2025
Copy link

codecov bot commented Feb 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.89%. Comparing base (5e51218) to head (0cee9a7).
Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21290      +/-   ##
==========================================
+ Coverage   79.82%   79.89%   +0.06%     
==========================================
  Files        1596     1596              
  Lines      228565   228562       -3     
  Branches     2608     2608              
==========================================
+ Hits       182462   182618     +156     
+ Misses      45507    45348     -159     
  Partials      596      596              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -645,26 +644,20 @@ impl ChunkSort<BinaryOffsetType> for BinaryOffsetChunked {
}
}

#[cfg(feature = "dtype-struct")]
impl StructChunked {
pub(crate) fn arg_sort(&self, options: SortOptions) -> IdxCa {
Copy link
Member

Choose a reason for hiding this comment

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

Why was this removed? Was it unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like this method was left over when ChunkSort was implemented for structs in #17522. A method with the same name, signature and implementation was added in ChunkSort impl and now some calls resolve to this one and some to the ChunkSort one. I removed it, because it is duplicate code, confusing to read, and a potential footgun if the implementations are not kept in sync.

This is the situation:

impl StructChunked {
    // <<< A >>> only used in `sort_with` below and one other place
    pub(crate) fn arg_sort(&self, options: SortOptions) -> IdxCa {
        let bin = _get_rows_encoded_ca(...).unwrap(); // same impl as B, just `get_row_encoded` inlined
        bin.arg_sort(Default::default())
    }
}

impl ChunkSort<StructType> for StructChunked {
    fn sort_with(&self, mut options: SortOptions) -> ChunkedArray<StructType> {
        ...
        let idx = self.arg_sort(options); // Looks like it calls B, but actually calls A
        ...
    }

    fn sort(&self, descending: bool) -> ChunkedArray<StructType> {
        self.sort_with(...) // Also calls A through `sort_with`
    }

    // <<< B >>> used in `T: ChunkSort` generic contexts
    fn arg_sort(&self, options: SortOptions) -> IdxCa {
        let bin = self.get_row_encoded(options).unwrap();
        bin.arg_sort(Default::default())
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

Right, now I see.

@ritchie46 ritchie46 merged commit 4e286d8 into pola-rs:main Feb 19, 2025
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flag sorted struct-type columns as sorted
2 participants