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 2019/10/17 07:18:03 UTC

[GitHub] [calcite] DonnyZone commented on a change in pull request #1507: [CALCITE-3414] Unify Expression's type cast and conversion as a robust one

DonnyZone commented on a change in pull request #1507: [CALCITE-3414] Unify Expression's type cast and conversion as a robust one
URL: https://github.com/apache/calcite/pull/1507#discussion_r335842052
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java
 ##########
 @@ -1143,14 +1156,20 @@ public static Expression convert(Expression operand, Type fromType,
                 "toString",
                 operand));
       } else {
-        // E.g. from "BigDecimal" to "String"
-        // Generate "x == null ? null : x.toString()"
-        return Expressions.condition(
-            Expressions.equal(operand, RexImpTable.NULL_EXPR),
-            RexImpTable.NULL_EXPR,
-            Expressions.call(
-                operand,
-                "toString"));
+        Expression result;
+        try {
+          // Try to call "toString()" method
+          // E.g. from "Integer" to "String"
+          // Generate "x == null ? null : x.toString()"
+          result = Expressions.condition(
+              Expressions.equal(operand, RexImpTable.NULL_EXPR),
+              RexImpTable.NULL_EXPR,
+              Expressions.call(operand, "toString"));
+        } catch (Exception e) {
+          // No "toString()" method, fall through to (String)x
 
 Review comment:
   Theoretically, a Class should contain `toString` method. However, there are some special cases in Calcite.
   For example (_QuidemTest#winagg.iq_), if the operand is `SqlFunctions.lesser(String.class, String.class)`,  its type will be `java.lang.Comparable`, which has no `toString `method, leading to Exception in `Expressions.call(operand, "toString")`.
   Because for `String.class`, the method matched is:
   ```
     public static <T extends Comparable<T>> T lesser(T b0, T b1) {
       return b0 == null || b0.compareTo(b1) > 0 ? b1 : b0;
     }
   ```

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


With regards,
Apache Git Services