DonnyZone opened a new pull request #1609: [CALCITE-3520] Type cast from 
primitive to box is not correct
URL: https://github.com/apache/calcite/pull/1609
 
 
   Current optimization:
   ```
   int x;
   Long.valueOf((long) x) -> Long.valueOf(x)
   ```
   However, it is not always safe to eliminate primitive cast when converting 
from primitive to box.
    ```
   boolean a = false;
   byte b = 0;
   char c = 'c';
   short d = 1;
   int e = 0;
   long f = 0L;
   float g = 1.1f;
   double h = 2.2D;
   
   Correct Statement:
   ==============================================================
   Boolean.valueOf(a);
   Byte.valueOf(b);
   Character.valueOf(c);
   Short.valueOf(b); Short.valueOf(d);
   Integer.valueOf(b); Integer.valueOf(c); Integer.valueOf(d); 
Integer.valueOf(e);
   Long.valueOf(b); Long.valueOf(c); Long.valueOf(d); Long.valueOf(e); 
Long.valueOf(f);
   Float.valueOf(b); Float.valueOf(c); Float.valueOf(d); Float.valueOf(e); 
Float.valueOf(f); Float.valueOf(g);
   Double.valueOf(b); Double.valueOf(c); Double.valueOf(d); Double.valueOf(e); 
Double.valueOf(f); Double.valueOf(g); Double.valueOf(h);
   ```
   
   If the case is:
   ```
   int x;
   Byte.valueOf((byte) x) -> Byte.valueOf(x)
   ```
   We will get the exception below.
   ```
   java.lang.RuntimeException: while resolving method 'valueOf[int]' in class 
class java.lang.Byte
        at org.apache.calcite.linq4j.tree.Types.lookupMethod(Types.java:323)
        at org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:445)
        at org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:457)
        at org.apache.calcite.linq4j.tree.Expressions.box(Expressions.java:1437)
        at 
org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:404)
        at 
org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:306)
        ......
   Caused by: java.lang.NoSuchMethodException: java.lang.Byte.valueOf(int)
        at java.base/java.lang.Class.getMethod(Class.java:2108)
        at org.apache.calcite.linq4j.tree.Types.lookupMethod(Types.java:314)
        ... 72 more
   ```
   Moreover, we cannot figure out queries to hit this code path. Because the 
codegen framework will conduct unbox operation when necessary.

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


With regards,
Apache Git Services

Reply via email to