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

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


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

commit d2612d4c16b30f990596a839cf290042298bbce3
Author: XLPE <cry...@gmail.com>
AuthorDate: Tue May 13 09:00:27 2025 +0800

    [bugfix](nerids) align locate function behavior with BE side (#50797)
    
    
    In Doris versions above 2.1.8 and above 3.0.3, , the newly added locate
    function does not behave consistently with the BE.
    
    **Refer to the example below.**
    
    **Constant folding is enabled by default, and the result calculated by
    the FE is returned.**
    mysql> select locate('', 'ε“ˆε“ˆπŸ˜ŠπŸ˜‚πŸ€£πŸ€£πŸ˜„', 26);
    | locate('', 'ε“ˆε“ˆπŸ˜ŠπŸ˜‚πŸ€£πŸ€£πŸ˜„', 26) |
    |--------------------------------|
    | 1                              |
    
    
    mysql> select locate('bc', 'abcabcabc', 4);
    | locate('bc', 'abcabcabc', 4) |
    |------------------------------|
    | 2                            |
    
    **Set to disable constant folding and return the result calculated by
    the BE.**
    
    mysql> set debug_skip_fold_constant=true;
    
    mysql> select locate('', 'ε“ˆε“ˆπŸ˜ŠπŸ˜‚πŸ€£πŸ€£πŸ˜„', 26);
    | locate('', 'ε“ˆε“ˆπŸ˜ŠπŸ˜‚πŸ€£πŸ€£πŸ˜„', 26) |
    |--------------------------------|
    | 26                             |
    
    
    mysql> select locate('bc', 'abcabcabc', 4);
    | locate('bc', 'abcabcabc', 4) |
    |------------------------------|
    | 5                            |
---
 .../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 2909c46abc8..221208dc9eb 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
@@ -346,7 +346,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));
     }
 
     /**
@@ -354,12 +354,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 ac8a97c705e..4a85deedd53 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
@@ -430,6 +430,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: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to