Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](Nereids) fix fe folding constant of string functions and add more cases #45233

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,20 @@ public static Expression right(StringLikeLiteral first, IntegerLiteral second) {
*/
@ExecFunction(name = "locate")
public static Expression locate(StringLikeLiteral first, StringLikeLiteral second) {
return new IntegerLiteral(second.getValue().trim().indexOf(first.getValue()) + 1);
return new IntegerLiteral(second.getValue().indexOf(first.getValue()) + 1);
}

/**
* Executable arithmetic functions Locate
*/
@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().length()).contains(first.getValue())) {
result = 0;
}
return new IntegerLiteral(result);
}

/**
Expand All @@ -333,12 +346,14 @@ public static Expression instr(StringLikeLiteral first, StringLikeLiteral second
* Executable arithmetic functions Ascii
*/
@ExecFunction(name = "ascii")
public static Expression ascii(StringLikeLiteral first) {
public static Expression ascii(StringLikeLiteral first) throws UnsupportedEncodingException {
morrySnow marked this conversation as resolved.
Show resolved Hide resolved
if (first.getValue().length() == 0) {
return new IntegerLiteral(0);
}
char firstChar = first.getValue().charAt(0);
return new IntegerLiteral(firstChar);
String character = first.getValue();
byte[] utf8Bytes = character.getBytes("UTF-8");
int firstByteAscii = utf8Bytes[0] & 0xFF;
return new IntegerLiteral(firstByteAscii);
}

/**
Expand Down Expand Up @@ -583,7 +598,7 @@ public static Expression fieldVarchar(StringLikeLiteral first, VarcharLiteral...
}

private static int findStringInSet(String target, String input) {
String[] split = input.split(",");
String[] split = input.split(",", -1);
for (int i = 0; i < split.length; i++) {
if (split[i].equals(target)) {
return i + 1;
Expand All @@ -605,6 +620,10 @@ public static Expression findInSetVarchar(StringLikeLiteral first, StringLikeLit
*/
@ExecFunction(name = "repeat")
public static Expression repeat(StringLikeLiteral first, IntegerLiteral second) {
// when it is too large for fe to make result string, do not folding on fe, limit 1 MB
if ((first.getValue().length() * second.getValue()) > 1000000) {
throw new AnalysisException("repeat too large to fold const by fe");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < second.getValue(); i++) {
sb.append(first.getValue());
Expand All @@ -627,6 +646,10 @@ public static Expression reverseVarchar(StringLikeLiteral first) {
*/
@ExecFunction(name = "space")
public static Expression space(IntegerLiteral first) {
// when it is too large for fe to make result string, do not folding on fe, limit 1 MB
if (first.getValue() > 1000000) {
throw new AnalysisException("space too large to fold const by fe");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < first.getValue(); i++) {
sb.append(' ');
Expand All @@ -639,7 +662,7 @@ public static Expression space(IntegerLiteral first) {
*/
@ExecFunction(name = "split_by_char")
public static Expression splitByChar(StringLikeLiteral first, StringLikeLiteral second) {
String[] result = first.getValue().split(second.getValue());
String[] result = first.getValue().split(second.getValue(), -1);
List<Literal> items = new ArrayList<>();
for (int i = 1; i < result.length; i++) {
items.add((Literal) castStringLikeLiteral(first, result[i]));
Expand All @@ -666,16 +689,16 @@ public static Expression splitPart(StringLikeLiteral first, StringLikeLiteral ch
if (".$|()[{^?*+\\".contains(separator) || separator.startsWith("\\")) {
separator = "\\" + separator;
}
parts = sb.reverse().toString().split(separator);
parts = sb.reverse().toString().split(separator, -1);
} else {
if (".$|()[{^?*+\\".contains(separator) || separator.startsWith("\\")) {
separator = "\\" + separator;
}
parts = first.getValue().split(separator);
parts = first.getValue().split(separator, -1);
}

if (parts.length < Math.abs(number.getValue()) || number.getValue() == 0) {
if (parts.length == Math.abs(number.getValue()) - 1) {
if (parts.length == Math.abs(number.getValue())) {
if (number.getValue() < 0 && first.getValue().startsWith(chr.getValue())
|| number.getValue() > 0 && first.getValue().endsWith(chr.getValue())) {
return castStringLikeLiteral(first, "");
Expand All @@ -695,7 +718,10 @@ public static Expression splitPart(StringLikeLiteral first, StringLikeLiteral ch
*/
@ExecFunction(name = "substring_index")
public static Expression substringIndex(StringLikeLiteral first, StringLikeLiteral chr, IntegerLiteral number) {
String[] parts = first.getValue().split(chr.getValue());
if (chr.getValue().isEmpty()) {
return chr;
}
String[] parts = first.getValue().split(chr.getValue(), -1);
if (Math.abs(number.getValue()) >= parts.length) {
return first;
}
Expand Down Expand Up @@ -907,13 +933,13 @@ public static Expression extractUrlParameter(StringLikeLiteral first, StringLike
return castStringLikeLiteral(first, "");
}

String[] urlParts = first.getValue().split("\\?");
String[] urlParts = first.getValue().split("\\?", -1);
if (urlParts.length > 1) {
String query = urlParts[1];
String[] pairs = query.split("&");
String[] pairs = query.split("&", -1);

for (String pair : pairs) {
String[] keyValue = pair.split("=");
String[] keyValue = pair.split("=", -1);
if (second.getValue().equals(keyValue[0])) {
return castStringLikeLiteral(first, keyValue[1]);
}
Expand Down
Loading
Loading