[ 
https://issues.apache.org/jira/browse/CALCITE-6921?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Mihai Budiu resolved CALCITE-6921.
----------------------------------
    Fix Version/s: 1.40.0
       Resolution: Fixed

Fixed in 
https://github.com/apache/calcite/commit/21277d89601dc4577ae2847b58dfc4af4c188c64
Thank you for your contribution [~kramerul]
Thank you for the review [~nobigo]

> 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
>            Priority: Minor
>              Labels: pull-request-available
>             Fix For: 1.40.0
>
>
> 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>1}} instead if {{pos != 1}}
> This statement will reproduce the error
> {code:SQL}
> SELECT REGEXP_REPLACE('','([bc]+)','','g')
> {code}



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

Reply via email to