julianhyde commented on code in PR #3460:
URL: https://github.com/apache/calcite/pull/3460#discussion_r1369166613


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -603,6 +605,38 @@ public String regexpReplace(String s, String regex, String 
replacement,
       return Unsafe.regexpReplace(s, pattern, replacement, pos, occurrence);
     }
 
+    /** SQL {@code REGEXP_REPLACE} function with 3 arguments with
+     * {@code \\} based indexing for capturing groups.
+     */
+    public String regexpReplaceNonDollarIndexed(String s, String regex,
+        String replacement) {
+      // Preprocessing to convert double-backslash based indexing for capturing
+      // groups into $ based indices recognized by java regex.
+
+      // Explicitly escaping any $ symbols coming from input
+      // to ignore them from being considered as capturing group index.
+      String indexedReplacement = replacement.replace("\\\\", "\\")
+          .replace("$", "\\$");
+
+      // Check each occurrence of escaped chars, convert \<n> integers into
+      // $<n> indices, keep \\ and \$, throw an error for any other invalid 
escapes.
+      int lastOccIdx = 0;
+      while (lastOccIdx != -1) {

Review Comment:
   The code to convert $-format to `\`-format can be a static utility method.
   
   That utility method can be tested directly in `SqlFunctionsTest`.
   
   You should add a (new) cache in `SqlRegexFunction`. Its type will be a 
`LoadingCache<String, String>`. Don't worry that most instances of 
`SqlRegexFunction` won't use this cache. The overhead of creating the cache is 
small.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to