alamb commented on code in PR #9051:
URL: https://github.com/apache/arrow-datafusion/pull/9051#discussion_r1470216940


##########
datafusion/physical-expr/src/string_expressions.rs:
##########
@@ -303,15 +303,29 @@ pub fn instr<T: OffsetSizeTrait>(args: &[ArrayRef]) -> 
Result<ArrayRef> {
     let string_array = as_generic_string_array::<T>(&args[0])?;
     let substr_array = as_generic_string_array::<T>(&args[1])?;
 
+    fn process_instr(string: &str, substr: &str) -> Option<i64> {
+        if substr.is_empty() {
+            return Some(1); // An empty substring is found at the start
+        }
+
+        let string_chars: Vec<char> = string.chars().collect();
+        let substr_chars: Vec<char> = substr.chars().collect();
+
+        string_chars
+            .windows(substr_chars.len())
+            .position(|window| window == substr_chars.as_slice())
+            .map_or(Some(0), |index| Some(index as i64 + 1))
+    }
+
     match args[0].data_type() {
         DataType::Utf8 => {
             let result = string_array
                 .iter()
                 .zip(substr_array.iter())
-                .map(|(string, substr)| match (string, substr) {
-                    (Some(string), Some(substr)) => string
-                        .find(substr)
-                        .map_or(Some(0), |index| Some((index + 1) as i32)),
+                .map(|(string_opt, substr_opt)| match (string_opt, substr_opt) 
{

Review Comment:
   I wonder if we could use `char_indices` here to find the relevant offset 
with the matched string? 
https://doc.rust-lang.org/std/primitive.str.html#method.char_indices
   
   🤔 
   
   If not, we can probably at least special case on `is_ascii` 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to