mihaibudiu commented on code in PR #3687:
URL: https://github.com/apache/calcite/pull/3687#discussion_r1507004405


##########
linq4j/src/main/java/org/apache/calcite/linq4j/tree/Expressions.java:
##########
@@ -2829,6 +2869,18 @@ public static ThrowStatement throw_(Expression 
expression) {
     return new ThrowStatement(expression);
   }
 
+  /**
+   * Creates an expression that represents the throwing of an exception.
+   */
+  public static Expression throwExpr(Expression expression) {

Review Comment:
   There's a ThrowStatement.
   Maybe having an expression is useful, since it can be used in an expression 
context - you don't need to break the current expression to create a new 
statement.
   I would document this feature.



##########
linq4j/src/main/java/org/apache/calcite/linq4j/tree/Expressions.java:
##########
@@ -622,9 +622,49 @@ public static UnaryExpression convert_(Expression 
expression, Type type,
    * operation that throws an exception if the target type is
    * overflowed.
    */
-  public static UnaryExpression convertChecked(Expression expression,
+  public static Expression convertChecked(Expression expression,
       Type type) {
-    throw Extensions.todo();
+    if (type == Byte.class

Review Comment:
   It's funny, I implemented this myself in a separate PR: 
https://github.com/apache/calcite/pull/3589
   There I am relying on an existing implementation in Primitive.numberValue.
   



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java:
##########
@@ -1374,11 +1375,24 @@ private Result toInnerStorageType(Result result, Type 
storageType) {
     }
     final Type storageType = currentStorageType != null
         ? currentStorageType : 
typeFactory.getJavaClass(dynamicParam.getType());
-    final Expression valueExpression =
+
+    final boolean isNumeric = 
SqlTypeFamily.NUMERIC.contains(dynamicParam.getType());
+
+    // For numeric types, use java.lang.Number to prevent cast exception
+    // when the parameter type differs from the target type
+    Expression argumentExpression =
         EnumUtils.convert(
             Expressions.call(root, BuiltInMethod.DATA_CONTEXT_GET.method,
                 Expressions.constant("?" + dynamicParam.getIndex())),
-            storageType);
+            isNumeric ? java.lang.Number.class : storageType);
+
+    // The cast operation will fail for null values

Review Comment:
   I can see why this comment may be confusing: what you mean is that casts 
have to be handled specially, because otherwise they would cause a failure. But 
the comment as phrased can be interpreted that a cast null will cause a failure.



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