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: Add cat.contains and cat.contains_any #20582

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mcrumiller
Copy link
Contributor

Followup to #20257. Again, expression input patterns not allowed in cat.contains, only string inputs. For contains_any, expression inputs are allowed, because these are interpreted as a list of possible patterns.

@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 Jan 7, 2025
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 97.14286% with 2 lines in your changes missing coverage. Please review.

Project coverage is 79.90%. Comparing base (ed19c73) to head (cface6b).
Report is 33 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-plan/src/dsl/function_expr/cat.rs 94.11% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20582      +/-   ##
==========================================
+ Coverage   79.86%   79.90%   +0.03%     
==========================================
  Files        1593     1596       +3     
  Lines      228268   228623     +355     
  Branches     2600     2608       +8     
==========================================
+ Hits       182311   182678     +367     
+ Misses      45360    45349      -11     
+ Partials      597      596       -1     

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

@connor-elliott
Copy link

Any movement on this one?

@mcrumiller mcrumiller marked this pull request as draft February 12, 2025 14:32
@mcrumiller mcrumiller marked this pull request as ready for review February 12, 2025 14:56
Copy link
Collaborator

@coastalwhite coastalwhite left a comment

Choose a reason for hiding this comment

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

very minor nits aside. I think this is okay to merge.

Comment on lines 316 to 318
Use the Aho-Corasick algorithm to find matches.

Determines if any of the patterns are contained in the string representation.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit. I think these two lines should be swapped

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 copied this (with modifications) from the str.contains_any description, but yes I agree. I've swapped, and also swapped for the str.contains_any docstring as well since it's very minor. Updated in cface6b.

Comment on lines 325 to 327
Use the Aho-Corasick algorithm to find matches.

Determines if any of the patterns are contained in the string representation.
Copy link
Collaborator

Choose a reason for hiding this comment

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

idem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +188 to +204
#[cfg(all(feature = "strings", feature = "regex"))]
pub(super) fn contains(s: &Column, pat: &str, literal: bool, strict: bool) -> PolarsResult<Column> {
let ca = s.categorical()?;
if literal {
try_apply_to_cats(ca, |s| s.contains_literal(pat))
} else {
try_apply_to_cats(ca, |s| s.contains(pat, strict))
}
}

#[cfg(all(feature = "strings", feature = "find_many"))]
fn contains_many(s: &[Column], ascii_case_insensitive: bool) -> PolarsResult<Column> {
let ca = s[0].categorical()?;
let patterns = s[1].str()?;
try_apply_to_cats(ca, |s| {
strings::contains_any(s, patterns, ascii_case_insensitive)
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

LGTM

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.

3 participants