Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -84,7 +84,7 @@ private static String substringImpl(String first, int second, int third) {
if (stringLength == 0) {
return "";
}
int leftIndex = 0;
long leftIndex = 0;
if (second < (- stringLength)) {
return "";
} else if (second < 0) {
Expand All @@ -94,15 +94,16 @@ private static String substringImpl(String first, int second, int third) {
} else {
return "";
}
int rightIndex = 0;
long rightIndex = 0;
if (third <= 0) {
return "";
} else if ((third + leftIndex) > stringLength) {
rightIndex = stringLength;
} else {
rightIndex = third + leftIndex;
}
return first.substring(leftIndex, rightIndex);
// left index and right index are in integer range because of definition, so we can safely cast it to int
return first.substring((int) leftIndex, (int) rightIndex);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ suite("fold_constant_string_arithmatic") {
testFoldConst("select substr('abcdef',3,-1)")
testFoldConst("select substr('',3,-1)")
testFoldConst("select substr('abcdef',3,10)")
testFoldConst("select substr('abcdef',-3)")
testFoldConst("select substr('abcdef',3)")
testFoldConst("select substr('',3)")

// substring
testFoldConst("select substring('1', 1, 1)")
Expand All @@ -679,6 +682,9 @@ suite("fold_constant_string_arithmatic") {
testFoldConst("select substring('Hello World', 1, 5)")
testFoldConst("select substring('', 1, 5)")
testFoldConst("select substring('Hello World', 1, 50)")
testFoldConst("select substring('abcdef',-3)")
testFoldConst("select substring('abcdef',3)")
testFoldConst("select substring('',3)")

// substring_index
testFoldConst("select substring_index('a,b,c', ',', 2)")
Expand Down
Loading