Skip to content

Commit 5080424

Browse files
authored
DRILL-8194: Fix the function of REPLACE throws IndexOutOfBoundsException If text's length is more than previously applied (#2522)
1 parent 66bc832 commit 5080424

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,15 @@ public void setup() {
894894

895895
@Override
896896
public void eval() {
897-
out.buffer = buffer;
898897
out.start = out.end = 0;
899898
int fromL = from.end - from.start;
900899
int textL = text.end - text.start;
900+
if (buffer.capacity() < textL) {
901+
// We realloc buffer, if actual length is more than previously applied.
902+
out.buffer = buffer.reallocIfNeeded(textL);
903+
} else {
904+
out.buffer = buffer;
905+
}
901906

902907
if (fromL > 0 && fromL <= textL) {
903908
//If "from" is not empty and it's length is no longer than text's length

exec/java-exec/src/test/java/org/apache/drill/exec/expr/fn/impl/TestStringFunctions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.junit.Assert.assertTrue;
2121

22+
import org.apache.commons.lang3.RandomStringUtils;
2223
import org.apache.drill.categories.UnlikelyTest;
2324
import org.apache.drill.test.BaseTestQuery;
2425
import org.apache.drill.categories.SqlFunctionTest;
@@ -335,6 +336,19 @@ public void testRegexpReplace() throws Exception {
335336
.run();
336337
}
337338

339+
@Test
340+
public void testReplaceOutBuffer() throws Exception {
341+
String originValue = RandomStringUtils.randomAlphabetic(8192).toLowerCase() + "12345";
342+
String expectValue = originValue.replace("12345", "67890");
343+
String sql = "select replace(c1, '12345', '67890') as col from (values('" + originValue + "')) as t(c1)";
344+
testBuilder()
345+
.sqlQuery(sql)
346+
.ordered()
347+
.baselineColumns("col")
348+
.baselineValues(expectValue)
349+
.go();
350+
}
351+
338352
@Test
339353
public void testLikeStartsWith() throws Exception {
340354

0 commit comments

Comments
 (0)