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

Mihai Budiu commented on CALCITE-6743:
--------------------------------------

I think that the implementation of return type inference for ARRAY_INSERT is 
incorrect.
The implementation is in SqlLibraryOperators::arrayInsertReturnType.
This function calls adjustTypeForArrayFunctions, which modifies the OpBinding 
whose type is being inferred.
However, while the OpBinding is mutated and the arguments are changed, the 
types that were inferred for the arguments are never adjusted, and they remain 
with their previous value in SqlValidatorImpl.nodeToTypeMap.
This bug reinforces the unwritten rule that the OpBinding should be immutable 
during the type inference.

For a concrete example, consider this test:

select array_insert(array(1, 2, 3), 3, cast(4 as double)

Type inference for array(1, 2, 3) infers the fact that this is an INTEGER ARRAY.
Then type inference for array_insert mutates this array into array(cast(1 as 
double), cast(2 as double), cast(3 as double)).
The type in the nodeToTypeMap for this array is however not changed to DOUBLE 
ARRAY, as it should be.
I believe that the function that infers return types for array_insert cannot 
change the type of the ARRAY, since it does not have a handle to the 
nodeToTypeMap of the validator.

The argument array should have been changed before return type inference. On 
the other hand, the change needs to know the types of the arguments, so it 
needs to be run after argument type inference.

[~caicancai] wrote the adjustTypeForArrayFunctions function in 
https://github.com/apache/calcite/pull/3705









> Type inference for ARRAY_INSERT function produces an inconsistent result
> ------------------------------------------------------------------------
>
>                 Key: CALCITE-6743
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6743
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.38.0
>            Reporter: Mihai Budiu
>            Priority: Minor
>
> Consider this test:
> {code}
> select array_insert(array(1, 2, 3), 3, cast(4 as double))
> {code}
> The type inference for array_insert first infers a type of INTEGER ARRAY for 
> the first argument of array_insert. That's correct. Then it infers a type of 
> double for the last argument. That's also fine. 
> Then it uses a function called adjustTypeForArrayFunction to rewrite the 
> array constructor into array(1.0, 2.0, 3.0) with double arguments. 
> Unfortunately the type in the typeMap for this array is never  adjusted, and 
> remains INTEGER ARRAY, which is incorrect.



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

Reply via email to