-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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: Expressify str.slice
#13747
feat: Expressify str.slice
#13747
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,117 @@ | ||
use arrow::array::Utf8Array; | ||
use polars_core::prelude::arity::{binary_elementwise, ternary_elementwise, unary_elementwise}; | ||
use polars_core::prelude::{Int64Chunked, StringChunked, UInt64Chunked}; | ||
|
||
/// 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); | ||
fn 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)) => { | ||
// If `offset` is negative, it counts from the end of the string. | ||
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) { | ||
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 = if let Some(length) = opt_length { | ||
length as usize | ||
} else { | ||
len_end | ||
}; | ||
|
||
// length to slice | ||
let length = length.unwrap_or(len_end); | ||
if length == 0 { | ||
return Some(""); | ||
} | ||
|
||
if length == 0 { | ||
return ""; | ||
} | ||
// compute | ||
let end_idx = iter_chars | ||
.nth(length.saturating_sub(1)) | ||
.map(|(idx, _)| idx) | ||
.unwrap_or(str_val.len()); | ||
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 { | ||
"" | ||
} | ||
}); | ||
Some(&str_val[offset_idx..end_idx]) | ||
} else { | ||
Some("") | ||
} | ||
}, | ||
_ => None, | ||
} | ||
} | ||
|
||
let new = Utf8Array::<i64>::from_trusted_len_values_iter(iter); | ||
new.with_validity(array.validity().cloned()) | ||
pub(super) fn substring( | ||
ca: &StringChunked, | ||
offset: &Int64Chunked, | ||
length: &UInt64Chunked, | ||
) -> StringChunked { | ||
match (ca.len(), offset.len(), length.len()) { | ||
(1, 1, _) => { | ||
// SAFETY: index `0` is in bound. | ||
let str_val = unsafe { ca.get_unchecked(0) }; | ||
// SAFETY: index `0` is in bound. | ||
let offset = unsafe { offset.get_unchecked(0) }; | ||
unary_elementwise(length, |length| substring_ternary(str_val, offset, length)) | ||
.with_name(ca.name()) | ||
}, | ||
(_, 1, 1) => { | ||
// SAFETY: index `0` is in bound. | ||
let offset = unsafe { offset.get_unchecked(0) }; | ||
// SAFETY: index `0` is in bound. | ||
let length = unsafe { length.get_unchecked(0) }; | ||
unary_elementwise(ca, |str_val| substring_ternary(str_val, offset, length)) | ||
}, | ||
(1, _, 1) => { | ||
// SAFETY: index `0` is in bound. | ||
let str_val = unsafe { ca.get_unchecked(0) }; | ||
// SAFETY: index `0` is in bound. | ||
let length = unsafe { length.get_unchecked(0) }; | ||
unary_elementwise(offset, |offset| substring_ternary(str_val, offset, length)) | ||
.with_name(ca.name()) | ||
}, | ||
(1, len_b, len_c) if len_b == len_c => { | ||
// SAFETY: index `0` is in bound. | ||
let str_val = unsafe { ca.get_unchecked(0) }; | ||
binary_elementwise(offset, length, |offset, length| { | ||
substring_ternary(str_val, offset, length) | ||
}) | ||
}, | ||
(len_a, 1, len_c) if len_a == len_c => { | ||
fn infer<F: for<'a> FnMut(Option<&'a str>, Option<u64>) -> Option<&'a str>>(f: F) -> F where | ||
{ | ||
f | ||
} | ||
// SAFETY: index `0` is in bound. | ||
let offset = unsafe { offset.get_unchecked(0) }; | ||
binary_elementwise( | ||
ca, | ||
length, | ||
infer(|str_val, length| substring_ternary(str_val, offset, length)), | ||
) | ||
}, | ||
(len_a, len_b, 1) if len_a == len_b => { | ||
fn infer<F: for<'a> FnMut(Option<&'a str>, Option<i64>) -> Option<&'a str>>(f: F) -> F where | ||
{ | ||
f | ||
} | ||
// SAFETY: index `0` is in bound. | ||
let length = unsafe { length.get_unchecked(0) }; | ||
binary_elementwise( | ||
ca, | ||
offset, | ||
infer(|str_val, offset| substring_ternary(str_val, offset, length)), | ||
) | ||
}, | ||
_ => ternary_elementwise(ca, offset, length, substring_ternary), | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -855,8 +855,8 @@ impl SQLFunctionVisitor<'_> { | |
#[cfg(feature = "nightly")] | ||
InitCap => self.visit_unary(|e| e.str().to_titlecase()), | ||
Left => self.try_visit_binary(|e, length| { | ||
Ok(e.str().slice(0, match length { | ||
Expr::Literal(LiteralValue::Int64(n)) => Some(n as u64), | ||
Ok(e.str().slice(lit(0), match length { | ||
Expr::Literal(LiteralValue::Int64(n)) => lit(n as u64), | ||
_ => { | ||
polars_bail!(InvalidOperation: "Invalid 'length' for Left: {}", function.args[1]); | ||
} | ||
|
@@ -905,11 +905,11 @@ impl SQLFunctionVisitor<'_> { | |
Reverse => self.visit_unary(|e| e.str().reverse()), | ||
Right => self.try_visit_binary(|e, length| { | ||
Ok(e.str().slice( match length { | ||
Expr::Literal(LiteralValue::Int64(n)) => -n, | ||
Expr::Literal(LiteralValue::Int64(n)) => lit(-n), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexander-beedie I've mostly left the SQL part as it is, so feel free to push a new commit or open a new PR if you want to improve it to work better with expressions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do; thx! :)) |
||
_ => { | ||
polars_bail!(InvalidOperation: "Invalid 'length' for Right: {}", function.args[1]); | ||
} | ||
}, None)) | ||
}, lit(Null))) | ||
}), | ||
RTrim => match function.args.len() { | ||
1 => self.visit_unary(|e| e.str().strip_chars_end(lit(Null))), | ||
|
@@ -925,19 +925,19 @@ impl SQLFunctionVisitor<'_> { | |
2 => self.try_visit_binary(|e, start| { | ||
Ok(e.str().slice( | ||
match start { | ||
Expr::Literal(LiteralValue::Int64(n)) => n - 1 , | ||
Expr::Literal(LiteralValue::Int64(n)) => lit(n - 1) , | ||
_ => polars_bail!(InvalidOperation: "Invalid 'start' for Substring: {}", function.args[1]), | ||
}, None)) | ||
}, lit(Null))) | ||
}), | ||
3 => self.try_visit_ternary(|e, start, length| { | ||
Ok(e.str().slice( | ||
match start { | ||
Expr::Literal(LiteralValue::Int64(n)) => n - 1, | ||
Expr::Literal(LiteralValue::Int64(n)) => lit(n - 1), | ||
_ => { | ||
polars_bail!(InvalidOperation: "Invalid 'start' for Substring: {}", function.args[1]); | ||
} | ||
}, match length { | ||
Expr::Literal(LiteralValue::Int64(n)) => Some(n as u64), | ||
Expr::Literal(LiteralValue::Int64(n)) => lit(n as u64), | ||
_ => { | ||
polars_bail!(InvalidOperation: "Invalid 'length' for Substring: {}", function.args[2]); | ||
} | ||
|
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.
The compiler is a bit annoying, so we have to introduce this
infer
function to keep it happy.