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

refactor: Merge similar functions ilike_scalar and nilike_scalar #3303

Merged
merged 2 commits into from Dec 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 17 additions & 92 deletions arrow-string/src/like.rs
Expand Up @@ -584,9 +584,10 @@ fn ilike_dict<K: ArrowPrimitiveType>(
}

#[inline]
fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
fn ilike_scalar_op<'a, F: Fn(bool) -> bool, L: ArrayAccessor<Item = &'a str>>(
left: L,
right: &str,
op: F,
) -> Result<BooleanArray, ArrowError> {
let null_bit_buffer = left.data().null_buffer().cloned();
let bytes = bit_util::ceil(left.len(), 8);
Expand All @@ -598,7 +599,7 @@ fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
let right_uppercase = right.to_uppercase();
for i in 0..left.len() {
unsafe {
if left.value_unchecked(i).to_uppercase() == right_uppercase {
if op(left.value_unchecked(i).to_uppercase() == right_uppercase) {
bit_util::set_bit(bool_slice, i);
}
}
Expand All @@ -611,10 +612,10 @@ fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
let start_str = &right[..right.len() - 1].to_uppercase();
for i in 0..left.len() {
unsafe {
if left
if op(left
.value_unchecked(i)
.to_uppercase()
.starts_with(start_str)
.starts_with(start_str))
{
bit_util::set_bit(bool_slice, i);
}
Expand All @@ -626,7 +627,7 @@ fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(

for i in 0..left.len() {
unsafe {
if left.value_unchecked(i).to_uppercase().ends_with(ends_str) {
if op(left.value_unchecked(i).to_uppercase().ends_with(ends_str)) {
bit_util::set_bit(bool_slice, i);
}
}
Expand All @@ -639,7 +640,7 @@ fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
let contains = &right[1..right.len() - 1].to_uppercase();
for i in 0..left.len() {
unsafe {
if left.value_unchecked(i).to_uppercase().contains(contains) {
if op(left.value_unchecked(i).to_uppercase().contains(contains)) {
bit_util::set_bit(bool_slice, i);
}
}
Expand All @@ -655,7 +656,7 @@ fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(

for i in 0..left.len() {
let haystack = unsafe { left.value_unchecked(i) };
if re.is_match(haystack) {
if op(re.is_match(haystack)) {
bit_util::set_bit(bool_slice, i);
}
}
Expand All @@ -675,6 +676,14 @@ fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
Ok(BooleanArray::from(data))
}

#[inline]
fn ilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
left: L,
right: &str,
) -> Result<BooleanArray, ArrowError> {
ilike_scalar_op(left, right, |x| x)
}

/// Perform SQL `left ILIKE right` operation on [`StringArray`] /
/// [`LargeStringArray`], or [`DictionaryArray`] with values
/// [`StringArray`]/[`LargeStringArray`] and a scalar.
Expand Down Expand Up @@ -852,91 +861,7 @@ fn nilike_scalar<'a, L: ArrayAccessor<Item = &'a str>>(
left: L,
right: &str,
) -> Result<BooleanArray, ArrowError> {
let null_bit_buffer = left.data().null_buffer().cloned();
let bytes = bit_util::ceil(left.len(), 8);
let mut bool_buf = MutableBuffer::from_len_zeroed(bytes);
let bool_slice = bool_buf.as_slice_mut();

if !right.contains(is_like_pattern) {
// fast path, can use equals
let right_uppercase = right.to_uppercase();
for i in 0..left.len() {
unsafe {
if left.value_unchecked(i).to_uppercase() != right_uppercase {
bit_util::set_bit(bool_slice, i);
}
}
}
} else if right.ends_with('%')
&& !right.ends_with("\\%")
&& !right[..right.len() - 1].contains(is_like_pattern)
{
// fast path, can use starts_with
let start_str = &right[..right.len() - 1].to_uppercase();
for i in 0..left.len() {
unsafe {
if !(left
.value_unchecked(i)
.to_uppercase()
.starts_with(start_str))
{
bit_util::set_bit(bool_slice, i);
}
}
}
} else if right.starts_with('%') && !right[1..].contains(is_like_pattern) {
// fast path, can use ends_with
let ends_str = &right[1..].to_uppercase();

for i in 0..left.len() {
unsafe {
if !(left.value_unchecked(i).to_uppercase().ends_with(ends_str)) {
bit_util::set_bit(bool_slice, i);
}
}
}
} else if right.starts_with('%')
&& right.ends_with('%')
&& !right[1..right.len() - 1].contains(is_like_pattern)
{
// fast path, can use contains
let contains = &right[1..right.len() - 1].to_uppercase();
for i in 0..left.len() {
unsafe {
if !(left.value_unchecked(i).to_uppercase().contains(contains)) {
bit_util::set_bit(bool_slice, i);
}
}
}
} else {
let re_pattern = replace_like_wildcards(right)?;
let re = Regex::new(&format!("(?i)^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from ILIKE pattern: {}",
e
))
})?;

for i in 0..left.len() {
let haystack = unsafe { left.value_unchecked(i) };
if !re.is_match(haystack) {
bit_util::set_bit(bool_slice, i);
}
}
};

let data = unsafe {
ArrayData::new_unchecked(
DataType::Boolean,
left.len(),
None,
null_bit_buffer,
0,
vec![bool_buf.into()],
vec![],
)
};
Ok(BooleanArray::from(data))
ilike_scalar_op(left, right, |x| !x)
}

/// Perform SQL `left NOT ILIKE right` operation on [`StringArray`] /
Expand Down