-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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(rust, python): expressify offset
and length
parameters for str.slice
#12071
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d524bf8
feat(rust,python): expressify `offset` and `length` for `str.slice`
cmdlineluser b68b37d
unneeded variable name
cmdlineluser 22d6a40
clippy
cmdlineluser 98719e9
remove broadcasting
cmdlineluser 396b7ef
add lifetimes infer helpers
cmdlineluser fafbdd1
clippy-nightly
cmdlineluser 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
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,51 +1,62 @@ | ||
use arrow::array::Utf8Array; | ||
use polars_core::prelude::arity::ternary_elementwise; | ||
|
||
use crate::chunked_array::{Int64Chunked, UInt64Chunked, Utf8Chunked}; | ||
|
||
/// Returns a Utf8Array<O> with a substring starting from `start` and with optional length `length` of each of the elements in `array`. | ||
/// `start` can be negative, in which case the start counts from the end of the string. | ||
pub(super) fn utf8_substring( | ||
array: &Utf8Array<i64>, | ||
start: i64, | ||
length: &Option<u64>, | ||
) -> Utf8Array<i64> { | ||
let length = length.map(|v| v as usize); | ||
/// `offset` can be negative, in which case the offset counts from the end of the string. | ||
fn utf8_substring_ternary( | ||
opt_str_val: Option<&str>, | ||
opt_offset: Option<i64>, | ||
opt_length: Option<u64>, | ||
) -> Option<&str> { | ||
match (opt_str_val, opt_offset) { | ||
(Some(str_val), Some(offset)) => { | ||
// compute where we should offset slicing this entry. | ||
let offset = if offset >= 0 { | ||
offset as usize | ||
} else { | ||
let offset = (0i64 - offset) as usize; | ||
str_val | ||
.char_indices() | ||
.rev() | ||
.nth(offset) | ||
.map(|(idx, _)| idx + 1) | ||
.unwrap_or(0) | ||
}; | ||
|
||
let iter = array.values_iter().map(|str_val| { | ||
// compute where we should start slicing this entry. | ||
let start = if start >= 0 { | ||
start as usize | ||
} else { | ||
let start = (0i64 - start) as usize; | ||
str_val | ||
.char_indices() | ||
.rev() | ||
.nth(start) | ||
.map(|(idx, _)| idx + 1) | ||
.unwrap_or(0) | ||
}; | ||
let mut iter_chars = str_val.char_indices(); | ||
if let Some((offset_idx, _)) = iter_chars.nth(offset) { | ||
// length of the str | ||
let len_end = str_val.len() - offset_idx; | ||
|
||
let mut iter_chars = str_val.char_indices(); | ||
if let Some((start_idx, _)) = iter_chars.nth(start) { | ||
// length of the str | ||
let len_end = str_val.len() - start_idx; | ||
// slice to end of str if no length given | ||
let length = match opt_length { | ||
Some(length) => length as usize, | ||
_ => len_end, | ||
}; | ||
|
||
// length to slice | ||
let length = length.unwrap_or(len_end); | ||
if length == 0 { | ||
return Some(""); | ||
} | ||
// compute | ||
let end_idx = iter_chars | ||
.nth(length.saturating_sub(1)) | ||
.map(|(idx, _)| idx) | ||
.unwrap_or(str_val.len()); | ||
|
||
if length == 0 { | ||
return ""; | ||
Some(&str_val[offset_idx..end_idx]) | ||
} else { | ||
Some("") | ||
} | ||
// compute | ||
let end_idx = iter_chars | ||
.nth(length.saturating_sub(1)) | ||
.map(|(idx, _)| idx) | ||
.unwrap_or(str_val.len()); | ||
|
||
&str_val[start_idx..end_idx] | ||
} else { | ||
"" | ||
} | ||
}); | ||
}, | ||
_ => None, | ||
} | ||
} | ||
|
||
let new = Utf8Array::<i64>::from_trusted_len_values_iter(iter); | ||
new.with_validity(array.validity().cloned()) | ||
pub(super) fn utf8_substring( | ||
ca: &Utf8Chunked, | ||
offset: &Int64Chunked, | ||
length: &UInt64Chunked, | ||
) -> Utf8Chunked { | ||
ternary_elementwise(ca, offset, length, utf8_substring_ternary) | ||
} |
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
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
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 don't want to add broadcasting by extending memory. See discussion here; #11900
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.
Ah! Excellent, thanks @ritchie46 - that explains the need for the added length checks / fallback logic.
I'll switch it around.