Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-6921

REGEXP_REPLACE with empty string causes Exception

    XMLWordPrintableJSON

Details

    Description

      Using REGEXP_REPLACE with an empty string raises

      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)
      

      This is the related code

          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);
          }
      

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

      Additionally the code in Unsafe.regexpReplace also contains an error

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

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

      This statement will reproduce the error

      SELECT REGEXP_REPLACE('','([bc]+)','','g')
      

      Attachments

        Issue Links

          Activity

            People

              kramerul Ulrich Kramer
              kramerul Ulrich Kramer
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: