[ 
https://issues.apache.org/jira/browse/BEAM-12033?focusedWorklogId=571993&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-571993
 ]

ASF GitHub Bot logged work on BEAM-12033:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 25/Mar/21 16:43
            Start Date: 25/Mar/21 16:43
    Worklog Time Spent: 10m 
      Work Description: ibzib commented on a change in pull request #14332:
URL: https://github.com/apache/beam/pull/14332#discussion_r601661407



##########
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:
       I moved the constants to ZetaSqlCalciteTranslationUtils.

##########
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:
       done

##########
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:
       done




-- 
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 571993)
    Time Spent: 50m  (was: 40m)

> Validate casts from double literals to numeric during expression conversion.
> ----------------------------------------------------------------------------
>
>                 Key: BEAM-12033
>                 URL: https://issues.apache.org/jira/browse/BEAM-12033
>             Project: Beam
>          Issue Type: Improvement
>          Components: dsl-sql-zetasql
>            Reporter: Kyle Weaver
>            Assignee: Kyle Weaver
>            Priority: P2
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> Numeric values in ZetaSQL are subject to constraints: they must fall within 
> certain hardcoded limits, and the scale also cannot exceed 9. Currently we 
> have stricter constraints than these when preparing the expression in 
> BeamZetaSqlCalcRel due to BEAM-12046. However, to force BeamCalcRel to have 
> correct behavior, we should check the constraints earlier, during expression 
> conversion.
> https://github.com/google/zetasql/blob/697da738be2af3b6b9492c73c55616d8b9fed0b6/javatests/com/google/zetasql/ValueTest.java#L727-L745



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to