This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 9db7a465360 branch-2.1: [bugfix](nerids) align locate function 
behavior with BE side #50797 (#50832)
9db7a465360 is described below

commit 9db7a465360220fa479a538f9945f36fc4c42bb6
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue May 13 15:19:21 2025 +0800

    branch-2.1: [bugfix](nerids) align locate function behavior with BE side 
#50797 (#50832)
    
    Cherry-picked from #50797
    
    Co-authored-by: XLPE <[email protected]>
---
 .../functions/executable/StringArithmetic.java     | 23 ++++++++++++++++------
 .../fold_constant_string_arithmatic.groovy         | 17 ++++++++++++++++
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
index 2e2b1da8af8..1eccd1a9be5 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
@@ -347,7 +347,7 @@ public class StringArithmetic {
      */
     @ExecFunction(name = "locate")
     public static Expression locate(StringLikeLiteral first, StringLikeLiteral 
second) {
-        return new IntegerLiteral(second.getValue().indexOf(first.getValue()) 
+ 1);
+        return locate(first, second, new IntegerLiteral(1));
     }
 
     /**
@@ -355,12 +355,23 @@ public class StringArithmetic {
      */
     @ExecFunction(name = "locate")
     public static Expression locate(StringLikeLiteral first, StringLikeLiteral 
second, IntegerLiteral third) {
-        int result = second.getValue().indexOf(first.getValue()) + 1;
-        if (third.getValue() <= 0 || !substringImpl(second.getValue(), 
third.getValue(),
-                second.getValue().codePointCount(0, 
second.getValue().length())).contains(first.getValue())) {
-            result = 0;
+        String searchStr = first.getValue();
+        String targetStr = second.getValue();
+        int startPos = third.getValue();
+        if (searchStr.isEmpty()) {
+            int byteLength = targetStr.getBytes(StandardCharsets.UTF_8).length;
+            return (startPos >= 1 && startPos <= byteLength)
+                    ? new IntegerLiteral(startPos)
+                    : new IntegerLiteral(startPos == 1 ? 1 : 0);
         }
-        return new IntegerLiteral(result);
+
+        int strLength = targetStr.codePointCount(0, targetStr.length());
+        if (startPos < 1 || startPos > strLength) {
+            return new IntegerLiteral(0);
+        }
+        int offset = targetStr.offsetByCodePoints(0, startPos - 1);
+        int loc = targetStr.indexOf(searchStr, offset);
+        return loc == -1 ? new IntegerLiteral(0) : new 
IntegerLiteral(targetStr.codePointCount(0, loc) + 1);
     }
 
     /**
diff --git 
a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy
 
b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy
index 7db3513292f..a68dc5080f6 100644
--- 
a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy
+++ 
b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy
@@ -429,6 +429,23 @@ suite("fold_constant_string_arithmatic") {
     testFoldConst("select locate('北京', '上海天津北京杭州', -4)")
     testFoldConst("select locate('北京', '上海天津北京杭州', -5)")
     testFoldConst("select locate('2', '   123  ', 1)")
+    testFoldConst("select locate('bc', 'abcbcbc', 4)")
+    testFoldConst("select locate('a', 'a')")
+    testFoldConst("select locate('', '')")
+    testFoldConst("select locate('', '', 2)")
+    testFoldConst("select locate('abc', 'abcd')")
+    testFoldConst("select locate('', 'hello', 5)")
+    testFoldConst("select locate('', 'hello', 6)")
+    testFoldConst("select locate('', '哈哈😊😂🤣🤣😄')")
+    testFoldConst("select locate('', '哈哈😊😂🤣🤣😄', 26)")
+    testFoldConst("select locate('', '哈哈😊😂🤣🤣😄', 27)")
+    testFoldConst("select locate('🤣🤣', '哈哈😊😂🤣🤣😄', 5)")
+    testFoldConst("select locate('🤣🤣🤣', '哈哈😊😂🤣🤣😄', 5)")
+    testFoldConst("select locate('🤣', '哈哈😊😂🤣🤣😄', 6)")
+    testFoldConst("select locate('😅', '哈哈😊😂🤣🤣😄', 6)")
+    testFoldConst("select locate('안녕', '哈哈こんにち안녕하세', 6)")
+    testFoldConst("select locate('하세', '哈哈こんにち안녕하세', 9)")
+    testFoldConst("select locate('세', '哈哈こんにち안녕하세', 11)")
 
     // lower
     testFoldConst("select lower('AbC123')")


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to