Cherry-picked from #50797 Co-authored-by: XLPE <crykix@gmail.com>
This commit is contained in:
committed by
GitHub
parent
550df8f4f1
commit
9db7a46536
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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')")
|
||||
|
||||
Reference in New Issue
Block a user