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



##########
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:
       > 
   
   guess I don't have a direct reply, but usually I will create another fork of 
the repo. and compare the behavior simultaneously with the patch and without 
the patch and adjust correpsondingly.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to