This is an automated email from the ASF dual-hosted git repository.

mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 21277d8960 [CALCITE-6921] REGEXP_REPLACE with empty string causes 
Exception
21277d8960 is described below

commit 21277d89601dc4577ae2847b58dfc4af4c188c64
Author: Ulrich Kramer <[email protected]>
AuthorDate: Mon Apr 7 08:48:36 2025 +0200

    [CALCITE-6921] REGEXP_REPLACE with empty string causes Exception
---
 core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java  | 2 +-
 core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 5cef5efeb2..7609a6a38b 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -737,7 +737,7 @@ public String regexpReplace(String s, String regex, String 
replacement,
     /** SQL {@code REGEXP_REPLACE} function with 6 arguments. */
     public String regexpReplace(String s, String regex, String replacement,
         int pos, int occurrence, @Nullable String matchType) {
-      if (pos < 1 || pos > s.length()) {
+      if (pos < 1 || pos > s.length() + 1) {
         throw 
RESOURCE.invalidInputForRegexpReplace(Integer.toString(pos)).ex();
       }
 
diff --git a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
index 439560d9c3..13f5a88744 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
@@ -597,6 +597,9 @@ static <E> List<E> list() {
         is("X X GHI"));
     assertThat(f.regexpReplacePg("ABC def GHI", "[a-z]+", "X", "i"),
         is("X def GHI"));
+    assertThat(f.regexpReplacePg("", "[a-z]+", "X", "i"), is(""));
+    assertThat(f.regexpReplace("", "[a-z]+", "X", 1, 1, "i"), is(""));
+
 
     try {
       f.regexpReplace("abc def ghi", "[a-z]+", "X", 0);

Reply via email to