-
Notifications
You must be signed in to change notification settings - Fork 147
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 .str.count_matches()
#2580
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45be2f9
Add count_matches expression
Vince7778 468134f
Add expression (not yet working)
Vince7778 296f399
Change API and add tests
Vince7778 217da49
Change match overlap behavior
Vince7778 35e2e94
Merge branch 'main' into conor/str-count-matches
Vince7778 5204d2b
Add single pattern test
Vince7778 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[dependencies] | ||
aho-corasick = "1.1.3" | ||
arrow2 = {workspace = true, features = [ | ||
"chrono-tz", | ||
"compute_take", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use common_error::{DaftError, DaftResult}; | ||
|
||
use daft_core::{datatypes::Field, schema::Schema, DataType, Series}; | ||
use daft_dsl::{ | ||
functions::{ScalarFunction, ScalarUDF}, | ||
ExprRef, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
struct CountMatchesFunction { | ||
pub(super) whole_words: bool, | ||
pub(super) case_sensitive: bool, | ||
} | ||
|
||
#[typetag::serde] | ||
impl ScalarUDF for CountMatchesFunction { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"count_matches" | ||
} | ||
|
||
fn to_field(&self, inputs: &[ExprRef], schema: &Schema) -> DaftResult<Field> { | ||
match inputs { | ||
[data, _] => match data.to_field(schema) { | ||
Ok(field) => match &field.dtype { | ||
DataType::Utf8 => Ok(Field::new(field.name, DataType::UInt64)), | ||
a => Err(DaftError::TypeError(format!( | ||
"Expects inputs to count_matches to be utf8, but received {a}", | ||
))), | ||
}, | ||
Err(e) => Err(e), | ||
}, | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 2 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series]) -> DaftResult<Series> { | ||
match inputs { | ||
[data, patterns] => { | ||
data.utf8_count_matches(patterns, self.whole_words, self.case_sensitive) | ||
} | ||
_ => Err(DaftError::ValueError(format!( | ||
"Expected 2 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
} | ||
|
||
pub fn utf8_count_matches( | ||
input: ExprRef, | ||
patterns: ExprRef, | ||
whole_words: bool, | ||
case_sensitive: bool, | ||
) -> ExprRef { | ||
ScalarFunction::new( | ||
CountMatchesFunction { | ||
whole_words, | ||
case_sensitive, | ||
}, | ||
vec![input, patterns], | ||
) | ||
.into() | ||
} | ||
|
||
#[cfg(feature = "python")] | ||
pub mod python { | ||
use daft_dsl::python::PyExpr; | ||
use pyo3::{pyfunction, PyResult}; | ||
|
||
#[pyfunction] | ||
pub fn utf8_count_matches( | ||
expr: PyExpr, | ||
patterns: PyExpr, | ||
whole_words: bool, | ||
case_sensitive: bool, | ||
) -> PyResult<PyExpr> { | ||
let expr = | ||
super::utf8_count_matches(expr.into(), patterns.into(), whole_words, case_sensitive); | ||
Ok(expr.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it'd be simpler to instead accept a regex?
the regex crate uses
aho-corasick
under the hood, so the perf implications should be negligible as long as we're only compiling the regex once.Regex would likely be a lot more intuitive/flexible from an end user perspective as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll change it to that. I didn't originally do this because I was worried about performance - I'll run some tests to make sure it isn't too affected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it does seem like using a regex is around 7x slower. I guess it just can't handle the large pattern created by concatenating the list of strings. So I think I'll keep it this way for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though it's slower, I think we should still allow for regex just for usability sake.
We can do this in a follow up PR, and have the python frontend map to a different backend implementation
count_matches('<text>') -> count_matches
count_matches(r'<pattern>') -> count_matches_regex