You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2021/11/23 06:02:00 UTC

[GitHub] [calcite] NobiGo commented on a change in pull request #2616: [CALCITE-4871] CAST a literal to DECIMAL type return wrong result

NobiGo commented on a change in pull request #2616:
URL: https://github.com/apache/calcite/pull/2616#discussion_r754821778



##########
File path: core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java
##########
@@ -677,6 +684,74 @@ Expression translateCast(
     return convert;
   }
 
+  private @Nullable Expression translateCastToDecimal(RelDataType sourceType, Expression operand,
+      RelDataType targetType) {
+    Expression convert = null;
+    switch (sourceType.getSqlTypeName()) {
+    case TINYINT:
+    case SMALLINT:
+    case INTEGER:
+    case BIGINT:
+    case REAL:
+    case FLOAT:
+    case DOUBLE:
+      Expression condition = operand;
+      if(operand instanceof MethodCallExpression) {
+        condition = ((MethodCallExpression) operand).targetExpression;
+      }
+      operand = Expressions.call(BuiltInMethod.STRING_VALUEOF.method, operand);
+      convert = translateCharToDecimal(operand, targetType, condition);
+      break;
+    case CHAR:
+    case VARCHAR:
+      convert = translateCharToDecimal(operand, targetType, operand);
+      break;
+    case DECIMAL:
+      Expression sourceIntDigits =
+          Expressions.constant(sourceType.getPrecision() - sourceType.getScale());
+      Expression targetIntDigits =
+          Expressions.constant(targetType.getPrecision() - targetType.getScale());
+      ConditionalStatement conditionalStatement =
+          Expressions.ifThen(
+              Expressions.andAlso(checkNotNull(operand),
+                  Expressions.greaterThan(sourceIntDigits, targetIntDigits)),
+              Expressions.throw_(
+                  Expressions.new_(RuntimeException.class,
+                      Expressions.constant("numeric field overflow"))));
+      list.add(conditionalStatement);
+      convert = Expressions.call(operand, BuiltInMethod.BIG_DECIMAL_SET_SCALE.method,
+          Expressions.constant(targetType.getScale()), Expressions.constant(RoundingMode.HALF_UP));
+      break;
+    default:
+      break;
+    }
+    return convert;
+  }
+
+  private @NotNull Expression translateCharToDecimal(Expression operand, RelDataType targetType,

Review comment:
       Ok. Thanks you. When the PR is ready I will repair this. 
   Actually I need more advire about this PR.
   Because When I try to make the cast operator to return the right result(I can confirm the test added in misc.iq have right behavior), then many unit tests throw exception or result have a different scale. So I want to know  how to handle this?




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