Skip to content

Commit

Permalink
fix(compute): LIKE escape parenthesis (#1042) (#1078)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Patsura <[email protected]>

Co-authored-by: Dmitry Patsura <[email protected]>
  • Loading branch information
alamb and ovr authored Dec 22, 2021
1 parent e0abdb9 commit 000bdb3
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ pub fn like_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
}
}
} else {
let re_pattern = right.replace("%", ".*").replace("_", ".");
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re = Regex::new(&format!("^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from LIKE pattern: {}",
Expand Down Expand Up @@ -436,7 +440,11 @@ pub fn nlike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
result.append(!left.value(i).ends_with(&right[1..]));
}
} else {
let re_pattern = right.replace("%", ".*").replace("_", ".");
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re = Regex::new(&format!("^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from LIKE pattern: {}",
Expand Down Expand Up @@ -517,7 +525,11 @@ pub fn ilike_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
);
}
} else {
let re_pattern = right.replace("%", ".*").replace("_", ".");
let re_pattern = right
.replace("%", ".*")
.replace("_", ".")
.replace("(", r#"\("#)
.replace(")", r#"\)"#);
let re = Regex::new(&format!("(?i)^{}$", re_pattern)).map_err(|e| {
ArrowError::ComputeError(format!(
"Unable to build regex from ILIKE pattern: {}",
Expand Down Expand Up @@ -2228,6 +2240,13 @@ mod tests {
vec![true, true, true, false, false, true, false]
);

test_utf8_scalar!(
test_utf8_array_like_scalar_escape_testing,
vec!["varchar(255)", "int(255)", "varchar", "int"],
"%(%)%",
like_utf8_scalar,
vec![true, true, false, false]
);
test_utf8_scalar!(
test_utf8_array_like_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down Expand Up @@ -2289,6 +2308,13 @@ mod tests {
nlike_utf8,
vec![false, false, false, true, true, false, true]
);
test_utf8_scalar!(
test_utf8_array_nlike_escape_testing,
vec!["varchar(255)", "int(255)", "varchar", "int"],
"%(%)%",
nlike_utf8_scalar,
vec![false, false, true, true]
);
test_utf8_scalar!(
test_utf8_array_nlike_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down Expand Up @@ -2336,6 +2362,13 @@ mod tests {
ilike_utf8,
vec![true, true, true, false, false, true, false]
);
test_utf8_scalar!(
ilike_utf8_scalar_escape_testing,
vec!["varchar(255)", "int(255)", "varchar", "int"],
"%(%)%",
ilike_utf8_scalar,
vec![true, true, false, false]
);
test_utf8_scalar!(
test_utf8_array_ilike_scalar,
vec!["arrow", "parquet", "datafusion", "flight"],
Expand Down

0 comments on commit 000bdb3

Please sign in to comment.