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

Benchao Li commented on CALCITE-4861:
-------------------------------------

Here is some insights after I dug into the issue:
1, There is already a feature for general cast elimination to avoid removing 
loss cast in RexSimplify#simplifyCast introduced in CALCITE-3712
2, The main problem causing this issue, is the literal's simplification in  
RexBuilder#makeCast
3, The runtime behavior for casting: throwing exception or truncation in loss 
casting. I think this is orthogonal with current issue and can be discussed in 
a separate issue.

If we agree with my analysis, I will submit a pr to fix #2.

> Optimisation of chained cast calls can lead to unexpected behaviour
> -------------------------------------------------------------------
>
>                 Key: CALCITE-4861
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4861
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Marios Trivyzas
>            Priority: Minor
>              Labels: pull-request-available
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> Simplification of Cast chained calls can lead to unexpected behaviour:
> {code}CAST(CAST(CAST(123456 AS TINYINT) AS INT) AS BIGINT){code}
> is simplified to 
> {code}
> CAST(123456 AS BIGINT){code}
> and returns *123456* with *BIGINT* data type, where the first inner cast as 
> TINYINT should already fail because the value is out of range.
> For example, for PostgreSQL:
> {noformat}
> postgres=# select 123456::smallint::int::bigint;
> ERROR: smallint out of range{noformat}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to