This is an automated email from the ASF dual-hosted git repository.
cgivre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/drill.git
The following commit(s) were added to refs/heads/master by this push:
new 5080424abb DRILL-8194: Fix the function of REPLACE throws
IndexOutOfBoundsException If text's length is more than previously applied
(#2522)
5080424abb is described below
commit 5080424abbd7cee0f603ede10ab8dbf29ec10c85
Author: xurenhe <[email protected]>
AuthorDate: Sun Apr 24 10:44:10 2022 +0800
DRILL-8194: Fix the function of REPLACE throws IndexOutOfBoundsException If
text's length is more than previously applied (#2522)
---
.../apache/drill/exec/expr/fn/impl/StringFunctions.java | 7 ++++++-
.../drill/exec/expr/fn/impl/TestStringFunctions.java | 14 ++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git
a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
index 27b06448db..305cfcdac3 100644
---
a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
+++
b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/StringFunctions.java
@@ -894,10 +894,15 @@ public class StringFunctions{
@Override
public void eval() {
- out.buffer = buffer;
out.start = out.end = 0;
int fromL = from.end - from.start;
int textL = text.end - text.start;
+ if (buffer.capacity() < textL) {
+ // We realloc buffer, if actual length is more than previously applied.
+ out.buffer = buffer.reallocIfNeeded(textL);
+ } else {
+ out.buffer = buffer;
+ }
if (fromL > 0 && fromL <= textL) {
//If "from" is not empty and it's length is no longer than text's
length
diff --git
a/exec/java-exec/src/test/java/org/apache/drill/exec/expr/fn/impl/TestStringFunctions.java
b/exec/java-exec/src/test/java/org/apache/drill/exec/expr/fn/impl/TestStringFunctions.java
index 555323b333..8dc109397a 100644
---
a/exec/java-exec/src/test/java/org/apache/drill/exec/expr/fn/impl/TestStringFunctions.java
+++
b/exec/java-exec/src/test/java/org/apache/drill/exec/expr/fn/impl/TestStringFunctions.java
@@ -19,6 +19,7 @@ package org.apache.drill.exec.expr.fn.impl;
import static org.junit.Assert.assertTrue;
+import org.apache.commons.lang3.RandomStringUtils;
import org.apache.drill.categories.UnlikelyTest;
import org.apache.drill.test.BaseTestQuery;
import org.apache.drill.categories.SqlFunctionTest;
@@ -335,6 +336,19 @@ public class TestStringFunctions extends BaseTestQuery {
.run();
}
+ @Test
+ public void testReplaceOutBuffer() throws Exception {
+ String originValue =
RandomStringUtils.randomAlphabetic(8192).toLowerCase() + "12345";
+ String expectValue = originValue.replace("12345", "67890");
+ String sql = "select replace(c1, '12345', '67890') as col from (values('"
+ originValue + "')) as t(c1)";
+ testBuilder()
+ .sqlQuery(sql)
+ .ordered()
+ .baselineColumns("col")
+ .baselineValues(expectValue)
+ .go();
+ }
+
@Test
public void testLikeStartsWith() throws Exception {