[ 
https://issues.apache.org/jira/browse/CALCITE-6453?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17863243#comment-17863243
 ] 

Krisztian Kasa commented on CALCITE-6453:
-----------------------------------------

[~nobigo] 
Here are my findings:
1)
{quote}`Simplify 'cast(Literal as nullable)' to 'Literal' (when Literal is not 
nullable)`.
{quote}
This optimization was added by CALCITE-2695
But it only works when the source and the target types are the same except 
nullability. 
https://github.com/apache/calcite/blob/8a96095a64bc1cc955438d219d5f1fbcbc5762b7/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L1257-L1264

2)
{quote}If this is a valid optimize, Do we think `Simplify 'cast(p as nullable)' 
to 'p' (when p is not nullable)` is valid too? I know this is strange.
{quote}
When the source and target type are different and {{p}} is a literal we use 
[RexExecutor.reduce|https://github.com/apache/calcite/blob/8a96095a64bc1cc955438d219d5f1fbcbc5762b7/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L2243-L2276]
 to simplify and we end up with a cast expression where the source type and the 
target type is the same except nullability. This is what we have in 1).
I found that 
[RexExecutor.reduce|https://github.com/apache/calcite/blob/8a96095a64bc1cc955438d219d5f1fbcbc5762b7/core/src/main/java/org/apache/calcite/rex/RexExecutorImpl.java#L127-L130]
 is used for constant reduction so I think the simplification is valid only if 
{{p}} is a constant expression.

3)
{quote}Besides This PR looks only handle the DATE Type.
{quote}
It handles any type pairs where the value having a source type which can be 
safely cast to the target type.
I tested it with integer too.
{code:java}
  @Test void testSimplifyCastWithConstantReductionFromCharToInt() {
    RexNode intText = literal("1000");
    RelDataType nullableIntType =
        
typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER),
 true);
    RexNode cast = rexBuilder.makeCast(nullableIntType, intText);
    checkSimplify(cast, "1000");
  }
{code}

> Simplify casts which are result of constant reduction
> -----------------------------------------------------
>
>                 Key: CALCITE-6453
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6453
>             Project: Calcite
>          Issue Type: Improvement
>            Reporter: Krisztian Kasa
>            Assignee: Krisztian Kasa
>            Priority: Major
>              Labels: pull-request-available
>
> RexSimplify transforms nullable cast expressions with a non-nullable literal 
> operand which type is different from the cast type to another cast expression 
> with a literal operand with the same type as the cast.
> If simplify is called again using the result cast expression of the first 
> simplification we get a literal only as a result.
> Example:
> Initial expression
> {code:java}
> CAST(_UTF-16LE'2020-10-30':VARCHAR(2147483647) CHARACTER SET "UTF-16LE"):DATE
> {code}
> where the operand is non-nullable varchar and the result of the cast is 
> nullable date.
> After the first simplify call we get
> {code:java}
> CAST(2020-10-30:DATE):DATE
> {code}
> where the operand type is non-nullable date and the cast type is nullable 
> date.
> https://github.com/apache/calcite/blob/8ab0b03326730aa2cc6b476b2cbd8f99799bdacb/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L2269-L2276
> After the second simplify call we get
> {code:java}
> 2020-10-30:DATE
> {code}
> where the literal type is non-nullable date
> https://github.com/apache/calcite/blob/8ab0b03326730aa2cc6b476b2cbd8f99799bdacb/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L2195-L2196



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to