You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Ulrich Kramer (Jira)" <ji...@apache.org> on 2025/03/28 10:15:00 UTC

[jira] [Created] (CALCITE-6921) REGEXP_REPLACE with empty string causes Exception

Ulrich Kramer created CALCITE-6921:
--------------------------------------

             Summary: REGEXP_REPLACE with empty string causes Exception
                 Key: CALCITE-6921
                 URL: https://issues.apache.org/jira/browse/CALCITE-6921
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.38.0
            Reporter: Ulrich Kramer


Using REGEXP_REPLACE with an empty string raises

{noformat}
Caused by: org.apache.calcite.runtime.CalciteException: Invalid input for REGEXP_REPLACE: '1'
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
	at org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:511)
	at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:605)
	at org.apache.calcite.runtime.SqlFunctions$RegexFunction.regexpReplace(SqlFunctions.java:676)
	at org.apache.calcite.runtime.SqlFunctions$RegexFunction.regexpReplacePg(SqlFunctions.java:687)
{noformat}

This is the related code
{code:java}
    public String regexpReplace(String s, String regex, String replacement,
        int pos, int occurrence, @Nullable String matchType) {
      if (pos < 1 || pos > s.length()) {
        throw RESOURCE.invalidInputForRegexpReplace(Integer.toString(pos)).ex();
      }

      final int flags = matchType == null ? 0 : makeRegexpFlags(matchType);
      final Pattern pattern = cache.getUnchecked(new Key(flags, regex));

      return Unsafe.regexpReplace(s, pattern, replacement, pos, occurrence);
    }
{code}

It seems quite clear that the condition {{pos < 1}} is wrong.

Additionally the code in {{Unsafe.regexpReplace}} also contains an error

{code:java}
    if (pos != 1) {
      sb.append(s, 0, pos - 1);
      input = s.substring(pos - 1);
    } else {
      input = s;
    }
{code}

I think it should be {{pos>0}} instead if {{pos != 1}}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)