tanclary commented on code in PR #3417:
URL: https://github.com/apache/calcite/pull/3417#discussion_r1321778995


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -1130,14 +1130,68 @@ public static String charFromUtf8(int n) {
       assert codePoint instanceof Number;
       long cp = ((Number) codePoint).longValue();
       if (cp < 0 || cp > 255) {
-        throw RESOURCE.inputArgumentsOfCodePointsToBytesOutOfRange(cp).ex();
+        throw 
RESOURCE.inputArgumentsOfFunctionOutOfRange("CODE_POINTS_TO_BYTES", cp).ex();
       }
       bytes[i] = (byte) cp;
     }
 
     return new ByteString(bytes);
   }
 
+  /**
+   * SQL CODE_POINTS_TO_STRING function.
+   */
+  public static @Nullable String codePointsToString(List codePoints) {
+    StringBuilder sb = new StringBuilder();
+    for (Object codePoint: codePoints) {
+      if (codePoint == null) {
+        return null;
+      }
+      assert codePoint instanceof Number;
+      long cp = ((Number) codePoint).longValue();
+      // Each valid code point should fall within the range of [0, 0xD7FF] and 
[0xE000, 0x10FFFF]
+      if (cp >= 0 && cp <= 0xD7FF || cp >= 0xE000 && cp <= 0x10FFFF) {
+        sb.append(charFromUtf8((int) cp));
+      } else {
+        throw 
RESOURCE.inputArgumentsOfFunctionOutOfRange("CODE_POINTS_TO_STRING", cp).ex();
+      }
+    }
+
+    return sb.toString();
+  }
+
+  /**
+   * SQL TO_CODE_POINTS(string) function.
+   */
+  public static @Nullable List<Integer> toCodePoints(String s) {
+    if (s.length() == 0) {
+      return null;
+    }
+    final ImmutableList.Builder<Integer> builder = new 
ImmutableList.Builder<>();
+    final int length = s.length();
+    int i = 0;
+    while (i < length) {
+      int cp = s.codePointAt(i);
+      builder.add(cp);
+      i += cp == s.charAt(i) ? 1 : 2;
+    }
+    return builder.build();
+  }
+
+  /**
+   * SQL TO_CODE_POINTS(binary) function.

Review Comment:
   nit: could you check if these javadocs are consistent with other functions 
that have two signatures (one for strings, one for binary strings)? 



##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -1130,14 +1130,68 @@ public static String charFromUtf8(int n) {
       assert codePoint instanceof Number;
       long cp = ((Number) codePoint).longValue();
       if (cp < 0 || cp > 255) {
-        throw RESOURCE.inputArgumentsOfCodePointsToBytesOutOfRange(cp).ex();
+        throw 
RESOURCE.inputArgumentsOfFunctionOutOfRange("CODE_POINTS_TO_BYTES", cp).ex();

Review Comment:
   Would you mind checking if there are other functions that have a similar 
error (input out of range?)? I think maybe some of the recently added regex 
functions have a similar error. It could be a good idea to reduce some 
redundancy if that's the case.



##########
core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java:
##########
@@ -1793,6 +1793,22 @@ private static RelDataType 
deriveTypeMapFromEntries(SqlOperatorBinding opBinding
           OperandTypes.ARRAY_OF_INTEGER,
           SqlFunctionCategory.STRING);
 
+  @LibraryOperator(libraries = {BIG_QUERY})
+  public static final SqlFunction CODE_POINTS_TO_STRING =
+      SqlBasicFunction.create("CODE_POINTS_TO_STRING",
+          ReturnTypes.VARCHAR_NULLABLE,
+          OperandTypes.ARRAY_OF_INTEGER,
+          SqlFunctionCategory.STRING);
+
+  @LibraryOperator(libraries = {BIG_QUERY})
+  public static final SqlFunction TO_CODE_POINTS =
+      SqlBasicFunction.create("TO_CODE_POINTS",
+          ReturnTypes.INTEGER

Review Comment:
   Just a question not a comment: I think the answer is yes, but is there a 
reason you can't use INTEGER_NULLABLE and then transform it to array? is it due 
to the ordering?



##########
core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java:
##########
@@ -1793,6 +1793,22 @@ private static RelDataType 
deriveTypeMapFromEntries(SqlOperatorBinding opBinding
           OperandTypes.ARRAY_OF_INTEGER,
           SqlFunctionCategory.STRING);
 
+  @LibraryOperator(libraries = {BIG_QUERY})

Review Comment:
   Nit: would you mind adding javadocs here? I know not all of the functions 
have them but given how many functions we've been adding to Calcite recently I 
think it would be a good practice to have them well-commented.



-- 
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