[CALCITE-3520] Type cast from primitive to box is not correct#1609
[CALCITE-3520] Type cast from primitive to box is not correct#1609DonnyZone wants to merge 2 commits intoapache:masterfrom
Conversation
| || Primitive.of(una.getType()) == toBox) { | ||
| return Expressions.box(una.expression, toBox); | ||
| } | ||
| } |
There was a problem hiding this comment.
If you check the following lines, there is
// E.g., from "int" to "Byte".
// Convert it first and generate "Byte.valueOf((byte)x)"
// Because there is no method "Byte.valueOf(int)" in Byte
return Expressions.box(
Expressions.convert_(operand, toBox.primitiveClass),
toBox);
So the case was aware of already, and it just seems like cases should have skipped this optimization fail to do so.
There was a problem hiding this comment.
Yes, this line of code for general case is also added by me in CALCITE-3414 .
But the optimization targets on the pattern like
(byte)(int)->Byte
In which the operand is UnaryExpression with ExpressionType.Convert
|
LGTM |
80f411d to
ca27fe9
Compare
| } | ||
|
|
||
| @Test public void testTypeFromPrimitiveToBox() { | ||
| final Expression intVariable = |
There was a problem hiding this comment.
It would be nice if we can add more test cases to cover all the integer data types conversion.
95f5b68 to
678b764
Compare
|
I found there is a function |
| || Primitive.of(una.getType()) == toBox) { | ||
| return Expressions.box(una.expression, toBox); | ||
| && Primitive.of(una.getType()) == toBox) { | ||
| Primitive origin = Primitive.of(una.expression.type); |
There was a problem hiding this comment.
According to the comment, "Eliminate primitive casts like Long.valueOf((long) x), Generate Long.valueOf(x)", we should satiesfy all these conditions.
There was a problem hiding this comment.
Thanks for the explanation.
…one) * Always cast the primitive first when converted to box type; * Remain the optimization with fine-grained type check conditions. close apache#1609
…one) * Always cast the primitive first when converted to box type; * Remain the optimization with fine-grained type check conditions. close apache#1609 Change-Id: I37b369b95b8b446caf34269ef64237aacb56af26
…one) * Always cast the primitive first when converted to box type; * Remain the optimization with fine-grained type check conditions. close apache#1609 Change-Id: I37b369b95b8b446caf34269ef64237aacb56af26
Current optimization:
However, it is not always safe to eliminate primitive cast when converting from primitive to box.
If the case is:
We will get the exception below.
Moreover, we cannot figure out queries to hit this code path. Because the codegen framework will conduct unbox operation when necessary.