Skip to content

Commit b607a04

Browse files
author
LiBinfeng
committed
[fix](Nereids) fix functions use split and add more cases about fe fold string function
1 parent ed11252 commit b607a04

File tree

2 files changed

+619
-713
lines changed

2 files changed

+619
-713
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ public static Expression locate(StringLikeLiteral first, StringLikeLiteral secon
321321
return new IntegerLiteral(second.getValue().trim().indexOf(first.getValue()) + 1);
322322
}
323323

324+
/**
325+
* Executable arithmetic functions Locate
326+
*/
327+
@ExecFunction(name = "locate")
328+
public static Expression locate(StringLikeLiteral first, StringLikeLiteral second, IntegerLiteral third) {
329+
int result = second.getValue().trim().indexOf(first.getValue()) + 1;
330+
if (third.getValue() <= 0 || !substringImpl(second.getValue(), third.getValue(),
331+
second.getValue().length()).trim().contains(first.getValue())) {
332+
result = 0;
333+
}
334+
return new IntegerLiteral(result);
335+
}
336+
324337
/**
325338
* Executable arithmetic functions Instr
326339
*/
@@ -333,12 +346,14 @@ public static Expression instr(StringLikeLiteral first, StringLikeLiteral second
333346
* Executable arithmetic functions Ascii
334347
*/
335348
@ExecFunction(name = "ascii")
336-
public static Expression ascii(StringLikeLiteral first) {
349+
public static Expression ascii(StringLikeLiteral first) throws UnsupportedEncodingException {
337350
if (first.getValue().length() == 0) {
338351
return new IntegerLiteral(0);
339352
}
340-
char firstChar = first.getValue().charAt(0);
341-
return new IntegerLiteral(firstChar);
353+
String character = first.getValue();
354+
byte[] utf8Bytes = character.getBytes("UTF-8");
355+
int firstByteAscii = utf8Bytes[0] & 0xFF;
356+
return new IntegerLiteral(firstByteAscii);
342357
}
343358

344359
/**
@@ -583,7 +598,7 @@ public static Expression fieldVarchar(StringLikeLiteral first, VarcharLiteral...
583598
}
584599

585600
private static int findStringInSet(String target, String input) {
586-
String[] split = input.split(",");
601+
String[] split = input.split(",", -1);
587602
for (int i = 0; i < split.length; i++) {
588603
if (split[i].equals(target)) {
589604
return i + 1;
@@ -605,6 +620,10 @@ public static Expression findInSetVarchar(StringLikeLiteral first, StringLikeLit
605620
*/
606621
@ExecFunction(name = "repeat")
607622
public static Expression repeat(StringLikeLiteral first, IntegerLiteral second) {
623+
// when it is too large for fe to make result string, do not folding on fe, limit 1 MB
624+
if ((first.getValue().length() * second.getValue()) > 1000000) {
625+
throw new AnalysisException("repeat too large to fold const by fe");
626+
}
608627
StringBuilder sb = new StringBuilder();
609628
for (int i = 0; i < second.getValue(); i++) {
610629
sb.append(first.getValue());
@@ -627,6 +646,10 @@ public static Expression reverseVarchar(StringLikeLiteral first) {
627646
*/
628647
@ExecFunction(name = "space")
629648
public static Expression space(IntegerLiteral first) {
649+
// when it is too large for fe to make result string, do not folding on fe, limit 1 MB
650+
if (first.getValue() > 1000000) {
651+
throw new AnalysisException("space too large to fold const by fe");
652+
}
630653
StringBuilder sb = new StringBuilder();
631654
for (int i = 0; i < first.getValue(); i++) {
632655
sb.append(' ');
@@ -635,13 +658,13 @@ public static Expression space(IntegerLiteral first) {
635658
}
636659

637660
/**
638-
* Executable arithmetic functions split_by_char
661+
* Executable arithmetic functions split_by_string
639662
*/
640-
@ExecFunction(name = "split_by_char")
641-
public static Expression splitByChar(StringLikeLiteral first, StringLikeLiteral second) {
642-
String[] result = first.getValue().split(second.getValue());
663+
@ExecFunction(name = "split_by_string")
664+
public static Expression splitByString(StringLikeLiteral first, StringLikeLiteral second) {
665+
String[] result = first.getValue().split(second.getValue(), -1);
643666
List<Literal> items = new ArrayList<>();
644-
for (int i = 1; i < result.length; i++) {
667+
for (int i = 0; i < result.length; i++) {
645668
items.add((Literal) castStringLikeLiteral(first, result[i]));
646669
}
647670
return new ArrayLiteral(items);
@@ -666,16 +689,16 @@ public static Expression splitPart(StringLikeLiteral first, StringLikeLiteral ch
666689
if (".$|()[{^?*+\\".contains(separator) || separator.startsWith("\\")) {
667690
separator = "\\" + separator;
668691
}
669-
parts = sb.reverse().toString().split(separator);
692+
parts = sb.reverse().toString().split(separator, -1);
670693
} else {
671694
if (".$|()[{^?*+\\".contains(separator) || separator.startsWith("\\")) {
672695
separator = "\\" + separator;
673696
}
674-
parts = first.getValue().split(separator);
697+
parts = first.getValue().split(separator, -1);
675698
}
676699

677700
if (parts.length < Math.abs(number.getValue()) || number.getValue() == 0) {
678-
if (parts.length == Math.abs(number.getValue()) - 1) {
701+
if (parts.length == Math.abs(number.getValue())) {
679702
if (number.getValue() < 0 && first.getValue().startsWith(chr.getValue())
680703
|| number.getValue() > 0 && first.getValue().endsWith(chr.getValue())) {
681704
return castStringLikeLiteral(first, "");
@@ -695,7 +718,7 @@ public static Expression splitPart(StringLikeLiteral first, StringLikeLiteral ch
695718
*/
696719
@ExecFunction(name = "substring_index")
697720
public static Expression substringIndex(StringLikeLiteral first, StringLikeLiteral chr, IntegerLiteral number) {
698-
String[] parts = first.getValue().split(chr.getValue());
721+
String[] parts = first.getValue().split(chr.getValue(), -1);
699722
if (Math.abs(number.getValue()) >= parts.length) {
700723
return first;
701724
}
@@ -907,13 +930,13 @@ public static Expression extractUrlParameter(StringLikeLiteral first, StringLike
907930
return castStringLikeLiteral(first, "");
908931
}
909932

910-
String[] urlParts = first.getValue().split("\\?");
933+
String[] urlParts = first.getValue().split("\\?", -1);
911934
if (urlParts.length > 1) {
912935
String query = urlParts[1];
913-
String[] pairs = query.split("&");
936+
String[] pairs = query.split("&", -1);
914937

915938
for (String pair : pairs) {
916-
String[] keyValue = pair.split("=");
939+
String[] keyValue = pair.split("=", -1);
917940
if (second.getValue().equals(keyValue[0])) {
918941
return castStringLikeLiteral(first, keyValue[1]);
919942
}

0 commit comments

Comments
 (0)