You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/03/25 00:37:28 UTC

[GitHub] [beam] apilloud commented on a change in pull request #14332: [BEAM-12033] Validate casts from double literals to numeric during ex…

apilloud commented on a change in pull request #14332:
URL: https://github.com/apache/beam/pull/14332#discussion_r600963716



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -835,12 +845,33 @@ private RexNode convertResolvedCast(ResolvedCast resolvedCast, RexNode input) {
     }
   }
 
-  private static void isCastingSupported(TypeKind fromType, TypeKind toType) {
+  private static void isCastingSupported(TypeKind fromType, TypeKind toType, RexNode input) {
     if (UNSUPPORTED_CASTING.containsKey(toType)
         && UNSUPPORTED_CASTING.get(toType).contains(fromType)) {
       throw new UnsupportedOperationException(
           "Does not support CAST(" + fromType + " AS " + toType + ")");
     }
+    if (fromType.equals(TYPE_DOUBLE)
+        && toType.equals(TYPE_NUMERIC)
+        && input instanceof RexLiteral) {
+      BigDecimal value = (BigDecimal) ((RexLiteral) input).getValue();
+      if (value.compareTo(MAX_NUMERIC_VALUE) == 1) {

Review comment:
       nit: I think it is preferred that you use `> 0` as the check here.

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -163,6 +164,15 @@
           + " The date_part includes: "
           + INTERVAL_DATE_PART_MSG;
 
+  // Maximum and minimum allowed values for the NUMERIC/DECIMAL data type.
+  // https://github.com/google/zetasql/blob/master/docs/data-types.md#decimal-type
+  private static final BigDecimal MAX_NUMERIC_VALUE =

Review comment:
       This code looks to define the same constants? Can we use those? https://github.com/apache/beam/blob/e9dcd541116098057c23d7132376e60a338adc44/sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlTypesUtils.java#L27

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -835,12 +845,33 @@ private RexNode convertResolvedCast(ResolvedCast resolvedCast, RexNode input) {
     }
   }
 
-  private static void isCastingSupported(TypeKind fromType, TypeKind toType) {
+  private static void isCastingSupported(TypeKind fromType, TypeKind toType, RexNode input) {
     if (UNSUPPORTED_CASTING.containsKey(toType)
         && UNSUPPORTED_CASTING.get(toType).contains(fromType)) {
       throw new UnsupportedOperationException(
           "Does not support CAST(" + fromType + " AS " + toType + ")");
     }
+    if (fromType.equals(TYPE_DOUBLE)
+        && toType.equals(TYPE_NUMERIC)
+        && input instanceof RexLiteral) {
+      BigDecimal value = (BigDecimal) ((RexLiteral) input).getValue();
+      if (value.compareTo(MAX_NUMERIC_VALUE) == 1) {
+        throw new UnsupportedOperationException(
+            String.format(
+                "Casting %s as %s would cause overflow of literal %s.", fromType, toType, value));
+      }
+      if (value.compareTo(MIN_NUMERIC_VALUE) == -1) {

Review comment:
       nit: same thing `< 0`.




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

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