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

jackylau commented on CALCITE-5674:
-----------------------------------

why i found this is here i supports the flink builtin functions like 
array_union [https://github.com/apache/flink/pull/22483/files]

for example in it
f0 is [1, 2, null] and type is INTEGER ARRAY
array_union(f0, [1.0E0, NULL, 4.0E0]) => it should return  [1.0, 2.0, NULL, 
4.0] but not. 

the reasion is following  
[1, 2, null] => INTEGER ARRAY
[1.0E0, NULL, 4.0E0] => DOUBLE NOT NULL ARRAY NOT NULL

so the array_union function type is the common of the both DOUBLE ARRAY.
so the flink will insert implicit cast for f0, 
the cast should like cast(f0 as DOUBLE ARRAY), but it return cast(f0 as DOUBLE 
NOT NULL ARRAY),
this will cast [1, 2, null] -> [1.0, 2.0. 0.0] in the runtime eval method, make 
the result not correct.

why it return cast(f0 as DOUBLE NOT NULL ARRAY)?
the root cause is calcite not transitive the nullable for array element

> Cast expr to target type should respect nullable when the type is array
> -----------------------------------------------------------------------
>
>                 Key: CALCITE-5674
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5674
>             Project: Calcite
>          Issue Type: Improvement
>    Affects Versions: 1.35.0
>            Reporter: jackylau
>            Priority: Major
>             Fix For: 1.35.0
>
>
> {code:java}
> // code placeholder
>  
> // code placeholder
> sql("select cast(array[1,null,2] as int array) from (values (1))")
> .columnType("INTEGER NOT NULL ARRAY NOT NULL");
> it is not correct, it should return INTEGER ARRAY NOT NULL   {code}
>  
> according the sql standard, cast to type, which can not be nulable and 
> nullable is tabel level attribute which can not changed by cast.
> current the calcite cast will using nullable of expr to replace target type , 
> it is true. but it shoud also set array element type
> {code:java}
> // code placeholder
> /** Derives the type of "CAST(expression AS targetType)". */
> public static RelDataType deriveType(RelDataTypeFactory typeFactory,
>     RelDataType expressionType, RelDataType targetType, boolean safe) {
>   return typeFactory.createTypeWithNullability(targetType,
>       expressionType.isNullable() || safe);
> } {code}
>  



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

Reply via email to