You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "macroguo-ghy (via GitHub)" <gi...@apache.org> on 2023/09/17 04:22:50 UTC

[GitHub] [calcite] macroguo-ghy commented on a diff in pull request #3417: [CALCITE-5993] Add CODE_POINTS_TO_STRING, TO_CODE_POINTS function (en…

macroguo-ghy commented on code in PR #3417:
URL: https://github.com/apache/calcite/pull/3417#discussion_r1328039931


##########
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:
   I replace it with `SQL TO_CODE_POINTS(string) function for binary string.` 



##########
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:
   Add some comments for three `code_points` functions.



##########
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:
   The order of transform will affect the return types. 
   `INTEGER_NULLABLE.andThen(SqlTypeTransforms.TO_ARRAY)` means: an not null array, which element may be null.
   `INTEGER.andThen(SqlTypeTransforms.TO_ARRAY).andThen(SqlTypeTransforms.TO_NULLABLE)` means: an array may be null, but its element is not null.
   
   In `to_code_points` function, we can not get a result like`[1, NULL, 2, 3]`, the element type of array never be `NULL`, so i use `INTEGER` instead of `INTEGER_NULLABLE`.



##########
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:
   I use some keywords like 'out of range' to search whole project, I find only `atanh` function may throw a out of range exception, I replace old code with `inputArgumentsOfFunctionOutOfRange `. And some other "out of range exception" may throw during vaidation phase, I think we don't need to change them.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org