Skip to content

Commit

Permalink
Refactor regexplike signature (#13394)
Browse files Browse the repository at this point in the history
* update

* update

* update

* clean up errors

* fix flags types

* fix failed example
  • Loading branch information
jiashenC authored Dec 8, 2024
1 parent 035fd3b commit f2de2c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion datafusion-examples/examples/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async fn main() -> Result<()> {

// invalid flags will result in an error
let result = ctx
.sql(r"select regexp_like('\b4(?!000)\d\d\d\b', 4010, 'g')")
.sql(r"select regexp_like('\b4(?!000)\d\d\d\b', '4010', 'g')")
.await?
.collect()
.await;
Expand Down
50 changes: 29 additions & 21 deletions datafusion/functions/src/regex/regexplike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,7 @@ impl RegexpLikeFunc {
pub fn new() -> Self {
Self {
signature: Signature::one_of(
vec![
TypeSignature::Exact(vec![Utf8View, Utf8]),
TypeSignature::Exact(vec![Utf8View, Utf8View]),
TypeSignature::Exact(vec![Utf8View, LargeUtf8]),
TypeSignature::Exact(vec![Utf8, Utf8]),
TypeSignature::Exact(vec![Utf8, Utf8View]),
TypeSignature::Exact(vec![Utf8, LargeUtf8]),
TypeSignature::Exact(vec![LargeUtf8, Utf8]),
TypeSignature::Exact(vec![LargeUtf8, Utf8View]),
TypeSignature::Exact(vec![LargeUtf8, LargeUtf8]),
TypeSignature::Exact(vec![Utf8View, Utf8, Utf8]),
TypeSignature::Exact(vec![Utf8View, Utf8View, Utf8]),
TypeSignature::Exact(vec![Utf8View, LargeUtf8, Utf8]),
TypeSignature::Exact(vec![Utf8, Utf8, Utf8]),
TypeSignature::Exact(vec![Utf8, Utf8View, Utf8]),
TypeSignature::Exact(vec![Utf8, LargeUtf8, Utf8]),
TypeSignature::Exact(vec![LargeUtf8, Utf8, Utf8]),
TypeSignature::Exact(vec![LargeUtf8, Utf8View, Utf8]),
TypeSignature::Exact(vec![LargeUtf8, LargeUtf8, Utf8]),
],
vec![TypeSignature::String(2), TypeSignature::String(3)],
Volatility::Immutable,
),
}
Expand Down Expand Up @@ -211,7 +192,34 @@ pub fn regexp_like(args: &[ArrayRef]) -> Result<ArrayRef> {
match args.len() {
2 => handle_regexp_like(&args[0], &args[1], None),
3 => {
let flags = args[2].as_string::<i32>();
let flags = match args[2].data_type() {
Utf8 => args[2].as_string::<i32>(),
LargeUtf8 => {
let large_string_array = args[2].as_string::<i64>();
let string_vec: Vec<Option<&str>> = (0..large_string_array.len()).map(|i| {
if large_string_array.is_null(i) {
None
} else {
Some(large_string_array.value(i))
}
})
.collect();

&GenericStringArray::<i32>::from(string_vec)
},
_ => {
let string_view_array = args[2].as_string_view();
let string_vec: Vec<Option<String>> = (0..string_view_array.len()).map(|i| {
if string_view_array.is_null(i) {
None
} else {
Some(string_view_array.value(i).to_string())
}
})
.collect();
&GenericStringArray::<i32>::from(string_vec)
},
};

if flags.iter().any(|s| s == Some("g")) {
return plan_err!("regexp_like() does not support the \"global\" option");
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/string/string_view.slt
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ EXPLAIN SELECT
FROM test;
----
logical_plan
01)Projection: regexp_like(test.column1_utf8view, Utf8("^https?://(?:www\.)?([^/]+)/.*$")) AS k
01)Projection: regexp_like(test.column1_utf8view, Utf8View("^https?://(?:www\.)?([^/]+)/.*$")) AS k
02)--TableScan: test projection=[column1_utf8view]

## Ensure no casts for REGEXP_MATCH
Expand Down

0 comments on commit f2de2c4

Please sign in to comment.