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

fix(rust, python): improve concat_list with empty list error message #7236

Merged
merged 1 commit into from
Mar 2, 2023
Merged

fix(rust, python): improve concat_list with empty list error message #7236

merged 1 commit into from
Mar 2, 2023

Conversation

josh
Copy link
Contributor

@josh josh commented Feb 28, 2023

Currently calling pl.concat_list with an empty list panics in Rust. It seems like the expected behavior should just be to return an empty list series.

pl.concat_list([])
# PanicException: index out of bounds: the len is 0 but the index is 0

My current fix is rather high level and only fixes it at the Python API surface. It might be better if this could somehow be patched at a lower level. But because pl.concat_list is implemented on top of a lower lhs.concat([]) interface, a ton of stuff just breaks expecting there to be a lhs. I think I maybe out of my depth there.

Related #7235

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels Feb 28, 2023
@ritchie46
Copy link
Member

I think we should raise an error. The [] in this case is not the list we want to concat, but the set of lists that we want to concattenate, which should not be empty.

This also keeps consistency with rust as we can raise the error there.

@josh josh marked this pull request as draft February 28, 2023 17:42
@josh
Copy link
Contributor Author

josh commented Feb 28, 2023

I tweaked the Python API to raise a nicer error. But there's still something inconsistent with this approach at the Rust level. pub fn concat_lst<E, IE>(s: E) -> Expr is defined as infallible. To be more specific about the panic on master, it happens at a very odd place in the execution phase.

index out of bounds: the len is 0 but the index is 0

If concat_lst should consistency not permit an empty list of expressions, it's signature should probably be pub fn concat_lst<E, IE>(s: E) -> PolarsResult<Expr> instead. And a nicer is_empty guard should be added to that function to throw earlier.

Also appears that pl.concat_str([]) leads to a similar error, and it's Rust API also returns Expr, not PolarsResult<Expr>.

So what do you think about me changing the types on those public functions? Or the other direction is keeping them infallible as is, but returning an empty value respectively.

@ritchie46
Copy link
Member

I tweaked the Python API to raise a nicer error. But there's still something inconsistent with this approach at the Rust level. pub fn concat_lst<E, IE>(s: E) -> Expr is defined as infallible. To be more specific about the panic on master, it happens at a very odd place in the execution phase.

Let's make concat_lst fallible then. :)

@josh josh changed the title fix(python): handle concat_list empty case fix(rust, python): improve concat_list with empty list error message Feb 28, 2023
@josh josh marked this pull request as ready for review February 28, 2023 19:46
@github-actions github-actions bot added the rust Related to Rust Polars label Feb 28, 2023
@josh
Copy link
Contributor Author

josh commented Feb 28, 2023

Pushed the error message down to Rust. So that covers it nicely from Python without any changes there. Let me know if NoDataError is the right choice here.

Side note, it would be neat if pl.lit([]) could represent that empty list use case #6608.

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

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

Almost there. :)

polars/polars-lazy/polars-plan/src/dsl/functions.rs Outdated Show resolved Hide resolved
@josh josh requested a review from ritchie46 March 1, 2023 17:38
@ritchie46
Copy link
Member

Thank you @josh!

@ritchie46 ritchie46 merged commit 5b5e3d7 into pola-rs:master Mar 2, 2023
@josh josh deleted the concat-list-empty branch March 2, 2023 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants