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

[GitHub] [calcite] olivrlee commented on a diff in pull request #3168: CALCITE-5662/ Allow CAST(BOOLEAN as INTEGER)

olivrlee commented on code in PR #3168:
URL: https://github.com/apache/calcite/pull/3168#discussion_r1178198833


##########
core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java:
##########
@@ -955,6 +961,52 @@ public static boolean canCastFrom(
     return rules.canApplyFrom(toTypeName, fromTypeName);
   }
 
+  /**
+   * Compares two types and returns true if fromType can be cast to toType or if the connection
+   * conformance allows exceptions.
+   *
+   * @param toType   target of assignment
+   * @param fromType source of assignment
+   * @param coerce   if true, the SQL rules for CAST are used; if false, the
+   *                 rules are similar to Java; e.g. you can't assign short x =
+   *                 (int) y, and you can't assign int x = (String) z.
+   * @param conformance value for connection conformance
+   * @return true if cast is legal or connection conformance allows exceptions
+   */
+  public static boolean canCastFrom(
+      RelDataType toType,
+      RelDataType fromType,
+      boolean coerce,
+      SqlConformance conformance) {
+    if (castingBooleanToNumber(toType, fromType)) {
+      return conformance.allowLenientBooleanCastTypes();
+    } else {
+      return canCastFrom(toType, fromType, coerce);
+    }
+  }
+
+  /**
+   * Returns whether the attempted cast is from a BOOLEAN to a NUMERIC where
+   * NUMERIC is one of [TINYINT, SMALLINT, INTEGER, BIGINT].
+   * @param toType Target of assignment
+   * @param fromType Source of assignment
+   * @return true iff fromType is boolean and toType is one of [TINYINT, SMALLINT, INTEGER, BIGINT]
+   */
+  public static boolean castingBooleanToNumber(
+      RelDataType toType,
+      RelDataType fromType) {
+    // These following types support casting from booleans in some dialects such as BigQuery.
+    List<String> numericTypes =
+            ImmutableList.of(TINYINT,
+            SMALLINT,
+            INTEGER,
+            BIGINT)
+        .stream().map(type -> type.toString()).collect(Collectors.toList());
+

Review Comment:
   Thanks for the review! Updated. 



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