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

xuyang commented on FLINK-33541:
--------------------------------

Hi, [~libenchao]  when I try to fix it and add tests, I notice currently the 
CAST rule is correct. It forbids casting from not null to nullable.  The root 
cause is not IF but is RAND_INTEGER. The details are added in the pr. Just copy 
it here.

Currently RAND and RAND_INTEGER are always return type "NOT NULL" as built in 
functions in {{FlinkSqlOperatorTable}} during validation. But when codegen, the 
result type is always nullable in {{{}RandCallGen{}}}.

Actually, the result type of RAND and RAND_INTEGER should depend on the types 
of arguments. For example, If the argument is nullable, the return type should 
be nullable.

This bug causes the error with pattern "IF(1 = 1, RAND(0), 0)", because the 
following logic:
 # the result type of RAND is always "not null" when validation in 
{{{}FlinkSqlOperatorTable{}}}, and the third param is 0 that is always "not 
null", so the return type of operator IF is always "not null".
 # when codegen operator RAND, the return type of RAND(0) is always "nullable" 
in `{{{}RandCallGen{}}}`.
 # when codegen operator IF, the CAST rule cannot cast from "nullable" (the 
second arg RAND) to "not null"(the result type), so the exception throws.

The pr is available, I'm grateful if you can help review it.

> RAND_INTEGER  can't be existed in a IF statement
> ------------------------------------------------
>
>                 Key: FLINK-33541
>                 URL: https://issues.apache.org/jira/browse/FLINK-33541
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / API
>    Affects Versions: 1.17.0, 1.18.0
>            Reporter: Guojun Li
>            Priority: Major
>              Labels: pull-request-available
>
> The minimum produce steps:
> Flink SQL> select if(1=1, rand_integer(100), 0);
> [ERROR] Could not execute SQL statement. Reason:
> java.lang.Exception: Unsupported operand types: IF(boolean, INT, INT NOT NULL)
>  
> But we do not see the exception reported in 1.14, not sure which version this 
> bug was introduced.



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

Reply via email to