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 2e2b1da8af..1eccd1a9be 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 7db3513292..a68dc5080f 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')")